blob: c8bf81abcd3cb5b2ef440328e1bdf7a2a0f60329 [file] [log] [blame]
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301/*
Linus Walleij1804edd2010-09-23 09:03:40 +02002 * Copyright (C) 2009 ST-Ericsson SA
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05303 * 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/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
srinidhi kasagar3f9900f2010-02-01 19:44:54 +053020#include <linux/interrupt.h>
21#include <linux/i2c.h>
22#include <linux/err.h>
23#include <linux/clk.h>
24#include <linux/io.h>
Jonas Aberga20d2392011-05-13 12:29:02 +020025#include <linux/regulator/consumer.h>
srinidhi kasagar3f9900f2010-02-01 19:44:54 +053026
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
Linus Walleijf868fc32010-09-23 09:04:11 +0200107/* per-transfer delay, required for the hardware to stabilize */
108#define I2C_DELAY 150
109
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530110enum i2c_status {
111 I2C_NOP,
112 I2C_ON_GOING,
113 I2C_OK,
114 I2C_ABORT
115};
116
117/* operation */
118enum i2c_operation {
119 I2C_NO_OPERATION = 0xff,
120 I2C_WRITE = 0x00,
121 I2C_READ = 0x01
122};
123
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530124/**
125 * struct i2c_nmk_client - client specific data
126 * @slave_adr: 7-bit slave address
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300127 * @count: no. bytes to be transferred
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530128 * @buffer: client data buffer
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300129 * @xfer_bytes: bytes transferred till now
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530130 * @operation: current I2C operation
131 */
132struct i2c_nmk_client {
133 unsigned short slave_adr;
134 unsigned long count;
135 unsigned char *buffer;
136 unsigned long xfer_bytes;
137 enum i2c_operation operation;
138};
139
140/**
141 * struct nmk_i2c_dev - private data structure of the controller
142 * @pdev: parent platform device
143 * @adap: corresponding I2C adapter
144 * @irq: interrupt line for the controller
145 * @virtbase: virtual io memory area
146 * @clk: hardware i2c block clock
147 * @cfg: machine provided controller configuration
148 * @cli: holder of client specific data
149 * @stop: stop condition
150 * @xfer_complete: acknowledge completion for a I2C message
151 * @result: controller propogated result
Jonas Aberga20d2392011-05-13 12:29:02 +0200152 * @busy: Busy doing transfer
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530153 */
154struct nmk_i2c_dev {
155 struct platform_device *pdev;
156 struct i2c_adapter adap;
157 int irq;
158 void __iomem *virtbase;
159 struct clk *clk;
160 struct nmk_i2c_controller cfg;
161 struct i2c_nmk_client cli;
162 int stop;
163 struct completion xfer_complete;
164 int result;
Jonas Aberga20d2392011-05-13 12:29:02 +0200165 struct regulator *regulator;
166 bool busy;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530167};
168
169/* controller's abort causes */
170static const char *abort_causes[] = {
171 "no ack received after address transmission",
172 "no ack received during data phase",
173 "ack received after xmission of master code",
174 "master lost arbitration",
175 "slave restarts",
176 "slave reset",
177 "overflow, maxsize is 2047 bytes",
178};
179
180static inline void i2c_set_bit(void __iomem *reg, u32 mask)
181{
182 writel(readl(reg) | mask, reg);
183}
184
185static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
186{
187 writel(readl(reg) & ~mask, reg);
188}
189
190/**
191 * flush_i2c_fifo() - This function flushes the I2C FIFO
192 * @dev: private data of I2C Driver
193 *
194 * This function flushes the I2C Tx and Rx FIFOs. It returns
195 * 0 on successful flushing of FIFO
196 */
197static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
198{
199#define LOOP_ATTEMPTS 10
200 int i;
201 unsigned long timeout;
202
203 /*
204 * flush the transmit and receive FIFO. The flushing
205 * operation takes several cycles before to be completed.
206 * On the completion, the I2C internal logic clears these
207 * bits, until then no one must access Tx, Rx FIFO and
208 * should poll on these bits waiting for the completion.
209 */
210 writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
211
212 for (i = 0; i < LOOP_ATTEMPTS; i++) {
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200213 timeout = jiffies + dev->adap.timeout;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530214
215 while (!time_after(jiffies, timeout)) {
216 if ((readl(dev->virtbase + I2C_CR) &
217 (I2C_CR_FTX | I2C_CR_FRX)) == 0)
218 return 0;
219 }
220 }
221
222 dev_err(&dev->pdev->dev, "flushing operation timed out "
223 "giving up after %d attempts", LOOP_ATTEMPTS);
224
225 return -ETIMEDOUT;
226}
227
228/**
229 * disable_all_interrupts() - Disable all interrupts of this I2c Bus
230 * @dev: private data of I2C Driver
231 */
232static void disable_all_interrupts(struct nmk_i2c_dev *dev)
233{
234 u32 mask = IRQ_MASK(0);
235 writel(mask, dev->virtbase + I2C_IMSCR);
236}
237
238/**
239 * clear_all_interrupts() - Clear all interrupts of I2C Controller
240 * @dev: private data of I2C Driver
241 */
242static void clear_all_interrupts(struct nmk_i2c_dev *dev)
243{
244 u32 mask;
245 mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
246 writel(mask, dev->virtbase + I2C_ICR);
247}
248
249/**
250 * init_hw() - initialize the I2C hardware
251 * @dev: private data of I2C Driver
252 */
253static int init_hw(struct nmk_i2c_dev *dev)
254{
255 int stat;
256
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200257 clk_enable(dev->clk);
258
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530259 stat = flush_i2c_fifo(dev);
260 if (stat)
Jonas Aberga20d2392011-05-13 12:29:02 +0200261 goto exit;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530262
263 /* disable the controller */
264 i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
265
266 disable_all_interrupts(dev);
267
268 clear_all_interrupts(dev);
269
270 dev->cli.operation = I2C_NO_OPERATION;
271
Jonas Aberga20d2392011-05-13 12:29:02 +0200272exit:
273 /* TODO: Why disable clocks after init hw? */
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200274 clk_disable(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +0200275 /*
276 * TODO: What is this delay for?
277 * Must be pretty pointless since the hw block
278 * is frozen. Or?
279 */
Linus Walleijf868fc32010-09-23 09:04:11 +0200280 udelay(I2C_DELAY);
Jonas Aberga20d2392011-05-13 12:29:02 +0200281 return stat;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530282}
283
284/* enable peripheral, master mode operation */
285#define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
286
287/**
288 * load_i2c_mcr_reg() - load the MCR register
289 * @dev: private data of controller
290 */
291static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)
292{
293 u32 mcr = 0;
294
295 /* 7-bit address transaction */
296 mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
297 mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
298
299 /* start byte procedure not applied */
300 mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
301
302 /* check the operation, master read/write? */
303 if (dev->cli.operation == I2C_WRITE)
304 mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
305 else
306 mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
307
308 /* stop or repeated start? */
309 if (dev->stop)
310 mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
311 else
312 mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
313
314 mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
315
316 return mcr;
317}
318
319/**
320 * setup_i2c_controller() - setup the controller
321 * @dev: private data of controller
322 */
323static void setup_i2c_controller(struct nmk_i2c_dev *dev)
324{
325 u32 brcr1, brcr2;
326 u32 i2c_clk, div;
327
328 writel(0x0, dev->virtbase + I2C_CR);
329 writel(0x0, dev->virtbase + I2C_HSMCR);
330 writel(0x0, dev->virtbase + I2C_TFTR);
331 writel(0x0, dev->virtbase + I2C_RFTR);
332 writel(0x0, dev->virtbase + I2C_DMAR);
333
334 /*
335 * set the slsu:
336 *
337 * slsu defines the data setup time after SCL clock
338 * stretching in terms of i2c clk cycles. The
339 * needed setup time for the three modes are 250ns,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300340 * 100ns, 10ns respectively thus leading to the values
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530341 * of 14, 6, 2 for a 48 MHz i2c clk.
342 */
343 writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
344
345 i2c_clk = clk_get_rate(dev->clk);
346
347 /* fallback to std. mode if machine has not provided it */
348 if (dev->cfg.clk_freq == 0)
349 dev->cfg.clk_freq = 100000;
350
351 /*
352 * The spec says, in case of std. mode the divider is
353 * 2 whereas it is 3 for fast and fastplus mode of
354 * operation. TODO - high speed support.
355 */
356 div = (dev->cfg.clk_freq > 100000) ? 3 : 2;
357
358 /*
359 * generate the mask for baud rate counters. The controller
360 * has two baud rate counters. One is used for High speed
361 * operation, and the other is for std, fast mode, fast mode
362 * plus operation. Currently we do not supprt high speed mode
363 * so set brcr1 to 0.
364 */
365 brcr1 = 0 << 16;
366 brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;
367
368 /* set the baud rate counter register */
369 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
370
371 /*
372 * set the speed mode. Currently we support
373 * only standard and fast mode of operation
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 * TODO - support for fast mode plus (up to 1Mb/s)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530375 * and high speed (up to 3.4 Mb/s)
376 */
377 if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
378 dev_err(&dev->pdev->dev, "do not support this mode "
379 "defaulting to std. mode\n");
380 brcr2 = i2c_clk/(100000 * 2) & 0xffff;
381 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
382 writel(I2C_FREQ_MODE_STANDARD << 4,
383 dev->virtbase + I2C_CR);
384 }
385 writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);
386
387 /* set the Tx and Rx FIFO threshold */
388 writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);
389 writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);
390}
391
392/**
393 * read_i2c() - Read from I2C client device
394 * @dev: private data of I2C Driver
395 *
396 * This function reads from i2c client device when controller is in
397 * master mode. There is a completion timeout. If there is no transfer
398 * before timeout error is returned.
399 */
400static int read_i2c(struct nmk_i2c_dev *dev)
401{
402 u32 status = 0;
403 u32 mcr;
404 u32 irq_mask = 0;
405 int timeout;
406
407 mcr = load_i2c_mcr_reg(dev);
408 writel(mcr, dev->virtbase + I2C_MCR);
409
410 /* load the current CR value */
411 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
412 dev->virtbase + I2C_CR);
413
414 /* enable the controller */
415 i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
416
417 init_completion(&dev->xfer_complete);
418
419 /* enable interrupts by setting the mask */
420 irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
421 I2C_IT_MAL | I2C_IT_BERR);
422
423 if (dev->stop)
424 irq_mask |= I2C_IT_MTD;
425 else
426 irq_mask |= I2C_IT_MTDWS;
427
428 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
429
430 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
431 dev->virtbase + I2C_IMSCR);
432
433 timeout = wait_for_completion_interruptible_timeout(
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200434 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530435
436 if (timeout < 0) {
437 dev_err(&dev->pdev->dev,
438 "wait_for_completion_interruptible_timeout"
439 "returned %d waiting for event\n", timeout);
440 status = timeout;
441 }
442
443 if (timeout == 0) {
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400444 /* controller has timedout, re-init the h/w */
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200445 dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",
446 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530447 (void) init_hw(dev);
448 status = -ETIMEDOUT;
449 }
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530450 return status;
451}
452
453/**
454 * write_i2c() - Write data to I2C client.
455 * @dev: private data of I2C Driver
456 *
457 * This function writes data to I2C client
458 */
459static int write_i2c(struct nmk_i2c_dev *dev)
460{
461 u32 status = 0;
462 u32 mcr;
463 u32 irq_mask = 0;
464 int timeout;
465
466 mcr = load_i2c_mcr_reg(dev);
467
468 writel(mcr, dev->virtbase + I2C_MCR);
469
470 /* load the current CR value */
471 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
472 dev->virtbase + I2C_CR);
473
474 /* enable the controller */
475 i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
476
477 init_completion(&dev->xfer_complete);
478
479 /* enable interrupts by settings the masks */
480 irq_mask = (I2C_IT_TXFNE | I2C_IT_TXFOVR |
481 I2C_IT_MAL | I2C_IT_BERR);
482
483 /*
484 * check if we want to transfer a single or multiple bytes, if so
485 * set the MTDWS bit (Master Transaction Done Without Stop)
486 * to start repeated start operation
487 */
488 if (dev->stop)
489 irq_mask |= I2C_IT_MTD;
490 else
491 irq_mask |= I2C_IT_MTDWS;
492
493 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
494
495 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
496 dev->virtbase + I2C_IMSCR);
497
498 timeout = wait_for_completion_interruptible_timeout(
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200499 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530500
501 if (timeout < 0) {
502 dev_err(&dev->pdev->dev,
503 "wait_for_completion_interruptible_timeout"
504 "returned %d waiting for event\n", timeout);
505 status = timeout;
506 }
507
508 if (timeout == 0) {
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400509 /* controller has timedout, re-init the h/w */
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200510 dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
511 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530512 (void) init_hw(dev);
513 status = -ETIMEDOUT;
514 }
515
516 return status;
517}
518
519/**
520 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
Linus Walleij1804edd2010-09-23 09:03:40 +0200521 * @i2c_adap: Adapter pointer to the controller
522 * @msgs: Pointer to data to be written.
523 * @num_msgs: Number of messages to be executed
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530524 *
525 * This is the function called by the generic kernel i2c_transfer()
526 * or i2c_smbus...() API calls. Note that this code is protected by the
527 * semaphore set in the kernel i2c_transfer() function.
528 *
529 * NOTE:
530 * READ TRANSFER : We impose a restriction of the first message to be the
531 * index message for any read transaction.
532 * - a no index is coded as '0',
533 * - 2byte big endian index is coded as '3'
534 * !!! msg[0].buf holds the actual index.
535 * This is compatible with generic messages of smbus emulator
536 * that send a one byte index.
537 * eg. a I2C transation to read 2 bytes from index 0
538 * idx = 0;
539 * msg[0].addr = client->addr;
540 * msg[0].flags = 0x0;
541 * msg[0].len = 1;
542 * msg[0].buf = &idx;
543 *
544 * msg[1].addr = client->addr;
545 * msg[1].flags = I2C_M_RD;
546 * msg[1].len = 2;
547 * msg[1].buf = rd_buff
548 * i2c_transfer(adap, msg, 2);
549 *
550 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
551 * If you want to emulate an SMBUS write transaction put the
552 * index as first byte(or first and second) in the payload.
553 * eg. a I2C transation to write 2 bytes from index 1
554 * wr_buff[0] = 0x1;
555 * wr_buff[1] = 0x23;
556 * wr_buff[2] = 0x46;
557 * msg[0].flags = 0x0;
558 * msg[0].len = 3;
559 * msg[0].buf = wr_buff;
560 * i2c_transfer(adap, msg, 1);
561 *
562 * To read or write a block of data (multiple bytes) using SMBUS emulation
563 * please use the i2c_smbus_read_i2c_block_data()
564 * or i2c_smbus_write_i2c_block_data() API
565 */
566static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
567 struct i2c_msg msgs[], int num_msgs)
568{
569 int status;
570 int i;
571 u32 cause;
572 struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200573 u32 i2c_sr;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530574
Jonas Aberga20d2392011-05-13 12:29:02 +0200575 dev->busy = true;
576
577 if (dev->regulator)
578 regulator_enable(dev->regulator);
579
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530580 status = init_hw(dev);
581 if (status)
Jonas Aberga20d2392011-05-13 12:29:02 +0200582 goto out2;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530583
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200584 clk_enable(dev->clk);
585
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530586 /* setup the i2c controller */
587 setup_i2c_controller(dev);
588
589 for (i = 0; i < num_msgs; i++) {
590 if (unlikely(msgs[i].flags & I2C_M_TEN)) {
591 dev_err(&dev->pdev->dev, "10 bit addressing"
592 "not supported\n");
Jonas Aberga20d2392011-05-13 12:29:02 +0200593
594 status = -EINVAL;
595 goto out;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530596 }
597 dev->cli.slave_adr = msgs[i].addr;
598 dev->cli.buffer = msgs[i].buf;
599 dev->cli.count = msgs[i].len;
600 dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
601 dev->result = 0;
602
603 if (msgs[i].flags & I2C_M_RD) {
604 /* it is a read operation */
605 dev->cli.operation = I2C_READ;
606 status = read_i2c(dev);
607 } else {
608 /* write operation */
609 dev->cli.operation = I2C_WRITE;
610 status = write_i2c(dev);
611 }
612 if (status || (dev->result)) {
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200613 i2c_sr = readl(dev->virtbase + I2C_SR);
614 /*
615 * Check if the controller I2C operation status is set
616 * to ABORT(11b).
617 */
618 if (((i2c_sr >> 2) & 0x3) == 0x3) {
619 /* get the abort cause */
620 cause = (i2c_sr >> 4)
621 & 0x7;
622 dev_err(&dev->pdev->dev, "%s\n", cause >=
623 ARRAY_SIZE(abort_causes) ?
624 "unknown reason" :
625 abort_causes[cause]);
626 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200627
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200628 status = status ? status : dev->result;
Jonas Aberga20d2392011-05-13 12:29:02 +0200629 goto out;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530630 }
Linus Walleijf868fc32010-09-23 09:04:11 +0200631 udelay(I2C_DELAY);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530632 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200633
634out:
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200635 clk_disable(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +0200636out2:
637 if (dev->regulator)
638 regulator_disable(dev->regulator);
639
640 dev->busy = false;
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200641
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530642 /* return the no. messages processed */
643 if (status)
644 return status;
645 else
646 return num_msgs;
647}
648
649/**
650 * disable_interrupts() - disable the interrupts
651 * @dev: private data of controller
Linus Walleij1804edd2010-09-23 09:03:40 +0200652 * @irq: interrupt number
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530653 */
654static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
655{
656 irq = IRQ_MASK(irq);
657 writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
658 dev->virtbase + I2C_IMSCR);
659 return 0;
660}
661
662/**
663 * i2c_irq_handler() - interrupt routine
664 * @irq: interrupt number
665 * @arg: data passed to the handler
666 *
667 * This is the interrupt handler for the i2c driver. Currently
668 * it handles the major interrupts like Rx & Tx FIFO management
669 * interrupts, master transaction interrupts, arbitration and
670 * bus error interrupts. The rest of the interrupts are treated as
671 * unhandled.
672 */
673static irqreturn_t i2c_irq_handler(int irq, void *arg)
674{
675 struct nmk_i2c_dev *dev = arg;
676 u32 tft, rft;
677 u32 count;
678 u32 misr;
679 u32 src = 0;
680
681 /* load Tx FIFO and Rx FIFO threshold values */
682 tft = readl(dev->virtbase + I2C_TFTR);
683 rft = readl(dev->virtbase + I2C_RFTR);
684
685 /* read interrupt status register */
686 misr = readl(dev->virtbase + I2C_MISR);
687
688 src = __ffs(misr);
689 switch ((1 << src)) {
690
691 /* Transmit FIFO nearly empty interrupt */
692 case I2C_IT_TXFNE:
693 {
694 if (dev->cli.operation == I2C_READ) {
695 /*
696 * in read operation why do we care for writing?
697 * so disable the Transmit FIFO interrupt
698 */
699 disable_interrupts(dev, I2C_IT_TXFNE);
700 } else {
701 for (count = (MAX_I2C_FIFO_THRESHOLD - tft - 2);
702 (count > 0) &&
703 (dev->cli.count != 0);
704 count--) {
705 /* write to the Tx FIFO */
706 writeb(*dev->cli.buffer,
707 dev->virtbase + I2C_TFR);
708 dev->cli.buffer++;
709 dev->cli.count--;
710 dev->cli.xfer_bytes++;
711 }
712 /*
713 * if done, close the transfer by disabling the
714 * corresponding TXFNE interrupt
715 */
716 if (dev->cli.count == 0)
717 disable_interrupts(dev, I2C_IT_TXFNE);
718 }
719 }
720 break;
721
722 /*
723 * Rx FIFO nearly full interrupt.
724 * This is set when the numer of entries in Rx FIFO is
725 * greater or equal than the threshold value programmed
726 * in RFT
727 */
728 case I2C_IT_RXFNF:
729 for (count = rft; count > 0; count--) {
730 /* Read the Rx FIFO */
731 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
732 dev->cli.buffer++;
733 }
734 dev->cli.count -= rft;
735 dev->cli.xfer_bytes += rft;
736 break;
737
738 /* Rx FIFO full */
739 case I2C_IT_RXFF:
740 for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
741 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
742 dev->cli.buffer++;
743 }
744 dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
745 dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
746 break;
747
748 /* Master Transaction Done with/without stop */
749 case I2C_IT_MTD:
750 case I2C_IT_MTDWS:
751 if (dev->cli.operation == I2C_READ) {
Rabin Vincent1df3ab12010-04-27 10:31:08 +0530752 while (!(readl(dev->virtbase + I2C_RISR)
753 & I2C_IT_RXFE)) {
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530754 if (dev->cli.count == 0)
755 break;
756 *dev->cli.buffer =
757 readb(dev->virtbase + I2C_RFR);
758 dev->cli.buffer++;
759 dev->cli.count--;
760 dev->cli.xfer_bytes++;
761 }
762 }
763
764 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTD);
765 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTDWS);
766
767 disable_interrupts(dev,
768 (I2C_IT_TXFNE | I2C_IT_TXFE | I2C_IT_TXFF
769 | I2C_IT_TXFOVR | I2C_IT_RXFNF
770 | I2C_IT_RXFF | I2C_IT_RXFE));
771
772 if (dev->cli.count) {
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200773 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530774 dev_err(&dev->pdev->dev, "%lu bytes still remain to be"
775 "xfered\n", dev->cli.count);
776 (void) init_hw(dev);
777 }
778 complete(&dev->xfer_complete);
779
780 break;
781
782 /* Master Arbitration lost interrupt */
783 case I2C_IT_MAL:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200784 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530785 (void) init_hw(dev);
786
787 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
788 complete(&dev->xfer_complete);
789
790 break;
791
792 /*
793 * Bus Error interrupt.
794 * This happens when an unexpected start/stop condition occurs
795 * during the transaction.
796 */
797 case I2C_IT_BERR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200798 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530799 /* get the status */
800 if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
801 (void) init_hw(dev);
802
803 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
804 complete(&dev->xfer_complete);
805
806 break;
807
808 /*
809 * Tx FIFO overrun interrupt.
810 * This is set when a write operation in Tx FIFO is performed and
811 * the Tx FIFO is full.
812 */
813 case I2C_IT_TXFOVR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200814 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530815 (void) init_hw(dev);
816
817 dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
818 complete(&dev->xfer_complete);
819
820 break;
821
822 /* unhandled interrupts by this driver - TODO*/
823 case I2C_IT_TXFE:
824 case I2C_IT_TXFF:
825 case I2C_IT_RXFE:
826 case I2C_IT_RFSR:
827 case I2C_IT_RFSE:
828 case I2C_IT_WTSR:
829 case I2C_IT_STD:
830 dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
831 break;
832 default:
833 dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
834 break;
835 }
836
837 return IRQ_HANDLED;
838}
839
Jonas Aberga20d2392011-05-13 12:29:02 +0200840
841#ifdef CONFIG_PM
842static int nmk_i2c_suspend(struct platform_device *pdev, pm_message_t mesg)
843{
844 struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
845
846 if (dev->busy)
847 return -EBUSY;
848 else
849 return 0;
850}
851#else
852#define nmk_i2c_suspend NULL
853#endif
854
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530855static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
856{
Linus Walleij5680bc62010-09-23 09:04:03 +0200857 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530858}
859
860static const struct i2c_algorithm nmk_i2c_algo = {
861 .master_xfer = nmk_i2c_xfer,
862 .functionality = nmk_i2c_functionality
863};
864
865static int __devinit nmk_i2c_probe(struct platform_device *pdev)
866{
867 int ret = 0;
868 struct resource *res;
869 struct nmk_i2c_controller *pdata =
870 pdev->dev.platform_data;
871 struct nmk_i2c_dev *dev;
872 struct i2c_adapter *adap;
873
874 dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
875 if (!dev) {
876 dev_err(&pdev->dev, "cannot allocate memory\n");
877 ret = -ENOMEM;
878 goto err_no_mem;
879 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200880 dev->busy = false;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530881 dev->pdev = pdev;
882 platform_set_drvdata(pdev, dev);
883
884 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
885 if (!res) {
886 ret = -ENOENT;
887 goto err_no_resource;
888 }
889
890 if (request_mem_region(res->start, resource_size(res),
891 DRIVER_NAME "I/O region") == NULL) {
892 ret = -EBUSY;
893 goto err_no_region;
894 }
895
896 dev->virtbase = ioremap(res->start, resource_size(res));
897 if (!dev->virtbase) {
898 ret = -ENOMEM;
899 goto err_no_ioremap;
900 }
901
902 dev->irq = platform_get_irq(pdev, 0);
903 ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED,
904 DRIVER_NAME, dev);
905 if (ret) {
906 dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
907 goto err_irq;
908 }
909
Jonas Aberga20d2392011-05-13 12:29:02 +0200910 dev->regulator = regulator_get(&pdev->dev, "v-i2c");
911 if (IS_ERR(dev->regulator)) {
912 dev_warn(&pdev->dev, "could not get i2c regulator\n");
913 dev->regulator = NULL;
914 }
915
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530916 dev->clk = clk_get(&pdev->dev, NULL);
917 if (IS_ERR(dev->clk)) {
918 dev_err(&pdev->dev, "could not get i2c clock\n");
919 ret = PTR_ERR(dev->clk);
920 goto err_no_clk;
921 }
922
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530923 adap = &dev->adap;
924 adap->dev.parent = &pdev->dev;
925 adap->owner = THIS_MODULE;
926 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
927 adap->algo = &nmk_i2c_algo;
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200928 adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
929 msecs_to_jiffies(20000);
Linus Walleij6d779a42010-11-30 16:59:29 +0100930 snprintf(adap->name, sizeof(adap->name),
931 "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530932
933 /* fetch the controller id */
934 adap->nr = pdev->id;
935
936 /* fetch the controller configuration from machine */
937 dev->cfg.clk_freq = pdata->clk_freq;
938 dev->cfg.slsu = pdata->slsu;
939 dev->cfg.tft = pdata->tft;
940 dev->cfg.rft = pdata->rft;
941 dev->cfg.sm = pdata->sm;
942
943 i2c_set_adapdata(adap, dev);
944
Linus Walleij6d779a42010-11-30 16:59:29 +0100945 dev_info(&pdev->dev, "initialize %s on virtual "
946 "base %p\n", adap->name, dev->virtbase);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530947
948 ret = i2c_add_numbered_adapter(adap);
949 if (ret) {
950 dev_err(&pdev->dev, "failed to add adapter\n");
951 goto err_add_adap;
952 }
953
954 return 0;
955
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530956 err_add_adap:
957 clk_put(dev->clk);
958 err_no_clk:
Jonas Aberga20d2392011-05-13 12:29:02 +0200959 if (dev->regulator)
960 regulator_put(dev->regulator);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530961 free_irq(dev->irq, dev);
962 err_irq:
963 iounmap(dev->virtbase);
964 err_no_ioremap:
965 release_mem_region(res->start, resource_size(res));
966 err_no_region:
967 platform_set_drvdata(pdev, NULL);
968 err_no_resource:
969 kfree(dev);
970 err_no_mem:
971
972 return ret;
973}
974
975static int __devexit nmk_i2c_remove(struct platform_device *pdev)
976{
Rabin Vincenta1c27672010-04-27 10:31:07 +0530977 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530978 struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
979
980 i2c_del_adapter(&dev->adap);
981 flush_i2c_fifo(dev);
982 disable_all_interrupts(dev);
983 clear_all_interrupts(dev);
984 /* disable the controller */
985 i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
986 free_irq(dev->irq, dev);
987 iounmap(dev->virtbase);
Rabin Vincenta1c27672010-04-27 10:31:07 +0530988 if (res)
989 release_mem_region(res->start, resource_size(res));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530990 clk_put(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +0200991 if (dev->regulator)
992 regulator_put(dev->regulator);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530993 platform_set_drvdata(pdev, NULL);
994 kfree(dev);
995
996 return 0;
997}
998
999static struct platform_driver nmk_i2c_driver = {
1000 .driver = {
1001 .owner = THIS_MODULE,
1002 .name = DRIVER_NAME,
1003 },
1004 .probe = nmk_i2c_probe,
1005 .remove = __devexit_p(nmk_i2c_remove),
Jonas Aberga20d2392011-05-13 12:29:02 +02001006 .suspend = nmk_i2c_suspend,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301007};
1008
1009static int __init nmk_i2c_init(void)
1010{
1011 return platform_driver_register(&nmk_i2c_driver);
1012}
1013
1014static void __exit nmk_i2c_exit(void)
1015{
1016 platform_driver_unregister(&nmk_i2c_driver);
1017}
1018
1019subsys_initcall(nmk_i2c_init);
1020module_exit(nmk_i2c_exit);
1021
1022MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
1023MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1024MODULE_LICENSE("GPL");
1025MODULE_ALIAS("platform:" DRIVER_NAME);