i2c-bfin-twi.c 20.1 KB
Newer Older
1
/*
2
 * Blackfin On-Chip Two Wire Interface Driver
3
 *
4
 * Copyright 2005-2007 Analog Devices Inc.
5
 *
6
 * Enter bugs at http://blackfin.uclinux.org/
7
 *
8
 * Licensed under the GPL-2 or later.
9 10 11 12 13 14
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/i2c.h>
15
#include <linux/slab.h>
16
#include <linux/io.h>
17 18 19 20 21 22
#include <linux/mm.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
23
#include <linux/delay.h>
24 25

#include <asm/blackfin.h>
26
#include <asm/portmux.h>
27 28 29
#include <asm/irq.h>

/* SMBus mode*/
30 31 32 33
#define TWI_I2C_MODE_STANDARD		1
#define TWI_I2C_MODE_STANDARDSUB	2
#define TWI_I2C_MODE_COMBINED		3
#define TWI_I2C_MODE_REPEAT		4
34 35 36 37 38 39 40 41 42 43 44 45 46 47

struct bfin_twi_iface {
	int			irq;
	spinlock_t		lock;
	char			read_write;
	u8			command;
	u8			*transPtr;
	int			readNum;
	int			writeNum;
	int			cur_mode;
	int			manual_stop;
	int			result;
	struct i2c_adapter	adap;
	struct completion	complete;
48 49 50
	struct i2c_msg 		*pmsg;
	int			msg_num;
	int			cur_msg;
51 52
	u16			saved_clkdiv;
	u16			saved_control;
53
	void __iomem		*regs_base;
54 55
};

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

#define DEFINE_TWI_REG(reg, off) \
static inline u16 read_##reg(struct bfin_twi_iface *iface) \
	{ return bfin_read16(iface->regs_base + (off)); } \
static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
	{ bfin_write16(iface->regs_base + (off), v); }

DEFINE_TWI_REG(CLKDIV, 0x00)
DEFINE_TWI_REG(CONTROL, 0x04)
DEFINE_TWI_REG(SLAVE_CTL, 0x08)
DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
DEFINE_TWI_REG(MASTER_CTL, 0x14)
DEFINE_TWI_REG(MASTER_STAT, 0x18)
DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
DEFINE_TWI_REG(INT_STAT, 0x20)
DEFINE_TWI_REG(INT_MASK, 0x24)
DEFINE_TWI_REG(FIFO_CTL, 0x28)
DEFINE_TWI_REG(FIFO_STAT, 0x2C)
DEFINE_TWI_REG(XMT_DATA8, 0x80)
DEFINE_TWI_REG(XMT_DATA16, 0x84)
DEFINE_TWI_REG(RCV_DATA8, 0x88)
DEFINE_TWI_REG(RCV_DATA16, 0x8C)
79

80 81 82 83 84
static const u16 pin_req[2][3] = {
	{P_TWI0_SCL, P_TWI0_SDA, 0},
	{P_TWI1_SCL, P_TWI1_SDA, 0},
};

85 86
static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
					unsigned short twi_int_status)
