i2c-algo-pca.c 9.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  i2c-algo-pca.c i2c driver algorithms for PCA9564 adapters
L
Linus Torvalds 已提交
3
 *    Copyright (C) 2004 Arcom Control Systems
4
 *    Copyright (C) 2008 Pengutronix
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/i2c.h>
#include <linux/i2c-algo-pca.h>

30 31 32 33 34 35
#define DEB1(fmt, args...) do { if (i2c_debug >= 1)			\
				 printk(KERN_DEBUG fmt, ## args); } while (0)
#define DEB2(fmt, args...) do { if (i2c_debug >= 2)			\
				 printk(KERN_DEBUG fmt, ## args); } while (0)
#define DEB3(fmt, args...) do { if (i2c_debug >= 3)			\
				 printk(KERN_DEBUG fmt, ## args); } while (0)
L
Linus Torvalds 已提交
36

37
static int i2c_debug;
L
Linus Torvalds 已提交
38

39 40
#define pca_outw(adap, reg, val) adap->write_byte(adap->data, reg, val)
#define pca_inw(adap, reg) adap->read_byte(adap->data, reg)
L
Linus Torvalds 已提交
41 42

#define pca_status(adap) pca_inw(adap, I2C_PCA_STA)
43
#define pca_clock(adap) adap->i2c_clock
L
Linus Torvalds 已提交
44 45
#define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val)
#define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON)
46 47
#define pca_wait(adap) adap->wait_for_completion(adap->data)
#define pca_reset(adap) adap->reset_chip(adap->data)
L
Linus Torvalds 已提交
48 49 50 51

/*
 * Generate a start condition on the i2c bus.
 *
52
 * returns after the start condition has occurred
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62 63 64
 */
static void pca_start(struct i2c_algo_pca_data *adap)
{
	int sta = pca_get_con(adap);
	DEB2("=== START\n");
	sta |= I2C_PCA_CON_STA;
	sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
	pca_set_con(adap, sta);
	pca_wait(adap);
}

/*
65
 * Generate a repeated start condition on the i2c bus
L
Linus Torvalds 已提交
66
 *
67
 * return after the repeated start condition has occurred
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
 */
static void pca_repeated_start(struct i2c_algo_pca_data *adap)
{
	int sta = pca_get_con(adap);
	DEB2("=== REPEATED START\n");
	sta |= I2C_PCA_CON_STA;
	sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
	pca_set_con(adap, sta);
	pca_wait(adap);
}

/*
 * Generate a stop condition on the i2c bus
 *
 * returns after the stop condition has been generated
 *
 * STOPs do not generate an interrupt or set the SI flag, since the
85
 * part returns the idle state (0xf8). Hence we don't need to
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
 * pca_wait here.
 */
static void pca_stop(struct i2c_algo_pca_data *adap)
{
	int sta = pca_get_con(adap);
	DEB2("=== STOP\n");
	sta |= I2C_PCA_CON_STO;
	sta &= ~(I2C_PCA_CON_STA|I2C_PCA_CON_SI);
	pca_set_con(adap, sta);
}

/*
 * Send the slave address and R/W bit
 *
 * returns after the address has been sent
 */
102
static void pca_address(struct i2c_algo_pca_data *adap,
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110
			struct i2c_msg *msg)
{
	int sta = pca_get_con(adap);
	int addr;

	addr = ( (0x7f & msg->addr) << 1 );
	if (msg->flags & I2C_M_RD )
		addr |= 1;
111
	DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
L
Linus Torvalds 已提交
112
	     msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
113

L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
	pca_outw(adap, I2C_PCA_DAT, addr);

	sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
	pca_set_con(adap, sta);

	pca_wait(adap);
}

/*
 * Transmit a byte.
 *
 * Returns after the byte has been transmitted
 */
127
static void pca_tx_byte(struct i2c_algo_pca_data *adap,
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
			__u8 b)
{
	int sta = pca_get_con(adap);
	DEB2("=== WRITE %#04x\n", b);
	pca_outw(adap, I2C_PCA_DAT, b);

	sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
	pca_set_con(adap, sta);

	pca_wait(adap);
}

/*
 * Receive a byte
 *
 * returns immediately.
 */
145
static void pca_rx_byte(struct i2c_algo_pca_data *adap,
L
Linus Torvalds 已提交
146 147 148 149 150 151
			__u8 *b, int ack)
{
	*b = pca_inw(adap, I2C_PCA_DAT);
	DEB2("=== READ %#04x %s\n", *b, ack ? "ACK" : "NACK");
}

152
/*
L
Linus Torvalds 已提交
153 154 155 156
 * Setup ACK or NACK for next received byte and wait for it to arrive.
 *
 * Returns after next byte has arrived.
 */
157
static void pca_rx_ack(struct i2c_algo_pca_data *adap,
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		       int ack)
{
	int sta = pca_get_con(adap);

	sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI|I2C_PCA_CON_AA);

	if ( ack )
		sta |= I2C_PCA_CON_AA;

	pca_set_con(adap, sta);
	pca_wait(adap);
}

static int pca_xfer(struct i2c_adapter *i2c_adap,
                    struct i2c_msg *msgs,
                    int num)
{
        struct i2c_algo_pca_data *adap = i2c_adap->algo_data;
        struct i2c_msg *msg = NULL;
        int curmsg;
	int numbytes = 0;
	int state;
	int ret;
181
	int timeout = i2c_adap->timeout;
L
Linus Torvalds 已提交
182

183 184 185 186 187
	while ((state = pca_status(adap)) != 0xf8 && timeout--) {
		msleep(10);
	}
	if (state != 0xf8) {
		dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state);
W
Wolfram Sang 已提交
188
		return -EAGAIN;
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196
	}

	DEB1("{{{ XFER %d messages\n", num);

	if (i2c_debug>=2) {
		for (curmsg = 0; curmsg < num; curmsg++) {
			int addr, i;
			msg = &msgs[curmsg];
197

L
Linus Torvalds 已提交
198
			addr = (0x7f & msg->addr) ;
199

L
Linus Torvalds 已提交
200
			if (msg->flags & I2C_M_RD )
201
				printk(KERN_INFO "    [%02d] RD %d bytes from %#02x [%#02x, ...]\n",
L
Linus Torvalds 已提交
202 203
				       curmsg, msg->len, addr, (addr<<1) | 1);
			else {
204
				printk(KERN_INFO "    [%02d] WR %d bytes to %#02x [%#02x%s",
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
				       curmsg, msg->len, addr, addr<<1,
				       msg->len == 0 ? "" : ", ");
				for(i=0; i < msg->len; i++)
					printk("%#04x%s", msg->buf[i], i == msg->len - 1 ? "" : ", ");
				printk("]\n");
			}
		}
	}

	curmsg = 0;
	ret = -EREMOTEIO;
	while (curmsg < num) {
		state = pca_status(adap);

		DEB3("STATE is 0x%02x\n", state);
		msg = &msgs[curmsg];

		switch (state) {
		case 0xf8: /* On reset or stop the bus is idle */
			pca_start(adap);
			break;

		case 0x08: /* A START condition has been transmitted */
		case 0x10: /* A repeated start condition has been transmitted */
			pca_address(adap, msg);
			break;
231

L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		case 0x18: /* SLA+W has been transmitted; ACK has been received */
		case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
			if (numbytes < msg->len) {
				pca_tx_byte(adap, msg->buf[numbytes]);
				numbytes++;
				break;
			}
			curmsg++; numbytes = 0;
			if (curmsg == num)
				pca_stop(adap);
			else
				pca_repeated_start(adap);
			break;

		case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
			DEB2("NOT ACK received after SLA+W\n");
			pca_stop(adap);
			goto out;

		case 0x40: /* SLA+R has been transmitted; ACK has been received */
			pca_rx_ack(adap, msg->len > 1);
			break;

		case 0x50: /* Data bytes has been received; ACK has been returned */
			if (numbytes < msg->len) {
				pca_rx_byte(adap, &msg->buf[numbytes], 1);
				numbytes++;
				pca_rx_ack(adap, numbytes < msg->len - 1);
				break;
			}
			curmsg++; numbytes = 0;
			if (curmsg == num)
				pca_stop(adap);
			else
				pca_repeated_start(adap);
			break;

		case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
			DEB2("NOT ACK received after SLA+R\n");
			pca_stop(adap);
			goto out;

		case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
			DEB2("NOT ACK received after data byte\n");
			goto out;

		case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
			DEB2("Arbitration lost\n");
			goto out;
281

L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		case 0x58: /* Data byte has been received; NOT ACK has been returned */
			if ( numbytes == msg->len - 1 ) {
				pca_rx_byte(adap, &msg->buf[numbytes], 0);
				curmsg++; numbytes = 0;
				if (curmsg == num)
					pca_stop(adap);
				else
					pca_repeated_start(adap);
			} else {
				DEB2("NOT ACK sent after data byte received. "
				     "Not final byte. numbytes %d. len %d\n",
				     numbytes, msg->len);
				pca_stop(adap);
				goto out;
			}
			break;
		case 0x70: /* Bus error - SDA stuck low */
			DEB2("BUS ERROR - SDA Stuck low\n");
			pca_reset(adap);
			goto out;
		case 0x90: /* Bus error - SCL stuck low */
			DEB2("BUS ERROR - SCL Stuck low\n");
			pca_reset(adap);
			goto out;
		case 0x00: /* Bus error during master or slave mode due to illegal START or STOP condition */
			DEB2("BUS ERROR - Illegal START or STOP\n");
			pca_reset(adap);
			goto out;
		default:
311
			dev_err(&i2c_adap->dev, "unhandled SIO state 0x%02x\n", state);
L
Linus Torvalds 已提交
312 313
			break;
		}
314

L
Linus Torvalds 已提交
315 316 317 318
	}

	ret = curmsg;
 out:
319
	DEB1("}}} transfered %d/%d messages. "
320
	     "status is %#04x. control is %#04x\n",
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330
	     curmsg, num, pca_status(adap),
	     pca_get_con(adap));
	return ret;
}

static u32 pca_func(struct i2c_adapter *adap)
{
        return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}

331 332 333 334 335 336
static const struct i2c_algorithm pca_algo = {
	.master_xfer	= pca_xfer,
	.functionality	= pca_func,
};

static int pca_init(struct i2c_adapter *adap)
L
Linus Torvalds 已提交
337 338
{
	static int freqs[] = {330,288,217,146,88,59,44,36};
339 340 341 342 343 344 345 346 347 348
	int clock;
	struct i2c_algo_pca_data *pca_data = adap->algo_data;

	if (pca_data->i2c_clock > 7) {
		printk(KERN_WARNING "%s: Invalid I2C clock speed selected. Trying default.\n",
			adap->name);
		pca_data->i2c_clock = I2C_PCA_CON_59kHz;
	}

	adap->algo = &pca_algo;
L
Linus Torvalds 已提交
349

350
	pca_reset(pca_data);
L
Linus Torvalds 已提交
351

352
	clock = pca_clock(pca_data);
353 354
	printk(KERN_INFO "%s: Clock frequency is %dkHz\n", adap->name,
	       freqs[clock]);
L
Linus Torvalds 已提交
355

356
	pca_set_con(pca_data, I2C_PCA_CON_ENSIO | clock);
357
	udelay(500); /* 500 us for oscilator to stabilise */
L
Linus Torvalds 已提交
358 359 360 361

	return 0;
}

362 363
/*
 * registering functions to load algorithms at runtime
L
Linus Torvalds 已提交
364 365 366 367 368
 */
int i2c_pca_add_bus(struct i2c_adapter *adap)
{
	int rval;

369 370 371
	rval = pca_init(adap);
	if (rval)
		return rval;
L
Linus Torvalds 已提交
372

373 374 375
	return i2c_add_adapter(adap);
}
EXPORT_SYMBOL(i2c_pca_add_bus);
L
Linus Torvalds 已提交
376

377 378 379
int i2c_pca_add_numbered_bus(struct i2c_adapter *adap)
{
	int rval;
L
Linus Torvalds 已提交
380

381 382 383
	rval = pca_init(adap);
	if (rval)
		return rval;
L
Linus Torvalds 已提交
384

385
	return i2c_add_numbered_adapter(adap);
L
Linus Torvalds 已提交
386
}
387
EXPORT_SYMBOL(i2c_pca_add_numbered_bus);
L
Linus Torvalds 已提交
388

389 390
MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>, "
	"Wolfram Sang <w.sang@pengutronix.de>");
L
Linus Torvalds 已提交
391 392 393 394
MODULE_DESCRIPTION("I2C-Bus PCA9564 algorithm");
MODULE_LICENSE("GPL");

module_param(i2c_debug, int, 0);