| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters | 
|  | 3 | *    Copyright (C) 2004 Arcom Control Systems | 
|  | 4 | * | 
|  | 5 | *  This program is free software; you can redistribute it and/or modify | 
|  | 6 | *  it under the terms of the GNU General Public License as published by | 
|  | 7 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 8 | *  (at your option) any later version. | 
|  | 9 | * | 
|  | 10 | *  This program is distributed in the hope that it will be useful, | 
|  | 11 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 13 | *  GNU General Public License for more details. | 
|  | 14 | * | 
|  | 15 | *  You should have received a copy of the GNU General Public License | 
|  | 16 | *  along with this program; if not, write to the Free Software | 
|  | 17 | *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 18 | */ | 
|  | 19 |  | 
|  | 20 | #include <linux/kernel.h> | 
|  | 21 | #include <linux/module.h> | 
|  | 22 | #include <linux/moduleparam.h> | 
|  | 23 | #include <linux/delay.h> | 
|  | 24 | #include <linux/slab.h> | 
|  | 25 | #include <linux/init.h> | 
|  | 26 | #include <linux/errno.h> | 
|  | 27 | #include <linux/i2c.h> | 
|  | 28 | #include <linux/i2c-algo-pca.h> | 
|  | 29 | #include "i2c-algo-pca.h" | 
|  | 30 |  | 
|  | 31 | #define DRIVER "i2c-algo-pca" | 
|  | 32 |  | 
|  | 33 | #define DEB1(fmt, args...) do { if (i2c_debug>=1) printk(fmt, ## args); } while(0) | 
|  | 34 | #define DEB2(fmt, args...) do { if (i2c_debug>=2) printk(fmt, ## args); } while(0) | 
|  | 35 | #define DEB3(fmt, args...) do { if (i2c_debug>=3) printk(fmt, ## args); } while(0) | 
|  | 36 |  | 
| Jean Delvare | 6050709 | 2005-09-25 16:23:07 +0200 | [diff] [blame] | 37 | static int i2c_debug; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | #define pca_outw(adap, reg, val) adap->write_byte(adap, reg, val) | 
|  | 40 | #define pca_inw(adap, reg) adap->read_byte(adap, reg) | 
|  | 41 |  | 
|  | 42 | #define pca_status(adap) pca_inw(adap, I2C_PCA_STA) | 
|  | 43 | #define pca_clock(adap) adap->get_clock(adap) | 
|  | 44 | #define pca_own(adap) adap->get_own(adap) | 
|  | 45 | #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val) | 
|  | 46 | #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON) | 
|  | 47 | #define pca_wait(adap) adap->wait_for_interrupt(adap) | 
|  | 48 |  | 
|  | 49 | /* | 
|  | 50 | * Generate a start condition on the i2c bus. | 
|  | 51 | * | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 52 | * returns after the start condition has occurred | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | */ | 
|  | 54 | static 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 Klauser | 46b615f | 2005-05-19 22:27:23 +0200 | [diff] [blame] | 65 | * Generate a repeated start condition on the i2c bus | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 67 | * return after the repeated start condition has occurred | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | */ | 
|  | 69 | static 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 Klauser | 46b615f | 2005-05-19 22:27:23 +0200 | [diff] [blame] | 85 | * part returns the idle state (0xf8). Hence we don't need to | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * pca_wait here. | 
|  | 87 | */ | 
|  | 88 | static 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 | */ | 
|  | 102 | static void pca_address(struct i2c_algo_pca_data *adap, | 
|  | 103 | 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; | 
|  | 111 | DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n", | 
|  | 112 | msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr); | 
|  | 113 |  | 
|  | 114 | 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 | */ | 
|  | 127 | static void pca_tx_byte(struct i2c_algo_pca_data *adap, | 
|  | 128 | __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 | */ | 
|  | 145 | static void pca_rx_byte(struct i2c_algo_pca_data *adap, | 
|  | 146 | __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 |  | 
|  | 152 | /* | 
|  | 153 | * Setup ACK or NACK for next received byte and wait for it to arrive. | 
|  | 154 | * | 
|  | 155 | * Returns after next byte has arrived. | 
|  | 156 | */ | 
|  | 157 | static void pca_rx_ack(struct i2c_algo_pca_data *adap, | 
|  | 158 | 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 |  | 
|  | 171 | /* | 
|  | 172 | * Reset the i2c bus / SIO | 
|  | 173 | */ | 
|  | 174 | static void pca_reset(struct i2c_algo_pca_data *adap) | 
|  | 175 | { | 
|  | 176 | /* apparently only an external reset will do it. not a lot can be done */ | 
|  | 177 | printk(KERN_ERR DRIVER ": Haven't figured out how to do a reset yet\n"); | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | static int pca_xfer(struct i2c_adapter *i2c_adap, | 
|  | 181 | struct i2c_msg *msgs, | 
|  | 182 | int num) | 
|  | 183 | { | 
|  | 184 | struct i2c_algo_pca_data *adap = i2c_adap->algo_data; | 
|  | 185 | struct i2c_msg *msg = NULL; | 
|  | 186 | int curmsg; | 
|  | 187 | int numbytes = 0; | 
|  | 188 | int state; | 
|  | 189 | int ret; | 
| Ian Campbell | 48edcb6 | 2005-08-10 08:51:16 +0100 | [diff] [blame] | 190 | int timeout = 100; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 |  | 
| Ian Campbell | 48edcb6 | 2005-08-10 08:51:16 +0100 | [diff] [blame] | 192 | while ((state = pca_status(adap)) != 0xf8 && timeout--) { | 
|  | 193 | msleep(10); | 
|  | 194 | } | 
|  | 195 | if (state != 0xf8) { | 
|  | 196 | dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state); | 
|  | 197 | return -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } | 
|  | 199 |  | 
|  | 200 | DEB1("{{{ XFER %d messages\n", num); | 
|  | 201 |  | 
|  | 202 | if (i2c_debug>=2) { | 
|  | 203 | for (curmsg = 0; curmsg < num; curmsg++) { | 
|  | 204 | int addr, i; | 
|  | 205 | msg = &msgs[curmsg]; | 
|  | 206 |  | 
|  | 207 | addr = (0x7f & msg->addr) ; | 
|  | 208 |  | 
|  | 209 | if (msg->flags & I2C_M_RD ) | 
|  | 210 | printk(KERN_INFO "    [%02d] RD %d bytes from %#02x [%#02x, ...]\n", | 
|  | 211 | curmsg, msg->len, addr, (addr<<1) | 1); | 
|  | 212 | else { | 
|  | 213 | printk(KERN_INFO "    [%02d] WR %d bytes to %#02x [%#02x%s", | 
|  | 214 | curmsg, msg->len, addr, addr<<1, | 
|  | 215 | msg->len == 0 ? "" : ", "); | 
|  | 216 | for(i=0; i < msg->len; i++) | 
|  | 217 | printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", "); | 
|  | 218 | printk("]\n"); | 
|  | 219 | } | 
|  | 220 | } | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | curmsg = 0; | 
|  | 224 | ret = -EREMOTEIO; | 
|  | 225 | while (curmsg < num) { | 
|  | 226 | state = pca_status(adap); | 
|  | 227 |  | 
|  | 228 | DEB3("STATE is 0x%02x\n", state); | 
|  | 229 | msg = &msgs[curmsg]; | 
|  | 230 |  | 
|  | 231 | switch (state) { | 
|  | 232 | case 0xf8: /* On reset or stop the bus is idle */ | 
|  | 233 | pca_start(adap); | 
|  | 234 | break; | 
|  | 235 |  | 
|  | 236 | case 0x08: /* A START condition has been transmitted */ | 
|  | 237 | case 0x10: /* A repeated start condition has been transmitted */ | 
|  | 238 | pca_address(adap, msg); | 
|  | 239 | break; | 
|  | 240 |  | 
|  | 241 | case 0x18: /* SLA+W has been transmitted; ACK has been received */ | 
|  | 242 | case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */ | 
|  | 243 | if (numbytes < msg->len) { | 
|  | 244 | pca_tx_byte(adap, msg->buf[numbytes]); | 
|  | 245 | numbytes++; | 
|  | 246 | break; | 
|  | 247 | } | 
|  | 248 | curmsg++; numbytes = 0; | 
|  | 249 | if (curmsg == num) | 
|  | 250 | pca_stop(adap); | 
|  | 251 | else | 
|  | 252 | pca_repeated_start(adap); | 
|  | 253 | break; | 
|  | 254 |  | 
|  | 255 | case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */ | 
|  | 256 | DEB2("NOT ACK received after SLA+W\n"); | 
|  | 257 | pca_stop(adap); | 
|  | 258 | goto out; | 
|  | 259 |  | 
|  | 260 | case 0x40: /* SLA+R has been transmitted; ACK has been received */ | 
|  | 261 | pca_rx_ack(adap, msg->len > 1); | 
|  | 262 | break; | 
|  | 263 |  | 
|  | 264 | case 0x50: /* Data bytes has been received; ACK has been returned */ | 
|  | 265 | if (numbytes < msg->len) { | 
|  | 266 | pca_rx_byte(adap, &msg->buf[numbytes], 1); | 
|  | 267 | numbytes++; | 
|  | 268 | pca_rx_ack(adap, numbytes < msg->len - 1); | 
|  | 269 | break; | 
|  | 270 | } | 
|  | 271 | curmsg++; numbytes = 0; | 
|  | 272 | if (curmsg == num) | 
|  | 273 | pca_stop(adap); | 
|  | 274 | else | 
|  | 275 | pca_repeated_start(adap); | 
|  | 276 | break; | 
|  | 277 |  | 
|  | 278 | case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */ | 
|  | 279 | DEB2("NOT ACK received after SLA+R\n"); | 
|  | 280 | pca_stop(adap); | 
|  | 281 | goto out; | 
|  | 282 |  | 
|  | 283 | case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */ | 
|  | 284 | DEB2("NOT ACK received after data byte\n"); | 
|  | 285 | goto out; | 
|  | 286 |  | 
|  | 287 | case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */ | 
|  | 288 | DEB2("Arbitration lost\n"); | 
|  | 289 | goto out; | 
|  | 290 |  | 
|  | 291 | case 0x58: /* Data byte has been received; NOT ACK has been returned */ | 
|  | 292 | if ( numbytes == msg->len - 1 ) { | 
|  | 293 | pca_rx_byte(adap, &msg->buf[numbytes], 0); | 
|  | 294 | curmsg++; numbytes = 0; | 
|  | 295 | if (curmsg == num) | 
|  | 296 | pca_stop(adap); | 
|  | 297 | else | 
|  | 298 | pca_repeated_start(adap); | 
|  | 299 | } else { | 
|  | 300 | DEB2("NOT ACK sent after data byte received. " | 
|  | 301 | "Not final byte. numbytes %d. len %d\n", | 
|  | 302 | numbytes, msg->len); | 
|  | 303 | pca_stop(adap); | 
|  | 304 | goto out; | 
|  | 305 | } | 
|  | 306 | break; | 
|  | 307 | case 0x70: /* Bus error - SDA stuck low */ | 
|  | 308 | DEB2("BUS ERROR - SDA Stuck low\n"); | 
|  | 309 | pca_reset(adap); | 
|  | 310 | goto out; | 
|  | 311 | case 0x90: /* Bus error - SCL stuck low */ | 
|  | 312 | DEB2("BUS ERROR - SCL Stuck low\n"); | 
|  | 313 | pca_reset(adap); | 
|  | 314 | goto out; | 
|  | 315 | case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */ | 
|  | 316 | DEB2("BUS ERROR - Illegal START or STOP\n"); | 
|  | 317 | pca_reset(adap); | 
|  | 318 | goto out; | 
|  | 319 | default: | 
|  | 320 | printk(KERN_ERR DRIVER ": unhandled SIO state 0x%02x\n", state); | 
|  | 321 | break; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | ret = curmsg; | 
|  | 327 | out: | 
|  | 328 | DEB1(KERN_CRIT "}}} transfered %d/%d messages. " | 
|  | 329 | "status is %#04x. control is %#04x\n", | 
|  | 330 | curmsg, num, pca_status(adap), | 
|  | 331 | pca_get_con(adap)); | 
|  | 332 | return ret; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | static u32 pca_func(struct i2c_adapter *adap) | 
|  | 336 | { | 
|  | 337 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | static int pca_init(struct i2c_algo_pca_data *adap) | 
|  | 341 | { | 
|  | 342 | static int freqs[] = {330,288,217,146,88,59,44,36}; | 
|  | 343 | int own, clock; | 
|  | 344 |  | 
|  | 345 | own = pca_own(adap); | 
|  | 346 | clock = pca_clock(adap); | 
|  | 347 | DEB1(KERN_INFO DRIVER ": own address is %#04x\n", own); | 
|  | 348 | DEB1(KERN_INFO DRIVER ": clock freqeuncy is %dkHz\n", freqs[clock]); | 
|  | 349 |  | 
|  | 350 | pca_outw(adap, I2C_PCA_ADR, own << 1); | 
|  | 351 |  | 
|  | 352 | pca_set_con(adap, I2C_PCA_CON_ENSIO | clock); | 
|  | 353 | udelay(500); /* 500 µs for oscilator to stabilise */ | 
|  | 354 |  | 
|  | 355 | return 0; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | static struct i2c_algorithm pca_algo = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | .master_xfer	= pca_xfer, | 
|  | 360 | .functionality	= pca_func, | 
|  | 361 | }; | 
|  | 362 |  | 
|  | 363 | /* | 
|  | 364 | * registering functions to load algorithms at runtime | 
|  | 365 | */ | 
|  | 366 | int i2c_pca_add_bus(struct i2c_adapter *adap) | 
|  | 367 | { | 
|  | 368 | struct i2c_algo_pca_data *pca_adap = adap->algo_data; | 
|  | 369 | int rval; | 
|  | 370 |  | 
|  | 371 | /* register new adapter to i2c module... */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | adap->algo = &pca_algo; | 
|  | 373 |  | 
|  | 374 | adap->timeout = 100;		/* default values, should	*/ | 
|  | 375 | adap->retries = 3;		/* be replaced by defines	*/ | 
|  | 376 |  | 
| Mark M. Hoffman | b39ad0c | 2006-07-01 17:16:06 +0200 | [diff] [blame] | 377 | if ((rval = pca_init(pca_adap))) | 
|  | 378 | return rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 |  | 
| Mark M. Hoffman | b39ad0c | 2006-07-01 17:16:06 +0200 | [diff] [blame] | 380 | rval = i2c_add_adapter(adap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 |  | 
|  | 382 | return rval; | 
|  | 383 | } | 
|  | 384 |  | 
|  | 385 | int i2c_pca_del_bus(struct i2c_adapter *adap) | 
|  | 386 | { | 
|  | 387 | return i2c_del_adapter(adap); | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | EXPORT_SYMBOL(i2c_pca_add_bus); | 
|  | 391 | EXPORT_SYMBOL(i2c_pca_del_bus); | 
|  | 392 |  | 
|  | 393 | MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>"); | 
|  | 394 | MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm"); | 
|  | 395 | MODULE_LICENSE("GPL"); | 
|  | 396 |  | 
|  | 397 | module_param(i2c_debug, int, 0); |