87
{
88
	unsigned short mast_stat = read_MASTER_STAT(iface);
89 90 91 92

	if (twi_int_status & XMTSERV) {
		/* Transmit next data */
		if (iface->writeNum > 0) {
93
			SSYNC();
94
			write_XMT_DATA8(iface, *(iface->transPtr++));
95 96 97 98 99
			iface->writeNum--;
		}
		/* start receive immediately after complete sending in
		 * combine mode.
		 */
100
		else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
101 102
			write_MASTER_CTL(iface,
				read_MASTER_CTL(iface) | MDIR | RSTART);
103
		else if (iface->manual_stop)
104 105
			write_MASTER_CTL(iface,
				read_MASTER_CTL(iface) | STOP);
106
		else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
107 108 109 110 111 112 113 114
		         iface->cur_msg + 1 < iface->msg_num) {
			if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
				write_MASTER_CTL(iface,
					read_MASTER_CTL(iface) | RSTART | MDIR);
			else
				write_MASTER_CTL(iface,
					(read_MASTER_CTL(iface) | RSTART) & ~MDIR);
		}
115 116 117 118
	}
	if (twi_int_status & RCVSERV) {
		if (iface->readNum > 0) {
			/* Receive next data */
119
			*(iface->transPtr) = read_RCV_DATA8(iface);
120 121 122 123 124 125 126 127 128 129 130 131 132 133
			if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
				/* Change combine mode into sub mode after
				 * read first data.
				 */
				iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
				/* Get read number from first byte in block
				 * combine mode.
				 */
				if (iface->readNum == 1 && iface->manual_stop)
					iface->readNum = *iface->transPtr + 1;
			}
			iface->transPtr++;
			iface->readNum--;
		} else if (iface->manual_stop) {
134 135 136 137
			/* Temporary workaround to avoid possible bus stall -
			 * Flush FIFO before issuing the STOP condition
			 */
			read_RCV_DATA16(iface);
138 139
			write_MASTER_CTL(iface,
				read_MASTER_CTL(iface) | STOP);
140
		} else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
141 142 143 144 145 146 147
		           iface->cur_msg + 1 < iface->msg_num) {
			if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
				write_MASTER_CTL(iface,
					read_MASTER_CTL(iface) | RSTART | MDIR);
			else
				write_MASTER_CTL(iface,
					(read_MASTER_CTL(iface) | RSTART) & ~MDIR);
148 149 150
		}
	}
	if (twi_int_status & MERR) {
151 152 153
		write_INT_MASK(iface, 0);
		write_MASTER_STAT(iface, 0x3e);
		write_MASTER_CTL(iface, 0);
154
		iface->result = -EIO;
155 156 157 158 159 160 161 162 163 164 165 166

		if (mast_stat & LOSTARB)
			dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
		if (mast_stat & ANAK)
			dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
		if (mast_stat & DNAK)
			dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
		if (mast_stat & BUFRDERR)
			dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
		if (mast_stat & BUFWRERR)
			dev_dbg(&iface->adap.dev, "Buffer Write Error\n");

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		/* Faulty slave devices, may drive SDA low after a transfer
		 * finishes. To release the bus this code generates up to 9
		 * extra clocks until SDA is released.
		 */

		if (read_MASTER_STAT(iface) & SDASEN) {
			int cnt = 9;
			do {
				write_MASTER_CTL(iface, SCLOVR);
				udelay(6);
				write_MASTER_CTL(iface, 0);
				udelay(6);
			} while ((read_MASTER_STAT(iface) & SDASEN) && cnt--);

			write_MASTER_CTL(iface, SDAOVR | SCLOVR);
			udelay(6);
			write_MASTER_CTL(iface, SDAOVR);
			udelay(6);
			write_MASTER_CTL(iface, 0);
		}

188 189
		/* If it is a quick transfer, only address without data,
		 * not an err, return 1.
190
		 */
191 192 193 194 195
		if (iface->cur_mode == TWI_I2C_MODE_STANDARD &&
			iface->transPtr == NULL &&
			(twi_int_status & MCOMP) && (mast_stat & DNAK))
			iface->result = 1;

196 197 198 199
		complete(&iface->complete);
		return;
	}
	if (twi_int_status & MCOMP) {
200 201 202 203 204 205 206
		if ((read_MASTER_CTL(iface) & MEN) == 0 &&
			(iface->cur_mode == TWI_I2C_MODE_REPEAT ||
			iface->cur_mode == TWI_I2C_MODE_COMBINED)) {
			iface->result = -1;
			write_INT_MASK(iface, 0);
			write_MASTER_CTL(iface, 0);
		} else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
207 208 209 210 211 212
			if (iface->readNum == 0) {
				/* set the read number to 1 and ask for manual
				 * stop in block combine mode
				 */
				iface->readNum = 1;
				iface->manual_stop = 1;
213 214
				write_MASTER_CTL(iface,
					read_MASTER_CTL(iface) | (0xff << 6));
215 216 217 218
			} else {
				/* set the readd number in other
				 * combine mode.
				 */
219 220
				write_MASTER_CTL(iface,
					(read_MASTER_CTL(iface) &
221
					(~(0xff << 6))) |
222
					(iface->readNum << 6));
223 224
			}
			/* remove restart bit and enable master receive */
225 226
			write_MASTER_CTL(iface,
				read_MASTER_CTL(iface) & ~RSTART);
227 228 229 230 231 232 233
		} else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
				iface->cur_msg+1 < iface->msg_num) {
			iface->cur_msg++;
			iface->transPtr = iface->pmsg[iface->cur_msg].buf;
			iface->writeNum = iface->readNum =
				iface->pmsg[iface->cur_msg].len;
			/* Set Transmit device address */
234
			write_MASTER_ADDR(iface,
235 236 237 238 239 240 241
				iface->pmsg[iface->cur_msg].addr);
			if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
				iface->read_write = I2C_SMBUS_READ;
			else {
				iface->read_write = I2C_SMBUS_WRITE;
				/* Transmit first data */
				if (iface->writeNum > 0) {
242
					write_XMT_DATA8(iface,
243 244 245 246 247 248
						*(iface->transPtr++));
					iface->writeNum--;
				}
			}

			if (iface->pmsg[iface->cur_msg].len <= 255)
249 250 251 252
					write_MASTER_CTL(iface,
					(read_MASTER_CTL(iface) &
					(~(0xff << 6))) |
				(iface->pmsg[iface->cur_msg].len << 6));
253
			else {
254 255 256
				write_MASTER_CTL(iface,
					(read_MASTER_CTL(iface) |
					(0xff << 6)));
257 258 259
				iface->manual_stop = 1;
			}
			/* remove restart bit and enable master receive */
260 261
			write_MASTER_CTL(iface,
				read_MASTER_CTL(iface) & ~RSTART);
262 263
		} else {
			iface->result = 1;
264 265
			write_INT_MASK(iface, 0);
			write_MASTER_CTL(iface, 0);
266 267
		}
	}
