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