i2c-bfin-twi.c 18.7 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
#include <linux/i2c/bfin_twi.h>
25 26

#include <asm/irq.h>
27
#include <asm/portmux.h>
28
#include <asm/bfin_twi.h>
29 30

/* SMBus mode*/
31 32 33 34
#define TWI_I2C_MODE_STANDARD		1
#define TWI_I2C_MODE_STANDARDSUB	2
#define TWI_I2C_MODE_COMBINED		3
#define TWI_I2C_MODE_REPEAT		4
35

36 37
static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
					unsigned short twi_int_status)
38
{
39
	unsigned short mast_stat = read_MASTER_STAT(iface);
40 41

	if (twi_int_status & XMTSERV) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
		if (iface->writeNum <= 0) {
			/* start receive immediately after complete sending in
			 * combine mode.
			 */
			if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
				write_MASTER_CTL(iface,
					read_MASTER_CTL(iface) | MDIR);
			else if (iface->manual_stop)
				write_MASTER_CTL(iface,
					read_MASTER_CTL(iface) | STOP);
			else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
				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) |
						MDIR);
				else
					write_MASTER_CTL(iface,
						read_MASTER_CTL(iface) &
						~MDIR);
			}
		}
65
		/* Transmit next data */
66 67
		while (iface->writeNum > 0 &&
			(read_FIFO_STAT(iface) & XMTSTAT) != XMT_FULL) {
68
			write_XMT_DATA8(iface, *(iface->transPtr++));
69 70 71 72
			iface->writeNum--;
		}
	}
	if (twi_int_status & RCVSERV) {
73 74
		while (iface->readNum > 0 &&
			(read_FIFO_STAT(iface) & RCVSTAT)) {
75
			/* Receive next data */
76
			*(iface->transPtr) = read_RCV_DATA8(iface);
77 78 79 80 81 82 83 84 85 86 87 88 89
			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--;
90 91 92 93 94 95 96 97
		}

		if (iface->readNum == 0) {
			if (iface->manual_stop) {
				/* Temporary workaround to avoid possible bus stall -
				 * Flush FIFO before issuing the STOP condition
				 */
				read_RCV_DATA16(iface);
98
				write_MASTER_CTL(iface,
99 100 101 102 103
					read_MASTER_CTL(iface) | STOP);
			} else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
					iface->cur_msg + 1 < iface->msg_num) {
				if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
					write_MASTER_CTL(iface,
104
						read_MASTER_CTL(iface) | MDIR);
105 106
				else
					write_MASTER_CTL(iface,
107
						read_MASTER_CTL(iface) & ~MDIR);
108
			}
109 110 111
		}
	}
	if (twi_int_status & MERR) {
112 113 114
		write_INT_MASK(iface, 0);
		write_MASTER_STAT(iface, 0x3e);
		write_MASTER_CTL(iface, 0);
115
		iface->result = -EIO;
116 117 118 119 120 121 122 123 124 125 126 127

		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");

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		/* 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);
		}

149 150
		/* If it is a quick transfer, only address without data,
		 * not an err, return 1.
151
		 */
152 153 154 155 156
		if (iface->cur_mode == TWI_I2C_MODE_STANDARD &&
			iface->transPtr == NULL &&
			(twi_int_status & MCOMP) && (mast_stat & DNAK))
			iface->result = 1;

157 158 159 160
		complete(&iface->complete);
		return;
	}
	if (twi_int_status & MCOMP) {
161 162
		if (twi_int_status & (XMTSERV | RCVSERV) &&
			(read_MASTER_CTL(iface) & MEN) == 0 &&
163 164 165 166 167 168
			(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) {
169 170 171 172 173 174
			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;
175 176
				write_MASTER_CTL(iface,
					read_MASTER_CTL(iface) | (0xff << 6));
177 178 179 180
			} else {
				/* set the readd number in other
				 * combine mode.
				 */
181 182
				write_MASTER_CTL(iface,
					(read_MASTER_CTL(iface) &
183
					(~(0xff << 6))) |
184
					(iface->readNum << 6));
185 186
			}
			/* remove restart bit and enable master receive */
187 188
			write_MASTER_CTL(iface,
				read_MASTER_CTL(iface) & ~RSTART);
189
		} else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
190
				iface->cur_msg + 1 < iface->msg_num) {
191 192 193 194 195
			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 */
196
			write_MASTER_ADDR(iface,
197 198 199 200 201 202 203
				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) {
204
					write_XMT_DATA8(iface,
205 206 207 208 209
						*(iface->transPtr++));
					iface->writeNum--;
				}
			}