268
	complete(&iface->complete);
269 270 271 272 273 274 275
}

/* Interrupt handler */
static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
{
	struct bfin_twi_iface *iface = dev_id;
	unsigned long flags;
276
	unsigned short twi_int_status;
277 278

	spin_lock_irqsave(&iface->lock, flags);
279 280 281 282 283 284 285 286 287
	while (1) {
		twi_int_status = read_INT_STAT(iface);
		if (!twi_int_status)
			break;
		/* Clear interrupt status */
		write_INT_STAT(iface, twi_int_status);
		bfin_twi_handle_interrupt(iface, twi_int_status);
		SSYNC();
	}
288 289 290 291 292
	spin_unlock_irqrestore(&iface->lock, flags);
	return IRQ_HANDLED;
}

/*
293
 * One i2c master transfer
294
 */
295
static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
296 297 298 299 300 301
				struct i2c_msg *msgs, int num)
{
	struct bfin_twi_iface *iface = adap->algo_data;
	struct i2c_msg *pmsg;
	int rc = 0;

302
	if (!(read_CONTROL(iface) & TWI_ENA))
303 304
		return -ENXIO;

305
	while (read_MASTER_STAT(iface) & BUSBUSY)
306 307
		yield();

308 309 310
	iface->pmsg = msgs;
	iface->msg_num = num;
	iface->cur_msg = 0;
311

312 313 314 315 316
	pmsg = &msgs[0];
	if (pmsg->flags & I2C_M_TEN) {
		dev_err(&adap->dev, "10 bits addr not supported!\n");
		return -EINVAL;
	}
317

318 319 320 321 322
	iface->cur_mode = TWI_I2C_MODE_REPEAT;
	iface->manual_stop = 0;
	iface->transPtr = pmsg->buf;
	iface->writeNum = iface->readNum = pmsg->len;
	iface->result = 0;
323
	init_completion(&(iface->complete));
324
	/* Set Transmit device address */
325
	write_MASTER_ADDR(iface, pmsg->addr);
326 327 328 329

