blob: e3cd62e40df580e5bb8cdb151bd41a308da4cd55 [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 Sadashivpetimathcd20e4fa2011-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 Sadashivpetimathcd20e4fa2011-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
Virupax Sadashivpetimath55355342011-05-13 12:30:34 +0200450static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
451{
452 int count;
453
454 for (count = (no_bytes - 2);
455 (count > 0) &&
456 (dev->cli.count != 0);
457 count--) {
458 /* write to the Tx FIFO */
459 writeb(*dev->cli.buffer,
460 dev->virtbase + I2C_TFR);
461 dev->cli.buffer++;
462 dev->cli.count--;
463 dev->cli.xfer_bytes++;
464 }
465
466}
467
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530468/**
469 * write_i2c() - Write data to I2C client.
470 * @dev: private data of I2C Driver
471 *
472 * This function writes data to I2C client
473 */
474static int write_i2c(struct nmk_i2c_dev *dev)
475{
476 u32 status = 0;
477 u32 mcr;
478 u32 irq_mask = 0;
479 int timeout;
480
481 mcr = load_i2c_mcr_reg(dev);
482
483 writel(mcr, dev->virtbase + I2C_MCR);
484
485 /* load the current CR value */
486 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
487 dev->virtbase + I2C_CR);
488
489 /* enable the controller */
490 i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
491
492 init_completion(&dev->xfer_complete);
493
494 /* enable interrupts by settings the masks */
Virupax Sadashivpetimath55355342011-05-13 12:30:34 +0200495 irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
496
497 /* Fill the TX FIFO with transmit data */
498 fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
499
500 if (dev->cli.count != 0)
501 irq_mask |= I2C_IT_TXFNE;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530502
503 /*
504 * check if we want to transfer a single or multiple bytes, if so
505 * set the MTDWS bit (Master Transaction Done Without Stop)
506 * to start repeated start operation
507 */
508 if (dev->stop)
509 irq_mask |= I2C_IT_MTD;
510 else
511 irq_mask |= I2C_IT_MTDWS;
512
513 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
514
515 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
516 dev->virtbase + I2C_IMSCR);
517
518 timeout = wait_for_completion_interruptible_timeout(
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200519 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530520
521 if (timeout < 0) {
522 dev_err(&dev->pdev->dev,
523 "wait_for_completion_interruptible_timeout"
524 "returned %d waiting for event\n", timeout);
525 status = timeout;
526 }
527
528 if (timeout == 0) {
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400529 /* controller has timedout, re-init the h/w */
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200530 dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
531 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530532 (void) init_hw(dev);
533 status = -ETIMEDOUT;
534 }
535
536 return status;
537}
538
539/**
540 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
Linus Walleij1804edd2010-09-23 09:03:40 +0200541 * @i2c_adap: Adapter pointer to the controller
542 * @msgs: Pointer to data to be written.
543 * @num_msgs: Number of messages to be executed
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530544 *
545 * This is the function called by the generic kernel i2c_transfer()
546 * or i2c_smbus...() API calls. Note that this code is protected by the
547 * semaphore set in the kernel i2c_transfer() function.
548 *
549 * NOTE:
550 * READ TRANSFER : We impose a restriction of the first message to be the
551 * index message for any read transaction.
552 * - a no index is coded as '0',
553 * - 2byte big endian index is coded as '3'
554 * !!! msg[0].buf holds the actual index.
555 * This is compatible with generic messages of smbus emulator
556 * that send a one byte index.
557 * eg. a I2C transation to read 2 bytes from index 0
558 * idx = 0;
559 * msg[0].addr = client->addr;
560 * msg[0].flags = 0x0;
561 * msg[0].len = 1;
562 * msg[0].buf = &idx;
563 *
564 * msg[1].addr = client->addr;
565 * msg[1].flags = I2C_M_RD;
566 * msg[1].len = 2;
567 * msg[1].buf = rd_buff
568 * i2c_transfer(adap, msg, 2);
569 *
570 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
571 * If you want to emulate an SMBUS write transaction put the
572 * index as first byte(or first and second) in the payload.
573 * eg. a I2C transation to write 2 bytes from index 1
574 * wr_buff[0] = 0x1;
575 * wr_buff[1] = 0x23;
576 * wr_buff[2] = 0x46;
577 * msg[0].flags = 0x0;
578 * msg[0].len = 3;
579 * msg[0].buf = wr_buff;
580 * i2c_transfer(adap, msg, 1);
581 *
582 * To read or write a block of data (multiple bytes) using SMBUS emulation
583 * please use the i2c_smbus_read_i2c_block_data()
584 * or i2c_smbus_write_i2c_block_data() API
585 */
586static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
587 struct i2c_msg msgs[], int num_msgs)
588{
589 int status;
590 int i;
591 u32 cause;
592 struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200593 u32 i2c_sr;
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200594 int j;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530595
Jonas Aberga20d2392011-05-13 12:29:02 +0200596 dev->busy = true;
597
598 if (dev->regulator)
599 regulator_enable(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200600 pm_runtime_get_sync(&dev->pdev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200601
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200602 clk_enable(dev->clk);
603
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200604 status = init_hw(dev);
605 if (status)
606 goto out;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530607
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200608 for (j = 0; j < 3; j++) {
609 /* setup the i2c controller */
610 setup_i2c_controller(dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200611
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200612 for (i = 0; i < num_msgs; i++) {
613 if (unlikely(msgs[i].flags & I2C_M_TEN)) {
614 dev_err(&dev->pdev->dev, "10 bit addressing"
615 "not supported\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530616
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200617 status = -EINVAL;
618 goto out;
619 }
620 dev->cli.slave_adr = msgs[i].addr;
621 dev->cli.buffer = msgs[i].buf;
622 dev->cli.count = msgs[i].len;
623 dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
624 dev->result = 0;
625
626 if (msgs[i].flags & I2C_M_RD) {
627 /* it is a read operation */
628 dev->cli.operation = I2C_READ;
629 status = read_i2c(dev);
630 } else {
631 /* write operation */
632 dev->cli.operation = I2C_WRITE;
633 status = write_i2c(dev);
634 }
635 if (status || (dev->result)) {
636 i2c_sr = readl(dev->virtbase + I2C_SR);
637 /*
638 * Check if the controller I2C operation status
639 * is set to ABORT(11b).
640 */
641 if (((i2c_sr >> 2) & 0x3) == 0x3) {
642 /* get the abort cause */
643 cause = (i2c_sr >> 4)
644 & 0x7;
645 dev_err(&dev->pdev->dev, "%s\n", cause
646 >= ARRAY_SIZE(abort_causes) ?
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200647 "unknown reason" :
648 abort_causes[cause]);
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200649 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200650
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200651 status = status ? status : dev->result;
652
653 break;
654 }
655 udelay(I2C_DELAY);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530656 }
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200657 if (status == 0)
658 break;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530659 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200660
661out:
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200662 clk_disable(dev->clk);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200663 pm_runtime_put_sync(&dev->pdev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200664 if (dev->regulator)
665 regulator_disable(dev->regulator);
666
667 dev->busy = false;
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200668
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530669 /* return the no. messages processed */
670 if (status)
671 return status;
672 else
673 return num_msgs;
674}
675
676/**
677 * disable_interrupts() - disable the interrupts
678 * @dev: private data of controller
Linus Walleij1804edd2010-09-23 09:03:40 +0200679 * @irq: interrupt number
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530680 */
681static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
682{
683 irq = IRQ_MASK(irq);
684 writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
685 dev->virtbase + I2C_IMSCR);
686 return 0;
687}
688
689/**
690 * i2c_irq_handler() - interrupt routine
691 * @irq: interrupt number
692 * @arg: data passed to the handler
693 *
694 * This is the interrupt handler for the i2c driver. Currently
695 * it handles the major interrupts like Rx & Tx FIFO management
696 * interrupts, master transaction interrupts, arbitration and
697 * bus error interrupts. The rest of the interrupts are treated as
698 * unhandled.
699 */
700static irqreturn_t i2c_irq_handler(int irq, void *arg)
701{
702 struct nmk_i2c_dev *dev = arg;
703 u32 tft, rft;
704 u32 count;
705 u32 misr;
706 u32 src = 0;
707
708 /* load Tx FIFO and Rx FIFO threshold values */
709 tft = readl(dev->virtbase + I2C_TFTR);
710 rft = readl(dev->virtbase + I2C_RFTR);
711
712 /* read interrupt status register */
713 misr = readl(dev->virtbase + I2C_MISR);
714
715 src = __ffs(misr);
716 switch ((1 << src)) {
717
718 /* Transmit FIFO nearly empty interrupt */
719 case I2C_IT_TXFNE:
720 {
721 if (dev->cli.operation == I2C_READ) {
722 /*
723 * in read operation why do we care for writing?
724 * so disable the Transmit FIFO interrupt
725 */
726 disable_interrupts(dev, I2C_IT_TXFNE);
727 } else {
Virupax Sadashivpetimath55355342011-05-13 12:30:34 +0200728 fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530729 /*
730 * if done, close the transfer by disabling the
731 * corresponding TXFNE interrupt
732 */
733 if (dev->cli.count == 0)
734 disable_interrupts(dev, I2C_IT_TXFNE);
735 }
736 }
737 break;
738
739 /*
740 * Rx FIFO nearly full interrupt.
741 * This is set when the numer of entries in Rx FIFO is
742 * greater or equal than the threshold value programmed
743 * in RFT
744 */
745 case I2C_IT_RXFNF:
746 for (count = rft; count > 0; count--) {
747 /* Read the Rx FIFO */
748 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
749 dev->cli.buffer++;
750 }
751 dev->cli.count -= rft;
752 dev->cli.xfer_bytes += rft;
753 break;
754
755 /* Rx FIFO full */
756 case I2C_IT_RXFF:
757 for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
758 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
759 dev->cli.buffer++;
760 }
761 dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
762 dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
763 break;
764
765 /* Master Transaction Done with/without stop */
766 case I2C_IT_MTD:
767 case I2C_IT_MTDWS:
768 if (dev->cli.operation == I2C_READ) {
Rabin Vincent1df3ab12010-04-27 10:31:08 +0530769 while (!(readl(dev->virtbase + I2C_RISR)
770 & I2C_IT_RXFE)) {
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530771 if (dev->cli.count == 0)
772 break;
773 *dev->cli.buffer =
774 readb(dev->virtbase + I2C_RFR);
775 dev->cli.buffer++;
776 dev->cli.count--;
777 dev->cli.xfer_bytes++;
778 }
779 }
780
781 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTD);
782 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTDWS);
783
784 disable_interrupts(dev,
785 (I2C_IT_TXFNE | I2C_IT_TXFE | I2C_IT_TXFF
786 | I2C_IT_TXFOVR | I2C_IT_RXFNF
787 | I2C_IT_RXFF | I2C_IT_RXFE));
788
789 if (dev->cli.count) {
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200790 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530791 dev_err(&dev->pdev->dev, "%lu bytes still remain to be"
792 "xfered\n", dev->cli.count);
793 (void) init_hw(dev);
794 }
795 complete(&dev->xfer_complete);
796
797 break;
798
799 /* Master Arbitration lost interrupt */
800 case I2C_IT_MAL:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200801 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530802 (void) init_hw(dev);
803
804 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
805 complete(&dev->xfer_complete);
806
807 break;
808
809 /*
810 * Bus Error interrupt.
811 * This happens when an unexpected start/stop condition occurs
812 * during the transaction.
813 */
814 case I2C_IT_BERR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200815 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530816 /* get the status */
817 if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
818 (void) init_hw(dev);
819
820 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
821 complete(&dev->xfer_complete);
822
823 break;
824
825 /*
826 * Tx FIFO overrun interrupt.
827 * This is set when a write operation in Tx FIFO is performed and
828 * the Tx FIFO is full.
829 */
830 case I2C_IT_TXFOVR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200831 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530832 (void) init_hw(dev);
833
834 dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
835 complete(&dev->xfer_complete);
836
837 break;
838
839 /* unhandled interrupts by this driver - TODO*/
840 case I2C_IT_TXFE:
841 case I2C_IT_TXFF:
842 case I2C_IT_RXFE:
843 case I2C_IT_RFSR:
844 case I2C_IT_RFSE:
845 case I2C_IT_WTSR:
846 case I2C_IT_STD:
847 dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
848 break;
849 default:
850 dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
851 break;
852 }
853
854 return IRQ_HANDLED;
855}
856
Jonas Aberga20d2392011-05-13 12:29:02 +0200857
858#ifdef CONFIG_PM
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200859static int nmk_i2c_suspend(struct device *dev)
Jonas Aberga20d2392011-05-13 12:29:02 +0200860{
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200861 struct platform_device *pdev = to_platform_device(dev);
862 struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200863
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200864 if (nmk_i2c->busy)
Jonas Aberga20d2392011-05-13 12:29:02 +0200865 return -EBUSY;
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200866
867 return 0;
868}
869
870static int nmk_i2c_resume(struct device *dev)
871{
872 return 0;
Jonas Aberga20d2392011-05-13 12:29:02 +0200873}
874#else
875#define nmk_i2c_suspend NULL
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200876#define nmk_i2c_resume NULL
Jonas Aberga20d2392011-05-13 12:29:02 +0200877#endif
878
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200879/*
880 * We use noirq so that we suspend late and resume before the wakeup interrupt
881 * to ensure that we do the !pm_runtime_suspended() check in resume before
882 * there has been a regular pm runtime resume (via pm_runtime_get_sync()).
883 */
884static const struct dev_pm_ops nmk_i2c_pm = {
885 .suspend_noirq = nmk_i2c_suspend,
886 .resume_noirq = nmk_i2c_resume,
887};
888
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530889static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
890{
Linus Walleij5680bc62010-09-23 09:04:03 +0200891 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530892}
893
894static const struct i2c_algorithm nmk_i2c_algo = {
895 .master_xfer = nmk_i2c_xfer,
896 .functionality = nmk_i2c_functionality
897};
898
899static int __devinit nmk_i2c_probe(struct platform_device *pdev)
900{
901 int ret = 0;
902 struct resource *res;
903 struct nmk_i2c_controller *pdata =
904 pdev->dev.platform_data;
905 struct nmk_i2c_dev *dev;
906 struct i2c_adapter *adap;
907
908 dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
909 if (!dev) {
910 dev_err(&pdev->dev, "cannot allocate memory\n");
911 ret = -ENOMEM;
912 goto err_no_mem;
913 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200914 dev->busy = false;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530915 dev->pdev = pdev;
916 platform_set_drvdata(pdev, dev);
917
918 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
919 if (!res) {
920 ret = -ENOENT;
921 goto err_no_resource;
922 }
923
924 if (request_mem_region(res->start, resource_size(res),
925 DRIVER_NAME "I/O region") == NULL) {
926 ret = -EBUSY;
927 goto err_no_region;
928 }
929
930 dev->virtbase = ioremap(res->start, resource_size(res));
931 if (!dev->virtbase) {
932 ret = -ENOMEM;
933 goto err_no_ioremap;
934 }
935
936 dev->irq = platform_get_irq(pdev, 0);
937 ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED,
938 DRIVER_NAME, dev);
939 if (ret) {
940 dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
941 goto err_irq;
942 }
943
Jonas Aberga20d2392011-05-13 12:29:02 +0200944 dev->regulator = regulator_get(&pdev->dev, "v-i2c");
945 if (IS_ERR(dev->regulator)) {
946 dev_warn(&pdev->dev, "could not get i2c regulator\n");
947 dev->regulator = NULL;
948 }
949
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200950 pm_suspend_ignore_children(&pdev->dev, true);
951 pm_runtime_enable(&pdev->dev);
952
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530953 dev->clk = clk_get(&pdev->dev, NULL);
954 if (IS_ERR(dev->clk)) {
955 dev_err(&pdev->dev, "could not get i2c clock\n");
956 ret = PTR_ERR(dev->clk);
957 goto err_no_clk;
958 }
959
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530960 adap = &dev->adap;
961 adap->dev.parent = &pdev->dev;
962 adap->owner = THIS_MODULE;
963 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
964 adap->algo = &nmk_i2c_algo;
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200965 adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
966 msecs_to_jiffies(20000);
Linus Walleij6d779a42010-11-30 16:59:29 +0100967 snprintf(adap->name, sizeof(adap->name),
968 "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530969
970 /* fetch the controller id */
971 adap->nr = pdev->id;
972
973 /* fetch the controller configuration from machine */
974 dev->cfg.clk_freq = pdata->clk_freq;
975 dev->cfg.slsu = pdata->slsu;
976 dev->cfg.tft = pdata->tft;
977 dev->cfg.rft = pdata->rft;
978 dev->cfg.sm = pdata->sm;
979
980 i2c_set_adapdata(adap, dev);
981
Linus Walleij6d779a42010-11-30 16:59:29 +0100982 dev_info(&pdev->dev, "initialize %s on virtual "
983 "base %p\n", adap->name, dev->virtbase);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530984
985 ret = i2c_add_numbered_adapter(adap);
986 if (ret) {
987 dev_err(&pdev->dev, "failed to add adapter\n");
988 goto err_add_adap;
989 }
990
991 return 0;
992
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530993 err_add_adap:
994 clk_put(dev->clk);
995 err_no_clk:
Jonas Aberga20d2392011-05-13 12:29:02 +0200996 if (dev->regulator)
997 regulator_put(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200998 pm_runtime_disable(&pdev->dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530999 free_irq(dev->irq, dev);
1000 err_irq:
1001 iounmap(dev->virtbase);
1002 err_no_ioremap:
1003 release_mem_region(res->start, resource_size(res));
1004 err_no_region:
1005 platform_set_drvdata(pdev, NULL);
1006 err_no_resource:
1007 kfree(dev);
1008 err_no_mem:
1009
1010 return ret;
1011}
1012
1013static int __devexit nmk_i2c_remove(struct platform_device *pdev)
1014{
Rabin Vincenta1c27672010-04-27 10:31:07 +05301015 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301016 struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
1017
1018 i2c_del_adapter(&dev->adap);
1019 flush_i2c_fifo(dev);
1020 disable_all_interrupts(dev);
1021 clear_all_interrupts(dev);
1022 /* disable the controller */
1023 i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
1024 free_irq(dev->irq, dev);
1025 iounmap(dev->virtbase);
Rabin Vincenta1c27672010-04-27 10:31:07 +05301026 if (res)
1027 release_mem_region(res->start, resource_size(res));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301028 clk_put(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +02001029 if (dev->regulator)
1030 regulator_put(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +02001031 pm_runtime_disable(&pdev->dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301032 platform_set_drvdata(pdev, NULL);
1033 kfree(dev);
1034
1035 return 0;
1036}
1037
1038static struct platform_driver nmk_i2c_driver = {
1039 .driver = {
1040 .owner = THIS_MODULE,
1041 .name = DRIVER_NAME,
Rabin Vincentb0e751a2011-05-13 12:30:07 +02001042 .pm = &nmk_i2c_pm,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301043 },
1044 .probe = nmk_i2c_probe,
1045 .remove = __devexit_p(nmk_i2c_remove),
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301046};
1047
1048static int __init nmk_i2c_init(void)
1049{
1050 return platform_driver_register(&nmk_i2c_driver);
1051}
1052
1053static void __exit nmk_i2c_exit(void)
1054{
1055 platform_driver_unregister(&nmk_i2c_driver);
1056}
1057
1058subsys_initcall(nmk_i2c_init);
1059module_exit(nmk_i2c_exit);
1060
1061MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
1062MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1063MODULE_LICENSE("GPL");
1064MODULE_ALIAS("platform:" DRIVER_NAME);