210 211
			if (iface->pmsg[iface->cur_msg].len <= 255) {
				write_MASTER_CTL(iface,
212 213
					(read_MASTER_CTL(iface) &
					(~(0xff << 6))) |
214 215 216
					(iface->pmsg[iface->cur_msg].len << 6));
				iface->manual_stop = 0;
			} else {
217 218 219
				write_MASTER_CTL(iface,
					(read_MASTER_CTL(iface) |
					(0xff << 6)));
220 221
				iface->manual_stop = 1;
			}
222 223 224 225
			/* remove restart bit before last message */
			if (iface->cur_msg + 1 == iface->msg_num)
				write_MASTER_CTL(iface,
					read_MASTER_CTL(iface) & ~RSTART);
226 227
		} else {
			iface->result = 1;
228 229
			write_INT_MASK(iface, 0);
			write_MASTER_CTL(iface, 0);
230
		}
231
		complete(&iface->complete);
232 233 234 235 236 237 238 239
	}
}

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

	spin_lock_irqsave(&iface->lock, flags);
243 244 245 246 247 248 249 250
	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);
	}
251 252 253 254 255
	spin_unlock_irqrestore(&iface->lock, flags);
	return IRQ_HANDLED;
}

/*
256
 * One i2c master transfer
257
 */
258
static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
259 260 261 262 263 264
				struct i2c_msg *msgs, int num)
{
	struct bfin_twi_iface *iface = adap->algo_data;
	struct i2c_msg *pmsg;
	int rc = 0;

265
	if (!(read_CONTROL(iface) & TWI_ENA))
266 267
		return -ENXIO;

268 269
	if (read_MASTER_STAT(iface) & BUSBUSY)
		return -EAGAIN;
270

271 272 273
	iface->pmsg = msgs;
	iface->msg_num = num;
	iface->cur_msg = 0;
274

275 276 277 278 279
	pmsg = &msgs[0];
	if (pmsg->flags & I2C_M_TEN) {
		dev_err(&adap->dev, "10 bits addr not supported!\n");
		return -EINVAL;
	}
280

281 282
	if (iface->msg_num > 1)
		iface->cur_mode = TWI_I2C_MODE_REPEAT;
283 284 285 286
	iface->manual_stop = 0;
	iface->transPtr = pmsg->buf;
	iface->writeNum = iface->readNum = pmsg->len;
	iface->result = 0;
287
	init_completion(&(iface->complete));
288
	/* Set Transmit device address */
289
	write_MASTER_ADDR(iface, pmsg->addr);
290 291 292 293

	/* FIFO Initiation. Data in FIFO should be
	 *  discarded before start a new operation.
	 */
294 295
	write_FIFO_CTL(iface, 0x3);
	write_FIFO_CTL(iface, 0);
296 297 298 299 300 301 302

	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) {
303
			write_XMT_DATA8(iface, *(iface->transPtr++));
304
			iface->writeNum--;
305
		}
306
	}
307

308
	/* clear int stat */
309
	write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
310

311
	/* Interrupt mask . Enable XMT, RCV interrupt */
312
	write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
313

314
	if (pmsg->len <= 255)
315
		write_MASTER_CTL(iface, pmsg->len << 6);
316
	else {
317
		write_MASTER_CTL(iface, 0xff << 6);
318 319
		iface->manual_stop = 1;
	}
320

321
	/* Master enable */
322
	write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
323
		(iface->msg_num > 1 ? RSTART : 0) |
324 325 326
		((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
		((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));

327 328 329 330 331 332 333
	while (!iface->result) {
		if (!wait_for_completion_timeout(&iface->complete,
			adap->timeout)) {
			iface->result = -1;
			dev_err(&adap->dev, "master transfer timeout\n");
		}
	}
334

335 336
	if (iface->result == 1)
		rc = iface->cur_msg + 1;
337
	else
338 339 340
		rc = iface->result;

	return rc;
341 342 343
}

/*
344
 * Generic i2c master transfer entrypoint
345
 */
346 347 348
static int bfin_twi_master_xfer(struct i2c_adapter *adap,
				struct i2c_msg *msgs, int num)
{
349
	return bfin_twi_do_master_xfer(adap, msgs, num);
350 351 352 353 354 355
}

/*
 * One I2C SMBus transfer
 */
int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
356 357 358 359 360 361
			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;

362
	if (!(read_CONTROL(iface) & TWI_ENA))
363 364
		return -ENXIO;

365 366
	if (read_MASTER_STAT(iface) & BUSBUSY)
		return -EAGAIN;
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

	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;
425 426 427 428 429 430 431 432 433 434
	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;
435 436 437 438 439 440 441 442
	default:
		return -1;
	}

	iface->result = 0;
	iface->manual_stop = 0;
	iface->read_write = read_write;
	iface->command = command;
