blob: b49ff25a2688d9c04354fdc620fa16c68940eb75 [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>
Rabin Vincentb0e751a2011-05-13 12:30:07 +020026#include <linux/pm_runtime.h>
srinidhi kasagar3f9900f2010-02-01 19:44:54 +053027
28#include <plat/i2c.h>
29
30#define DRIVER_NAME "nmk-i2c"
31
32/* I2C Controller register offsets */
33#define I2C_CR (0x000)
34#define I2C_SCR (0x004)
35#define I2C_HSMCR (0x008)
36#define I2C_MCR (0x00C)
37#define I2C_TFR (0x010)
38#define I2C_SR (0x014)
39#define I2C_RFR (0x018)
40#define I2C_TFTR (0x01C)
41#define I2C_RFTR (0x020)
42#define I2C_DMAR (0x024)
43#define I2C_BRCR (0x028)
44#define I2C_IMSCR (0x02C)
45#define I2C_RISR (0x030)
46#define I2C_MISR (0x034)
47#define I2C_ICR (0x038)
48
49/* Control registers */
50#define I2C_CR_PE (0x1 << 0) /* Peripheral Enable */
51#define I2C_CR_OM (0x3 << 1) /* Operating mode */
52#define I2C_CR_SAM (0x1 << 3) /* Slave addressing mode */
53#define I2C_CR_SM (0x3 << 4) /* Speed mode */
54#define I2C_CR_SGCM (0x1 << 6) /* Slave general call mode */
55#define I2C_CR_FTX (0x1 << 7) /* Flush Transmit */
56#define I2C_CR_FRX (0x1 << 8) /* Flush Receive */
57#define I2C_CR_DMA_TX_EN (0x1 << 9) /* DMA Tx enable */
58#define I2C_CR_DMA_RX_EN (0x1 << 10) /* DMA Rx Enable */
59#define I2C_CR_DMA_SLE (0x1 << 11) /* DMA sync. logic enable */
60#define I2C_CR_LM (0x1 << 12) /* Loopback mode */
61#define I2C_CR_FON (0x3 << 13) /* Filtering on */
62#define I2C_CR_FS (0x3 << 15) /* Force stop enable */
63
64/* Master controller (MCR) register */
65#define I2C_MCR_OP (0x1 << 0) /* Operation */
66#define I2C_MCR_A7 (0x7f << 1) /* 7-bit address */
67#define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */
68#define I2C_MCR_SB (0x1 << 11) /* Extended address */
69#define I2C_MCR_AM (0x3 << 12) /* Address type */
70#define I2C_MCR_STOP (0x1 << 14) /* Stop condition */
71#define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */
72
73/* Status register (SR) */
74#define I2C_SR_OP (0x3 << 0) /* Operation */
75#define I2C_SR_STATUS (0x3 << 2) /* controller status */
76#define I2C_SR_CAUSE (0x7 << 4) /* Abort cause */
77#define I2C_SR_TYPE (0x3 << 7) /* Receive type */
78#define I2C_SR_LENGTH (0x7ff << 9) /* Transfer length */
79
80/* Interrupt mask set/clear (IMSCR) bits */
81#define I2C_IT_TXFE (0x1 << 0)
82#define I2C_IT_TXFNE (0x1 << 1)
83#define I2C_IT_TXFF (0x1 << 2)
84#define I2C_IT_TXFOVR (0x1 << 3)
85#define I2C_IT_RXFE (0x1 << 4)
86#define I2C_IT_RXFNF (0x1 << 5)
87#define I2C_IT_RXFF (0x1 << 6)
88#define I2C_IT_RFSR (0x1 << 16)
89#define I2C_IT_RFSE (0x1 << 17)
90#define I2C_IT_WTSR (0x1 << 18)
91#define I2C_IT_MTD (0x1 << 19)
92#define I2C_IT_STD (0x1 << 20)
93#define I2C_IT_MAL (0x1 << 24)
94#define I2C_IT_BERR (0x1 << 25)
95#define I2C_IT_MTDWS (0x1 << 28)
96
97#define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask))
98
99/* some bits in ICR are reserved */
100#define I2C_CLEAR_ALL_INTS 0x131f007f
101
102/* first three msb bits are reserved */
103#define IRQ_MASK(mask) (mask & 0x1fffffff)
104
105/* maximum threshold value */
106#define MAX_I2C_FIFO_THRESHOLD 15
107
Linus Walleijf868fc32010-09-23 09:04:11 +0200108/* per-transfer delay, required for the hardware to stabilize */
109#define I2C_DELAY 150
110
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530111enum i2c_status {
112 I2C_NOP,
113 I2C_ON_GOING,
114 I2C_OK,
115 I2C_ABORT
116};
117
118/* operation */
119enum i2c_operation {
120 I2C_NO_OPERATION = 0xff,
121 I2C_WRITE = 0x00,
122 I2C_READ = 0x01
123};
124
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530125/**
126 * struct i2c_nmk_client - client specific data
127 * @slave_adr: 7-bit slave address
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300128 * @count: no. bytes to be transferred
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530129 * @buffer: client data buffer
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300130 * @xfer_bytes: bytes transferred till now
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530131 * @operation: current I2C operation
132 */
133struct i2c_nmk_client {
134 unsigned short slave_adr;
135 unsigned long count;
136 unsigned char *buffer;
137 unsigned long xfer_bytes;
138 enum i2c_operation operation;
139};
140
141/**
142 * struct nmk_i2c_dev - private data structure of the controller
143 * @pdev: parent platform device
144 * @adap: corresponding I2C adapter
145 * @irq: interrupt line for the controller
146 * @virtbase: virtual io memory area
147 * @clk: hardware i2c block clock
148 * @cfg: machine provided controller configuration
149 * @cli: holder of client specific data
150 * @stop: stop condition
151 * @xfer_complete: acknowledge completion for a I2C message
152 * @result: controller propogated result
Jonas Aberga20d2392011-05-13 12:29:02 +0200153 * @busy: Busy doing transfer
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530154 */
155struct nmk_i2c_dev {
156 struct platform_device *pdev;
157 struct i2c_adapter adap;
158 int irq;
159 void __iomem *virtbase;
160 struct clk *clk;
161 struct nmk_i2c_controller cfg;
162 struct i2c_nmk_client cli;
163 int stop;
164 struct completion xfer_complete;
165 int result;
Jonas Aberga20d2392011-05-13 12:29:02 +0200166 struct regulator *regulator;
167 bool busy;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530168};
169
170/* controller's abort causes */
171static const char *abort_causes[] = {
172 "no ack received after address transmission",
173 "no ack received during data phase",
174 "ack received after xmission of master code",
175 "master lost arbitration",
176 "slave restarts",
177 "slave reset",
178 "overflow, maxsize is 2047 bytes",
179};
180
181static inline void i2c_set_bit(void __iomem *reg, u32 mask)
182{
183 writel(readl(reg) | mask, reg);
184}
185
186static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
187{
188 writel(readl(reg) & ~mask, reg);
189}
190
191/**
192 * flush_i2c_fifo() - This function flushes the I2C FIFO
193 * @dev: private data of I2C Driver
194 *
195 * This function flushes the I2C Tx and Rx FIFOs. It returns
196 * 0 on successful flushing of FIFO
197 */
198static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
199{
200#define LOOP_ATTEMPTS 10
201 int i;
202 unsigned long timeout;
203
204 /*
205 * flush the transmit and receive FIFO. The flushing
206 * operation takes several cycles before to be completed.
207 * On the completion, the I2C internal logic clears these
208 * bits, until then no one must access Tx, Rx FIFO and
209 * should poll on these bits waiting for the completion.
210 */
211 writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
212
213 for (i = 0; i < LOOP_ATTEMPTS; i++) {
Virupax Sadashivpetimathcd20e4f2011-05-13 12:29:46 +0200214 timeout = jiffies + dev->adap.timeout;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530215
216 while (!time_after(jiffies, timeout)) {
217 if ((readl(dev->virtbase + I2C_CR) &
218 (I2C_CR_FTX | I2C_CR_FRX)) == 0)
219 return 0;
220 }
221 }
222
223 dev_err(&dev->pdev->dev, "flushing operation timed out "
224 "giving up after %d attempts", LOOP_ATTEMPTS);
225
226 return -ETIMEDOUT;
227}
228
229/**
230 * disable_all_interrupts() - Disable all interrupts of this I2c Bus
231 * @dev: private data of I2C Driver
232 */
233static void disable_all_interrupts(struct nmk_i2c_dev *dev)
234{
235 u32 mask = IRQ_MASK(0);
236 writel(mask, dev->virtbase + I2C_IMSCR);
237}
238
239/**
240 * clear_all_interrupts() - Clear all interrupts of I2C Controller
241 * @dev: private data of I2C Driver
242 */
243static void clear_all_interrupts(struct nmk_i2c_dev *dev)
244{
245 u32 mask;
246 mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
247 writel(mask, dev->virtbase + I2C_ICR);
248}
249
250/**
251 * init_hw() - initialize the I2C hardware
252 * @dev: private data of I2C Driver
253 */
254static int init_hw(struct nmk_i2c_dev *dev)
255{
256 int stat;
257
258 stat = flush_i2c_fifo(dev);
259 if (stat)
Jonas Aberga20d2392011-05-13 12:29:02 +0200260 goto exit;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530261
262 /* disable the controller */
263 i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
264
265 disable_all_interrupts(dev);
266
267 clear_all_interrupts(dev);
268
269 dev->cli.operation = I2C_NO_OPERATION;
270
Jonas Aberga20d2392011-05-13 12:29:02 +0200271exit:
Jonas Aberga20d2392011-05-13 12:29:02 +0200272 /*
273 * TODO: What is this delay for?
274 * Must be pretty pointless since the hw block
275 * is frozen. Or?
276 */
Linus Walleijf868fc32010-09-23 09:04:11 +0200277 udelay(I2C_DELAY);
Jonas Aberga20d2392011-05-13 12:29:02 +0200278 return stat;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530279}
280
281/* enable peripheral, master mode operation */
282#define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
283
284/**
285 * load_i2c_mcr_reg() - load the MCR register
286 * @dev: private data of controller
287 */
288static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)
289{
290 u32 mcr = 0;
291
292 /* 7-bit address transaction */
293 mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
294 mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
295
296 /* start byte procedure not applied */
297 mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
298
299 /* check the operation, master read/write? */
300 if (dev->cli.operation == I2C_WRITE)
301 mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
302 else
303 mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
304
305 /* stop or repeated start? */
306 if (dev->stop)
307 mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
308 else
309 mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
310
311 mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
312
313 return mcr;
314}
315
316/**
317 * setup_i2c_controller() - setup the controller
318 * @dev: private data of controller
319 */
320static void setup_i2c_controller(struct nmk_i2c_dev *dev)
321{
322 u32 brcr1, brcr2;
323 u32 i2c_clk, div;
324
325 writel(0x0, dev->virtbase + I2C_CR);
326 writel(0x0, dev->virtbase + I2C_HSMCR);
327 writel(0x0, dev->virtbase + I2C_TFTR);
328 writel(0x0, dev->virtbase + I2C_RFTR);
329 writel(0x0, dev->virtbase + I2C_DMAR);
330
331 /*
332 * set the slsu:
333 *
334 * slsu defines the data setup time after SCL clock
335 * stretching in terms of i2c clk cycles. The
336 * needed setup time for the three modes are 250ns,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300337 * 100ns, 10ns respectively thus leading to the values
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530338 * of 14, 6, 2 for a 48 MHz i2c clk.
339 */
340 writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
341
342 i2c_clk = clk_get_rate(dev->clk);
343
344 /* fallback to std. mode if machine has not provided it */
345 if (dev->cfg.clk_freq == 0)
346 dev->cfg.clk_freq = 100000;
347
348 /*
349 * The spec says, in case of std. mode the divider is
350 * 2 whereas it is 3 for fast and fastplus mode of
351 * operation. TODO - high speed support.
352 */
353 div = (dev->cfg.clk_freq > 100000) ? 3 : 2;
354
355 /*
356 * generate the mask for baud rate counters. The controller
357 * has two baud rate counters. One is used for High speed
358 * operation, and the other is for std, fast mode, fast mode
359 * plus operation. Currently we do not supprt high speed mode
360 * so set brcr1 to 0.
361 */
362 brcr1 = 0 << 16;
363 brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;
364
365 /* set the baud rate counter register */
366 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
367
368 /*
369 * set the speed mode. Currently we support
370 * only standard and fast mode of operation
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300371 * TODO - support for fast mode plus (up to 1Mb/s)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530372 * and high speed (up to 3.4 Mb/s)
373 */
374 if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
375 dev_err(&dev->pdev->dev, "do not support this mode "
376 "defaulting to std. mode\n");
377 brcr2 = i2c_clk/(100000 * 2) & 0xffff;
378 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
379 writel(I2C_FREQ_MODE_STANDARD << 4,
380 dev->virtbase + I2C_CR);
381 }
382 writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);
383
384 /* set the Tx and Rx FIFO threshold */
385 writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);
386 writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);
387}
388
389/**
390 * read_i2c() - Read from I2C client device
391 * @dev: private data of I2C Driver
392 *
393 * This function reads from i2c client device when controller is in
394 * master mode. There is a completion timeout. If there is no transfer
395 * before timeout error is returned.
396 */
397static int read_i2c(struct nmk_i2c_dev *dev)
398{
399 u32 status = 0;
400 u32 mcr;
401 u32 irq_mask = 0;
402 int timeout;
403
404 mcr = load_i2c_mcr_reg(dev);
405 writel(mcr, dev->virtbase + I2C_MCR);
406
407 /* load the current CR value */
408 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
409 dev->virtbase + I2C_CR);
410
411 /* enable the controller */
412 i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
413
414 init_completion(&dev->xfer_complete);
415
416 /* enable interrupts by setting the mask */
417 irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
418 I2C_IT_MAL | I2C_IT_BERR);
419
420 if (dev->stop)
421 irq_mask |= I2C_IT_MTD;
422 else
423 irq_mask |= I2C_IT_MTDWS;
424
425 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
426
427 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
428 dev->virtbase + I2C_IMSCR);
429
430 timeout = wait_for_completion_interruptible_timeout(
Virupax Sadashivpetimathcd20e4f2011-05-13 12:29:46 +0200431 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530432
433 if (timeout < 0) {
434 dev_err(&dev->pdev->dev,
435 "wait_for_completion_interruptible_timeout"
436 "returned %d waiting for event\n", timeout);
437 status = timeout;
438 }
439
440 if (timeout == 0) {
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400441 /* controller has timedout, re-init the h/w */
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200442 dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",
443 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530444 (void) init_hw(dev);
445 status = -ETIMEDOUT;
446 }
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530447 return status;
448}
449
450/**
451 * write_i2c() - Write data to I2C client.
452 * @dev: private data of I2C Driver
453 *
454 * This function writes data to I2C client
455 */
456static int write_i2c(struct nmk_i2c_dev *dev)
457{
458 u32 status = 0;
459 u32 mcr;
460 u32 irq_mask = 0;
461 int timeout;
462
463 mcr = load_i2c_mcr_reg(dev);
464
465 writel(mcr, dev->virtbase + I2C_MCR);
466
467 /* load the current CR value */
468 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
469 dev->virtbase + I2C_CR);
470
471 /* enable the controller */
472 i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
473
474 init_completion(&dev->xfer_complete);
475
476 /* enable interrupts by settings the masks */
477 irq_mask = (I2C_IT_TXFNE | I2C_IT_TXFOVR |
478 I2C_IT_MAL | I2C_IT_BERR);
479
480 /*
481 * check if we want to transfer a single or multiple bytes, if so
482 * set the MTDWS bit (Master Transaction Done Without Stop)
483 * to start repeated start operation
484 */
485 if (dev->stop)
486 irq_mask |= I2C_IT_MTD;
487 else
488 irq_mask |= I2C_IT_MTDWS;
489
490 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
491
492 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
493 dev->virtbase + I2C_IMSCR);
494
495 timeout = wait_for_completion_interruptible_timeout(
Virupax Sadashivpetimathcd20e4f2011-05-13 12:29:46 +0200496 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530497
498 if (timeout < 0) {
499 dev_err(&dev->pdev->dev,
500 "wait_for_completion_interruptible_timeout"
501 "returned %d waiting for event\n", timeout);
502 status = timeout;
503 }
504
505 if (timeout == 0) {
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400506 /* controller has timedout, re-init the h/w */
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200507 dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
508 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530509 (void) init_hw(dev);
510 status = -ETIMEDOUT;
511 }
512
513 return status;
514}
515
516/**
517 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
Linus Walleij1804edd2010-09-23 09:03:40 +0200518 * @i2c_adap: Adapter pointer to the controller
519 * @msgs: Pointer to data to be written.
520 * @num_msgs: Number of messages to be executed
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530521 *
522 * This is the function called by the generic kernel i2c_transfer()
523 * or i2c_smbus...() API calls. Note that this code is protected by the
524 * semaphore set in the kernel i2c_transfer() function.
525 *
526 * NOTE:
527 * READ TRANSFER : We impose a restriction of the first message to be the
528 * index message for any read transaction.
529 * - a no index is coded as '0',
530 * - 2byte big endian index is coded as '3'
531 * !!! msg[0].buf holds the actual index.
532 * This is compatible with generic messages of smbus emulator
533 * that send a one byte index.
534 * eg. a I2C transation to read 2 bytes from index 0
535 * idx = 0;
536 * msg[0].addr = client->addr;
537 * msg[0].flags = 0x0;
538 * msg[0].len = 1;
539 * msg[0].buf = &idx;
540 *
541 * msg[1].addr = client->addr;
542 * msg[1].flags = I2C_M_RD;
543 * msg[1].len = 2;
544 * msg[1].buf = rd_buff
545 * i2c_transfer(adap, msg, 2);
546 *
547 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
548 * If you want to emulate an SMBUS write transaction put the
549 * index as first byte(or first and second) in the payload.
550 * eg. a I2C transation to write 2 bytes from index 1
551 * wr_buff[0] = 0x1;
552 * wr_buff[1] = 0x23;
553 * wr_buff[2] = 0x46;
554 * msg[0].flags = 0x0;
555 * msg[0].len = 3;
556 * msg[0].buf = wr_buff;
557 * i2c_transfer(adap, msg, 1);
558 *
559 * To read or write a block of data (multiple bytes) using SMBUS emulation
560 * please use the i2c_smbus_read_i2c_block_data()
561 * or i2c_smbus_write_i2c_block_data() API
562 */
563static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
564 struct i2c_msg msgs[], int num_msgs)
565{
566 int status;
567 int i;
568 u32 cause;
569 struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200570 u32 i2c_sr;
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200571 int j;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530572
Jonas Aberga20d2392011-05-13 12:29:02 +0200573 dev->busy = true;
574
575 if (dev->regulator)
576 regulator_enable(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200577 pm_runtime_get_sync(&dev->pdev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200578
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200579 clk_enable(dev->clk);
580
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200581 status = init_hw(dev);
582 if (status)
583 goto out;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530584
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200585 for (j = 0; j < 3; j++) {
586 /* setup the i2c controller */
587 setup_i2c_controller(dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200588
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200589 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");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530593
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200594 status = -EINVAL;
595 goto out;
596 }
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)) {
613 i2c_sr = readl(dev->virtbase + I2C_SR);
614 /*
615 * Check if the controller I2C operation status
616 * is set 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) ?
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200624 "unknown reason" :
625 abort_causes[cause]);
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200626 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200627
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200628 status = status ? status : dev->result;
629
630 break;
631 }
632 udelay(I2C_DELAY);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530633 }
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200634 if (status == 0)
635 break;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530636 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200637
638out:
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200639 clk_disable(dev->clk);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200640 pm_runtime_put_sync(&dev->pdev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200641 if (dev->regulator)
642 regulator_disable(dev->regulator);
643
644 dev->busy = false;
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200645
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530646 /* return the no. messages processed */
647 if (status)
648 return status;
649 else
650 return num_msgs;
651}
652
653/**
654 * disable_interrupts() - disable the interrupts
655 * @dev: private data of controller
Linus Walleij1804edd2010-09-23 09:03:40 +0200656 * @irq: interrupt number
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530657 */
658static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
659{
660 irq = IRQ_MASK(irq);
661 writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
662 dev->virtbase + I2C_IMSCR);
663 return 0;
664}
665
666/**
667 * i2c_irq_handler() - interrupt routine
668 * @irq: interrupt number
669 * @arg: data passed to the handler
670 *
671 * This is the interrupt handler for the i2c driver. Currently
672 * it handles the major interrupts like Rx & Tx FIFO management
673 * interrupts, master transaction interrupts, arbitration and
674 * bus error interrupts. The rest of the interrupts are treated as
675 * unhandled.
676 */
677static irqreturn_t i2c_irq_handler(int irq, void *arg)
678{
679 struct nmk_i2c_dev *dev = arg;
680 u32 tft, rft;
681 u32 count;
682 u32 misr;
683 u32 src = 0;
684
685 /* load Tx FIFO and Rx FIFO threshold values */
686 tft = readl(dev->virtbase + I2C_TFTR);
687 rft = readl(dev->virtbase + I2C_RFTR);
688
689 /* read interrupt status register */
690 misr = readl(dev->virtbase + I2C_MISR);
691
692 src = __ffs(misr);
693 switch ((1 << src)) {
694
695 /* Transmit FIFO nearly empty interrupt */
696 case I2C_IT_TXFNE:
697 {
698 if (dev->cli.operation == I2C_READ) {
699 /*
700 * in read operation why do we care for writing?
701 * so disable the Transmit FIFO interrupt
702 */
703 disable_interrupts(dev, I2C_IT_TXFNE);
704 } else {
705 for (count = (MAX_I2C_FIFO_THRESHOLD - tft - 2);
706 (count > 0) &&
707 (dev->cli.count != 0);
708 count--) {
709 /* write to the Tx FIFO */
710 writeb(*dev->cli.buffer,
711 dev->virtbase + I2C_TFR);
712 dev->cli.buffer++;
713 dev->cli.count--;
714 dev->cli.xfer_bytes++;
715 }
716 /*
717 * if done, close the transfer by disabling the
718 * corresponding TXFNE interrupt
719 */
720 if (dev->cli.count == 0)
721 disable_interrupts(dev, I2C_IT_TXFNE);
722 }
723 }
724 break;
725
726 /*
727 * Rx FIFO nearly full interrupt.
728 * This is set when the numer of entries in Rx FIFO is
729 * greater or equal than the threshold value programmed
730 * in RFT
731 */
732 case I2C_IT_RXFNF:
733 for (count = rft; count > 0; count--) {
734 /* Read the Rx FIFO */
735 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
736 dev->cli.buffer++;
737 }
738 dev->cli.count -= rft;
739 dev->cli.xfer_bytes += rft;
740 break;
741
742 /* Rx FIFO full */
743 case I2C_IT_RXFF:
744 for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
745 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
746 dev->cli.buffer++;
747 }
748 dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
749 dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
750 break;
751
752 /* Master Transaction Done with/without stop */
753 case I2C_IT_MTD:
754 case I2C_IT_MTDWS:
755 if (dev->cli.operation == I2C_READ) {
Rabin Vincent1df3ab12010-04-27 10:31:08 +0530756 while (!(readl(dev->virtbase + I2C_RISR)
757 & I2C_IT_RXFE)) {
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530758 if (dev->cli.count == 0)
759 break;
760 *dev->cli.buffer =
761 readb(dev->virtbase + I2C_RFR);
762 dev->cli.buffer++;
763 dev->cli.count--;
764 dev->cli.xfer_bytes++;
765 }
766 }
767
768 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTD);
769 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTDWS);
770
771 disable_interrupts(dev,
772 (I2C_IT_TXFNE | I2C_IT_TXFE | I2C_IT_TXFF
773 | I2C_IT_TXFOVR | I2C_IT_RXFNF
774 | I2C_IT_RXFF | I2C_IT_RXFE));
775
776 if (dev->cli.count) {
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200777 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530778 dev_err(&dev->pdev->dev, "%lu bytes still remain to be"
779 "xfered\n", dev->cli.count);
780 (void) init_hw(dev);
781 }
782 complete(&dev->xfer_complete);
783
784 break;
785
786 /* Master Arbitration lost interrupt */
787 case I2C_IT_MAL:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200788 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530789 (void) init_hw(dev);
790
791 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
792 complete(&dev->xfer_complete);
793
794 break;
795
796 /*
797 * Bus Error interrupt.
798 * This happens when an unexpected start/stop condition occurs
799 * during the transaction.
800 */
801 case I2C_IT_BERR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200802 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530803 /* get the status */
804 if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
805 (void) init_hw(dev);
806
807 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
808 complete(&dev->xfer_complete);
809
810 break;
811
812 /*
813 * Tx FIFO overrun interrupt.
814 * This is set when a write operation in Tx FIFO is performed and
815 * the Tx FIFO is full.
816 */
817 case I2C_IT_TXFOVR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200818 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530819 (void) init_hw(dev);
820
821 dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
822 complete(&dev->xfer_complete);
823
824 break;
825
826 /* unhandled interrupts by this driver - TODO*/
827 case I2C_IT_TXFE:
828 case I2C_IT_TXFF:
829 case I2C_IT_RXFE:
830 case I2C_IT_RFSR:
831 case I2C_IT_RFSE:
832 case I2C_IT_WTSR:
833 case I2C_IT_STD:
834 dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
835 break;
836 default:
837 dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
838 break;
839 }
840
841 return IRQ_HANDLED;
842}
843
Jonas Aberga20d2392011-05-13 12:29:02 +0200844
845#ifdef CONFIG_PM
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200846static int nmk_i2c_suspend(struct device *dev)
Jonas Aberga20d2392011-05-13 12:29:02 +0200847{
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200848 struct platform_device *pdev = to_platform_device(dev);
849 struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200850
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200851 if (nmk_i2c->busy)
Jonas Aberga20d2392011-05-13 12:29:02 +0200852 return -EBUSY;
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200853
854 return 0;
855}
856
857static int nmk_i2c_resume(struct device *dev)
858{
859 return 0;
Jonas Aberga20d2392011-05-13 12:29:02 +0200860}
861#else
862#define nmk_i2c_suspend NULL
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200863#define nmk_i2c_resume NULL
Jonas Aberga20d2392011-05-13 12:29:02 +0200864#endif
865
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200866/*
867 * We use noirq so that we suspend late and resume before the wakeup interrupt
868 * to ensure that we do the !pm_runtime_suspended() check in resume before
869 * there has been a regular pm runtime resume (via pm_runtime_get_sync()).
870 */
871static const struct dev_pm_ops nmk_i2c_pm = {
872 .suspend_noirq = nmk_i2c_suspend,
873 .resume_noirq = nmk_i2c_resume,
874};
875
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530876static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
877{
Linus Walleij5680bc62010-09-23 09:04:03 +0200878 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530879}
880
881static const struct i2c_algorithm nmk_i2c_algo = {
882 .master_xfer = nmk_i2c_xfer,
883 .functionality = nmk_i2c_functionality
884};
885
886static int __devinit nmk_i2c_probe(struct platform_device *pdev)
887{
888 int ret = 0;
889 struct resource *res;
890 struct nmk_i2c_controller *pdata =
891 pdev->dev.platform_data;
892 struct nmk_i2c_dev *dev;
893 struct i2c_adapter *adap;
894
895 dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
896 if (!dev) {
897 dev_err(&pdev->dev, "cannot allocate memory\n");
898 ret = -ENOMEM;
899 goto err_no_mem;
900 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200901 dev->busy = false;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530902 dev->pdev = pdev;
903 platform_set_drvdata(pdev, dev);
904
905 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
906 if (!res) {
907 ret = -ENOENT;
908 goto err_no_resource;
909 }
910
911 if (request_mem_region(res->start, resource_size(res),
912 DRIVER_NAME "I/O region") == NULL) {
913 ret = -EBUSY;
914 goto err_no_region;
915 }
916
917 dev->virtbase = ioremap(res->start, resource_size(res));
918 if (!dev->virtbase) {
919 ret = -ENOMEM;
920 goto err_no_ioremap;
921 }
922
923 dev->irq = platform_get_irq(pdev, 0);
924 ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED,
925 DRIVER_NAME, dev);
926 if (ret) {
927 dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
928 goto err_irq;
929 }
930
Jonas Aberga20d2392011-05-13 12:29:02 +0200931 dev->regulator = regulator_get(&pdev->dev, "v-i2c");
932 if (IS_ERR(dev->regulator)) {
933 dev_warn(&pdev->dev, "could not get i2c regulator\n");
934 dev->regulator = NULL;
935 }
936
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200937 pm_suspend_ignore_children(&pdev->dev, true);
938 pm_runtime_enable(&pdev->dev);
939
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530940 dev->clk = clk_get(&pdev->dev, NULL);
941 if (IS_ERR(dev->clk)) {
942 dev_err(&pdev->dev, "could not get i2c clock\n");
943 ret = PTR_ERR(dev->clk);
944 goto err_no_clk;
945 }
946
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530947 adap = &dev->adap;
948 adap->dev.parent = &pdev->dev;
949 adap->owner = THIS_MODULE;
950 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
951 adap->algo = &nmk_i2c_algo;
Virupax Sadashivpetimathcd20e4f2011-05-13 12:29:46 +0200952 adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
953 msecs_to_jiffies(20000);
Linus Walleij6d779a42010-11-30 16:59:29 +0100954 snprintf(adap->name, sizeof(adap->name),
955 "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530956
957 /* fetch the controller id */
958 adap->nr = pdev->id;
959
960 /* fetch the controller configuration from machine */
961 dev->cfg.clk_freq = pdata->clk_freq;
962 dev->cfg.slsu = pdata->slsu;
963 dev->cfg.tft = pdata->tft;
964 dev->cfg.rft = pdata->rft;
965 dev->cfg.sm = pdata->sm;
966
967 i2c_set_adapdata(adap, dev);
968
Linus Walleij6d779a42010-11-30 16:59:29 +0100969 dev_info(&pdev->dev, "initialize %s on virtual "
970 "base %p\n", adap->name, dev->virtbase);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530971
972 ret = i2c_add_numbered_adapter(adap);
973 if (ret) {
974 dev_err(&pdev->dev, "failed to add adapter\n");
975 goto err_add_adap;
976 }
977
978 return 0;
979
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530980 err_add_adap:
981 clk_put(dev->clk);
982 err_no_clk:
Jonas Aberga20d2392011-05-13 12:29:02 +0200983 if (dev->regulator)
984 regulator_put(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200985 pm_runtime_disable(&pdev->dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530986 free_irq(dev->irq, dev);
987 err_irq:
988 iounmap(dev->virtbase);
989 err_no_ioremap:
990 release_mem_region(res->start, resource_size(res));
991 err_no_region:
992 platform_set_drvdata(pdev, NULL);
993 err_no_resource:
994 kfree(dev);
995 err_no_mem:
996
997 return ret;
998}
999
1000static int __devexit nmk_i2c_remove(struct platform_device *pdev)
1001{
Rabin Vincenta1c27672010-04-27 10:31:07 +05301002 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301003 struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
1004
1005 i2c_del_adapter(&dev->adap);
1006 flush_i2c_fifo(dev);
1007 disable_all_interrupts(dev);
1008 clear_all_interrupts(dev);
1009 /* disable the controller */
1010 i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
1011 free_irq(dev->irq, dev);
1012 iounmap(dev->virtbase);
Rabin Vincenta1c27672010-04-27 10:31:07 +05301013 if (res)
1014 release_mem_region(res->start, resource_size(res));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301015 clk_put(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +02001016 if (dev->regulator)
1017 regulator_put(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +02001018 pm_runtime_disable(&pdev->dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301019 platform_set_drvdata(pdev, NULL);
1020 kfree(dev);
1021
1022 return 0;
1023}
1024
1025static struct platform_driver nmk_i2c_driver = {
1026 .driver = {
1027 .owner = THIS_MODULE,
1028 .name = DRIVER_NAME,
Rabin Vincentb0e751a2011-05-13 12:30:07 +02001029 .pm = &nmk_i2c_pm,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301030 },
1031 .probe = nmk_i2c_probe,
1032 .remove = __devexit_p(nmk_i2c_remove),
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301033};
1034
1035static int __init nmk_i2c_init(void)
1036{
1037 return platform_driver_register(&nmk_i2c_driver);
1038}
1039
1040static void __exit nmk_i2c_exit(void)
1041{
1042 platform_driver_unregister(&nmk_i2c_driver);
1043}
1044
1045subsys_initcall(nmk_i2c_init);
1046module_exit(nmk_i2c_exit);
1047
1048MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
1049MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1050MODULE_LICENSE("GPL");
1051MODULE_ALIAS("platform:" DRIVER_NAME);