Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters |
| 3 | * |
| 4 | * Copyright (C) 1995-1997 Simon G. Vogl |
| 5 | * 1998-2000 Hans Berglund |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 17 | * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and |
| 18 | * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey |
| 19 | * <mbailey@littlefeet-inc.com> |
| 20 | * |
| 21 | * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple |
| 22 | * messages, proper stop/repstart signaling during receive, added detect code |
| 23 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/errno.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/i2c-algo-pcf.h> |
| 31 | #include "i2c-algo-pcf.h" |
| 32 | |
| 33 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 34 | #define DEB2(x) if (i2c_debug >= 2) x |
| 35 | #define DEB3(x) if (i2c_debug >= 3) x /* print several statistical values */ |
| 36 | #define DEBPROTO(x) if (i2c_debug >= 9) x; |
| 37 | /* debug the protocol by showing transferred bits */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #define DEF_TIMEOUT 16 |
| 39 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 40 | /* |
| 41 | * module parameters: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | */ |
| 43 | static int i2c_debug; |
| 44 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 45 | /* setting states on the bus with the right timing: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val) |
| 48 | #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl) |
| 49 | #define get_own(adap) adap->getown(adap->data) |
| 50 | #define get_clock(adap) adap->getclock(adap->data) |
| 51 | #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val) |
| 52 | #define i2c_inb(adap) adap->getpcf(adap->data, 0) |
| 53 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 54 | /* other auxiliary functions */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 56 | static void i2c_start(struct i2c_algo_pcf_data *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Frank Seidel | 154d22b | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 58 | DEBPROTO(printk(KERN_DEBUG "S ")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | set_pcf(adap, 1, I2C_PCF_START); |
| 60 | } |
| 61 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 62 | static void i2c_repstart(struct i2c_algo_pcf_data *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | DEBPROTO(printk(" Sr ")); |
| 65 | set_pcf(adap, 1, I2C_PCF_REPSTART); |
| 66 | } |
| 67 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 68 | static void i2c_stop(struct i2c_algo_pcf_data *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | DEBPROTO(printk("P\n")); |
| 71 | set_pcf(adap, 1, I2C_PCF_STOP); |
| 72 | } |
| 73 | |
Eric Brower | 0573d11 | 2008-07-14 22:38:31 +0200 | [diff] [blame] | 74 | static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status) |
| 75 | { |
| 76 | DEB2(printk(KERN_INFO |
| 77 | "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n", |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 78 | *status)); |
| 79 | /* |
| 80 | * Cleanup from LAB -- reset and enable ESO. |
Eric Brower | 0573d11 | 2008-07-14 22:38:31 +0200 | [diff] [blame] | 81 | * This resets the PCF8584; since we've lost the bus, no |
| 82 | * further attempts should be made by callers to clean up |
| 83 | * (no i2c_stop() etc.) |
| 84 | */ |
| 85 | set_pcf(adap, 1, I2C_PCF_PIN); |
| 86 | set_pcf(adap, 1, I2C_PCF_ESO); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 87 | /* |
| 88 | * We pause for a time period sufficient for any running |
Eric Brower | 0573d11 | 2008-07-14 22:38:31 +0200 | [diff] [blame] | 89 | * I2C transaction to complete -- the arbitration logic won't |
| 90 | * work properly until the next START is seen. |
| 91 | * It is assumed the bus driver or client has set a proper value. |
| 92 | * |
| 93 | * REVISIT: should probably use msleep instead of mdelay if we |
| 94 | * know we can sleep. |
| 95 | */ |
| 96 | if (adap->lab_mdelay) |
| 97 | mdelay(adap->lab_mdelay); |
| 98 | |
| 99 | DEB2(printk(KERN_INFO |
| 100 | "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n", |
| 101 | get_pcf(adap, 1))); |
| 102 | } |
| 103 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 104 | static int wait_for_bb(struct i2c_algo_pcf_data *adap) |
| 105 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | int timeout = DEF_TIMEOUT; |
| 108 | int status; |
| 109 | |
| 110 | status = get_pcf(adap, 1); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 111 | |
Roel Kluin | 94d78e1 | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 112 | while (!(status & I2C_PCF_BB) && --timeout) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | udelay(100); /* wait for 100 us */ |
| 114 | status = get_pcf(adap, 1); |
| 115 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 116 | |
Roel Kluin | 94d78e1 | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 117 | if (timeout == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | printk(KERN_ERR "Timeout waiting for Bus Busy\n"); |
Roel Kluin | 94d78e1 | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 119 | return -ETIMEDOUT; |
| 120 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 121 | |
Roel Kluin | 94d78e1 | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 122 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 125 | static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | int timeout = DEF_TIMEOUT; |
| 129 | |
| 130 | *status = get_pcf(adap, 1); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 131 | |
Roel Kluin | 94d78e1 | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 132 | while ((*status & I2C_PCF_PIN) && --timeout) { |
David Miller | 08e5338 | 2008-10-22 20:21:29 +0200 | [diff] [blame] | 133 | adap->waitforpin(adap->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | *status = get_pcf(adap, 1); |
| 135 | } |
| 136 | if (*status & I2C_PCF_LAB) { |
Eric Brower | 0573d11 | 2008-07-14 22:38:31 +0200 | [diff] [blame] | 137 | handle_lab(adap, status); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 138 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 140 | |
Roel Kluin | 94d78e1 | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 141 | if (timeout == 0) |
| 142 | return -ETIMEDOUT; |
| 143 | |
| 144 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 147 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | * This should perform the 'PCF8584 initialization sequence' as described |
| 149 | * in the Philips IC12 data book (1995, Aug 29). |
| 150 | * There should be a 30 clock cycle wait after reset, I assume this |
| 151 | * has been fulfilled. |
| 152 | * There should be a delay at the end equal to the longest I2C message |
| 153 | * to synchronize the BB-bit (in multimaster systems). How long is |
| 154 | * this? I assume 1 second is always long enough. |
| 155 | * |
| 156 | * vdovikin: added detect code for PCF8584 |
| 157 | */ |
| 158 | static int pcf_init_8584 (struct i2c_algo_pcf_data *adap) |
| 159 | { |
| 160 | unsigned char temp; |
| 161 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 162 | DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n", |
| 163 | get_pcf(adap, 1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* S1=0x80: S0 selected, serial interface off */ |
| 166 | set_pcf(adap, 1, I2C_PCF_PIN); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 167 | /* |
| 168 | * check to see S1 now used as R/W ctrl - |
| 169 | * PCF8584 does that when ESO is zero |
| 170 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) { |
| 172 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp)); |
Uwe Kleine-König | 7d9b48e | 2010-01-28 22:09:43 +0100 | [diff] [blame] | 173 | return -ENXIO; /* definitely not PCF8584 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 176 | /* load own address in S0, effective address is (own << 1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | i2c_outb(adap, get_own(adap)); |
| 178 | /* check it's really written */ |
| 179 | if ((temp = i2c_inb(adap)) != get_own(adap)) { |
| 180 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp)); |
| 181 | return -ENXIO; |
| 182 | } |
| 183 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 184 | /* S1=0xA0, next byte in S2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1); |
| 186 | /* check to see S2 now selected */ |
| 187 | if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) { |
| 188 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp)); |
| 189 | return -ENXIO; |
| 190 | } |
| 191 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 192 | /* load clock register S2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | i2c_outb(adap, get_clock(adap)); |
| 194 | /* check it's really written, the only 5 lowest bits does matter */ |
| 195 | if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) { |
| 196 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp)); |
| 197 | return -ENXIO; |
| 198 | } |
| 199 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 200 | /* Enable serial interface, idle, S0 selected */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | set_pcf(adap, 1, I2C_PCF_IDLE); |
| 202 | |
| 203 | /* check to see PCF is really idled and we can access status register */ |
| 204 | if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) { |
| 205 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp)); |
| 206 | return -ENXIO; |
| 207 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 208 | |
David Miller | a672b4c | 2008-10-22 20:21:30 +0200 | [diff] [blame] | 209 | printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf, |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 215 | int count, int last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | struct i2c_algo_pcf_data *adap = i2c_adap->algo_data; |
| 218 | int wrcount, status, timeout; |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | for (wrcount=0; wrcount<count; ++wrcount) { |
| 221 | DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n", |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 222 | buf[wrcount] & 0xff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | i2c_outb(adap, buf[wrcount]); |
| 224 | timeout = wait_for_pin(adap, &status); |
| 225 | if (timeout) { |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 226 | if (timeout == -EINTR) |
| 227 | return -EINTR; /* arbitration lost */ |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | i2c_stop(adap); |
| 230 | dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n"); |
| 231 | return -EREMOTEIO; /* got a better one ?? */ |
| 232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (status & I2C_PCF_LRB) { |
| 234 | i2c_stop(adap); |
| 235 | dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n"); |
| 236 | return -EREMOTEIO; /* got a better one ?? */ |
| 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 239 | if (last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | i2c_stop(adap); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 241 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | i2c_repstart(adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 244 | return wrcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf, |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 248 | int count, int last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | int i, status; |
| 251 | struct i2c_algo_pcf_data *adap = i2c_adap->algo_data; |
| 252 | int wfp; |
| 253 | |
| 254 | /* increment number of bytes to read by one -- read dummy byte */ |
| 255 | for (i = 0; i <= count; i++) { |
| 256 | |
| 257 | if ((wfp = wait_for_pin(adap, &status))) { |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 258 | if (wfp == -EINTR) |
| 259 | return -EINTR; /* arbitration lost */ |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | i2c_stop(adap); |
| 262 | dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n"); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 263 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | if ((status & I2C_PCF_LRB) && (i != count)) { |
| 267 | i2c_stop(adap); |
| 268 | dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n"); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 269 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (i == count - 1) { |
| 273 | set_pcf(adap, 1, I2C_PCF_ESO); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 274 | } else if (i == count) { |
| 275 | if (last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | i2c_stop(adap); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 277 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | i2c_repstart(adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 280 | |
| 281 | if (i) |
| 282 | buf[i - 1] = i2c_inb(adap); |
| 283 | else |
| 284 | i2c_inb(adap); /* dummy read */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 287 | return i - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
Jean Delvare | 6408a83 | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 291 | static int pcf_doAddress(struct i2c_algo_pcf_data *adap, |
| 292 | struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | unsigned short flags = msg->flags; |
| 295 | unsigned char addr; |
Jean Delvare | 6408a83 | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 296 | |
| 297 | addr = msg->addr << 1; |
| 298 | if (flags & I2C_M_RD) |
| 299 | addr |= 1; |
| 300 | if (flags & I2C_M_REV_DIR_ADDR) |
| 301 | addr ^= 1; |
| 302 | i2c_outb(adap, addr); |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int pcf_xfer(struct i2c_adapter *i2c_adap, |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 308 | struct i2c_msg *msgs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | int num) |
| 310 | { |
| 311 | struct i2c_algo_pcf_data *adap = i2c_adap->algo_data; |
| 312 | struct i2c_msg *pmsg; |
| 313 | int i; |
| 314 | int ret=0, timeout, status; |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 315 | |
David Miller | 3009140 | 2008-10-22 20:21:30 +0200 | [diff] [blame] | 316 | if (adap->xfer_begin) |
| 317 | adap->xfer_begin(adap->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | /* Check for bus busy */ |
| 320 | timeout = wait_for_bb(adap); |
| 321 | if (timeout) { |
| 322 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: " |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 323 | "Timeout waiting for BB in pcf_xfer\n");) |
David Miller | 3009140 | 2008-10-22 20:21:30 +0200 | [diff] [blame] | 324 | i = -EIO; |
| 325 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | for (i = 0;ret >= 0 && i < num; i++) { |
| 329 | pmsg = &msgs[i]; |
| 330 | |
| 331 | DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n", |
| 332 | pmsg->flags & I2C_M_RD ? "read" : "write", |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 333 | pmsg->len, pmsg->addr, i + 1, num);) |
| 334 | |
Jean Delvare | 6408a83 | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 335 | ret = pcf_doAddress(adap, pmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | /* Send START */ |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 338 | if (i == 0) |
| 339 | i2c_start(adap); |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | /* Wait for PIN (pending interrupt NOT) */ |
| 342 | timeout = wait_for_pin(adap, &status); |
| 343 | if (timeout) { |
| 344 | if (timeout == -EINTR) { |
| 345 | /* arbitration lost */ |
David Miller | 3009140 | 2008-10-22 20:21:30 +0200 | [diff] [blame] | 346 | i = -EINTR; |
| 347 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | i2c_stop(adap); |
| 350 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting " |
| 351 | "for PIN(1) in pcf_xfer\n");) |
David Miller | 3009140 | 2008-10-22 20:21:30 +0200 | [diff] [blame] | 352 | i = -EREMOTEIO; |
| 353 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /* Check LRB (last rcvd bit - slave ack) */ |
| 357 | if (status & I2C_PCF_LRB) { |
| 358 | i2c_stop(adap); |
| 359 | DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");) |
David Miller | 3009140 | 2008-10-22 20:21:30 +0200 | [diff] [blame] | 360 | i = -EREMOTEIO; |
| 361 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n", |
| 365 | i, msgs[i].addr, msgs[i].flags, msgs[i].len);) |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | if (pmsg->flags & I2C_M_RD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len, |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 369 | (i + 1 == num)); |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (ret != pmsg->len) { |
| 372 | DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: " |
| 373 | "only read %d bytes.\n",ret)); |
| 374 | } else { |
| 375 | DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret)); |
| 376 | } |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 377 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len, |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 379 | (i + 1 == num)); |
| 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (ret != pmsg->len) { |
| 382 | DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: " |
| 383 | "only wrote %d bytes.\n",ret)); |
| 384 | } else { |
| 385 | DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret)); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
David Miller | 3009140 | 2008-10-22 20:21:30 +0200 | [diff] [blame] | 390 | out: |
| 391 | if (adap->xfer_end) |
| 392 | adap->xfer_end(adap->data); |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 393 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static u32 pcf_func(struct i2c_adapter *adap) |
| 397 | { |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 398 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | |
Jean Delvare | 6408a83 | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 399 | I2C_FUNC_PROTOCOL_MANGLING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 402 | /* exported algorithm data: */ |
Jean Delvare | 9e11a9f | 2006-09-03 22:38:52 +0200 | [diff] [blame] | 403 | static const struct i2c_algorithm pcf_algo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | .master_xfer = pcf_xfer, |
| 405 | .functionality = pcf_func, |
| 406 | }; |
| 407 | |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 408 | /* |
| 409 | * registering functions to load algorithms at runtime |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | */ |
| 411 | int i2c_pcf_add_bus(struct i2c_adapter *adap) |
| 412 | { |
| 413 | struct i2c_algo_pcf_data *pcf_adap = adap->algo_data; |
| 414 | int rval; |
| 415 | |
| 416 | DEB2(dev_dbg(&adap->dev, "hw routines registered.\n")); |
| 417 | |
| 418 | /* register new adapter to i2c module... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | adap->algo = &pcf_algo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Mark M. Hoffman | b39ad0c | 2006-07-01 17:16:06 +0200 | [diff] [blame] | 421 | if ((rval = pcf_init_8584(pcf_adap))) |
| 422 | return rval; |
| 423 | |
| 424 | rval = i2c_add_adapter(adap); |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return rval; |
| 427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | EXPORT_SYMBOL(i2c_pcf_add_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>"); |
| 431 | MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm"); |
| 432 | MODULE_LICENSE("GPL"); |
| 433 | |
| 434 | module_param(i2c_debug, int, S_IRUGO | S_IWUSR); |
| 435 | MODULE_PARM_DESC(i2c_debug, |
Roel Kluin | 0c168ce | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 436 | "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol"); |