	/* FIFO Initiation. Data in FIFO should be
	 *  discarded before start a new operation.
	 */
330
	write_FIFO_CTL(iface, 0x3);
331
	SSYNC();
332
	write_FIFO_CTL(iface, 0);
333 334 335 336 337 338 339 340
	SSYNC();

	if (pmsg->flags & I2C_M_RD)
		iface->read_write = I2C_SMBUS_READ;
	else {
		iface->read_write = I2C_SMBUS_WRITE;
		/* Transmit first data */
		if (iface->writeNum > 0) {
341
			write_XMT_DATA8(iface, *(iface->transPtr++));
342 343
			iface->writeNum--;
			SSYNC();
344
		}
345
	}
346

347
	/* clear int stat */
348
	write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
349

350
	/* Interrupt mask . Enable XMT, RCV interrupt */
351
	write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
352
	SSYNC();
353

354
	if (pmsg->len <= 255)
355
		write_MASTER_CTL(iface, pmsg->len << 6);
356
	else {
357
		write_MASTER_CTL(iface, 0xff << 6);
358 359
		iface->manual_stop = 1;
	}
360

361
	/* Master enable */
362
	write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
363 364 365 366
		((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
		((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
	SSYNC();

367 368 369 370 371 372 373
	while (!iface->result) {
		if (!wait_for_completion_timeout(&iface->complete,
			adap->timeout)) {
			iface->result = -1;
			dev_err(&adap->dev, "master transfer timeout\n");
		}
	}
374

375 376
	if (iface->result == 1)
		rc = iface->cur_msg + 1;
377
	else
378 379 380
		rc = iface->result;

	return rc;
381 382 383
}

/*
384
 * Generic i2c master transfer entrypoint
385
 */
386 387 388
static int bfin_twi_master_xfer(struct i2c_adapter *adap,
				struct i2c_msg *msgs, int num)
{
389
	return bfin_twi_do_master_xfer(adap, msgs, num);
390 391 392 393 394 395
}

/*
 * One I2C SMBus transfer
 */
int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
396 397 398 399 400 401
			unsigned short flags, char read_write,
			u8 command, int size, union i2c_smbus_data *data)
{
	struct bfin_twi_iface *iface = adap->algo_data;
	int rc = 0;

402
	if (!(read_CONTROL(iface) & TWI_ENA))
403 404
		return -ENXIO;

405
	while (read_MASTER_STAT(iface) & BUSBUSY)
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
		yield();

	iface->writeNum = 0;
	iface->readNum = 0;

	/* Prepare datas & select mode */
	switch (size) {
	case I2C_SMBUS_QUICK:
		iface->transPtr = NULL;
		iface->cur_mode = TWI_I2C_MODE_STANDARD;
		break;
	case I2C_SMBUS_BYTE:
		if (data == NULL)
			iface->transPtr = NULL;
		else {
			if (read_write == I2C_SMBUS_READ)
				iface->readNum = 1;
			else
				iface->writeNum = 1;
			iface->transPtr = &data->byte;
		}
		iface->cur_mode = TWI_I2C_MODE_STANDARD;
		break;
	case I2C_SMBUS_BYTE_DATA:
		if (read_write == I2C_SMBUS_READ) {
			iface->readNum = 1;
			iface->cur_mode = TWI_I2C_MODE_COMBINED;
		} else {
			iface->writeNum = 1;
			iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
		}
		iface->transPtr = &data->byte;
		break;
	case I2C_SMBUS_WORD_DATA:
		if (read_write == I2C_SMBUS_READ) {
			iface->readNum = 2;
			iface->cur_mode = TWI_I2C_MODE_COMBINED;
		} else {
			iface->writeNum = 2;
			iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
		}
		iface->transPtr = (u8 *)&data->word;
		break;
	case I2C_SMBUS_PROC_CALL:
		iface->writeNum = 2;
		iface->readNum = 2;
		iface->cur_mode = TWI_I2C_MODE_COMBINED;
		iface->transPtr = (u8 *)&data->word;
		break;
	case I2C_SMBUS_BLOCK_DATA:
		if (read_write == I2C_SMBUS_READ) {
			iface->readNum = 0;
			iface->cur_mode = TWI_I2C_MODE_COMBINED;
		} else {
			iface->writeNum = data->block[0] + 1;
			iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
		}
		iface->transPtr = data->block;
		break;
465 466 467 468 469 470 471 472 473 474
	case I2C_SMBUS_I2C_BLOCK_DATA:
		if (read_write == I2C_SMBUS_READ) {
			iface->readNum = data->block[0];
			iface->cur_mode = TWI_I2C_MODE_COMBINED;
		} else {
			iface->writeNum = data->block[0];
			iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
		}
		iface->transPtr = (u8 *)&data->block[1];
		break;
475 476 477 478 479 480 481 482
	default:
		return -1;
	}

	iface->result = 0;
	iface->manual_stop = 0;
	iface->read_write = read_write;
	iface->command = command;
483
	init_completion(&(iface->complete));
484 485 486 487

	/* FIFO Initiation. Data in FIFO should be discarded before
	 * start a new operation.
	 */
488
	write_FIFO_CTL(iface, 0x3);
489
	SSYNC();
490
	write_FIFO_CTL(iface, 0);
491 492

	/* clear int stat */
493
	write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
494 495

	/* Set Transmit device address */
496
	write_MASTER_ADDR(iface, addr);
497 498 499 500
	SSYNC();

	switch (iface->cur_mode) {
	case TWI_I2C_MODE_STANDARDSUB:
501 502
		write_XMT_DATA8(iface, iface->command);
		write_INT_MASK(iface, MCOMP | MERR |
503 504 505 506 507
			((iface->read_write == I2C_SMBUS_READ) ?
			RCVSERV : XMTSERV));
		SSYNC();

		if (iface->writeNum + 1 <= 255)
508
			write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
509
		else {
510
			write_MASTER_CTL(iface, 0xff << 6);
511 512 513
			iface->manual_stop = 1;
		}
		/* Master enable */
514
		write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
515 516 517
			((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
		break;
	case TWI_I2C_MODE_COMBINED:
518 519
		write_XMT_DATA8(iface, iface->command);
		write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
520 521 522
		SSYNC();

		if (iface->writeNum > 0)
523
			write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
524
		else
525
			write_MASTER_CTL(iface, 0x1 << 6);
526
		/* Master enable */
527
		write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
528 529 530
			((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
		break;
	default:
531
		write_MASTER_CTL(iface, 0);
532 533 534 535 536 537
		if (size != I2C_SMBUS_QUICK) {
			/* Don't access xmit data register when this is a
			 * read operation.
			 */
			if (iface->read_write != I2C_SMBUS_READ) {
				if (iface->writeNum > 0) {
538 539
					write_XMT_DATA8(iface,
						*(iface->transPtr++));
540
					if (iface->writeNum <= 255)
541 542
						write_MASTER_CTL(iface,
							iface->writeNum << 6);
543
					else {
544 545
						write_MASTER_CTL(iface,
							0xff << 6);
546 547 548 549
						iface->manual_stop = 1;
					}
					iface->writeNum--;
				} else {
550 551
					write_XMT_DATA8(iface, iface->command);
					write_MASTER_CTL(iface, 1 << 6);
552 553 554
				}
			} else {
				if (iface->readNum > 0 && iface->readNum <= 255)
555 556
					write_MASTER_CTL(iface,
						iface->readNum << 6);
557
				else if (iface->readNum > 255) {
558
					write_MASTER_CTL(iface, 0xff << 6);
559
					iface->manual_stop = 1;
560
				} else
561 562 563
					break;
			}
		}
564
		write_INT_MASK(iface, MCOMP | MERR |
565 566 567 568 569
			((iface->read_write == I2C_SMBUS_READ) ?
			RCVSERV : XMTSERV));
		SSYNC();

		/* Master enable */
570
		write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
571 572 573 574 575 576
			((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
			((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
		break;
	}
	SSYNC();

577 578 579 580 581 582 583
	while (!iface->result) {
		if (!wait_for_completion_timeout(&iface->complete,
			adap->timeout)) {
			iface->result = -1;
			dev_err(&adap->dev, "smbus transfer timeout\n");
		}
	}
584 585 586 587 588 589

	rc = (iface->result >= 0) ? 0 : -1;

	return rc;
}

590 591 592 593 594 595 596
/*
 * Generic I2C SMBus transfer entrypoint
 */
int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
			unsigned short flags, char read_write,
			u8 command, int size, union i2c_smbus_data *data)
{
597
	return bfin_twi_do_smbus_xfer(adap, addr, flags,
598 599 600
			read_write, command, size, data);
}

601 602 603 604 605 606 607 608
/*
 * Return what the adapter supports
 */
static u32 bfin_twi_functionality(struct i2c_adapter *adap)
{
	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
	       I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
	       I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
609
	       I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
610 611 612 613 614 615 616 617
}

static struct i2c_algorithm bfin_twi_algorithm = {
	.master_xfer   = bfin_twi_master_xfer,
	.smbus_xfer    = bfin_twi_smbus_xfer,
	.functionality = bfin_twi_functionality,
};

618
static int i2c_bfin_twi_suspend(struct device *dev)
619
{
620
	struct bfin_twi_iface *iface = dev_get_drvdata(dev);
621 622 623 624 625

	iface->saved_clkdiv = read_CLKDIV(iface);
	iface->saved_control = read_CONTROL(iface);

	free_irq(iface->irq, iface);
626 627

	/* Disable TWI */
628
	write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
629 630 631 632

	return 0;
}

633
static int i2c_bfin_twi_resume(struct device *dev)
634
{
635
	struct bfin_twi_iface *iface = dev_get_drvdata(dev);
636

637
	int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
638
		0, to_platform_device(dev)->name, iface);
639
	if (rc) {
640
		dev_err(dev, "Can't get IRQ %d !\n", iface->irq);
641 642 643 644 645 646 647 648
		return -ENODEV;
	}

	/* Resume TWI interface clock as specified */
	write_CLKDIV(iface, iface->saved_clkdiv);

	/* Resume TWI */
	write_CONTROL(iface, iface->saved_control);
649 650 651 652

	return 0;
}

653 654 655
static SIMPLE_DEV_PM_OPS(i2c_bfin_twi_pm,
			 i2c_bfin_twi_suspend, i2c_bfin_twi_resume);

656
static int i2c_bfin_twi_probe(struct platform_device *pdev)
657
{
658
	struct bfin_twi_iface *iface;
659
	struct i2c_adapter *p_adap;
660
	struct resource *res;
661
	int rc;
662
	unsigned int clkhilow;
663

664 665 666 667 668 669 670
	iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
	if (!iface) {
		dev_err(&pdev->dev, "Cannot allocate memory\n");
		rc = -ENOMEM;
		goto out_error_nomem;
	}

671
	spin_lock_init(&(iface->lock));
672 673 674 675 676 677 678 679 680

	/* Find and map our resources */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
		rc = -ENOENT;
		goto out_error_get_res;
	}

L
Linus Walleij 已提交
681
	iface->regs_base = ioremap(res->start, resource_size(res));
682 683 684 685 686 687 688 689 690 691 692 693
	if (iface->regs_base == NULL) {
		dev_err(&pdev->dev, "Cannot map IO\n");
		rc = -ENXIO;
		goto out_error_ioremap;
	}

	iface->irq = platform_get_irq(pdev, 0);
	if (iface->irq < 0) {
		dev_err(&pdev->dev, "No IRQ specified\n");
		rc = -ENOENT;
		goto out_error_no_irq;
	}
694 695

	p_adap = &iface->adap;
696 697
	p_adap->nr = pdev->id;
	strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
698 699
	p_adap->algo = &bfin_twi_algorithm;
	p_adap->algo_data = iface;
J
Jean Delvare 已提交
700
	p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
701
	p_adap->dev.parent = &pdev->dev;
702 703
	p_adap->timeout = 5 * HZ;
	p_adap->retries = 3;
704

705 706 707 708 709 710
	rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
	if (rc) {
		dev_err(&pdev->dev, "Can't setup pin mux!\n");
		goto out_error_pin_mux;
	}

711
	rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Y
Yong Zhang 已提交
712
		0, pdev->name, iface);
713
	if (rc) {
714 715 716
		dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
		rc = -ENODEV;
		goto out_error_req_irq;
717 718 719
	}

	/* Set TWI internal clock as 10MHz */
S
Sonic Zhang 已提交
720
	write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
721

722 723
	/*
	 * We will not end up with a CLKDIV=0 because no one will specify
S
Sonic Zhang 已提交
724
	 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
725
	 */
S
Sonic Zhang 已提交
726
	clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
727

728
	/* Set Twi interface clock as specified */
729
	write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
730 731

	/* Enable TWI */
732
	write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
733 734
	SSYNC();

735
	rc = i2c_add_numbered_adapter(p_adap);
736 737 738 739 740 741
	if (rc < 0) {
		dev_err(&pdev->dev, "Can't add i2c adapter!\n");
		goto out_error_add_adapter;
	}

	platform_set_drvdata(pdev, iface);
742

743 744
	dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
		"regs_base@%p\n", iface->regs_base);
745 746 747 748 749 750 751

	return 0;

out_error_add_adapter:
	free_irq(iface->irq, iface);
out_error_req_irq:
out_error_no_irq:
752 753
	peripheral_free_list(pin_req[pdev->id]);
out_error_pin_mux:
754 755 756 757 758
	iounmap(iface->regs_base);
out_error_ioremap:
out_error_get_res:
	kfree(iface);
out_error_nomem:
759 760 761 762 763 764 765 766 767 768 769
	return rc;
}

static int i2c_bfin_twi_remove(struct platform_device *pdev)
{
	struct bfin_twi_iface *iface = platform_get_drvdata(pdev);

	platform_set_drvdata(pdev, NULL);

	i2c_del_adapter(&(iface->adap));
	free_irq(iface->irq, iface);
770
	peripheral_free_list(pin_req[pdev->id]);
771 772
	iounmap(iface->regs_base);
	kfree(iface);
773 774 775 776 777 778 779 780 781 782

	return 0;
}

static struct platform_driver i2c_bfin_twi_driver = {
	.probe		= i2c_bfin_twi_probe,
	.remove		= i2c_bfin_twi_remove,
	.driver		= {
		.name	= "i2c-bfin-twi",
		.owner	= THIS_MODULE,
783
		.pm	= &i2c_bfin_twi_pm,
784 785 786 787 788 789 790 791 792 793 794 795 796
	},
};

static int __init i2c_bfin_twi_init(void)
{
	return platform_driver_register(&i2c_bfin_twi_driver);
}

static void __exit i2c_bfin_twi_exit(void)
{
	platform_driver_unregister(&i2c_bfin_twi_driver);
}

797
subsys_initcall(i2c_bfin_twi_init);
798
module_exit(i2c_bfin_twi_exit);
799 800 801 802

MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
MODULE_LICENSE("GPL");
803
MODULE_ALIAS("platform:i2c-bfin-twi");