2 * i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
3 * Copyright (C) 2004 Arcom Control Systems
4 * Copyright (C) 2008 Pengutronix
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/kernel.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/i2c.h>
28 #include <linux/i2c-algo-pca.h>
30 #define DEB1(fmt, args...) do { if (i2c_debug>=1) printk(fmt, ## args); } while(0)
31 #define DEB2(fmt, args...) do { if (i2c_debug>=2) printk(fmt, ## args); } while(0)
32 #define DEB3(fmt, args...) do { if (i2c_debug>=3) printk(fmt, ## args); } while(0)
36 #define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
37 #define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
39 #define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
40 #define pca_clock(adap) adap->i2c_clock
41 #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
42 #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
43 #define pca_wait(adap) adap->wait_for_completion(adap->data)
44 #define pca_reset(adap) adap->reset_chip(adap->data)
47 * Generate a start condition on the i2c bus.
49 * returns after the start condition has occurred
51 static void pca_start(struct i2c_algo_pca_data *adap)
53 int sta = pca_get_con(adap);
55 sta |= I2C_PCA_CON_STA;
56 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
57 pca_set_con(adap, sta);
62 * Generate a repeated start condition on the i2c bus
64 * return after the repeated start condition has occurred
66 static void pca_repeated_start(struct i2c_algo_pca_data *adap)
68 int sta = pca_get_con(adap);
69 DEB2("=== REPEATED START\n");
70 sta |= I2C_PCA_CON_STA;
71 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
72 pca_set_con(adap, sta);
77 * Generate a stop condition on the i2c bus
79 * returns after the stop condition has been generated
81 * STOPs do not generate an interrupt or set the SI flag, since the
82 * part returns the idle state (0xf8). Hence we don't need to
85 static void pca_stop(struct i2c_algo_pca_data *adap)
87 int sta = pca_get_con(adap);
89 sta |= I2C_PCA_CON_STO;
90 sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
91 pca_set_con(adap, sta);
95 * Send the slave address and R/W bit
97 * returns after the address has been sent
99 static void pca_address(struct i2c_algo_pca_data *adap,
102 int sta = pca_get_con(adap);
105 addr = ( (0x7f & msg->addr) << 1 );
106 if (msg->flags & I2C_M_RD )
108 DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
109 msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
111 pca_outw(adap, I2C_PCA_DAT, addr);
113 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
114 pca_set_con(adap, sta);
122 * Returns after the byte has been transmitted
124 static void pca_tx_byte(struct i2c_algo_pca_data *adap,
127 int sta = pca_get_con(adap);
128 DEB2("=== WRITE %#04x\n", b);
129 pca_outw(adap, I2C_PCA_DAT, b);
131 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
132 pca_set_con(adap, sta);
140 * returns immediately.
142 static void pca_rx_byte(struct i2c_algo_pca_data *adap,
145 *b = pca_inw(adap, I2C_PCA_DAT);
146 DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
150 * Setup ACK or NACK for next received byte and wait for it to arrive.
152 * Returns after next byte has arrived.
154 static void pca_rx_ack(struct i2c_algo_pca_data *adap,
157 int sta = pca_get_con(adap);
159 sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);
162 sta |= I2C_PCA_CON_AA;
164 pca_set_con(adap, sta);
168 static int pca_xfer(struct i2c_adapter *i2c_adap,
169 struct i2c_msg *msgs,
172 struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
173 struct i2c_msg *msg = NULL;
178 int timeout = i2c_adap->timeout;
180 while ((state = pca_status(adap)) != 0xf8 && timeout--) {
184 dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state);
188 DEB1("{{{ XFER %d messages\n", num);
191 for (curmsg = 0; curmsg < num; curmsg++) {
195 addr = (0x7f & msg->addr) ;
197 if (msg->flags & I2C_M_RD )
198 printk(KERN_INFO " [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
199 curmsg, msg->len, addr, (addr<<1) | 1);
201 printk(KERN_INFO " [%02d] WR %d bytes to %#02x [%#02x%s",
202 curmsg, msg->len, addr, addr<<1,
203 msg->len == 0 ? "" : ", ");
204 for(i=0; i < msg->len; i++)
205 printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
213 while (curmsg < num) {
214 state = pca_status(adap);
216 DEB3("STATE is 0x%02x\n", state);
220 case 0xf8: /* On reset or stop the bus is idle */
224 case 0x08: /* A START condition has been transmitted */
225 case 0x10: /* A repeated start condition has been transmitted */
226 pca_address(adap, msg);
229 case 0x18: /* SLA+W has been transmitted; ACK has been received */
230 case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
231 if (numbytes < msg->len) {
232 pca_tx_byte(adap, msg->buf[numbytes]);
236 curmsg++; numbytes = 0;
240 pca_repeated_start(adap);
243 case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
244 DEB2("NOT ACK received after SLA+W\n");
248 case 0x40: /* SLA+R has been transmitted; ACK has been received */
249 pca_rx_ack(adap, msg->len > 1);
252 case 0x50: /* Data bytes has been received; ACK has been returned */
253 if (numbytes < msg->len) {
254 pca_rx_byte(adap, &msg->buf[numbytes], 1);
256 pca_rx_ack(adap, numbytes < msg->len - 1);
259 curmsg++; numbytes = 0;
263 pca_repeated_start(adap);
266 case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
267 DEB2("NOT ACK received after SLA+R\n");
271 case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
272 DEB2("NOT ACK received after data byte\n");
275 case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
276 DEB2("Arbitration lost\n");
279 case 0x58: /* Data byte has been received; NOT ACK has been returned */
280 if ( numbytes == msg->len - 1 ) {
281 pca_rx_byte(adap, &msg->buf[numbytes], 0);
282 curmsg++; numbytes = 0;
286 pca_repeated_start(adap);
288 DEB2("NOT ACK sent after data byte received. "
289 "Not final byte. numbytes %d. len %d\n",
295 case 0x70: /* Bus error - SDA stuck low */
296 DEB2("BUS ERROR - SDA Stuck low\n");
299 case 0x90: /* Bus error - SCL stuck low */
300 DEB2("BUS ERROR - SCL Stuck low\n");
303 case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
304 DEB2("BUS ERROR - Illegal START or STOP\n");
308 dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
316 DEB1(KERN_CRIT "}}} transfered %d/%d messages. "
317 "status is %#04x. control is %#04x\n",
318 curmsg, num, pca_status(adap),
323 static u32 pca_func(struct i2c_adapter *adap)
325 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
328 static const struct i2c_algorithm pca_algo = {
329 .master_xfer = pca_xfer,
330 .functionality = pca_func,
333 static int pca_init(struct i2c_adapter *adap)
335 static int freqs[] = {330,288,217,146,88,59,44,36};
337 struct i2c_algo_pca_data *pca_data = adap->algo_data;
339 if (pca_data->i2c_clock > 7) {
340 printk(KERN_WARNING "%s: Invalid I2C clock speed selected. Trying default.\n",
342 pca_data->i2c_clock = I2C_PCA_CON_59kHz;
345 adap->algo = &pca_algo;
349 clock = pca_clock(pca_data);
350 DEB1(KERN_INFO "%s: Clock frequency is %dkHz\n", adap->name, freqs[clock]);
352 pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
353 udelay(500); /* 500 us for oscilator to stabilise */
359 * registering functions to load algorithms at runtime
361 int i2c_pca_add_bus(struct i2c_adapter *adap)
365 rval = pca_init(adap);
369 return i2c_add_adapter(adap);
371 EXPORT_SYMBOL(i2c_pca_add_bus);
373 int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
377 rval = pca_init(adap);
381 return i2c_add_numbered_adapter(adap);
383 EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
385 MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
386 "Wolfram Sang <w.sang@pengutronix.de>");
387 MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
388 MODULE_LICENSE("GPL");
390 module_param(i2c_debug, int, 0);