Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2009 ST-Ericsson SA
   4 * Copyright (C) 2009 STMicroelectronics
   5 *
   6 * I2C master mode controller driver, used in Nomadik 8815
   7 * and Ux500 platforms.
   8 *
   9 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  10 * Author: Sachin Verma <sachin.verma@st.com>
 
 
 
 
  11 */
  12#include <linux/init.h>
  13#include <linux/module.h>
  14#include <linux/amba/bus.h>
  15#include <linux/slab.h>
  16#include <linux/interrupt.h>
  17#include <linux/i2c.h>
  18#include <linux/err.h>
  19#include <linux/clk.h>
  20#include <linux/io.h>
 
  21#include <linux/pm_runtime.h>
  22#include <linux/of.h>
  23#include <linux/pinctrl/consumer.h>
  24
  25#define DRIVER_NAME "nmk-i2c"
  26
  27/* I2C Controller register offsets */
  28#define I2C_CR		(0x000)
  29#define I2C_SCR		(0x004)
  30#define I2C_HSMCR	(0x008)
  31#define I2C_MCR		(0x00C)
  32#define I2C_TFR		(0x010)
  33#define I2C_SR		(0x014)
  34#define I2C_RFR		(0x018)
  35#define I2C_TFTR	(0x01C)
  36#define I2C_RFTR	(0x020)
  37#define I2C_DMAR	(0x024)
  38#define I2C_BRCR	(0x028)
  39#define I2C_IMSCR	(0x02C)
  40#define I2C_RISR	(0x030)
  41#define I2C_MISR	(0x034)
  42#define I2C_ICR		(0x038)
  43
  44/* Control registers */
  45#define I2C_CR_PE		(0x1 << 0)	/* Peripheral Enable */
  46#define I2C_CR_OM		(0x3 << 1)	/* Operating mode */
  47#define I2C_CR_SAM		(0x1 << 3)	/* Slave addressing mode */
  48#define I2C_CR_SM		(0x3 << 4)	/* Speed mode */
  49#define I2C_CR_SGCM		(0x1 << 6)	/* Slave general call mode */
  50#define I2C_CR_FTX		(0x1 << 7)	/* Flush Transmit */
  51#define I2C_CR_FRX		(0x1 << 8)	/* Flush Receive */
  52#define I2C_CR_DMA_TX_EN	(0x1 << 9)	/* DMA Tx enable */
  53#define I2C_CR_DMA_RX_EN	(0x1 << 10)	/* DMA Rx Enable */
  54#define I2C_CR_DMA_SLE		(0x1 << 11)	/* DMA sync. logic enable */
  55#define I2C_CR_LM		(0x1 << 12)	/* Loopback mode */
  56#define I2C_CR_FON		(0x3 << 13)	/* Filtering on */
  57#define I2C_CR_FS		(0x3 << 15)	/* Force stop enable */
  58
  59/* Master controller (MCR) register */
  60#define I2C_MCR_OP		(0x1 << 0)	/* Operation */
  61#define I2C_MCR_A7		(0x7f << 1)	/* 7-bit address */
  62#define I2C_MCR_EA10		(0x7 << 8)	/* 10-bit Extended address */
  63#define I2C_MCR_SB		(0x1 << 11)	/* Extended address */
  64#define I2C_MCR_AM		(0x3 << 12)	/* Address type */
  65#define I2C_MCR_STOP		(0x1 << 14)	/* Stop condition */
  66#define I2C_MCR_LENGTH		(0x7ff << 15)	/* Transaction length */
  67
  68/* Status register (SR) */
  69#define I2C_SR_OP		(0x3 << 0)	/* Operation */
  70#define I2C_SR_STATUS		(0x3 << 2)	/* controller status */
  71#define I2C_SR_CAUSE		(0x7 << 4)	/* Abort cause */
  72#define I2C_SR_TYPE		(0x3 << 7)	/* Receive type */
  73#define I2C_SR_LENGTH		(0x7ff << 9)	/* Transfer length */
  74
  75/* Interrupt mask set/clear (IMSCR) bits */
  76#define I2C_IT_TXFE		(0x1 << 0)
  77#define I2C_IT_TXFNE		(0x1 << 1)
  78#define I2C_IT_TXFF		(0x1 << 2)
  79#define I2C_IT_TXFOVR		(0x1 << 3)
  80#define I2C_IT_RXFE		(0x1 << 4)
  81#define I2C_IT_RXFNF		(0x1 << 5)
  82#define I2C_IT_RXFF		(0x1 << 6)
  83#define I2C_IT_RFSR		(0x1 << 16)
  84#define I2C_IT_RFSE		(0x1 << 17)
  85#define I2C_IT_WTSR		(0x1 << 18)
  86#define I2C_IT_MTD		(0x1 << 19)
  87#define I2C_IT_STD		(0x1 << 20)
  88#define I2C_IT_MAL		(0x1 << 24)
  89#define I2C_IT_BERR		(0x1 << 25)
  90#define I2C_IT_MTDWS		(0x1 << 28)
  91
  92#define GEN_MASK(val, mask, sb)  (((val) << (sb)) & (mask))
  93
  94/* some bits in ICR are reserved */
  95#define I2C_CLEAR_ALL_INTS	0x131f007f
  96
  97/* first three msb bits are reserved */
  98#define IRQ_MASK(mask)		(mask & 0x1fffffff)
  99
 100/* maximum threshold value */
 101#define MAX_I2C_FIFO_THRESHOLD	15
 102
 103enum i2c_freq_mode {
 104	I2C_FREQ_MODE_STANDARD,		/* up to 100 Kb/s */
 105	I2C_FREQ_MODE_FAST,		/* up to 400 Kb/s */
 106	I2C_FREQ_MODE_HIGH_SPEED,	/* up to 3.4 Mb/s */
 107	I2C_FREQ_MODE_FAST_PLUS,	/* up to 1 Mb/s */
 108};
 109
 110/**
 111 * struct i2c_vendor_data - per-vendor variations
 112 * @has_mtdws: variant has the MTDWS bit
 113 * @fifodepth: variant FIFO depth
 114 */
 115struct i2c_vendor_data {
 116	bool has_mtdws;
 117	u32 fifodepth;
 118};
 119
 120enum i2c_status {
 121	I2C_NOP,
 122	I2C_ON_GOING,
 123	I2C_OK,
 124	I2C_ABORT
 125};
 126
 127/* operation */
 128enum i2c_operation {
 129	I2C_NO_OPERATION = 0xff,
 130	I2C_WRITE = 0x00,
 131	I2C_READ = 0x01
 132};
 133
 134/**
 135 * struct i2c_nmk_client - client specific data
 136 * @slave_adr: 7-bit slave address
 137 * @count: no. bytes to be transferred
 138 * @buffer: client data buffer
 139 * @xfer_bytes: bytes transferred till now
 140 * @operation: current I2C operation
 141 */
 142struct i2c_nmk_client {
 143	unsigned short		slave_adr;
 144	unsigned long		count;
 145	unsigned char		*buffer;
 146	unsigned long		xfer_bytes;
 147	enum i2c_operation	operation;
 148};
 149
 150/**
 151 * struct nmk_i2c_dev - private data structure of the controller.
 152 * @vendor: vendor data for this variant.
 153 * @adev: parent amba device.
 154 * @adap: corresponding I2C adapter.
 155 * @irq: interrupt line for the controller.
 156 * @virtbase: virtual io memory area.
 157 * @clk: hardware i2c block clock.
 
 158 * @cli: holder of client specific data.
 159 * @clk_freq: clock frequency for the operation mode
 160 * @tft: Tx FIFO Threshold in bytes
 161 * @rft: Rx FIFO Threshold in bytes
 162 * @timeout: Slave response timeout (ms)
 163 * @sm: speed mode
 164 * @stop: stop condition.
 165 * @xfer_complete: acknowledge completion for a I2C message.
 166 * @result: controller propogated result.
 
 
 167 */
 168struct nmk_i2c_dev {
 169	struct i2c_vendor_data		*vendor;
 170	struct amba_device		*adev;
 171	struct i2c_adapter		adap;
 172	int				irq;
 173	void __iomem			*virtbase;
 174	struct clk			*clk;
 
 175	struct i2c_nmk_client		cli;
 176	u32				clk_freq;
 177	unsigned char			tft;
 178	unsigned char			rft;
 179	int				timeout;
 180	enum i2c_freq_mode		sm;
 181	int				stop;
 182	struct completion		xfer_complete;
 183	int				result;
 
 
 184};
 185
 186/* controller's abort causes */
 187static const char *abort_causes[] = {
 188	"no ack received after address transmission",
 189	"no ack received during data phase",
 190	"ack received after xmission of master code",
 191	"master lost arbitration",
 192	"slave restarts",
 193	"slave reset",
 194	"overflow, maxsize is 2047 bytes",
 195};
 196
 197static inline void i2c_set_bit(void __iomem *reg, u32 mask)
 198{
 199	writel(readl(reg) | mask, reg);
 200}
 201
 202static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
 203{
 204	writel(readl(reg) & ~mask, reg);
 205}
 206
 207/**
 208 * flush_i2c_fifo() - This function flushes the I2C FIFO
 209 * @dev: private data of I2C Driver
 210 *
 211 * This function flushes the I2C Tx and Rx FIFOs. It returns
 212 * 0 on successful flushing of FIFO
 213 */
 214static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
 215{
 216#define LOOP_ATTEMPTS 10
 217	int i;
 218	unsigned long timeout;
 219
 220	/*
 221	 * flush the transmit and receive FIFO. The flushing
 222	 * operation takes several cycles before to be completed.
 223	 * On the completion, the I2C internal logic clears these
 224	 * bits, until then no one must access Tx, Rx FIFO and
 225	 * should poll on these bits waiting for the completion.
 226	 */
 227	writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
 228
 229	for (i = 0; i < LOOP_ATTEMPTS; i++) {
 230		timeout = jiffies + dev->adap.timeout;
 231
 232		while (!time_after(jiffies, timeout)) {
 233			if ((readl(dev->virtbase + I2C_CR) &
 234				(I2C_CR_FTX | I2C_CR_FRX)) == 0)
 235					return 0;
 236		}
 237	}
 238
 239	dev_err(&dev->adev->dev,
 240		"flushing operation timed out giving up after %d attempts",
 241		LOOP_ATTEMPTS);
 242
 243	return -ETIMEDOUT;
 244}
 245
 246/**
 247 * disable_all_interrupts() - Disable all interrupts of this I2c Bus
 248 * @dev: private data of I2C Driver
 249 */
 250static void disable_all_interrupts(struct nmk_i2c_dev *dev)
 251{
 252	u32 mask = IRQ_MASK(0);
 253	writel(mask, dev->virtbase + I2C_IMSCR);
 254}
 255
 256/**
 257 * clear_all_interrupts() - Clear all interrupts of I2C Controller
 258 * @dev: private data of I2C Driver
 259 */
 260static void clear_all_interrupts(struct nmk_i2c_dev *dev)
 261{
 262	u32 mask;
 263	mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
 264	writel(mask, dev->virtbase + I2C_ICR);
 265}
 266
 267/**
 268 * init_hw() - initialize the I2C hardware
 269 * @dev: private data of I2C Driver
 270 */
 271static int init_hw(struct nmk_i2c_dev *dev)
 272{
 273	int stat;
 274
 275	stat = flush_i2c_fifo(dev);
 276	if (stat)
 277		goto exit;
 278
 279	/* disable the controller */
 280	i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
 281
 282	disable_all_interrupts(dev);
 283
 284	clear_all_interrupts(dev);
 285
 286	dev->cli.operation = I2C_NO_OPERATION;
 287
 288exit:
 289	return stat;
 290}
 291
 292/* enable peripheral, master mode operation */
 293#define DEFAULT_I2C_REG_CR	((1 << 1) | I2C_CR_PE)
 294
 295/**
 296 * load_i2c_mcr_reg() - load the MCR register
 297 * @dev: private data of controller
 298 * @flags: message flags
 299 */
 300static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev, u16 flags)
 301{
 302	u32 mcr = 0;
 303	unsigned short slave_adr_3msb_bits;
 304
 
 
 305	mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
 306
 307	if (unlikely(flags & I2C_M_TEN)) {
 308		/* 10-bit address transaction */
 309		mcr |= GEN_MASK(2, I2C_MCR_AM, 12);
 310		/*
 311		 * Get the top 3 bits.
 312		 * EA10 represents extended address in MCR. This includes
 313		 * the extension (MSB bits) of the 7 bit address loaded
 314		 * in A7
 315		 */
 316		slave_adr_3msb_bits = (dev->cli.slave_adr >> 7) & 0x7;
 317
 318		mcr |= GEN_MASK(slave_adr_3msb_bits, I2C_MCR_EA10, 8);
 319	} else {
 320		/* 7-bit address transaction */
 321		mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
 322	}
 323
 324	/* start byte procedure not applied */
 325	mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
 326
 327	/* check the operation, master read/write? */
 328	if (dev->cli.operation == I2C_WRITE)
 329		mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
 330	else
 331		mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
 332
 333	/* stop or repeated start? */
 334	if (dev->stop)
 335		mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
 336	else
 337		mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
 338
 339	mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
 340
 341	return mcr;
 342}
 343
 344/**
 345 * setup_i2c_controller() - setup the controller
 346 * @dev: private data of controller
 347 */
 348static void setup_i2c_controller(struct nmk_i2c_dev *dev)
 349{
 350	u32 brcr1, brcr2;
 351	u32 i2c_clk, div;
 352	u32 ns;
 353	u16 slsu;
 354
 355	writel(0x0, dev->virtbase + I2C_CR);
 356	writel(0x0, dev->virtbase + I2C_HSMCR);
 357	writel(0x0, dev->virtbase + I2C_TFTR);
 358	writel(0x0, dev->virtbase + I2C_RFTR);
 359	writel(0x0, dev->virtbase + I2C_DMAR);
 360
 361	i2c_clk = clk_get_rate(dev->clk);
 362
 363	/*
 364	 * set the slsu:
 365	 *
 366	 * slsu defines the data setup time after SCL clock
 367	 * stretching in terms of i2c clk cycles + 1 (zero means
 368	 * "wait one cycle"), the needed setup time for the three
 369	 * modes are 250ns, 100ns, 10ns respectively.
 370	 *
 371	 * As the time for one cycle T in nanoseconds is
 372	 * T = (1/f) * 1000000000 =>
 373	 * slsu = cycles / (1000000000 / f) + 1
 374	 */
 375	ns = DIV_ROUND_UP_ULL(1000000000ULL, i2c_clk);
 376	switch (dev->sm) {
 377	case I2C_FREQ_MODE_FAST:
 378	case I2C_FREQ_MODE_FAST_PLUS:
 379		slsu = DIV_ROUND_UP(100, ns); /* Fast */
 380		break;
 381	case I2C_FREQ_MODE_HIGH_SPEED:
 382		slsu = DIV_ROUND_UP(10, ns); /* High */
 383		break;
 384	case I2C_FREQ_MODE_STANDARD:
 385	default:
 386		slsu = DIV_ROUND_UP(250, ns); /* Standard */
 387		break;
 388	}
 389	slsu += 1;
 390
 391	dev_dbg(&dev->adev->dev, "calculated SLSU = %04x\n", slsu);
 392	writel(slsu << 16, dev->virtbase + I2C_SCR);
 
 393
 394	/*
 395	 * The spec says, in case of std. mode the divider is
 396	 * 2 whereas it is 3 for fast and fastplus mode of
 397	 * operation. TODO - high speed support.
 398	 */
 399	div = (dev->clk_freq > I2C_MAX_STANDARD_MODE_FREQ) ? 3 : 2;
 400
 401	/*
 402	 * generate the mask for baud rate counters. The controller
 403	 * has two baud rate counters. One is used for High speed
 404	 * operation, and the other is for std, fast mode, fast mode
 405	 * plus operation. Currently we do not supprt high speed mode
 406	 * so set brcr1 to 0.
 407	 */
 408	brcr1 = 0 << 16;
 409	brcr2 = (i2c_clk/(dev->clk_freq * div)) & 0xffff;
 410
 411	/* set the baud rate counter register */
 412	writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
 413
 414	/*
 415	 * set the speed mode. Currently we support
 416	 * only standard and fast mode of operation
 417	 * TODO - support for fast mode plus (up to 1Mb/s)
 418	 * and high speed (up to 3.4 Mb/s)
 419	 */
 420	if (dev->sm > I2C_FREQ_MODE_FAST) {
 421		dev_err(&dev->adev->dev,
 422			"do not support this mode defaulting to std. mode\n");
 423		brcr2 = i2c_clk / (I2C_MAX_STANDARD_MODE_FREQ * 2) & 0xffff;
 424		writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
 425		writel(I2C_FREQ_MODE_STANDARD << 4,
 426				dev->virtbase + I2C_CR);
 427	}
 428	writel(dev->sm << 4, dev->virtbase + I2C_CR);
 429
 430	/* set the Tx and Rx FIFO threshold */
 431	writel(dev->tft, dev->virtbase + I2C_TFTR);
 432	writel(dev->rft, dev->virtbase + I2C_RFTR);
 433}
 434
 435/**
 436 * read_i2c() - Read from I2C client device
 437 * @dev: private data of I2C Driver
 438 * @flags: message flags
 439 *
 440 * This function reads from i2c client device when controller is in
 441 * master mode. There is a completion timeout. If there is no transfer
 442 * before timeout error is returned.
 443 */
 444static int read_i2c(struct nmk_i2c_dev *dev, u16 flags)
 445{
 446	int status = 0;
 447	u32 mcr, irq_mask;
 448	unsigned long timeout;
 
 449
 450	mcr = load_i2c_mcr_reg(dev, flags);
 451	writel(mcr, dev->virtbase + I2C_MCR);
 452
 453	/* load the current CR value */
 454	writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
 455			dev->virtbase + I2C_CR);
 456
 457	/* enable the controller */
 458	i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
 459
 460	init_completion(&dev->xfer_complete);
 461
 462	/* enable interrupts by setting the mask */
 463	irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
 464			I2C_IT_MAL | I2C_IT_BERR);
 465
 466	if (dev->stop || !dev->vendor->has_mtdws)
 467		irq_mask |= I2C_IT_MTD;
 468	else
 469		irq_mask |= I2C_IT_MTDWS;
 470
 471	irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
 472
 473	writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
 474			dev->virtbase + I2C_IMSCR);
 475
 476	timeout = wait_for_completion_timeout(
 477		&dev->xfer_complete, dev->adap.timeout);
 478
 
 
 
 
 
 
 
 479	if (timeout == 0) {
 480		/* Controller timed out */
 481		dev_err(&dev->adev->dev, "read from slave 0x%x timed out\n",
 482				dev->cli.slave_adr);
 483		status = -ETIMEDOUT;
 484	}
 485	return status;
 486}
 487
 488static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
 489{
 490	int count;
 491
 492	for (count = (no_bytes - 2);
 493			(count > 0) &&
 494			(dev->cli.count != 0);
 495			count--) {
 496		/* write to the Tx FIFO */
 497		writeb(*dev->cli.buffer,
 498			dev->virtbase + I2C_TFR);
 499		dev->cli.buffer++;
 500		dev->cli.count--;
 501		dev->cli.xfer_bytes++;
 502	}
 503
 504}
 505
 506/**
 507 * write_i2c() - Write data to I2C client.
 508 * @dev: private data of I2C Driver
 509 * @flags: message flags
 510 *
 511 * This function writes data to I2C client
 512 */
 513static int write_i2c(struct nmk_i2c_dev *dev, u16 flags)
 514{
 515	u32 status = 0;
 516	u32 mcr, irq_mask;
 517	unsigned long timeout;
 
 518
 519	mcr = load_i2c_mcr_reg(dev, flags);
 520
 521	writel(mcr, dev->virtbase + I2C_MCR);
 522
 523	/* load the current CR value */
 524	writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
 525			dev->virtbase + I2C_CR);
 526
 527	/* enable the controller */
 528	i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
 529
 530	init_completion(&dev->xfer_complete);
 531
 532	/* enable interrupts by settings the masks */
 533	irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
 534
 535	/* Fill the TX FIFO with transmit data */
 536	fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
 537
 538	if (dev->cli.count != 0)
 539		irq_mask |= I2C_IT_TXFNE;
 540
 541	/*
 542	 * check if we want to transfer a single or multiple bytes, if so
 543	 * set the MTDWS bit (Master Transaction Done Without Stop)
 544	 * to start repeated start operation
 545	 */
 546	if (dev->stop || !dev->vendor->has_mtdws)
 547		irq_mask |= I2C_IT_MTD;
 548	else
 549		irq_mask |= I2C_IT_MTDWS;
 550
 551	irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
 552
 553	writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
 554			dev->virtbase + I2C_IMSCR);
 555
 556	timeout = wait_for_completion_timeout(
 557		&dev->xfer_complete, dev->adap.timeout);
 558
 
 
 
 
 
 
 
 559	if (timeout == 0) {
 560		/* Controller timed out */
 561		dev_err(&dev->adev->dev, "write to slave 0x%x timed out\n",
 562				dev->cli.slave_adr);
 563		status = -ETIMEDOUT;
 564	}
 565
 566	return status;
 567}
 568
 569/**
 570 * nmk_i2c_xfer_one() - transmit a single I2C message
 571 * @dev: device with a message encoded into it
 572 * @flags: message flags
 573 */
 574static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
 575{
 576	int status;
 577
 578	if (flags & I2C_M_RD) {
 579		/* read operation */
 580		dev->cli.operation = I2C_READ;
 581		status = read_i2c(dev, flags);
 582	} else {
 583		/* write operation */
 584		dev->cli.operation = I2C_WRITE;
 585		status = write_i2c(dev, flags);
 586	}
 587
 588	if (status || (dev->result)) {
 589		u32 i2c_sr;
 590		u32 cause;
 591
 592		i2c_sr = readl(dev->virtbase + I2C_SR);
 593		/*
 594		 * Check if the controller I2C operation status
 595		 * is set to ABORT(11b).
 596		 */
 597		if (((i2c_sr >> 2) & 0x3) == 0x3) {
 598			/* get the abort cause */
 599			cause =	(i2c_sr >> 4) & 0x7;
 600			dev_err(&dev->adev->dev, "%s\n",
 601				cause >= ARRAY_SIZE(abort_causes) ?
 602				"unknown reason" :
 603				abort_causes[cause]);
 604		}
 605
 606		(void) init_hw(dev);
 607
 608		status = status ? status : dev->result;
 609	}
 610
 611	return status;
 612}
 613
 614/**
 615 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
 616 * @i2c_adap: Adapter pointer to the controller
 617 * @msgs: Pointer to data to be written.
 618 * @num_msgs: Number of messages to be executed
 619 *
 620 * This is the function called by the generic kernel i2c_transfer()
 621 * or i2c_smbus...() API calls. Note that this code is protected by the
 622 * semaphore set in the kernel i2c_transfer() function.
 623 *
 624 * NOTE:
 625 * READ TRANSFER : We impose a restriction of the first message to be the
 626 *		index message for any read transaction.
 627 *		- a no index is coded as '0',
 628 *		- 2byte big endian index is coded as '3'
 629 *		!!! msg[0].buf holds the actual index.
 630 *		This is compatible with generic messages of smbus emulator
 631 *		that send a one byte index.
 632 *		eg. a I2C transation to read 2 bytes from index 0
 633 *			idx = 0;
 634 *			msg[0].addr = client->addr;
 635 *			msg[0].flags = 0x0;
 636 *			msg[0].len = 1;
 637 *			msg[0].buf = &idx;
 638 *
 639 *			msg[1].addr = client->addr;
 640 *			msg[1].flags = I2C_M_RD;
 641 *			msg[1].len = 2;
 642 *			msg[1].buf = rd_buff
 643 *			i2c_transfer(adap, msg, 2);
 644 *
 645 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
 646 *		If you want to emulate an SMBUS write transaction put the
 647 *		index as first byte(or first and second) in the payload.
 648 *		eg. a I2C transation to write 2 bytes from index 1
 649 *			wr_buff[0] = 0x1;
 650 *			wr_buff[1] = 0x23;
 651 *			wr_buff[2] = 0x46;
 652 *			msg[0].flags = 0x0;
 653 *			msg[0].len = 3;
 654 *			msg[0].buf = wr_buff;
 655 *			i2c_transfer(adap, msg, 1);
 656 *
 657 * To read or write a block of data (multiple bytes) using SMBUS emulation
 658 * please use the i2c_smbus_read_i2c_block_data()
 659 * or i2c_smbus_write_i2c_block_data() API
 660 */
 661static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
 662		struct i2c_msg msgs[], int num_msgs)
 663{
 664	int status = 0;
 665	int i;
 666	struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
 667	int j;
 668
 669	pm_runtime_get_sync(&dev->adev->dev);
 
 
 
 
 
 
 
 
 
 
 670
 671	/* Attempt three times to send the message queue */
 672	for (j = 0; j < 3; j++) {
 673		/* setup the i2c controller */
 674		setup_i2c_controller(dev);
 675
 676		for (i = 0; i < num_msgs; i++) {
 
 
 
 
 
 
 
 677			dev->cli.slave_adr	= msgs[i].addr;
 678			dev->cli.buffer		= msgs[i].buf;
 679			dev->cli.count		= msgs[i].len;
 680			dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
 681			dev->result = 0;
 682
 683			status = nmk_i2c_xfer_one(dev, msgs[i].flags);
 684			if (status != 0)
 685				break;
 686		}
 687		if (status == 0)
 688			break;
 689	}
 690
 691	pm_runtime_put_sync(&dev->adev->dev);
 
 
 
 
 
 
 692
 693	/* return the no. messages processed */
 694	if (status)
 695		return status;
 696	else
 697		return num_msgs;
 698}
 699
 700/**
 701 * disable_interrupts() - disable the interrupts
 702 * @dev: private data of controller
 703 * @irq: interrupt number
 704 */
 705static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
 706{
 707	irq = IRQ_MASK(irq);
 708	writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
 709			dev->virtbase + I2C_IMSCR);
 710	return 0;
 711}
 712
 713/**
 714 * i2c_irq_handler() - interrupt routine
 715 * @irq: interrupt number
 716 * @arg: data passed to the handler
 717 *
 718 * This is the interrupt handler for the i2c driver. Currently
 719 * it handles the major interrupts like Rx & Tx FIFO management
 720 * interrupts, master transaction interrupts, arbitration and
 721 * bus error interrupts. The rest of the interrupts are treated as
 722 * unhandled.
 723 */
 724static irqreturn_t i2c_irq_handler(int irq, void *arg)
 725{
 726	struct nmk_i2c_dev *dev = arg;
 727	u32 tft, rft;
 728	u32 count;
 729	u32 misr, src;
 
 730
 731	/* load Tx FIFO and Rx FIFO threshold values */
 732	tft = readl(dev->virtbase + I2C_TFTR);
 733	rft = readl(dev->virtbase + I2C_RFTR);
 734
 735	/* read interrupt status register */
 736	misr = readl(dev->virtbase + I2C_MISR);
 737
 738	src = __ffs(misr);
 739	switch ((1 << src)) {
 740
 741	/* Transmit FIFO nearly empty interrupt */
 742	case I2C_IT_TXFNE:
 743	{
 744		if (dev->cli.operation == I2C_READ) {
 745			/*
 746			 * in read operation why do we care for writing?
 747			 * so disable the Transmit FIFO interrupt
 748			 */
 749			disable_interrupts(dev, I2C_IT_TXFNE);
 750		} else {
 751			fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
 752			/*
 753			 * if done, close the transfer by disabling the
 754			 * corresponding TXFNE interrupt
 755			 */
 756			if (dev->cli.count == 0)
 757				disable_interrupts(dev,	I2C_IT_TXFNE);
 758		}
 759	}
 760	break;
 761
 762	/*
 763	 * Rx FIFO nearly full interrupt.
 764	 * This is set when the numer of entries in Rx FIFO is
 765	 * greater or equal than the threshold value programmed
 766	 * in RFT
 767	 */
 768	case I2C_IT_RXFNF:
 769		for (count = rft; count > 0; count--) {
 770			/* Read the Rx FIFO */
 771			*dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
 772			dev->cli.buffer++;
 773		}
 774		dev->cli.count -= rft;
 775		dev->cli.xfer_bytes += rft;
 776		break;
 777
 778	/* Rx FIFO full */
 779	case I2C_IT_RXFF:
 780		for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
 781			*dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
 782			dev->cli.buffer++;
 783		}
 784		dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
 785		dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
 786		break;
 787
 788	/* Master Transaction Done with/without stop */
 789	case I2C_IT_MTD:
 790	case I2C_IT_MTDWS:
 791		if (dev->cli.operation == I2C_READ) {
 792			while (!(readl(dev->virtbase + I2C_RISR)
 793				 & I2C_IT_RXFE)) {
 794				if (dev->cli.count == 0)
 795					break;
 796				*dev->cli.buffer =
 797					readb(dev->virtbase + I2C_RFR);
 798				dev->cli.buffer++;
 799				dev->cli.count--;
 800				dev->cli.xfer_bytes++;
 801			}
 802		}
 803
 804		disable_all_interrupts(dev);
 805		clear_all_interrupts(dev);
 806
 807		if (dev->cli.count) {
 808			dev->result = -EIO;
 809			dev_err(&dev->adev->dev,
 810				"%lu bytes still remain to be xfered\n",
 811				dev->cli.count);
 812			(void) init_hw(dev);
 813		}
 814		complete(&dev->xfer_complete);
 815
 816		break;
 817
 818	/* Master Arbitration lost interrupt */
 819	case I2C_IT_MAL:
 820		dev->result = -EIO;
 821		(void) init_hw(dev);
 822
 823		i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
 824		complete(&dev->xfer_complete);
 825
 826		break;
 827
 828	/*
 829	 * Bus Error interrupt.
 830	 * This happens when an unexpected start/stop condition occurs
 831	 * during the transaction.
 832	 */
 833	case I2C_IT_BERR:
 834		dev->result = -EIO;
 835		/* get the status */
 836		if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
 837			(void) init_hw(dev);
 838
 839		i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
 840		complete(&dev->xfer_complete);
 841
 842		break;
 843
 844	/*
 845	 * Tx FIFO overrun interrupt.
 846	 * This is set when a write operation in Tx FIFO is performed and
 847	 * the Tx FIFO is full.
 848	 */
 849	case I2C_IT_TXFOVR:
 850		dev->result = -EIO;
 851		(void) init_hw(dev);
 852
 853		dev_err(&dev->adev->dev, "Tx Fifo Over run\n");
 854		complete(&dev->xfer_complete);
 855
 856		break;
 857
 858	/* unhandled interrupts by this driver - TODO*/
 859	case I2C_IT_TXFE:
 860	case I2C_IT_TXFF:
 861	case I2C_IT_RXFE:
 862	case I2C_IT_RFSR:
 863	case I2C_IT_RFSE:
 864	case I2C_IT_WTSR:
 865	case I2C_IT_STD:
 866		dev_err(&dev->adev->dev, "unhandled Interrupt\n");
 867		break;
 868	default:
 869		dev_err(&dev->adev->dev, "spurious Interrupt..\n");
 870		break;
 871	}
 872
 873	return IRQ_HANDLED;
 874}
 875
 876static int nmk_i2c_suspend_late(struct device *dev)
 877{
 878	int ret;
 879
 880	ret = pm_runtime_force_suspend(dev);
 881	if (ret)
 882		return ret;
 883
 884	pinctrl_pm_select_sleep_state(dev);
 885	return 0;
 886}
 887
 888static int nmk_i2c_resume_early(struct device *dev)
 889{
 890	return pm_runtime_force_resume(dev);
 891}
 892
 893static int nmk_i2c_runtime_suspend(struct device *dev)
 894{
 895	struct amba_device *adev = to_amba_device(dev);
 896	struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
 897
 898	clk_disable_unprepare(nmk_i2c->clk);
 899	pinctrl_pm_select_idle_state(dev);
 900	return 0;
 901}
 902
 903static int nmk_i2c_runtime_resume(struct device *dev)
 904{
 905	struct amba_device *adev = to_amba_device(dev);
 906	struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
 907	int ret;
 908
 909	ret = clk_prepare_enable(nmk_i2c->clk);
 910	if (ret) {
 911		dev_err(dev, "can't prepare_enable clock\n");
 912		return ret;
 913	}
 914
 915	pinctrl_pm_select_default_state(dev);
 916
 917	ret = init_hw(nmk_i2c);
 918	if (ret) {
 919		clk_disable_unprepare(nmk_i2c->clk);
 920		pinctrl_pm_select_idle_state(dev);
 921	}
 922
 923	return ret;
 924}
 
 
 
 
 925
 
 
 
 
 
 926static const struct dev_pm_ops nmk_i2c_pm = {
 927	LATE_SYSTEM_SLEEP_PM_OPS(nmk_i2c_suspend_late, nmk_i2c_resume_early)
 928	RUNTIME_PM_OPS(nmk_i2c_runtime_suspend, nmk_i2c_runtime_resume, NULL)
 929};
 930
 931static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
 932{
 933	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
 934}
 935
 936static const struct i2c_algorithm nmk_i2c_algo = {
 937	.master_xfer	= nmk_i2c_xfer,
 938	.functionality	= nmk_i2c_functionality
 939};
 940
 941static void nmk_i2c_of_probe(struct device_node *np,
 942			     struct nmk_i2c_dev *nmk)
 943{
 944	/* Default to 100 kHz if no frequency is given in the node */
 945	if (of_property_read_u32(np, "clock-frequency", &nmk->clk_freq))
 946		nmk->clk_freq = I2C_MAX_STANDARD_MODE_FREQ;
 947
 948	/* This driver only supports 'standard' and 'fast' modes of operation. */
 949	if (nmk->clk_freq <= I2C_MAX_STANDARD_MODE_FREQ)
 950		nmk->sm = I2C_FREQ_MODE_STANDARD;
 951	else
 952		nmk->sm = I2C_FREQ_MODE_FAST;
 953	nmk->tft = 1; /* Tx FIFO threshold */
 954	nmk->rft = 8; /* Rx FIFO threshold */
 955	nmk->timeout = 200; /* Slave response timeout(ms) */
 956}
 957
 958static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
 959{
 960	int ret = 0;
 961	struct device_node *np = adev->dev.of_node;
 
 
 962	struct nmk_i2c_dev	*dev;
 963	struct i2c_adapter *adap;
 964	struct i2c_vendor_data *vendor = id->data;
 965	u32 max_fifo_threshold = (vendor->fifodepth / 2) - 1;
 966
 967	dev = devm_kzalloc(&adev->dev, sizeof(*dev), GFP_KERNEL);
 968	if (!dev)
 969		return -ENOMEM;
 970
 971	dev->vendor = vendor;
 972	dev->adev = adev;
 973	nmk_i2c_of_probe(np, dev);
 974
 975	if (dev->tft > max_fifo_threshold) {
 976		dev_warn(&adev->dev, "requested TX FIFO threshold %u, adjusted down to %u\n",
 977			 dev->tft, max_fifo_threshold);
 978		dev->tft = max_fifo_threshold;
 979	}
 980
 981	if (dev->rft > max_fifo_threshold) {
 982		dev_warn(&adev->dev, "requested RX FIFO threshold %u, adjusted down to %u\n",
 983			dev->rft, max_fifo_threshold);
 984		dev->rft = max_fifo_threshold;
 
 
 
 985	}
 986
 987	amba_set_drvdata(adev, dev);
 
 
 
 
 988
 989	dev->virtbase = devm_ioremap(&adev->dev, adev->res.start,
 990				resource_size(&adev->res));
 991	if (!dev->virtbase)
 992		return -ENOMEM;
 
 993
 994	dev->irq = adev->irq[0];
 995	ret = devm_request_irq(&adev->dev, dev->irq, i2c_irq_handler, 0,
 996				DRIVER_NAME, dev);
 997	if (ret)
 998		return dev_err_probe(&adev->dev, ret,
 999				     "cannot claim the irq %d\n", dev->irq);
1000
1001	dev->clk = devm_clk_get_enabled(&adev->dev, NULL);
1002	if (IS_ERR(dev->clk))
1003		return dev_err_probe(&adev->dev, PTR_ERR(dev->clk),
1004				     "could enable i2c clock\n");
1005
1006	init_hw(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1007
1008	adap = &dev->adap;
1009	adap->dev.of_node = np;
1010	adap->dev.parent = &adev->dev;
1011	adap->owner = THIS_MODULE;
1012	adap->class = I2C_CLASS_DEPRECATED;
1013	adap->algo = &nmk_i2c_algo;
1014	adap->timeout = msecs_to_jiffies(dev->timeout);
1015	snprintf(adap->name, sizeof(adap->name),
1016		 "Nomadik I2C at %pR", &adev->res);
 
 
 
 
 
 
 
 
 
 
1017
1018	i2c_set_adapdata(adap, dev);
1019
1020	dev_info(&adev->dev,
1021		 "initialize %s on virtual base %p\n",
1022		 adap->name, dev->virtbase);
1023
1024	ret = i2c_add_adapter(adap);
1025	if (ret)
1026		return ret;
1027
1028	pm_runtime_put(&adev->dev);
1029
1030	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1031}
1032
1033static void nmk_i2c_remove(struct amba_device *adev)
1034{
1035	struct nmk_i2c_dev *dev = amba_get_drvdata(adev);
 
1036
1037	i2c_del_adapter(&dev->adap);
1038	flush_i2c_fifo(dev);
1039	disable_all_interrupts(dev);
1040	clear_all_interrupts(dev);
1041	/* disable the controller */
1042	i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
1043}
 
 
 
 
 
 
 
 
 
1044
1045static struct i2c_vendor_data vendor_stn8815 = {
1046	.has_mtdws = false,
1047	.fifodepth = 16, /* Guessed from TFTR/RFTR = 7 */
1048};
1049
1050static struct i2c_vendor_data vendor_db8500 = {
1051	.has_mtdws = true,
1052	.fifodepth = 32, /* Guessed from TFTR/RFTR = 15 */
1053};
1054
1055static const struct amba_id nmk_i2c_ids[] = {
1056	{
1057		.id	= 0x00180024,
1058		.mask	= 0x00ffffff,
1059		.data	= &vendor_stn8815,
1060	},
1061	{
1062		.id	= 0x00380024,
1063		.mask	= 0x00ffffff,
1064		.data	= &vendor_db8500,
1065	},
1066	{},
1067};
1068
1069MODULE_DEVICE_TABLE(amba, nmk_i2c_ids);
1070
1071static struct amba_driver nmk_i2c_driver = {
1072	.drv = {
1073		.owner = THIS_MODULE,
1074		.name = DRIVER_NAME,
1075		.pm = pm_ptr(&nmk_i2c_pm),
1076	},
1077	.id_table = nmk_i2c_ids,
1078	.probe = nmk_i2c_probe,
1079	.remove = nmk_i2c_remove,
1080};
1081
1082static int __init nmk_i2c_init(void)
1083{
1084	return amba_driver_register(&nmk_i2c_driver);
1085}
1086
1087static void __exit nmk_i2c_exit(void)
1088{
1089	amba_driver_unregister(&nmk_i2c_driver);
1090}
1091
1092subsys_initcall(nmk_i2c_init);
1093module_exit(nmk_i2c_exit);
1094
1095MODULE_AUTHOR("Sachin Verma");
1096MODULE_AUTHOR("Srinidhi KASAGAR");
1097MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1098MODULE_LICENSE("GPL");
v3.5.6
 
   1/*
   2 * Copyright (C) 2009 ST-Ericsson SA
   3 * Copyright (C) 2009 STMicroelectronics
   4 *
   5 * I2C master mode controller driver, used in Nomadik 8815
   6 * and Ux500 platforms.
   7 *
   8 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
   9 * Author: Sachin Verma <sachin.verma@st.com>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License version 2, as
  13 * published by the Free Software Foundation.
  14 */
  15#include <linux/init.h>
  16#include <linux/module.h>
  17#include <linux/platform_device.h>
  18#include <linux/slab.h>
  19#include <linux/interrupt.h>
  20#include <linux/i2c.h>
  21#include <linux/err.h>
  22#include <linux/clk.h>
  23#include <linux/io.h>
  24#include <linux/regulator/consumer.h>
  25#include <linux/pm_runtime.h>
  26
  27#include <plat/i2c.h>
  28
  29#define DRIVER_NAME "nmk-i2c"
  30
  31/* I2C Controller register offsets */
  32#define I2C_CR		(0x000)
  33#define I2C_SCR		(0x004)
  34#define I2C_HSMCR	(0x008)
  35#define I2C_MCR		(0x00C)
  36#define I2C_TFR		(0x010)
  37#define I2C_SR		(0x014)
  38#define I2C_RFR		(0x018)
  39#define I2C_TFTR	(0x01C)
  40#define I2C_RFTR	(0x020)
  41#define I2C_DMAR	(0x024)
  42#define I2C_BRCR	(0x028)
  43#define I2C_IMSCR	(0x02C)
  44#define I2C_RISR	(0x030)
  45#define I2C_MISR	(0x034)
  46#define I2C_ICR		(0x038)
  47
  48/* Control registers */
  49#define I2C_CR_PE		(0x1 << 0)	/* Peripheral Enable */
  50#define I2C_CR_OM		(0x3 << 1)	/* Operating mode */
  51#define I2C_CR_SAM		(0x1 << 3)	/* Slave addressing mode */
  52#define I2C_CR_SM		(0x3 << 4)	/* Speed mode */
  53#define I2C_CR_SGCM		(0x1 << 6)	/* Slave general call mode */
  54#define I2C_CR_FTX		(0x1 << 7)	/* Flush Transmit */
  55#define I2C_CR_FRX		(0x1 << 8)	/* Flush Receive */
  56#define I2C_CR_DMA_TX_EN	(0x1 << 9)	/* DMA Tx enable */
  57#define I2C_CR_DMA_RX_EN	(0x1 << 10)	/* DMA Rx Enable */
  58#define I2C_CR_DMA_SLE		(0x1 << 11)	/* DMA sync. logic enable */
  59#define I2C_CR_LM		(0x1 << 12)	/* Loopback mode */
  60#define I2C_CR_FON		(0x3 << 13)	/* Filtering on */
  61#define I2C_CR_FS		(0x3 << 15)	/* Force stop enable */
  62
  63/* Master controller (MCR) register */
  64#define I2C_MCR_OP		(0x1 << 0)	/* Operation */
  65#define I2C_MCR_A7		(0x7f << 1)	/* 7-bit address */
  66#define I2C_MCR_EA10		(0x7 << 8)	/* 10-bit Extended address */
  67#define I2C_MCR_SB		(0x1 << 11)	/* Extended address */
  68#define I2C_MCR_AM		(0x3 << 12)	/* Address type */
  69#define I2C_MCR_STOP		(0x1 << 14)	/* Stop condition */
  70#define I2C_MCR_LENGTH		(0x7ff << 15)	/* Transaction length */
  71
  72/* Status register (SR) */
  73#define I2C_SR_OP		(0x3 << 0)	/* Operation */
  74#define I2C_SR_STATUS		(0x3 << 2)	/* controller status */
  75#define I2C_SR_CAUSE		(0x7 << 4)	/* Abort cause */
  76#define I2C_SR_TYPE		(0x3 << 7)	/* Receive type */
  77#define I2C_SR_LENGTH		(0x7ff << 9)	/* Transfer length */
  78
  79/* Interrupt mask set/clear (IMSCR) bits */
  80#define I2C_IT_TXFE		(0x1 << 0)
  81#define I2C_IT_TXFNE		(0x1 << 1)
  82#define I2C_IT_TXFF		(0x1 << 2)
  83#define I2C_IT_TXFOVR		(0x1 << 3)
  84#define I2C_IT_RXFE		(0x1 << 4)
  85#define I2C_IT_RXFNF		(0x1 << 5)
  86#define I2C_IT_RXFF		(0x1 << 6)
  87#define I2C_IT_RFSR		(0x1 << 16)
  88#define I2C_IT_RFSE		(0x1 << 17)
  89#define I2C_IT_WTSR		(0x1 << 18)
  90#define I2C_IT_MTD		(0x1 << 19)
  91#define I2C_IT_STD		(0x1 << 20)
  92#define I2C_IT_MAL		(0x1 << 24)
  93#define I2C_IT_BERR		(0x1 << 25)
  94#define I2C_IT_MTDWS		(0x1 << 28)
  95
  96#define GEN_MASK(val, mask, sb)  (((val) << (sb)) & (mask))
  97
  98/* some bits in ICR are reserved */
  99#define I2C_CLEAR_ALL_INTS	0x131f007f
 100
 101/* first three msb bits are reserved */
 102#define IRQ_MASK(mask)		(mask & 0x1fffffff)
 103
 104/* maximum threshold value */
 105#define MAX_I2C_FIFO_THRESHOLD	15
 106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 107enum i2c_status {
 108	I2C_NOP,
 109	I2C_ON_GOING,
 110	I2C_OK,
 111	I2C_ABORT
 112};
 113
 114/* operation */
 115enum i2c_operation {
 116	I2C_NO_OPERATION = 0xff,
 117	I2C_WRITE = 0x00,
 118	I2C_READ = 0x01
 119};
 120
 121/**
 122 * struct i2c_nmk_client - client specific data
 123 * @slave_adr: 7-bit slave address
 124 * @count: no. bytes to be transferred
 125 * @buffer: client data buffer
 126 * @xfer_bytes: bytes transferred till now
 127 * @operation: current I2C operation
 128 */
 129struct i2c_nmk_client {
 130	unsigned short		slave_adr;
 131	unsigned long		count;
 132	unsigned char		*buffer;
 133	unsigned long		xfer_bytes;
 134	enum i2c_operation	operation;
 135};
 136
 137/**
 138 * struct nmk_i2c_dev - private data structure of the controller.
 139 * @pdev: parent platform device.
 
 140 * @adap: corresponding I2C adapter.
 141 * @irq: interrupt line for the controller.
 142 * @virtbase: virtual io memory area.
 143 * @clk: hardware i2c block clock.
 144 * @cfg: machine provided controller configuration.
 145 * @cli: holder of client specific data.
 
 
 
 
 
 146 * @stop: stop condition.
 147 * @xfer_complete: acknowledge completion for a I2C message.
 148 * @result: controller propogated result.
 149 * @regulator: pointer to i2c regulator.
 150 * @busy: Busy doing transfer.
 151 */
 152struct nmk_i2c_dev {
 153	struct platform_device		*pdev;
 
 154	struct i2c_adapter		adap;
 155	int				irq;
 156	void __iomem			*virtbase;
 157	struct clk			*clk;
 158	struct nmk_i2c_controller	cfg;
 159	struct i2c_nmk_client		cli;
 
 
 
 
 
 160	int				stop;
 161	struct completion		xfer_complete;
 162	int				result;
 163	struct regulator		*regulator;
 164	bool				busy;
 165};
 166
 167/* controller's abort causes */
 168static const char *abort_causes[] = {
 169	"no ack received after address transmission",
 170	"no ack received during data phase",
 171	"ack received after xmission of master code",
 172	"master lost arbitration",
 173	"slave restarts",
 174	"slave reset",
 175	"overflow, maxsize is 2047 bytes",
 176};
 177
 178static inline void i2c_set_bit(void __iomem *reg, u32 mask)
 179{
 180	writel(readl(reg) | mask, reg);
 181}
 182
 183static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
 184{
 185	writel(readl(reg) & ~mask, reg);
 186}
 187
 188/**
 189 * flush_i2c_fifo() - This function flushes the I2C FIFO
 190 * @dev: private data of I2C Driver
 191 *
 192 * This function flushes the I2C Tx and Rx FIFOs. It returns
 193 * 0 on successful flushing of FIFO
 194 */
 195static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
 196{
 197#define LOOP_ATTEMPTS 10
 198	int i;
 199	unsigned long timeout;
 200
 201	/*
 202	 * flush the transmit and receive FIFO. The flushing
 203	 * operation takes several cycles before to be completed.
 204	 * On the completion, the I2C internal logic clears these
 205	 * bits, until then no one must access Tx, Rx FIFO and
 206	 * should poll on these bits waiting for the completion.
 207	 */
 208	writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
 209
 210	for (i = 0; i < LOOP_ATTEMPTS; i++) {
 211		timeout = jiffies + dev->adap.timeout;
 212
 213		while (!time_after(jiffies, timeout)) {
 214			if ((readl(dev->virtbase + I2C_CR) &
 215				(I2C_CR_FTX | I2C_CR_FRX)) == 0)
 216					return 0;
 217		}
 218	}
 219
 220	dev_err(&dev->pdev->dev,
 221		"flushing operation timed out giving up after %d attempts",
 222		LOOP_ATTEMPTS);
 223
 224	return -ETIMEDOUT;
 225}
 226
 227/**
 228 * disable_all_interrupts() - Disable all interrupts of this I2c Bus
 229 * @dev: private data of I2C Driver
 230 */
 231static void disable_all_interrupts(struct nmk_i2c_dev *dev)
 232{
 233	u32 mask = IRQ_MASK(0);
 234	writel(mask, dev->virtbase + I2C_IMSCR);
 235}
 236
 237/**
 238 * clear_all_interrupts() - Clear all interrupts of I2C Controller
 239 * @dev: private data of I2C Driver
 240 */
 241static void clear_all_interrupts(struct nmk_i2c_dev *dev)
 242{
 243	u32 mask;
 244	mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
 245	writel(mask, dev->virtbase + I2C_ICR);
 246}
 247
 248/**
 249 * init_hw() - initialize the I2C hardware
 250 * @dev: private data of I2C Driver
 251 */
 252static int init_hw(struct nmk_i2c_dev *dev)
 253{
 254	int stat;
 255
 256	stat = flush_i2c_fifo(dev);
 257	if (stat)
 258		goto exit;
 259
 260	/* disable the controller */
 261	i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
 262
 263	disable_all_interrupts(dev);
 264
 265	clear_all_interrupts(dev);
 266
 267	dev->cli.operation = I2C_NO_OPERATION;
 268
 269exit:
 270	return stat;
 271}
 272
 273/* enable peripheral, master mode operation */
 274#define DEFAULT_I2C_REG_CR	((1 << 1) | I2C_CR_PE)
 275
 276/**
 277 * load_i2c_mcr_reg() - load the MCR register
 278 * @dev: private data of controller
 
 279 */
 280static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)
 281{
 282	u32 mcr = 0;
 
 283
 284	/* 7-bit address transaction */
 285	mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
 286	mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
 287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 288	/* start byte procedure not applied */
 289	mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
 290
 291	/* check the operation, master read/write? */
 292	if (dev->cli.operation == I2C_WRITE)
 293		mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
 294	else
 295		mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
 296
 297	/* stop or repeated start? */
 298	if (dev->stop)
 299		mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
 300	else
 301		mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
 302
 303	mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
 304
 305	return mcr;
 306}
 307
 308/**
 309 * setup_i2c_controller() - setup the controller
 310 * @dev: private data of controller
 311 */
 312static void setup_i2c_controller(struct nmk_i2c_dev *dev)
 313{
 314	u32 brcr1, brcr2;
 315	u32 i2c_clk, div;
 
 
 316
 317	writel(0x0, dev->virtbase + I2C_CR);
 318	writel(0x0, dev->virtbase + I2C_HSMCR);
 319	writel(0x0, dev->virtbase + I2C_TFTR);
 320	writel(0x0, dev->virtbase + I2C_RFTR);
 321	writel(0x0, dev->virtbase + I2C_DMAR);
 322
 
 
 323	/*
 324	 * set the slsu:
 325	 *
 326	 * slsu defines the data setup time after SCL clock
 327	 * stretching in terms of i2c clk cycles. The
 328	 * needed setup time for the three modes are 250ns,
 329	 * 100ns, 10ns respectively thus leading to the values
 330	 * of 14, 6, 2 for a 48 MHz i2c clk.
 
 
 
 331	 */
 332	writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
 333
 334	i2c_clk = clk_get_rate(dev->clk);
 
 
 
 
 
 
 
 
 
 
 
 
 335
 336	/* fallback to std. mode if machine has not provided it */
 337	if (dev->cfg.clk_freq == 0)
 338		dev->cfg.clk_freq = 100000;
 339
 340	/*
 341	 * The spec says, in case of std. mode the divider is
 342	 * 2 whereas it is 3 for fast and fastplus mode of
 343	 * operation. TODO - high speed support.
 344	 */
 345	div = (dev->cfg.clk_freq > 100000) ? 3 : 2;
 346
 347	/*
 348	 * generate the mask for baud rate counters. The controller
 349	 * has two baud rate counters. One is used for High speed
 350	 * operation, and the other is for std, fast mode, fast mode
 351	 * plus operation. Currently we do not supprt high speed mode
 352	 * so set brcr1 to 0.
 353	 */
 354	brcr1 = 0 << 16;
 355	brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;
 356
 357	/* set the baud rate counter register */
 358	writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
 359
 360	/*
 361	 * set the speed mode. Currently we support
 362	 * only standard and fast mode of operation
 363	 * TODO - support for fast mode plus (up to 1Mb/s)
 364	 * and high speed (up to 3.4 Mb/s)
 365	 */
 366	if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
 367		dev_err(&dev->pdev->dev,
 368			"do not support this mode defaulting to std. mode\n");
 369		brcr2 = i2c_clk/(100000 * 2) & 0xffff;
 370		writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
 371		writel(I2C_FREQ_MODE_STANDARD << 4,
 372				dev->virtbase + I2C_CR);
 373	}
 374	writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);
 375
 376	/* set the Tx and Rx FIFO threshold */
 377	writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);
 378	writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);
 379}
 380
 381/**
 382 * read_i2c() - Read from I2C client device
 383 * @dev: private data of I2C Driver
 
 384 *
 385 * This function reads from i2c client device when controller is in
 386 * master mode. There is a completion timeout. If there is no transfer
 387 * before timeout error is returned.
 388 */
 389static int read_i2c(struct nmk_i2c_dev *dev)
 390{
 391	u32 status = 0;
 392	u32 mcr;
 393	u32 irq_mask = 0;
 394	int timeout;
 395
 396	mcr = load_i2c_mcr_reg(dev);
 397	writel(mcr, dev->virtbase + I2C_MCR);
 398
 399	/* load the current CR value */
 400	writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
 401			dev->virtbase + I2C_CR);
 402
 403	/* enable the controller */
 404	i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
 405
 406	init_completion(&dev->xfer_complete);
 407
 408	/* enable interrupts by setting the mask */
 409	irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
 410			I2C_IT_MAL | I2C_IT_BERR);
 411
 412	if (dev->stop)
 413		irq_mask |= I2C_IT_MTD;
 414	else
 415		irq_mask |= I2C_IT_MTDWS;
 416
 417	irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
 418
 419	writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
 420			dev->virtbase + I2C_IMSCR);
 421
 422	timeout = wait_for_completion_timeout(
 423		&dev->xfer_complete, dev->adap.timeout);
 424
 425	if (timeout < 0) {
 426		dev_err(&dev->pdev->dev,
 427			"wait_for_completion_timeout "
 428			"returned %d waiting for event\n", timeout);
 429		status = timeout;
 430	}
 431
 432	if (timeout == 0) {
 433		/* Controller timed out */
 434		dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",
 435				dev->cli.slave_adr);
 436		status = -ETIMEDOUT;
 437	}
 438	return status;
 439}
 440
 441static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
 442{
 443	int count;
 444
 445	for (count = (no_bytes - 2);
 446			(count > 0) &&
 447			(dev->cli.count != 0);
 448			count--) {
 449		/* write to the Tx FIFO */
 450		writeb(*dev->cli.buffer,
 451			dev->virtbase + I2C_TFR);
 452		dev->cli.buffer++;
 453		dev->cli.count--;
 454		dev->cli.xfer_bytes++;
 455	}
 456
 457}
 458
 459/**
 460 * write_i2c() - Write data to I2C client.
 461 * @dev: private data of I2C Driver
 
 462 *
 463 * This function writes data to I2C client
 464 */
 465static int write_i2c(struct nmk_i2c_dev *dev)
 466{
 467	u32 status = 0;
 468	u32 mcr;
 469	u32 irq_mask = 0;
 470	int timeout;
 471
 472	mcr = load_i2c_mcr_reg(dev);
 473
 474	writel(mcr, dev->virtbase + I2C_MCR);
 475
 476	/* load the current CR value */
 477	writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
 478			dev->virtbase + I2C_CR);
 479
 480	/* enable the controller */
 481	i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
 482
 483	init_completion(&dev->xfer_complete);
 484
 485	/* enable interrupts by settings the masks */
 486	irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
 487
 488	/* Fill the TX FIFO with transmit data */
 489	fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
 490
 491	if (dev->cli.count != 0)
 492		irq_mask |= I2C_IT_TXFNE;
 493
 494	/*
 495	 * check if we want to transfer a single or multiple bytes, if so
 496	 * set the MTDWS bit (Master Transaction Done Without Stop)
 497	 * to start repeated start operation
 498	 */
 499	if (dev->stop)
 500		irq_mask |= I2C_IT_MTD;
 501	else
 502		irq_mask |= I2C_IT_MTDWS;
 503
 504	irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
 505
 506	writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
 507			dev->virtbase + I2C_IMSCR);
 508
 509	timeout = wait_for_completion_timeout(
 510		&dev->xfer_complete, dev->adap.timeout);
 511
 512	if (timeout < 0) {
 513		dev_err(&dev->pdev->dev,
 514			"wait_for_completion_timeout "
 515			"returned %d waiting for event\n", timeout);
 516		status = timeout;
 517	}
 518
 519	if (timeout == 0) {
 520		/* Controller timed out */
 521		dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
 522				dev->cli.slave_adr);
 523		status = -ETIMEDOUT;
 524	}
 525
 526	return status;
 527}
 528
 529/**
 530 * nmk_i2c_xfer_one() - transmit a single I2C message
 531 * @dev: device with a message encoded into it
 532 * @flags: message flags
 533 */
 534static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
 535{
 536	int status;
 537
 538	if (flags & I2C_M_RD) {
 539		/* read operation */
 540		dev->cli.operation = I2C_READ;
 541		status = read_i2c(dev);
 542	} else {
 543		/* write operation */
 544		dev->cli.operation = I2C_WRITE;
 545		status = write_i2c(dev);
 546	}
 547
 548	if (status || (dev->result)) {
 549		u32 i2c_sr;
 550		u32 cause;
 551
 552		i2c_sr = readl(dev->virtbase + I2C_SR);
 553		/*
 554		 * Check if the controller I2C operation status
 555		 * is set to ABORT(11b).
 556		 */
 557		if (((i2c_sr >> 2) & 0x3) == 0x3) {
 558			/* get the abort cause */
 559			cause =	(i2c_sr >> 4) & 0x7;
 560			dev_err(&dev->pdev->dev, "%s\n",
 561				cause >= ARRAY_SIZE(abort_causes) ?
 562				"unknown reason" :
 563				abort_causes[cause]);
 564		}
 565
 566		(void) init_hw(dev);
 567
 568		status = status ? status : dev->result;
 569	}
 570
 571	return status;
 572}
 573
 574/**
 575 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
 576 * @i2c_adap: Adapter pointer to the controller
 577 * @msgs: Pointer to data to be written.
 578 * @num_msgs: Number of messages to be executed
 579 *
 580 * This is the function called by the generic kernel i2c_transfer()
 581 * or i2c_smbus...() API calls. Note that this code is protected by the
 582 * semaphore set in the kernel i2c_transfer() function.
 583 *
 584 * NOTE:
 585 * READ TRANSFER : We impose a restriction of the first message to be the
 586 *		index message for any read transaction.
 587 *		- a no index is coded as '0',
 588 *		- 2byte big endian index is coded as '3'
 589 *		!!! msg[0].buf holds the actual index.
 590 *		This is compatible with generic messages of smbus emulator
 591 *		that send a one byte index.
 592 *		eg. a I2C transation to read 2 bytes from index 0
 593 *			idx = 0;
 594 *			msg[0].addr = client->addr;
 595 *			msg[0].flags = 0x0;
 596 *			msg[0].len = 1;
 597 *			msg[0].buf = &idx;
 598 *
 599 *			msg[1].addr = client->addr;
 600 *			msg[1].flags = I2C_M_RD;
 601 *			msg[1].len = 2;
 602 *			msg[1].buf = rd_buff
 603 *			i2c_transfer(adap, msg, 2);
 604 *
 605 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
 606 *		If you want to emulate an SMBUS write transaction put the
 607 *		index as first byte(or first and second) in the payload.
 608 *		eg. a I2C transation to write 2 bytes from index 1
 609 *			wr_buff[0] = 0x1;
 610 *			wr_buff[1] = 0x23;
 611 *			wr_buff[2] = 0x46;
 612 *			msg[0].flags = 0x0;
 613 *			msg[0].len = 3;
 614 *			msg[0].buf = wr_buff;
 615 *			i2c_transfer(adap, msg, 1);
 616 *
 617 * To read or write a block of data (multiple bytes) using SMBUS emulation
 618 * please use the i2c_smbus_read_i2c_block_data()
 619 * or i2c_smbus_write_i2c_block_data() API
 620 */
 621static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
 622		struct i2c_msg msgs[], int num_msgs)
 623{
 624	int status;
 625	int i;
 626	struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
 627	int j;
 628
 629	dev->busy = true;
 630
 631	if (dev->regulator)
 632		regulator_enable(dev->regulator);
 633	pm_runtime_get_sync(&dev->pdev->dev);
 634
 635	clk_enable(dev->clk);
 636
 637	status = init_hw(dev);
 638	if (status)
 639		goto out;
 640
 641	/* Attempt three times to send the message queue */
 642	for (j = 0; j < 3; j++) {
 643		/* setup the i2c controller */
 644		setup_i2c_controller(dev);
 645
 646		for (i = 0; i < num_msgs; i++) {
 647			if (unlikely(msgs[i].flags & I2C_M_TEN)) {
 648				dev_err(&dev->pdev->dev,
 649					"10 bit addressing not supported\n");
 650
 651				status = -EINVAL;
 652				goto out;
 653			}
 654			dev->cli.slave_adr	= msgs[i].addr;
 655			dev->cli.buffer		= msgs[i].buf;
 656			dev->cli.count		= msgs[i].len;
 657			dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
 658			dev->result = 0;
 659
 660			status = nmk_i2c_xfer_one(dev, msgs[i].flags);
 661			if (status != 0)
 662				break;
 663		}
 664		if (status == 0)
 665			break;
 666	}
 667
 668out:
 669	clk_disable(dev->clk);
 670	pm_runtime_put_sync(&dev->pdev->dev);
 671	if (dev->regulator)
 672		regulator_disable(dev->regulator);
 673
 674	dev->busy = false;
 675
 676	/* return the no. messages processed */
 677	if (status)
 678		return status;
 679	else
 680		return num_msgs;
 681}
 682
 683/**
 684 * disable_interrupts() - disable the interrupts
 685 * @dev: private data of controller
 686 * @irq: interrupt number
 687 */
 688static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
 689{
 690	irq = IRQ_MASK(irq);
 691	writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
 692			dev->virtbase + I2C_IMSCR);
 693	return 0;
 694}
 695
 696/**
 697 * i2c_irq_handler() - interrupt routine
 698 * @irq: interrupt number
 699 * @arg: data passed to the handler
 700 *
 701 * This is the interrupt handler for the i2c driver. Currently
 702 * it handles the major interrupts like Rx & Tx FIFO management
 703 * interrupts, master transaction interrupts, arbitration and
 704 * bus error interrupts. The rest of the interrupts are treated as
 705 * unhandled.
 706 */
 707static irqreturn_t i2c_irq_handler(int irq, void *arg)
 708{
 709	struct nmk_i2c_dev *dev = arg;
 710	u32 tft, rft;
 711	u32 count;
 712	u32 misr;
 713	u32 src = 0;
 714
 715	/* load Tx FIFO and Rx FIFO threshold values */
 716	tft = readl(dev->virtbase + I2C_TFTR);
 717	rft = readl(dev->virtbase + I2C_RFTR);
 718
 719	/* read interrupt status register */
 720	misr = readl(dev->virtbase + I2C_MISR);
 721
 722	src = __ffs(misr);
 723	switch ((1 << src)) {
 724
 725	/* Transmit FIFO nearly empty interrupt */
 726	case I2C_IT_TXFNE:
 727	{
 728		if (dev->cli.operation == I2C_READ) {
 729			/*
 730			 * in read operation why do we care for writing?
 731			 * so disable the Transmit FIFO interrupt
 732			 */
 733			disable_interrupts(dev, I2C_IT_TXFNE);
 734		} else {
 735			fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
 736			/*
 737			 * if done, close the transfer by disabling the
 738			 * corresponding TXFNE interrupt
 739			 */
 740			if (dev->cli.count == 0)
 741				disable_interrupts(dev,	I2C_IT_TXFNE);
 742		}
 743	}
 744	break;
 745
 746	/*
 747	 * Rx FIFO nearly full interrupt.
 748	 * This is set when the numer of entries in Rx FIFO is
 749	 * greater or equal than the threshold value programmed
 750	 * in RFT
 751	 */
 752	case I2C_IT_RXFNF:
 753		for (count = rft; count > 0; count--) {
 754			/* Read the Rx FIFO */
 755			*dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
 756			dev->cli.buffer++;
 757		}
 758		dev->cli.count -= rft;
 759		dev->cli.xfer_bytes += rft;
 760		break;
 761
 762	/* Rx FIFO full */
 763	case I2C_IT_RXFF:
 764		for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
 765			*dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
 766			dev->cli.buffer++;
 767		}
 768		dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
 769		dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
 770		break;
 771
 772	/* Master Transaction Done with/without stop */
 773	case I2C_IT_MTD:
 774	case I2C_IT_MTDWS:
 775		if (dev->cli.operation == I2C_READ) {
 776			while (!(readl(dev->virtbase + I2C_RISR)
 777				 & I2C_IT_RXFE)) {
 778				if (dev->cli.count == 0)
 779					break;
 780				*dev->cli.buffer =
 781					readb(dev->virtbase + I2C_RFR);
 782				dev->cli.buffer++;
 783				dev->cli.count--;
 784				dev->cli.xfer_bytes++;
 785			}
 786		}
 787
 788		disable_all_interrupts(dev);
 789		clear_all_interrupts(dev);
 790
 791		if (dev->cli.count) {
 792			dev->result = -EIO;
 793			dev_err(&dev->pdev->dev,
 794				"%lu bytes still remain to be xfered\n",
 795				dev->cli.count);
 796			(void) init_hw(dev);
 797		}
 798		complete(&dev->xfer_complete);
 799
 800		break;
 801
 802	/* Master Arbitration lost interrupt */
 803	case I2C_IT_MAL:
 804		dev->result = -EIO;
 805		(void) init_hw(dev);
 806
 807		i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
 808		complete(&dev->xfer_complete);
 809
 810		break;
 811
 812	/*
 813	 * Bus Error interrupt.
 814	 * This happens when an unexpected start/stop condition occurs
 815	 * during the transaction.
 816	 */
 817	case I2C_IT_BERR:
 818		dev->result = -EIO;
 819		/* get the status */
 820		if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
 821			(void) init_hw(dev);
 822
 823		i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
 824		complete(&dev->xfer_complete);
 825
 826		break;
 827
 828	/*
 829	 * Tx FIFO overrun interrupt.
 830	 * This is set when a write operation in Tx FIFO is performed and
 831	 * the Tx FIFO is full.
 832	 */
 833	case I2C_IT_TXFOVR:
 834		dev->result = -EIO;
 835		(void) init_hw(dev);
 836
 837		dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
 838		complete(&dev->xfer_complete);
 839
 840		break;
 841
 842	/* unhandled interrupts by this driver - TODO*/
 843	case I2C_IT_TXFE:
 844	case I2C_IT_TXFF:
 845	case I2C_IT_RXFE:
 846	case I2C_IT_RFSR:
 847	case I2C_IT_RFSE:
 848	case I2C_IT_WTSR:
 849	case I2C_IT_STD:
 850		dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
 851		break;
 852	default:
 853		dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
 854		break;
 855	}
 856
 857	return IRQ_HANDLED;
 858}
 859
 
 
 
 
 
 
 
 860
 861#ifdef CONFIG_PM
 862static int nmk_i2c_suspend(struct device *dev)
 
 
 
 863{
 864	struct platform_device *pdev = to_platform_device(dev);
 865	struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);
 866
 867	if (nmk_i2c->busy)
 868		return -EBUSY;
 
 
 869
 
 
 870	return 0;
 871}
 872
 873static int nmk_i2c_resume(struct device *dev)
 874{
 875	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 876}
 877#else
 878#define nmk_i2c_suspend	NULL
 879#define nmk_i2c_resume	NULL
 880#endif
 881
 882/*
 883 * We use noirq so that we suspend late and resume before the wakeup interrupt
 884 * to ensure that we do the !pm_runtime_suspended() check in resume before
 885 * there has been a regular pm runtime resume (via pm_runtime_get_sync()).
 886 */
 887static const struct dev_pm_ops nmk_i2c_pm = {
 888	.suspend_noirq	= nmk_i2c_suspend,
 889	.resume_noirq	= nmk_i2c_resume,
 890};
 891
 892static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
 893{
 894	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
 895}
 896
 897static const struct i2c_algorithm nmk_i2c_algo = {
 898	.master_xfer	= nmk_i2c_xfer,
 899	.functionality	= nmk_i2c_functionality
 900};
 901
 902static int __devinit nmk_i2c_probe(struct platform_device *pdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 903{
 904	int ret = 0;
 905	struct resource *res;
 906	struct nmk_i2c_controller *pdata =
 907			pdev->dev.platform_data;
 908	struct nmk_i2c_dev	*dev;
 909	struct i2c_adapter *adap;
 
 
 910
 911	dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
 912	if (!dev) {
 913		dev_err(&pdev->dev, "cannot allocate memory\n");
 914		ret = -ENOMEM;
 915		goto err_no_mem;
 
 
 
 
 
 
 
 916	}
 917	dev->busy = false;
 918	dev->pdev = pdev;
 919	platform_set_drvdata(pdev, dev);
 920
 921	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 922	if (!res) {
 923		ret = -ENOENT;
 924		goto err_no_resource;
 925	}
 926
 927	if (request_mem_region(res->start, resource_size(res),
 928		DRIVER_NAME "I/O region") == NULL) {
 929		ret = -EBUSY;
 930		goto err_no_region;
 931	}
 932
 933	dev->virtbase = ioremap(res->start, resource_size(res));
 934	if (!dev->virtbase) {
 935		ret = -ENOMEM;
 936		goto err_no_ioremap;
 937	}
 938
 939	dev->irq = platform_get_irq(pdev, 0);
 940	ret = request_irq(dev->irq, i2c_irq_handler, 0,
 941				DRIVER_NAME, dev);
 942	if (ret) {
 943		dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
 944		goto err_irq;
 945	}
 
 
 
 
 946
 947	dev->regulator = regulator_get(&pdev->dev, "v-i2c");
 948	if (IS_ERR(dev->regulator)) {
 949		dev_warn(&pdev->dev, "could not get i2c regulator\n");
 950		dev->regulator = NULL;
 951	}
 952
 953	pm_suspend_ignore_children(&pdev->dev, true);
 954	pm_runtime_enable(&pdev->dev);
 955
 956	dev->clk = clk_get(&pdev->dev, NULL);
 957	if (IS_ERR(dev->clk)) {
 958		dev_err(&pdev->dev, "could not get i2c clock\n");
 959		ret = PTR_ERR(dev->clk);
 960		goto err_no_clk;
 961	}
 962
 963	adap = &dev->adap;
 964	adap->dev.parent = &pdev->dev;
 965	adap->owner	= THIS_MODULE;
 966	adap->class	= I2C_CLASS_HWMON | I2C_CLASS_SPD;
 967	adap->algo	= &nmk_i2c_algo;
 968	adap->timeout	= pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
 969		msecs_to_jiffies(20000);
 970	snprintf(adap->name, sizeof(adap->name),
 971		 "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
 972
 973	/* fetch the controller id */
 974	adap->nr	= pdev->id;
 975
 976	/* fetch the controller configuration from machine */
 977	dev->cfg.clk_freq = pdata->clk_freq;
 978	dev->cfg.slsu	= pdata->slsu;
 979	dev->cfg.tft	= pdata->tft;
 980	dev->cfg.rft	= pdata->rft;
 981	dev->cfg.sm	= pdata->sm;
 982
 983	i2c_set_adapdata(adap, dev);
 984
 985	dev_info(&pdev->dev,
 986		 "initialize %s on virtual base %p\n",
 987		 adap->name, dev->virtbase);
 988
 989	ret = i2c_add_numbered_adapter(adap);
 990	if (ret) {
 991		dev_err(&pdev->dev, "failed to add adapter\n");
 992		goto err_add_adap;
 993	}
 994
 995	return 0;
 996
 997 err_add_adap:
 998	clk_put(dev->clk);
 999 err_no_clk:
1000	if (dev->regulator)
1001		regulator_put(dev->regulator);
1002	pm_runtime_disable(&pdev->dev);
1003	free_irq(dev->irq, dev);
1004 err_irq:
1005	iounmap(dev->virtbase);
1006 err_no_ioremap:
1007	release_mem_region(res->start, resource_size(res));
1008 err_no_region:
1009	platform_set_drvdata(pdev, NULL);
1010 err_no_resource:
1011	kfree(dev);
1012 err_no_mem:
1013
1014	return ret;
1015}
1016
1017static int __devexit nmk_i2c_remove(struct platform_device *pdev)
1018{
1019	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1020	struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
1021
1022	i2c_del_adapter(&dev->adap);
1023	flush_i2c_fifo(dev);
1024	disable_all_interrupts(dev);
1025	clear_all_interrupts(dev);
1026	/* disable the controller */
1027	i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
1028	free_irq(dev->irq, dev);
1029	iounmap(dev->virtbase);
1030	if (res)
1031		release_mem_region(res->start, resource_size(res));
1032	clk_put(dev->clk);
1033	if (dev->regulator)
1034		regulator_put(dev->regulator);
1035	pm_runtime_disable(&pdev->dev);
1036	platform_set_drvdata(pdev, NULL);
1037	kfree(dev);
1038
1039	return 0;
1040}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1041
1042static struct platform_driver nmk_i2c_driver = {
1043	.driver = {
1044		.owner = THIS_MODULE,
1045		.name = DRIVER_NAME,
1046		.pm = &nmk_i2c_pm,
1047	},
 
1048	.probe = nmk_i2c_probe,
1049	.remove = __devexit_p(nmk_i2c_remove),
1050};
1051
1052static int __init nmk_i2c_init(void)
1053{
1054	return platform_driver_register(&nmk_i2c_driver);
1055}
1056
1057static void __exit nmk_i2c_exit(void)
1058{
1059	platform_driver_unregister(&nmk_i2c_driver);
1060}
1061
1062subsys_initcall(nmk_i2c_init);
1063module_exit(nmk_i2c_exit);
1064
1065MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
 
1066MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1067MODULE_LICENSE("GPL");
1068MODULE_ALIAS("platform:" DRIVER_NAME);