blob: 234e4a9070b82a5a57ca873629e25dc8dc4a83d1 [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
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200258 clk_enable(dev->clk);
259
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530260 stat = flush_i2c_fifo(dev);
261 if (stat)
Jonas Aberga20d2392011-05-13 12:29:02 +0200262 goto exit;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530263
264 /* disable the controller */
265 i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
266
267 disable_all_interrupts(dev);
268
269 clear_all_interrupts(dev);
270
271 dev->cli.operation = I2C_NO_OPERATION;
272
Jonas Aberga20d2392011-05-13 12:29:02 +0200273exit:
274 /* TODO: Why disable clocks after init hw? */
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200275 clk_disable(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +0200276 /*
277 * TODO: What is this delay for?
278 * Must be pretty pointless since the hw block
279 * is frozen. Or?
280 */
Linus Walleijf868fc32010-09-23 09:04:11 +0200281 udelay(I2C_DELAY);
Jonas Aberga20d2392011-05-13 12:29:02 +0200282 return stat;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530283}
284
285/* enable peripheral, master mode operation */
286#define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
287
288/**
289 * load_i2c_mcr_reg() - load the MCR register
290 * @dev: private data of controller
291 */
292static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)
293{
294 u32 mcr = 0;
295
296 /* 7-bit address transaction */
297 mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
298 mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
299
300 /* start byte procedure not applied */
301 mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
302
303 /* check the operation, master read/write? */
304 if (dev->cli.operation == I2C_WRITE)
305 mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
306 else
307 mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
308
309 /* stop or repeated start? */
310 if (dev->stop)
311 mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
312 else
313 mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
314
315 mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
316
317 return mcr;
318}
319
320/**
321 * setup_i2c_controller() - setup the controller
322 * @dev: private data of controller
323 */
324static void setup_i2c_controller(struct nmk_i2c_dev *dev)
325{
326 u32 brcr1, brcr2;
327 u32 i2c_clk, div;
328
329 writel(0x0, dev->virtbase + I2C_CR);
330 writel(0x0, dev->virtbase + I2C_HSMCR);
331 writel(0x0, dev->virtbase + I2C_TFTR);
332 writel(0x0, dev->virtbase + I2C_RFTR);
333 writel(0x0, dev->virtbase + I2C_DMAR);
334
335 /*
336 * set the slsu:
337 *
338 * slsu defines the data setup time after SCL clock
339 * stretching in terms of i2c clk cycles. The
340 * needed setup time for the three modes are 250ns,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300341 * 100ns, 10ns respectively thus leading to the values
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530342 * of 14, 6, 2 for a 48 MHz i2c clk.
343 */
344 writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
345
346 i2c_clk = clk_get_rate(dev->clk);
347
348 /* fallback to std. mode if machine has not provided it */
349 if (dev->cfg.clk_freq == 0)
350 dev->cfg.clk_freq = 100000;
351
352 /*
353 * The spec says, in case of std. mode the divider is
354 * 2 whereas it is 3 for fast and fastplus mode of
355 * operation. TODO - high speed support.
356 */
357 div = (dev->cfg.clk_freq > 100000) ? 3 : 2;
358
359 /*
360 * generate the mask for baud rate counters. The controller
361 * has two baud rate counters. One is used for High speed
362 * operation, and the other is for std, fast mode, fast mode
363 * plus operation. Currently we do not supprt high speed mode
364 * so set brcr1 to 0.
365 */
366 brcr1 = 0 << 16;
367 brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;
368
369 /* set the baud rate counter register */
370 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
371
372 /*
373 * set the speed mode. Currently we support
374 * only standard and fast mode of operation
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300375 * TODO - support for fast mode plus (up to 1Mb/s)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530376 * and high speed (up to 3.4 Mb/s)
377 */
378 if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
379 dev_err(&dev->pdev->dev, "do not support this mode "
380 "defaulting to std. mode\n");
381 brcr2 = i2c_clk/(100000 * 2) & 0xffff;
382 writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
383 writel(I2C_FREQ_MODE_STANDARD << 4,
384 dev->virtbase + I2C_CR);
385 }
386 writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);
387
388 /* set the Tx and Rx FIFO threshold */
389 writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);
390 writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);
391}
392
393/**
394 * read_i2c() - Read from I2C client device
395 * @dev: private data of I2C Driver
396 *
397 * This function reads from i2c client device when controller is in
398 * master mode. There is a completion timeout. If there is no transfer
399 * before timeout error is returned.
400 */
401static int read_i2c(struct nmk_i2c_dev *dev)
402{
403 u32 status = 0;
404 u32 mcr;
405 u32 irq_mask = 0;
406 int timeout;
407
408 mcr = load_i2c_mcr_reg(dev);
409 writel(mcr, dev->virtbase + I2C_MCR);
410
411 /* load the current CR value */
412 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
413 dev->virtbase + I2C_CR);
414
415 /* enable the controller */
416 i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
417
418 init_completion(&dev->xfer_complete);
419
420 /* enable interrupts by setting the mask */
421 irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
422 I2C_IT_MAL | I2C_IT_BERR);
423
424 if (dev->stop)
425 irq_mask |= I2C_IT_MTD;
426 else
427 irq_mask |= I2C_IT_MTDWS;
428
429 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
430
431 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
432 dev->virtbase + I2C_IMSCR);
433
434 timeout = wait_for_completion_interruptible_timeout(
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200435 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530436
437 if (timeout < 0) {
438 dev_err(&dev->pdev->dev,
439 "wait_for_completion_interruptible_timeout"
440 "returned %d waiting for event\n", timeout);
441 status = timeout;
442 }
443
444 if (timeout == 0) {
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400445 /* controller has timedout, re-init the h/w */
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200446 dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n",
447 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530448 (void) init_hw(dev);
449 status = -ETIMEDOUT;
450 }
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530451 return status;
452}
453
454/**
455 * write_i2c() - Write data to I2C client.
456 * @dev: private data of I2C Driver
457 *
458 * This function writes data to I2C client
459 */
460static int write_i2c(struct nmk_i2c_dev *dev)
461{
462 u32 status = 0;
463 u32 mcr;
464 u32 irq_mask = 0;
465 int timeout;
466
467 mcr = load_i2c_mcr_reg(dev);
468
469 writel(mcr, dev->virtbase + I2C_MCR);
470
471 /* load the current CR value */
472 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
473 dev->virtbase + I2C_CR);
474
475 /* enable the controller */
476 i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
477
478 init_completion(&dev->xfer_complete);
479
480 /* enable interrupts by settings the masks */
481 irq_mask = (I2C_IT_TXFNE | I2C_IT_TXFOVR |
482 I2C_IT_MAL | I2C_IT_BERR);
483
484 /*
485 * check if we want to transfer a single or multiple bytes, if so
486 * set the MTDWS bit (Master Transaction Done Without Stop)
487 * to start repeated start operation
488 */
489 if (dev->stop)
490 irq_mask |= I2C_IT_MTD;
491 else
492 irq_mask |= I2C_IT_MTDWS;
493
494 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
495
496 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
497 dev->virtbase + I2C_IMSCR);
498
499 timeout = wait_for_completion_interruptible_timeout(
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200500 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530501
502 if (timeout < 0) {
503 dev_err(&dev->pdev->dev,
504 "wait_for_completion_interruptible_timeout"
505 "returned %d waiting for event\n", timeout);
506 status = timeout;
507 }
508
509 if (timeout == 0) {
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400510 /* controller has timedout, re-init the h/w */
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200511 dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n",
512 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530513 (void) init_hw(dev);
514 status = -ETIMEDOUT;
515 }
516
517 return status;
518}
519
520/**
521 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
Linus Walleij1804edd2010-09-23 09:03:40 +0200522 * @i2c_adap: Adapter pointer to the controller
523 * @msgs: Pointer to data to be written.
524 * @num_msgs: Number of messages to be executed
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530525 *
526 * This is the function called by the generic kernel i2c_transfer()
527 * or i2c_smbus...() API calls. Note that this code is protected by the
528 * semaphore set in the kernel i2c_transfer() function.
529 *
530 * NOTE:
531 * READ TRANSFER : We impose a restriction of the first message to be the
532 * index message for any read transaction.
533 * - a no index is coded as '0',
534 * - 2byte big endian index is coded as '3'
535 * !!! msg[0].buf holds the actual index.
536 * This is compatible with generic messages of smbus emulator
537 * that send a one byte index.
538 * eg. a I2C transation to read 2 bytes from index 0
539 * idx = 0;
540 * msg[0].addr = client->addr;
541 * msg[0].flags = 0x0;
542 * msg[0].len = 1;
543 * msg[0].buf = &idx;
544 *
545 * msg[1].addr = client->addr;
546 * msg[1].flags = I2C_M_RD;
547 * msg[1].len = 2;
548 * msg[1].buf = rd_buff
549 * i2c_transfer(adap, msg, 2);
550 *
551 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
552 * If you want to emulate an SMBUS write transaction put the
553 * index as first byte(or first and second) in the payload.
554 * eg. a I2C transation to write 2 bytes from index 1
555 * wr_buff[0] = 0x1;
556 * wr_buff[1] = 0x23;
557 * wr_buff[2] = 0x46;
558 * msg[0].flags = 0x0;
559 * msg[0].len = 3;
560 * msg[0].buf = wr_buff;
561 * i2c_transfer(adap, msg, 1);
562 *
563 * To read or write a block of data (multiple bytes) using SMBUS emulation
564 * please use the i2c_smbus_read_i2c_block_data()
565 * or i2c_smbus_write_i2c_block_data() API
566 */
567static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
568 struct i2c_msg msgs[], int num_msgs)
569{
570 int status;
571 int i;
572 u32 cause;
573 struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200574 u32 i2c_sr;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530575
Jonas Aberga20d2392011-05-13 12:29:02 +0200576 dev->busy = true;
577
578 if (dev->regulator)
579 regulator_enable(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200580 pm_runtime_get_sync(&dev->pdev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200581
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530582 status = init_hw(dev);
583 if (status)
Jonas Aberga20d2392011-05-13 12:29:02 +0200584 goto out2;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530585
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200586 clk_enable(dev->clk);
587
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530588 /* setup the i2c controller */
589 setup_i2c_controller(dev);
590
591 for (i = 0; i < num_msgs; i++) {
592 if (unlikely(msgs[i].flags & I2C_M_TEN)) {
593 dev_err(&dev->pdev->dev, "10 bit addressing"
594 "not supported\n");
Jonas Aberga20d2392011-05-13 12:29:02 +0200595
596 status = -EINVAL;
597 goto out;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530598 }
599 dev->cli.slave_adr = msgs[i].addr;
600 dev->cli.buffer = msgs[i].buf;
601 dev->cli.count = msgs[i].len;
602 dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
603 dev->result = 0;
604
605 if (msgs[i].flags & I2C_M_RD) {
606 /* it is a read operation */
607 dev->cli.operation = I2C_READ;
608 status = read_i2c(dev);
609 } else {
610 /* write operation */
611 dev->cli.operation = I2C_WRITE;
612 status = write_i2c(dev);
613 }
614 if (status || (dev->result)) {
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200615 i2c_sr = readl(dev->virtbase + I2C_SR);
616 /*
617 * Check if the controller I2C operation status is set
618 * to ABORT(11b).
619 */
620 if (((i2c_sr >> 2) & 0x3) == 0x3) {
621 /* get the abort cause */
622 cause = (i2c_sr >> 4)
623 & 0x7;
624 dev_err(&dev->pdev->dev, "%s\n", cause >=
625 ARRAY_SIZE(abort_causes) ?
626 "unknown reason" :
627 abort_causes[cause]);
628 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200629
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200630 status = status ? status : dev->result;
Jonas Aberga20d2392011-05-13 12:29:02 +0200631 goto out;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530632 }
Linus Walleijf868fc32010-09-23 09:04:11 +0200633 udelay(I2C_DELAY);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530634 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200635
636out:
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200637 clk_disable(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +0200638out2:
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200639 pm_runtime_put_sync(&dev->pdev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200640 if (dev->regulator)
641 regulator_disable(dev->regulator);
642
643 dev->busy = false;
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200644
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530645 /* return the no. messages processed */
646 if (status)
647 return status;
648 else
649 return num_msgs;
650}
651
652/**
653 * disable_interrupts() - disable the interrupts
654 * @dev: private data of controller
Linus Walleij1804edd2010-09-23 09:03:40 +0200655 * @irq: interrupt number
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530656 */
657static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
658{
659 irq = IRQ_MASK(irq);
660 writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
661 dev->virtbase + I2C_IMSCR);
662 return 0;
663}
664
665/**
666 * i2c_irq_handler() - interrupt routine
667 * @irq: interrupt number
668 * @arg: data passed to the handler
669 *
670 * This is the interrupt handler for the i2c driver. Currently
671 * it handles the major interrupts like Rx & Tx FIFO management
672 * interrupts, master transaction interrupts, arbitration and
673 * bus error interrupts. The rest of the interrupts are treated as
674 * unhandled.
675 */
676static irqreturn_t i2c_irq_handler(int irq, void *arg)
677{
678 struct nmk_i2c_dev *dev = arg;
679 u32 tft, rft;
680 u32 count;
681 u32 misr;
682 u32 src = 0;
683
684 /* load Tx FIFO and Rx FIFO threshold values */
685 tft = readl(dev->virtbase + I2C_TFTR);
686 rft = readl(dev->virtbase + I2C_RFTR);
687
688 /* read interrupt status register */
689 misr = readl(dev->virtbase + I2C_MISR);
690
691 src = __ffs(misr);
692 switch ((1 << src)) {
693
694 /* Transmit FIFO nearly empty interrupt */
695 case I2C_IT_TXFNE:
696 {
697 if (dev->cli.operation == I2C_READ) {
698 /*
699 * in read operation why do we care for writing?
700 * so disable the Transmit FIFO interrupt
701 */
702 disable_interrupts(dev, I2C_IT_TXFNE);
703 } else {
704 for (count = (MAX_I2C_FIFO_THRESHOLD - tft - 2);
705 (count > 0) &&
706 (dev->cli.count != 0);
707 count--) {
708 /* write to the Tx FIFO */
709 writeb(*dev->cli.buffer,
710 dev->virtbase + I2C_TFR);
711 dev->cli.buffer++;
712 dev->cli.count--;
713 dev->cli.xfer_bytes++;
714 }
715 /*
716 * if done, close the transfer by disabling the
717 * corresponding TXFNE interrupt
718 */
719 if (dev->cli.count == 0)
720 disable_interrupts(dev, I2C_IT_TXFNE);
721 }
722 }
723 break;
724
725 /*
726 * Rx FIFO nearly full interrupt.
727 * This is set when the numer of entries in Rx FIFO is
728 * greater or equal than the threshold value programmed
729 * in RFT
730 */
731 case I2C_IT_RXFNF:
732 for (count = rft; count > 0; count--) {
733 /* Read the Rx FIFO */
734 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
735 dev->cli.buffer++;
736 }
737 dev->cli.count -= rft;
738 dev->cli.xfer_bytes += rft;
739 break;
740
741 /* Rx FIFO full */
742 case I2C_IT_RXFF:
743 for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
744 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
745 dev->cli.buffer++;
746 }
747 dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
748 dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
749 break;
750
751 /* Master Transaction Done with/without stop */
752 case I2C_IT_MTD:
753 case I2C_IT_MTDWS:
754 if (dev->cli.operation == I2C_READ) {
Rabin Vincent1df3ab12010-04-27 10:31:08 +0530755 while (!(readl(dev->virtbase + I2C_RISR)
756 & I2C_IT_RXFE)) {
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530757 if (dev->cli.count == 0)
758 break;
759 *dev->cli.buffer =
760 readb(dev->virtbase + I2C_RFR);
761 dev->cli.buffer++;
762 dev->cli.count--;
763 dev->cli.xfer_bytes++;
764 }
765 }
766
767 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTD);
768 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTDWS);
769
770 disable_interrupts(dev,
771 (I2C_IT_TXFNE | I2C_IT_TXFE | I2C_IT_TXFF
772 | I2C_IT_TXFOVR | I2C_IT_RXFNF
773 | I2C_IT_RXFF | I2C_IT_RXFE));
774
775 if (dev->cli.count) {
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200776 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530777 dev_err(&dev->pdev->dev, "%lu bytes still remain to be"
778 "xfered\n", dev->cli.count);
779 (void) init_hw(dev);
780 }
781 complete(&dev->xfer_complete);
782
783 break;
784
785 /* Master Arbitration lost interrupt */
786 case I2C_IT_MAL:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200787 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530788 (void) init_hw(dev);
789
790 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
791 complete(&dev->xfer_complete);
792
793 break;
794
795 /*
796 * Bus Error interrupt.
797 * This happens when an unexpected start/stop condition occurs
798 * during the transaction.
799 */
800 case I2C_IT_BERR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200801 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530802 /* get the status */
803 if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
804 (void) init_hw(dev);
805
806 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
807 complete(&dev->xfer_complete);
808
809 break;
810
811 /*
812 * Tx FIFO overrun interrupt.
813 * This is set when a write operation in Tx FIFO is performed and
814 * the Tx FIFO is full.
815 */
816 case I2C_IT_TXFOVR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200817 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530818 (void) init_hw(dev);
819
820 dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
821 complete(&dev->xfer_complete);
822
823 break;
824
825 /* unhandled interrupts by this driver - TODO*/
826 case I2C_IT_TXFE:
827 case I2C_IT_TXFF:
828 case I2C_IT_RXFE:
829 case I2C_IT_RFSR:
830 case I2C_IT_RFSE:
831 case I2C_IT_WTSR:
832 case I2C_IT_STD:
833 dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
834 break;
835 default:
836 dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
837 break;
838 }
839
840 return IRQ_HANDLED;
841}
842
Jonas Aberga20d2392011-05-13 12:29:02 +0200843
844#ifdef CONFIG_PM
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200845static int nmk_i2c_suspend(struct device *dev)
Jonas Aberga20d2392011-05-13 12:29:02 +0200846{
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200847 struct platform_device *pdev = to_platform_device(dev);
848 struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200849
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200850 if (nmk_i2c->busy)
Jonas Aberga20d2392011-05-13 12:29:02 +0200851 return -EBUSY;
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200852
853 return 0;
854}
855
856static int nmk_i2c_resume(struct device *dev)
857{
858 return 0;
Jonas Aberga20d2392011-05-13 12:29:02 +0200859}
860#else
861#define nmk_i2c_suspend NULL
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200862#define nmk_i2c_resume NULL
Jonas Aberga20d2392011-05-13 12:29:02 +0200863#endif
864
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200865/*
866 * We use noirq so that we suspend late and resume before the wakeup interrupt
867 * to ensure that we do the !pm_runtime_suspended() check in resume before
868 * there has been a regular pm runtime resume (via pm_runtime_get_sync()).
869 */
870static const struct dev_pm_ops nmk_i2c_pm = {
871 .suspend_noirq = nmk_i2c_suspend,
872 .resume_noirq = nmk_i2c_resume,
873};
874
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530875static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
876{
Linus Walleij5680bc62010-09-23 09:04:03 +0200877 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530878}
879
880static const struct i2c_algorithm nmk_i2c_algo = {
881 .master_xfer = nmk_i2c_xfer,
882 .functionality = nmk_i2c_functionality
883};
884
885static int __devinit nmk_i2c_probe(struct platform_device *pdev)
886{
887 int ret = 0;
888 struct resource *res;
889 struct nmk_i2c_controller *pdata =
890 pdev->dev.platform_data;
891 struct nmk_i2c_dev *dev;
892 struct i2c_adapter *adap;
893
894 dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
895 if (!dev) {
896 dev_err(&pdev->dev, "cannot allocate memory\n");
897 ret = -ENOMEM;
898 goto err_no_mem;
899 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200900 dev->busy = false;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530901 dev->pdev = pdev;
902 platform_set_drvdata(pdev, dev);
903
904 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
905 if (!res) {
906 ret = -ENOENT;
907 goto err_no_resource;
908 }
909
910 if (request_mem_region(res->start, resource_size(res),
911 DRIVER_NAME "I/O region") == NULL) {
912 ret = -EBUSY;
913 goto err_no_region;
914 }
915
916 dev->virtbase = ioremap(res->start, resource_size(res));
917 if (!dev->virtbase) {
918 ret = -ENOMEM;
919 goto err_no_ioremap;
920 }
921
922 dev->irq = platform_get_irq(pdev, 0);
923 ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED,
924 DRIVER_NAME, dev);
925 if (ret) {
926 dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
927 goto err_irq;
928 }
929
Jonas Aberga20d2392011-05-13 12:29:02 +0200930 dev->regulator = regulator_get(&pdev->dev, "v-i2c");
931 if (IS_ERR(dev->regulator)) {
932 dev_warn(&pdev->dev, "could not get i2c regulator\n");
933 dev->regulator = NULL;
934 }
935
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200936 pm_suspend_ignore_children(&pdev->dev, true);
937 pm_runtime_enable(&pdev->dev);
938
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530939 dev->clk = clk_get(&pdev->dev, NULL);
940 if (IS_ERR(dev->clk)) {
941 dev_err(&pdev->dev, "could not get i2c clock\n");
942 ret = PTR_ERR(dev->clk);
943 goto err_no_clk;
944 }
945
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530946 adap = &dev->adap;
947 adap->dev.parent = &pdev->dev;
948 adap->owner = THIS_MODULE;
949 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
950 adap->algo = &nmk_i2c_algo;
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200951 adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
952 msecs_to_jiffies(20000);
Linus Walleij6d779a42010-11-30 16:59:29 +0100953 snprintf(adap->name, sizeof(adap->name),
954 "Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530955
956 /* fetch the controller id */
957 adap->nr = pdev->id;
958
959 /* fetch the controller configuration from machine */
960 dev->cfg.clk_freq = pdata->clk_freq;
961 dev->cfg.slsu = pdata->slsu;
962 dev->cfg.tft = pdata->tft;
963 dev->cfg.rft = pdata->rft;
964 dev->cfg.sm = pdata->sm;
965
966 i2c_set_adapdata(adap, dev);
967
Linus Walleij6d779a42010-11-30 16:59:29 +0100968 dev_info(&pdev->dev, "initialize %s on virtual "
969 "base %p\n", adap->name, dev->virtbase);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530970
971 ret = i2c_add_numbered_adapter(adap);
972 if (ret) {
973 dev_err(&pdev->dev, "failed to add adapter\n");
974 goto err_add_adap;
975 }
976
977 return 0;
978
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530979 err_add_adap:
980 clk_put(dev->clk);
981 err_no_clk:
Jonas Aberga20d2392011-05-13 12:29:02 +0200982 if (dev->regulator)
983 regulator_put(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200984 pm_runtime_disable(&pdev->dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530985 free_irq(dev->irq, dev);
986 err_irq:
987 iounmap(dev->virtbase);
988 err_no_ioremap:
989 release_mem_region(res->start, resource_size(res));
990 err_no_region:
991 platform_set_drvdata(pdev, NULL);
992 err_no_resource:
993 kfree(dev);
994 err_no_mem:
995
996 return ret;
997}
998
999static int __devexit nmk_i2c_remove(struct platform_device *pdev)
1000{
Rabin Vincenta1c27672010-04-27 10:31:07 +05301001 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301002 struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
1003
1004 i2c_del_adapter(&dev->adap);
1005 flush_i2c_fifo(dev);
1006 disable_all_interrupts(dev);
1007 clear_all_interrupts(dev);
1008 /* disable the controller */
1009 i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
1010 free_irq(dev->irq, dev);
1011 iounmap(dev->virtbase);
Rabin Vincenta1c27672010-04-27 10:31:07 +05301012 if (res)
1013 release_mem_region(res->start, resource_size(res));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301014 clk_put(dev->clk);
Jonas Aberga20d2392011-05-13 12:29:02 +02001015 if (dev->regulator)
1016 regulator_put(dev->regulator);
Rabin Vincentb0e751a2011-05-13 12:30:07 +02001017 pm_runtime_disable(&pdev->dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301018 platform_set_drvdata(pdev, NULL);
1019 kfree(dev);
1020
1021 return 0;
1022}
1023
1024static struct platform_driver nmk_i2c_driver = {
1025 .driver = {
1026 .owner = THIS_MODULE,
1027 .name = DRIVER_NAME,
Rabin Vincentb0e751a2011-05-13 12:30:07 +02001028 .pm = &nmk_i2c_pm,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301029 },
1030 .probe = nmk_i2c_probe,
1031 .remove = __devexit_p(nmk_i2c_remove),
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301032};
1033
1034static int __init nmk_i2c_init(void)
1035{
1036 return platform_driver_register(&nmk_i2c_driver);
1037}
1038
1039static void __exit nmk_i2c_exit(void)
1040{
1041 platform_driver_unregister(&nmk_i2c_driver);
1042}
1043
1044subsys_initcall(nmk_i2c_init);
1045module_exit(nmk_i2c_exit);
1046
1047MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
1048MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1049MODULE_LICENSE("GPL");
1050MODULE_ALIAS("platform:" DRIVER_NAME);