blob: 943d70ee5d5984220dc7f0253aff270707b6474c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Wolfram Sang3d438292008-04-22 22:16:46 +02002 * i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2004 Arcom Control Systems
Wolfram Sangc01b0832008-04-22 22:16:46 +02004 * Copyright (C) 2008 Pengutronix
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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/kernel.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
26#include <linux/errno.h>
27#include <linux/i2c.h>
28#include <linux/i2c-algo-pca.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Frank Seidelbac3e7c2009-03-28 21:34:44 +010030#define DEB1(fmt, args...) do { if (i2c_debug >= 1) \
31 printk(KERN_DEBUG fmt, ## args); } while (0)
32#define DEB2(fmt, args...) do { if (i2c_debug >= 2) \
33 printk(KERN_DEBUG fmt, ## args); } while (0)
34#define DEB3(fmt, args...) do { if (i2c_debug >= 3) \
35 printk(KERN_DEBUG fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Jean Delvare60507092005-09-25 16:23:07 +020037static int i2c_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Wolfram Sangc01b0832008-04-22 22:16:46 +020039#define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
40#define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
Wolfram Sangc01b0832008-04-22 22:16:46 +020043#define pca_clock(adap) adap->i2c_clock
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
45#define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
Wolfram Sangc01b0832008-04-22 22:16:46 +020046#define pca_wait(adap) adap->wait_for_completion(adap->data)
47#define pca_reset(adap) adap->reset_chip(adap->data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Generate a start condition on the i2c bus.
51 *
Steven Cole44bbe872005-05-03 18:21:25 -060052 * returns after the start condition has occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54static void pca_start(struct i2c_algo_pca_data *adap)
55{
56 int sta = pca_get_con(adap);
57 DEB2("=== START\n");
58 sta |= I2C_PCA_CON_STA;
59 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
60 pca_set_con(adap, sta);
61 pca_wait(adap);
62}
63
64/*
Tobias Klauser46b615f2005-05-19 22:27:23 +020065 * Generate a repeated start condition on the i2c bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
Steven Cole44bbe872005-05-03 18:21:25 -060067 * return after the repeated start condition has occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
69static void pca_repeated_start(struct i2c_algo_pca_data *adap)
70{
71 int sta = pca_get_con(adap);
72 DEB2("=== REPEATED START\n");
73 sta |= I2C_PCA_CON_STA;
74 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
75 pca_set_con(adap, sta);
76 pca_wait(adap);
77}
78
79/*
80 * Generate a stop condition on the i2c bus
81 *
82 * returns after the stop condition has been generated
83 *
84 * STOPs do not generate an interrupt or set the SI flag, since the
Tobias Klauser46b615f2005-05-19 22:27:23 +020085 * part returns the idle state (0xf8). Hence we don't need to
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * pca_wait here.
87 */
88static void pca_stop(struct i2c_algo_pca_data *adap)
89{
90 int sta = pca_get_con(adap);
91 DEB2("=== STOP\n");
92 sta |= I2C_PCA_CON_STO;
93 sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
94 pca_set_con(adap, sta);
95}
96
97/*
98 * Send the slave address and R/W bit
99 *
100 * returns after the address has been sent
101 */
Wolfram Sang3d438292008-04-22 22:16:46 +0200102static void pca_address(struct i2c_algo_pca_data *adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct i2c_msg *msg)
104{
105 int sta = pca_get_con(adap);
106 int addr;
107
108 addr = ( (0x7f & msg->addr) << 1 );
109 if (msg->flags & I2C_M_RD )
110 addr |= 1;
Wolfram Sang3d438292008-04-22 22:16:46 +0200111 DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
Wolfram Sang3d438292008-04-22 22:16:46 +0200113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 pca_outw(adap, I2C_PCA_DAT, addr);
115
116 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
117 pca_set_con(adap, sta);
118
119 pca_wait(adap);
120}
121
122/*
123 * Transmit a byte.
124 *
125 * Returns after the byte has been transmitted
126 */
Wolfram Sang3d438292008-04-22 22:16:46 +0200127static void pca_tx_byte(struct i2c_algo_pca_data *adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 __u8 b)
129{
130 int sta = pca_get_con(adap);
131 DEB2("=== WRITE %#04x\n", b);
132 pca_outw(adap, I2C_PCA_DAT, b);
133
134 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
135 pca_set_con(adap, sta);
136
137 pca_wait(adap);
138}
139
140/*
141 * Receive a byte
142 *
143 * returns immediately.
144 */
Wolfram Sang3d438292008-04-22 22:16:46 +0200145static void pca_rx_byte(struct i2c_algo_pca_data *adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 __u8 *b, int ack)
147{
148 *b = pca_inw(adap, I2C_PCA_DAT);
149 DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
150}
151
Wolfram Sang3d438292008-04-22 22:16:46 +0200152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * Setup ACK or NACK for next received byte and wait for it to arrive.
154 *
155 * Returns after next byte has arrived.
156 */
Wolfram Sang3d438292008-04-22 22:16:46 +0200157static void pca_rx_ack(struct i2c_algo_pca_data *adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int ack)
159{
160 int sta = pca_get_con(adap);
161
162 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
163
164 if ( ack )
165 sta |= I2C_PCA_CON_AA;
166
167 pca_set_con(adap, sta);
168 pca_wait(adap);
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static int pca_xfer(struct i2c_adapter *i2c_adap,
172 struct i2c_msg *msgs,
173 int num)
174{
175 struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
176 struct i2c_msg *msg = NULL;
177 int curmsg;
178 int numbytes = 0;
179 int state;
180 int ret;
Wolfram Sangc01b0832008-04-22 22:16:46 +0200181 int timeout = i2c_adap->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Ian Campbell48edcb62005-08-10 08:51:16 +0100183 while ((state = pca_status(adap)) != 0xf8 && timeout--) {
184 msleep(10);
185 }
186 if (state != 0xf8) {
187 dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state);
Wolfram Sangc80ebe72008-07-14 22:38:26 +0200188 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
191 DEB1("{{{ XFER %d messages\n", num);
192
193 if (i2c_debug>=2) {
194 for (curmsg = 0; curmsg < num; curmsg++) {
195 int addr, i;
196 msg = &msgs[curmsg];
Wolfram Sang3d438292008-04-22 22:16:46 +0200197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 addr = (0x7f & msg->addr) ;
Wolfram Sang3d438292008-04-22 22:16:46 +0200199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (msg->flags & I2C_M_RD )
Wolfram Sang3d438292008-04-22 22:16:46 +0200201 printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 curmsg, msg->len, addr, (addr<<1) | 1);
203 else {
Wolfram Sang3d438292008-04-22 22:16:46 +0200204 printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 curmsg, msg->len, addr, addr<<1,
206 msg->len == 0 ? "" : ", ");
207 for(i=0; i < msg->len; i++)
208 printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
209 printk("]\n");
210 }
211 }
212 }
213
214 curmsg = 0;
215 ret = -EREMOTEIO;
216 while (curmsg < num) {
217 state = pca_status(adap);
218
219 DEB3("STATE is 0x%02x\n", state);
220 msg = &msgs[curmsg];
221
222 switch (state) {
223 case 0xf8: /* On reset or stop the bus is idle */
224 pca_start(adap);
225 break;
226
227 case 0x08: /* A START condition has been transmitted */
228 case 0x10: /* A repeated start condition has been transmitted */
229 pca_address(adap, msg);
230 break;
Wolfram Sang3d438292008-04-22 22:16:46 +0200231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 case 0x18: /* SLA+W has been transmitted; ACK has been received */
233 case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
234 if (numbytes < msg->len) {
235 pca_tx_byte(adap, msg->buf[numbytes]);
236 numbytes++;
237 break;
238 }
239 curmsg++; numbytes = 0;
240 if (curmsg == num)
241 pca_stop(adap);
242 else
243 pca_repeated_start(adap);
244 break;
245
246 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
247 DEB2("NOT ACK received after SLA+W\n");
248 pca_stop(adap);
249 goto out;
250
251 case 0x40: /* SLA+R has been transmitted; ACK has been received */
252 pca_rx_ack(adap, msg->len > 1);
253 break;
254
255 case 0x50: /* Data bytes has been received; ACK has been returned */
256 if (numbytes < msg->len) {
257 pca_rx_byte(adap, &msg->buf[numbytes], 1);
258 numbytes++;
259 pca_rx_ack(adap, numbytes < msg->len - 1);
260 break;
261 }
262 curmsg++; numbytes = 0;
263 if (curmsg == num)
264 pca_stop(adap);
265 else
266 pca_repeated_start(adap);
267 break;
268
269 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
270 DEB2("NOT ACK received after SLA+R\n");
271 pca_stop(adap);
272 goto out;
273
274 case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
275 DEB2("NOT ACK received after data byte\n");
276 goto out;
277
278 case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
279 DEB2("Arbitration lost\n");
280 goto out;
Wolfram Sang3d438292008-04-22 22:16:46 +0200281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 case 0x58: /* Data byte has been received; NOT ACK has been returned */
283 if ( numbytes == msg->len - 1 ) {
284 pca_rx_byte(adap, &msg->buf[numbytes], 0);
285 curmsg++; numbytes = 0;
286 if (curmsg == num)
287 pca_stop(adap);
288 else
289 pca_repeated_start(adap);
290 } else {
291 DEB2("NOT ACK sent after data byte received. "
292 "Not final byte. numbytes %d. len %d\n",
293 numbytes, msg->len);
294 pca_stop(adap);
295 goto out;
296 }
297 break;
298 case 0x70: /* Bus error - SDA stuck low */
299 DEB2("BUS ERROR - SDA Stuck low\n");
300 pca_reset(adap);
301 goto out;
302 case 0x90: /* Bus error - SCL stuck low */
303 DEB2("BUS ERROR - SCL Stuck low\n");
304 pca_reset(adap);
305 goto out;
306 case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
307 DEB2("BUS ERROR - Illegal START or STOP\n");
308 pca_reset(adap);
309 goto out;
310 default:
Wolfram Sangc01b0832008-04-22 22:16:46 +0200311 dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
313 }
Wolfram Sang3d438292008-04-22 22:16:46 +0200314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 ret = curmsg;
318 out:
Frank Seidelbac3e7c2009-03-28 21:34:44 +0100319 DEB1("}}} transfered %d/%d messages. "
Wolfram Sang3d438292008-04-22 22:16:46 +0200320 "status is %#04x. control is %#04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 curmsg, num, pca_status(adap),
322 pca_get_con(adap));
323 return ret;
324}
325
326static u32 pca_func(struct i2c_adapter *adap)
327{
328 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
329}
330
Jean Delvare9e11a9f2006-09-03 22:38:52 +0200331static const struct i2c_algorithm pca_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .master_xfer = pca_xfer,
333 .functionality = pca_func,
334};
335
Wolfram Sangc01b0832008-04-22 22:16:46 +0200336static int pca_init(struct i2c_adapter *adap)
337{
338 static int freqs[] = {330,288,217,146,88,59,44,36};
339 int clock;
340 struct i2c_algo_pca_data *pca_data = adap->algo_data;
341
342 if (pca_data->i2c_clock > 7) {
343 printk(KERN_WARNING "%s: Invalid I2C clock speed selected. Trying default.\n",
344 adap->name);
345 pca_data->i2c_clock = I2C_PCA_CON_59kHz;
346 }
347
348 adap->algo = &pca_algo;
349
350 pca_reset(pca_data);
351
352 clock = pca_clock(pca_data);
Frank Seidelbac3e7c2009-03-28 21:34:44 +0100353 printk(KERN_INFO "%s: Clock frequency is %dkHz\n", adap->name,
354 freqs[clock]);
Wolfram Sangc01b0832008-04-22 22:16:46 +0200355
356 pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
357 udelay(500); /* 500 us for oscilator to stabilise */
358
359 return 0;
360}
361
Wolfram Sang3d438292008-04-22 22:16:46 +0200362/*
363 * registering functions to load algorithms at runtime
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365int i2c_pca_add_bus(struct i2c_adapter *adap)
366{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int rval;
368
Wolfram Sangc01b0832008-04-22 22:16:46 +0200369 rval = pca_init(adap);
370 if (rval)
Mark M. Hoffmanb39ad0c2006-07-01 17:16:06 +0200371 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Wolfram Sangc01b0832008-04-22 22:16:46 +0200373 return i2c_add_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375EXPORT_SYMBOL(i2c_pca_add_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Wolfram Sangc01b0832008-04-22 22:16:46 +0200377int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
378{
379 int rval;
380
381 rval = pca_init(adap);
382 if (rval)
383 return rval;
384
385 return i2c_add_numbered_adapter(adap);
386}
387EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
388
389MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
390 "Wolfram Sang <w.sang@pengutronix.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
392MODULE_LICENSE("GPL");
393
394module_param(i2c_debug, int, 0);