443
	init_completion(&(iface->complete));
444 445 446 447

	/* FIFO Initiation. Data in FIFO should be discarded before
	 * start a new operation.
	 */
448 449
	write_FIFO_CTL(iface, 0x3);
	write_FIFO_CTL(iface, 0);
450 451

	/* clear int stat */
452
	write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
453 454

	/* Set Transmit device address */
455
	write_MASTER_ADDR(iface, addr);
456 457 458

	switch (iface->cur_mode) {
	case TWI_I2C_MODE_STANDARDSUB:
459 460
		write_XMT_DATA8(iface, iface->command);
		write_INT_MASK(iface, MCOMP | MERR |
461 462 463 464
			((iface->read_write == I2C_SMBUS_READ) ?
			RCVSERV : XMTSERV));

		if (iface->writeNum + 1 <= 255)
465
			write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
466
		else {
467
			write_MASTER_CTL(iface, 0xff << 6);
468 469 470
			iface->manual_stop = 1;
		}
		/* Master enable */
471
		write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
472 473 474
			((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
		break;
	case TWI_I2C_MODE_COMBINED:
475 476
		write_XMT_DATA8(iface, iface->command);
		write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
477 478

		if (iface->writeNum > 0)
479
			write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
480
		else
481
			write_MASTER_CTL(iface, 0x1 << 6);
482
		/* Master enable */
483
		write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | RSTART |
484 485 486
			((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
		break;
	default:
487
		write_MASTER_CTL(iface, 0);
488 489 490 491 492 493
		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) {
494 495
					write_XMT_DATA8(iface,
						*(iface->transPtr++));
496
					if (iface->writeNum <= 255)
497 498
						write_MASTER_CTL(iface,
							iface->writeNum << 6);
499
					else {
500 501
						write_MASTER_CTL(iface,
							0xff << 6);
502 503 504 505
						iface->manual_stop = 1;
					}
					iface->writeNum--;
				} else {
506 507
					write_XMT_DATA8(iface, iface->command);
					write_MASTER_CTL(iface, 1 << 6);
508 509 510
				}
			} else {
				if (iface->readNum > 0 && iface->readNum <= 255)
511 512
					write_MASTER_CTL(iface,
						iface->readNum << 6);
513
				else if (iface->readNum > 255) {
514
					write_MASTER_CTL(iface, 0xff << 6);
515
					iface->manual_stop = 1;
516
				} else
517 518 519
					break;
			}
		}
520
		write_INT_MASK(iface, MCOMP | MERR |
521 522 523 524
			((iface->read_write == I2C_SMBUS_READ) ?
			RCVSERV : XMTSERV));

		/* Master enable */
525
		write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
526 527 528 529 530
			((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
			((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
		break;
	}

531 532 533 534 535 536 537
	while (!iface->result) {
		if (!wait_for_completion_timeout(&iface->complete,
			adap->timeout)) {
			iface->result = -1;
			dev_err(&adap->dev, "smbus transfer timeout\n");
		}
	}
538 539 540 541 542 543

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

	return rc;
}

544 545 546 547 548 549 550
/*
 * 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)
{
551
	return bfin_twi_do_smbus_xfer(adap, addr, flags,
552 553 554
			read_write, command, size, data);
}

555 556 557 558 559 560 561 562
/*
 * 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 |
563
	       I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
564 565
}

566
static const struct i2c_algorithm bfin_twi_algorithm = {
567 568 569 570 571
	.master_xfer   = bfin_twi_master_xfer,
	.smbus_xfer    = bfin_twi_smbus_xfer,
	.functionality = bfin_twi_functionality,
};

572
#ifdef CONFIG_PM_SLEEP
573
static int i2c_bfin_twi_suspend(struct device *dev)
574
{
575
	struct bfin_twi_iface *iface = dev_get_drvdata(dev);
576 577 578 579 580

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

	free_irq(iface->irq, iface);
581 582

	/* Disable TWI */
583
	write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
584 585 586 587

	return 0;
}

588
static int i2c_bfin_twi_resume(struct device *dev)
589
{
590
	struct bfin_twi_iface *iface = dev_get_drvdata(dev);
591

592
	int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
593
		0, to_platform_device(dev)->name, iface);
594
	if (rc) {
595
		dev_err(dev, "Can't get IRQ %d !\n", iface->irq);
596 597 598 599 600 601 602 603
		return -ENODEV;
	}

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

	/* Resume TWI */
	write_CONTROL(iface, iface->saved_control);
604 605 606 607

	return 0;
}

608 609
static SIMPLE_DEV_PM_OPS(i2c_bfin_twi_pm,
			 i2c_bfin_twi_suspend, i2c_bfin_twi_resume);
610 611 612 613
#define I2C_BFIN_TWI_PM_OPS	(&i2c_bfin_twi_pm)
#else
#define I2C_BFIN_TWI_PM_OPS	NULL
#endif
614

615
static int i2c_bfin_twi_probe(struct platform_device *pdev)
616
{
617
	struct bfin_twi_iface *iface;
618
	struct i2c_adapter *p_adap;
619
	struct resource *res;
620
	int rc;
621
	unsigned int clkhilow;
622

623 624
	iface = devm_kzalloc(&pdev->dev, sizeof(struct bfin_twi_iface),
			GFP_KERNEL);
625 626
	if (!iface) {
		dev_err(&pdev->dev, "Cannot allocate memory\n");
627
		return -ENOMEM;
628 629
	}

630
	spin_lock_init(&(iface->lock));
631 632 633

	/* Find and map our resources */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
634 635
	iface->regs_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(iface->regs_base)) {
636
		dev_err(&pdev->dev, "Cannot map IO\n");
637
		return PTR_ERR(iface->regs_base);
638 639 640 641 642
	}

	iface->irq = platform_get_irq(pdev, 0);
	if (iface->irq < 0) {
		dev_err(&pdev->dev, "No IRQ specified\n");
643
		return -ENOENT;
644
	}
645 646

	p_adap = &iface->adap;
647 648
	p_adap->nr = pdev->id;
	strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
649 650
	p_adap->algo = &bfin_twi_algorithm;
	p_adap->algo_data = iface;
651
	p_adap->class = I2C_CLASS_DEPRECATED;
652
	p_adap->dev.parent = &pdev->dev;
653 654
	p_adap->timeout = 5 * HZ;
	p_adap->retries = 3;
655

J
Jingoo Han 已提交
656
	rc = peripheral_request_list(
657
			dev_get_platdata(&pdev->dev),
J
Jingoo Han 已提交
658
			"i2c-bfin-twi");
659 660
	if (rc) {
		dev_err(&pdev->dev, "Can't setup pin mux!\n");
661
		return -EBUSY;
662 663
	}

664
	rc = devm_request_irq(&pdev->dev, iface->irq, bfin_twi_interrupt_entry,
Y
Yong Zhang 已提交
665
		0, pdev->name, iface);
666
	if (rc) {
667 668
		dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
		rc = -ENODEV;
669
		goto out_error;
670 671 672
	}

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

675 676
	/*
	 * We will not end up with a CLKDIV=0 because no one will specify
S
Sonic Zhang 已提交
677
	 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
678
	 */
S
Sonic Zhang 已提交
679
	clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
680

681
	/* Set Twi interface clock as specified */
682
	write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
683 684

	/* Enable TWI */
685
	write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
686

687
	rc = i2c_add_numbered_adapter(p_adap);
688
	if (rc < 0)
689
		goto out_error;
690 691

	platform_set_drvdata(pdev, iface);
692

M
Masanari Iida 已提交
693
	dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Controller, "
694
		"regs_base@%p\n", iface->regs_base);
695 696 697

	return 0;

698
out_error:
699
	peripheral_free_list(dev_get_platdata(&pdev->dev));
700 701 702 703 704 705 706 707
	return rc;
}

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

	i2c_del_adapter(&(iface->adap));
708
	peripheral_free_list(dev_get_platdata(&pdev->dev));
709 710 711 712 713 714 715 716 717

	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",
718
		.pm	= I2C_BFIN_TWI_PM_OPS,
719 720 721 722 723 724 725 726 727 728 729 730 731
	},
};

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);
}

732
subsys_initcall(i2c_bfin_twi_init);
733
module_exit(i2c_bfin_twi_exit);
734 735

MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
M
Masanari Iida 已提交
736
MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Controller Driver");
737
MODULE_LICENSE("GPL");
738
MODULE_ALIAS("platform:i2c-bfin-twi");