blob: d72ee47dc6e4fb1c74c732db949e27f1e6de09a7 [file] [log] [blame]
Manu Abraham41e840b2009-12-02 21:57:10 -03001/*
2 Mantis PCI bridge driver
3
Manu Abraham8825a092009-12-15 09:13:49 -03004 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
Manu Abraham41e840b2009-12-02 21:57:10 -03005
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
Manu Abraham41e840b2009-12-02 21:57:10 -030021#include <asm/io.h>
22#include <linux/ioport.h>
Manu Abrahamb3b96142009-12-04 05:41:11 -030023#include <linux/pci.h>
24#include <linux/i2c.h>
25
26#include "dmxdev.h"
27#include "dvbdev.h"
28#include "dvb_demux.h"
29#include "dvb_frontend.h"
30#include "dvb_net.h"
31
Manu Abraham41e840b2009-12-02 21:57:10 -030032#include "mantis_common.h"
Manu Abrahamb3b96142009-12-04 05:41:11 -030033#include "mantis_reg.h"
34#include "mantis_i2c.h"
Manu Abraham41e840b2009-12-02 21:57:10 -030035
Manu Abrahambc832fa2009-12-04 05:57:28 -030036#define TRIALS 10000
37
Manu Abraham41e840b2009-12-02 21:57:10 -030038static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg)
39{
Manu Abraham3e978a82009-12-04 05:56:35 -030040 u32 rxd, i, stat, trials;
Manu Abraham41e840b2009-12-02 21:57:10 -030041
Manu Abrahamb3b96142009-12-04 05:41:11 -030042 dprintk(MANTIS_INFO, 0, " %s: Address=[0x%02x] <R>[ ",
Manu Abraham715d3412009-12-03 05:37:51 -030043 __func__, msg->addr);
44
Manu Abraham41e840b2009-12-02 21:57:10 -030045 for (i = 0; i < msg->len; i++) {
46 rxd = (msg->addr << 25) | (1 << 24)
47 | MANTIS_I2C_RATE_3
48 | MANTIS_I2C_STOP
49 | MANTIS_I2C_PGMODE;
50
51 if (i == (msg->len - 1))
52 rxd &= ~MANTIS_I2C_STOP;
53
Manu Abrahamdf0cca12009-12-02 22:07:24 -030054 mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
Manu Abraham41e840b2009-12-02 21:57:10 -030055 mmwrite(rxd, MANTIS_I2CDATA_CTL);
Manu Abraham3e978a82009-12-04 05:56:35 -030056
57 /* wait for xfer completion */
Manu Abrahambc832fa2009-12-04 05:57:28 -030058 for (trials = 0; trials < TRIALS; trials++) {
Manu Abraham3e978a82009-12-04 05:56:35 -030059 stat = mmread(MANTIS_INT_STAT);
60 if (stat & MANTIS_INT_I2CDONE)
61 break;
Manu Abraham41e840b2009-12-02 21:57:10 -030062 }
Manu Abraham3e978a82009-12-04 05:56:35 -030063
Manu Abrahambc832fa2009-12-04 05:57:28 -030064 dprintk(MANTIS_TMG, 0, "I2CDONE: trials=%d\n", trials);
65
66 /* wait for xfer completion */
67 for (trials = 0; trials < TRIALS; trials++) {
68 stat = mmread(MANTIS_INT_STAT);
69 if (stat & MANTIS_INT_I2CRACK)
70 break;
Manu Abrahambc832fa2009-12-04 05:57:28 -030071 }
72
73 dprintk(MANTIS_TMG, 0, "I2CRACK: trials=%d\n", trials);
74
Manu Abraham41e840b2009-12-02 21:57:10 -030075 rxd = mmread(MANTIS_I2CDATA_CTL);
76 msg->buf[i] = (u8)((rxd >> 8) & 0xFF);
Manu Abrahamb3b96142009-12-04 05:41:11 -030077 dprintk(MANTIS_INFO, 0, "%02x ", msg->buf[i]);
Manu Abraham41e840b2009-12-02 21:57:10 -030078 }
Manu Abrahamb3b96142009-12-04 05:41:11 -030079 dprintk(MANTIS_INFO, 0, "]\n");
Manu Abraham41e840b2009-12-02 21:57:10 -030080
81 return 0;
82}
83
84static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg)
85{
86 int i;
Manu Abraham3e978a82009-12-04 05:56:35 -030087 u32 txd = 0, stat, trials;
Manu Abraham41e840b2009-12-02 21:57:10 -030088
Manu Abrahamb3b96142009-12-04 05:41:11 -030089 dprintk(MANTIS_INFO, 0, " %s: Address=[0x%02x] <W>[ ",
Manu Abraham715d3412009-12-03 05:37:51 -030090 __func__, msg->addr);
91
Manu Abraham41e840b2009-12-02 21:57:10 -030092 for (i = 0; i < msg->len; i++) {
Manu Abrahamb3b96142009-12-04 05:41:11 -030093 dprintk(MANTIS_INFO, 0, "%02x ", msg->buf[i]);
Manu Abraham41e840b2009-12-02 21:57:10 -030094 txd = (msg->addr << 25) | (msg->buf[i] << 8)
95 | MANTIS_I2C_RATE_3
96 | MANTIS_I2C_STOP
97 | MANTIS_I2C_PGMODE;
98
99 if (i == (msg->len - 1))
100 txd &= ~MANTIS_I2C_STOP;
101
Manu Abrahamdf0cca12009-12-02 22:07:24 -0300102 mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
Manu Abraham41e840b2009-12-02 21:57:10 -0300103 mmwrite(txd, MANTIS_I2CDATA_CTL);
Manu Abraham3e978a82009-12-04 05:56:35 -0300104
105 /* wait for xfer completion */
Manu Abrahambc832fa2009-12-04 05:57:28 -0300106 for (trials = 0; trials < TRIALS; trials++) {
Manu Abraham3e978a82009-12-04 05:56:35 -0300107 stat = mmread(MANTIS_INT_STAT);
108 if (stat & MANTIS_INT_I2CDONE)
109 break;
Manu Abraham41e840b2009-12-02 21:57:10 -0300110 }
Manu Abrahambc832fa2009-12-04 05:57:28 -0300111
112 dprintk(MANTIS_TMG, 0, "I2CDONE: trials=%d\n", trials);
113
114 /* wait for xfer completion */
115 for (trials = 0; trials < TRIALS; trials++) {
116 stat = mmread(MANTIS_INT_STAT);
117 if (stat & MANTIS_INT_I2CRACK)
118 break;
Manu Abrahambc832fa2009-12-04 05:57:28 -0300119 }
120
121 dprintk(MANTIS_TMG, 0, "I2CRACK: trials=%d\n", trials);
Manu Abraham41e840b2009-12-02 21:57:10 -0300122 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300123 dprintk(MANTIS_INFO, 0, "]\n");
Manu Abraham41e840b2009-12-02 21:57:10 -0300124
125 return 0;
126}
127
128static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
129{
Manu Abraham3e978a82009-12-04 05:56:35 -0300130 int ret = 0, i = 0, trials;
131 u32 stat, data, txd;
Manu Abraham41e840b2009-12-02 21:57:10 -0300132 struct mantis_pci *mantis;
Manu Abraham3e978a82009-12-04 05:56:35 -0300133 struct mantis_hwconfig *config;
Manu Abraham41e840b2009-12-02 21:57:10 -0300134
135 mantis = i2c_get_adapdata(adapter);
Manu Abraham3e978a82009-12-04 05:56:35 -0300136 BUG_ON(!mantis);
137 config = mantis->hwconfig;
138 BUG_ON(!config);
Manu Abraham41e840b2009-12-02 21:57:10 -0300139
Manu Abraham3e978a82009-12-04 05:56:35 -0300140 dprintk(MANTIS_DEBUG, 1, "Messages:%d", num);
141 mutex_lock(&mantis->i2c_lock);
142
143 while (i < num) {
144 /* Byte MODE */
Manu Abrahambc832fa2009-12-04 05:57:28 -0300145 if ((config->i2c_mode & MANTIS_BYTE_MODE) &&
146 ((i + 1) < num) &&
147 (msgs[i].len < 2) &&
148 (msgs[i + 1].len < 2) &&
149 (msgs[i + 1].flags & I2C_M_RD)) {
Manu Abraham3e978a82009-12-04 05:56:35 -0300150
151 dprintk(MANTIS_DEBUG, 0, " Byte MODE:\n");
152
153 /* Read operation */
154 txd = msgs[i].addr << 25 | (0x1 << 24)
155 | (msgs[i].buf[0] << 16)
156 | MANTIS_I2C_RATE_3;
157
158 mmwrite(txd, MANTIS_I2CDATA_CTL);
159 /* wait for xfer completion */
Manu Abrahambc832fa2009-12-04 05:57:28 -0300160 for (trials = 0; trials < TRIALS; trials++) {
Manu Abraham3e978a82009-12-04 05:56:35 -0300161 stat = mmread(MANTIS_INT_STAT);
162 if (stat & MANTIS_INT_I2CDONE)
163 break;
164 }
165
166 /* check for xfer completion */
167 if (stat & MANTIS_INT_I2CDONE) {
168 /* check xfer was acknowledged */
169 if (stat & MANTIS_INT_I2CRACK) {
170 data = mmread(MANTIS_I2CDATA_CTL);
171 msgs[i + 1].buf[0] = (data >> 8) & 0xff;
172 dprintk(MANTIS_DEBUG, 0, " Byte <%d> RXD=0x%02x [%02x]\n", 0x0, data, msgs[i + 1].buf[0]);
173 } else {
174 /* I/O error */
175 dprintk(MANTIS_ERROR, 1, " I/O error, LINE:%d", __LINE__);
176 ret = -EIO;
177 break;
178 }
179 } else {
180 /* I/O error */
181 dprintk(MANTIS_ERROR, 1, " I/O error, LINE:%d", __LINE__);
182 ret = -EIO;
183 break;
184 }
185 i += 2; /* Write/Read operation in one go */
186 }
187
188 if (i < num) {
189 if (msgs[i].flags & I2C_M_RD)
190 ret = mantis_i2c_read(mantis, &msgs[i]);
191 else
192 ret = mantis_i2c_write(mantis, &msgs[i]);
193
194 i++;
195 if (ret < 0)
196 goto bail_out;
197 }
198
Manu Abraham41e840b2009-12-02 21:57:10 -0300199 }
Manu Abraham3e978a82009-12-04 05:56:35 -0300200
Manu Abrahame2f67e42009-12-03 05:42:10 -0300201 mutex_unlock(&mantis->i2c_lock);
Manu Abraham41e840b2009-12-02 21:57:10 -0300202
203 return num;
Manu Abraham99d96e42009-12-04 04:40:16 -0300204
205bail_out:
206 mutex_unlock(&mantis->i2c_lock);
207 return ret;
Manu Abraham41e840b2009-12-02 21:57:10 -0300208}
209
210static u32 mantis_i2c_func(struct i2c_adapter *adapter)
211{
212 return I2C_FUNC_SMBUS_EMUL;
213}
214
215static struct i2c_algorithm mantis_algo = {
216 .master_xfer = mantis_i2c_xfer,
217 .functionality = mantis_i2c_func,
218};
219
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800220int mantis_i2c_init(struct mantis_pci *mantis)
Manu Abraham41e840b2009-12-02 21:57:10 -0300221{
Mauro Carvalho Chehab6fe2d692015-06-18 14:31:38 -0300222 u32 intstat;
Manu Abraham8154bad2009-12-03 05:46:06 -0300223 struct i2c_adapter *i2c_adapter = &mantis->adapter;
224 struct pci_dev *pdev = mantis->pdev;
Manu Abraham41e840b2009-12-02 21:57:10 -0300225
Manu Abrahamb3b96142009-12-04 05:41:11 -0300226 init_waitqueue_head(&mantis->i2c_wq);
Manu Abrahame2f67e42009-12-03 05:42:10 -0300227 mutex_init(&mantis->i2c_lock);
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300228 strncpy(i2c_adapter->name, "Mantis I2C", sizeof(i2c_adapter->name));
Manu Abraham8154bad2009-12-03 05:46:06 -0300229 i2c_set_adapdata(i2c_adapter, mantis);
230
Manu Abrahamb3b96142009-12-04 05:41:11 -0300231 i2c_adapter->owner = THIS_MODULE;
Manu Abrahamb3b96142009-12-04 05:41:11 -0300232 i2c_adapter->algo = &mantis_algo;
233 i2c_adapter->algo_data = NULL;
Manu Abrahamb3b96142009-12-04 05:41:11 -0300234 i2c_adapter->timeout = 500;
235 i2c_adapter->retries = 3;
236 i2c_adapter->dev.parent = &pdev->dev;
237
Manu Abraham8154bad2009-12-03 05:46:06 -0300238 mantis->i2c_rc = i2c_add_adapter(i2c_adapter);
Manu Abraham41e840b2009-12-02 21:57:10 -0300239 if (mantis->i2c_rc < 0)
240 return mantis->i2c_rc;
241
Manu Abrahamb3b96142009-12-04 05:41:11 -0300242 dprintk(MANTIS_DEBUG, 1, "Initializing I2C ..");
Manu Abraham41e840b2009-12-02 21:57:10 -0300243
Manu Abraham41e840b2009-12-02 21:57:10 -0300244 intstat = mmread(MANTIS_INT_STAT);
Mauro Carvalho Chehab6fe2d692015-06-18 14:31:38 -0300245 mmread(MANTIS_INT_MASK);
Manu Abraham41e840b2009-12-02 21:57:10 -0300246 mmwrite(intstat, MANTIS_INT_STAT);
Manu Abraham3e978a82009-12-04 05:56:35 -0300247 dprintk(MANTIS_DEBUG, 1, "Disabling I2C interrupt");
Jan Klötzkea96762d2015-06-06 16:58:13 -0300248 mantis_mask_ints(mantis, MANTIS_INT_I2CDONE);
Manu Abraham41e840b2009-12-02 21:57:10 -0300249
250 return 0;
251}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300252EXPORT_SYMBOL_GPL(mantis_i2c_init);
Manu Abraham41e840b2009-12-02 21:57:10 -0300253
Mauro Carvalho Chehab4cf0b3f2009-12-18 09:58:46 -0200254int mantis_i2c_exit(struct mantis_pci *mantis)
Manu Abraham41e840b2009-12-02 21:57:10 -0300255{
Manu Abraham3e978a82009-12-04 05:56:35 -0300256 dprintk(MANTIS_DEBUG, 1, "Disabling I2C interrupt");
Jan Klötzkea96762d2015-06-06 16:58:13 -0300257 mantis_mask_ints(mantis, MANTIS_INT_I2CDONE);
Manu Abraham3e978a82009-12-04 05:56:35 -0300258
Manu Abrahamb3b96142009-12-04 05:41:11 -0300259 dprintk(MANTIS_DEBUG, 1, "Removing I2C adapter");
Lars-Peter Clausenbf51a8c2013-03-09 08:16:46 +0000260 i2c_del_adapter(&mantis->adapter);
261
262 return 0;
Manu Abraham41e840b2009-12-02 21:57:10 -0300263}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300264EXPORT_SYMBOL_GPL(mantis_i2c_exit);