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