blob: 698d7acb0f083c10ea2028ad895e58043105f44e [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>
Alessandro Rubini23560212012-06-11 22:56:38 +020017#include <linux/amba/bus.h>
18#include <linux/atomic.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>
Rabin Vincentb0e751a2011-05-13 12:30:07 +020025#include <linux/pm_runtime.h>
Alessandro Rubiniaf97bac2012-06-11 22:56:26 +020026#include <linux/platform_data/i2c-nomadik.h>
Lee Jones43fea582012-08-06 11:09:57 +010027#include <linux/of.h>
28#include <linux/of_i2c.h>
srinidhi kasagar3f9900f2010-02-01 19:44:54 +053029
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 */
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +020067#define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */
srinidhi kasagar3f9900f2010-02-01 19:44:54 +053068#define I2C_MCR_SB (0x1 << 11) /* Extended address */
69#define I2C_MCR_AM (0x3 << 12) /* Address type */
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +020070#define I2C_MCR_STOP (0x1 << 14) /* Stop condition */
71#define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */
srinidhi kasagar3f9900f2010-02-01 19:44:54 +053072
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 */
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +020081#define I2C_IT_TXFE (0x1 << 0)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +053082#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
108enum i2c_status {
109 I2C_NOP,
110 I2C_ON_GOING,
111 I2C_OK,
112 I2C_ABORT
113};
114
115/* operation */
116enum i2c_operation {
117 I2C_NO_OPERATION = 0xff,
118 I2C_WRITE = 0x00,
119 I2C_READ = 0x01
120};
121
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530122/**
123 * struct i2c_nmk_client - client specific data
124 * @slave_adr: 7-bit slave address
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300125 * @count: no. bytes to be transferred
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530126 * @buffer: client data buffer
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300127 * @xfer_bytes: bytes transferred till now
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530128 * @operation: current I2C operation
129 */
130struct i2c_nmk_client {
131 unsigned short slave_adr;
132 unsigned long count;
133 unsigned char *buffer;
134 unsigned long xfer_bytes;
135 enum i2c_operation operation;
136};
137
138/**
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200139 * struct nmk_i2c_dev - private data structure of the controller.
Alessandro Rubini23560212012-06-11 22:56:38 +0200140 * @adev: parent amba device.
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200141 * @adap: corresponding I2C adapter.
142 * @irq: interrupt line for the controller.
143 * @virtbase: virtual io memory area.
144 * @clk: hardware i2c block clock.
145 * @cfg: machine provided controller configuration.
146 * @cli: holder of client specific data.
147 * @stop: stop condition.
148 * @xfer_complete: acknowledge completion for a I2C message.
149 * @result: controller propogated result.
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200150 * @busy: Busy doing transfer.
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530151 */
152struct nmk_i2c_dev {
Alessandro Rubini23560212012-06-11 22:56:38 +0200153 struct amba_device *adev;
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200154 struct i2c_adapter adap;
155 int irq;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530156 void __iomem *virtbase;
157 struct clk *clk;
158 struct nmk_i2c_controller cfg;
159 struct i2c_nmk_client cli;
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200160 int stop;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530161 struct completion xfer_complete;
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200162 int result;
Jonas Aberga20d2392011-05-13 12:29:02 +0200163 bool busy;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530164};
165
166/* controller's abort causes */
167static const char *abort_causes[] = {
168 "no ack received after address transmission",
169 "no ack received during data phase",
170 "ack received after xmission of master code",
171 "master lost arbitration",
172 "slave restarts",
173 "slave reset",
174 "overflow, maxsize is 2047 bytes",
175};
176
177static inline void i2c_set_bit(void __iomem *reg, u32 mask)
178{
179 writel(readl(reg) | mask, reg);
180}
181
182static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
183{
184 writel(readl(reg) & ~mask, reg);
185}
186
187/**
188 * flush_i2c_fifo() - This function flushes the I2C FIFO
189 * @dev: private data of I2C Driver
190 *
191 * This function flushes the I2C Tx and Rx FIFOs. It returns
192 * 0 on successful flushing of FIFO
193 */
194static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
195{
196#define LOOP_ATTEMPTS 10
197 int i;
198 unsigned long timeout;
199
200 /*
201 * flush the transmit and receive FIFO. The flushing
202 * operation takes several cycles before to be completed.
203 * On the completion, the I2C internal logic clears these
204 * bits, until then no one must access Tx, Rx FIFO and
205 * should poll on these bits waiting for the completion.
206 */
207 writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
208
209 for (i = 0; i < LOOP_ATTEMPTS; i++) {
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200210 timeout = jiffies + dev->adap.timeout;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530211
212 while (!time_after(jiffies, timeout)) {
213 if ((readl(dev->virtbase + I2C_CR) &
214 (I2C_CR_FTX | I2C_CR_FRX)) == 0)
215 return 0;
216 }
217 }
218
Alessandro Rubini23560212012-06-11 22:56:38 +0200219 dev_err(&dev->adev->dev,
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200220 "flushing operation timed out giving up after %d attempts",
221 LOOP_ATTEMPTS);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530222
223 return -ETIMEDOUT;
224}
225
226/**
227 * disable_all_interrupts() - Disable all interrupts of this I2c Bus
228 * @dev: private data of I2C Driver
229 */
230static void disable_all_interrupts(struct nmk_i2c_dev *dev)
231{
232 u32 mask = IRQ_MASK(0);
233 writel(mask, dev->virtbase + I2C_IMSCR);
234}
235
236/**
237 * clear_all_interrupts() - Clear all interrupts of I2C Controller
238 * @dev: private data of I2C Driver
239 */
240static void clear_all_interrupts(struct nmk_i2c_dev *dev)
241{
242 u32 mask;
243 mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
244 writel(mask, dev->virtbase + I2C_ICR);
245}
246
247/**
248 * init_hw() - initialize the I2C hardware
249 * @dev: private data of I2C Driver
250 */
251static int init_hw(struct nmk_i2c_dev *dev)
252{
253 int stat;
254
255 stat = flush_i2c_fifo(dev);
256 if (stat)
Jonas Aberga20d2392011-05-13 12:29:02 +0200257 goto exit;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530258
259 /* disable the controller */
260 i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
261
262 disable_all_interrupts(dev);
263
264 clear_all_interrupts(dev);
265
266 dev->cli.operation = I2C_NO_OPERATION;
267
Jonas Aberga20d2392011-05-13 12:29:02 +0200268exit:
Jonas Aberga20d2392011-05-13 12:29:02 +0200269 return stat;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530270}
271
272/* enable peripheral, master mode operation */
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200273#define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530274
275/**
276 * load_i2c_mcr_reg() - load the MCR register
277 * @dev: private data of controller
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530278 * @flags: message flags
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530279 */
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530280static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev, u16 flags)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530281{
282 u32 mcr = 0;
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530283 unsigned short slave_adr_3msb_bits;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530284
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530285 mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
286
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530287 if (unlikely(flags & I2C_M_TEN)) {
288 /* 10-bit address transaction */
289 mcr |= GEN_MASK(2, I2C_MCR_AM, 12);
290 /*
291 * Get the top 3 bits.
292 * EA10 represents extended address in MCR. This includes
293 * the extension (MSB bits) of the 7 bit address loaded
294 * in A7
295 */
296 slave_adr_3msb_bits = (dev->cli.slave_adr >> 7) & 0x7;
297
298 mcr |= GEN_MASK(slave_adr_3msb_bits, I2C_MCR_EA10, 8);
299 } else {
300 /* 7-bit address transaction */
301 mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
302 }
303
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530304 /* start byte procedure not applied */
305 mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
306
307 /* check the operation, master read/write? */
308 if (dev->cli.operation == I2C_WRITE)
309 mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
310 else
311 mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
312
313 /* stop or repeated start? */
314 if (dev->stop)
315 mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
316 else
317 mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
318
319 mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
320
321 return mcr;
322}
323
324/**
325 * setup_i2c_controller() - setup the controller
326 * @dev: private data of controller
327 */
328static void setup_i2c_controller(struct nmk_i2c_dev *dev)
329{
330 u32 brcr1, brcr2;
331 u32 i2c_clk, div;
332
333 writel(0x0, dev->virtbase + I2C_CR);
334 writel(0x0, dev->virtbase + I2C_HSMCR);
335 writel(0x0, dev->virtbase + I2C_TFTR);
336 writel(0x0, dev->virtbase + I2C_RFTR);
337 writel(0x0, dev->virtbase + I2C_DMAR);
338
339 /*
340 * set the slsu:
341 *
342 * slsu defines the data setup time after SCL clock
343 * stretching in terms of i2c clk cycles. The
344 * needed setup time for the three modes are 250ns,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300345 * 100ns, 10ns respectively thus leading to the values
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530346 * of 14, 6, 2 for a 48 MHz i2c clk.
347 */
348 writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
349
350 i2c_clk = clk_get_rate(dev->clk);
351
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530352 /*
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) {
Alessandro Rubini23560212012-06-11 22:56:38 +0200379 dev_err(&dev->adev->dev,
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200380 "do not support this mode defaulting to std. mode\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530381 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
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530396 * @flags: message flags
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530397 *
398 * This function reads from i2c client device when controller is in
399 * master mode. There is a completion timeout. If there is no transfer
400 * before timeout error is returned.
401 */
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530402static int read_i2c(struct nmk_i2c_dev *dev, u16 flags)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530403{
404 u32 status = 0;
405 u32 mcr;
406 u32 irq_mask = 0;
407 int timeout;
408
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530409 mcr = load_i2c_mcr_reg(dev, flags);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530410 writel(mcr, dev->virtbase + I2C_MCR);
411
412 /* load the current CR value */
413 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
414 dev->virtbase + I2C_CR);
415
416 /* enable the controller */
417 i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
418
419 init_completion(&dev->xfer_complete);
420
421 /* enable interrupts by setting the mask */
422 irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
423 I2C_IT_MAL | I2C_IT_BERR);
424
425 if (dev->stop)
426 irq_mask |= I2C_IT_MTD;
427 else
428 irq_mask |= I2C_IT_MTDWS;
429
430 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
431
432 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
433 dev->virtbase + I2C_IMSCR);
434
srinidhi kasagar4b723a42011-08-09 20:17:22 +0200435 timeout = wait_for_completion_timeout(
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200436 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530437
438 if (timeout < 0) {
Alessandro Rubini23560212012-06-11 22:56:38 +0200439 dev_err(&dev->adev->dev,
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200440 "wait_for_completion_timeout "
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530441 "returned %d waiting for event\n", timeout);
442 status = timeout;
443 }
444
445 if (timeout == 0) {
Virupax Sadashivpetimath0511f642011-05-13 12:30:53 +0200446 /* Controller timed out */
Alessandro Rubini23560212012-06-11 22:56:38 +0200447 dev_err(&dev->adev->dev, "read from slave 0x%x timed out\n",
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200448 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530449 status = -ETIMEDOUT;
450 }
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530451 return status;
452}
453
Virupax Sadashivpetimath55355342011-05-13 12:30:34 +0200454static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
455{
456 int count;
457
458 for (count = (no_bytes - 2);
459 (count > 0) &&
460 (dev->cli.count != 0);
461 count--) {
462 /* write to the Tx FIFO */
463 writeb(*dev->cli.buffer,
464 dev->virtbase + I2C_TFR);
465 dev->cli.buffer++;
466 dev->cli.count--;
467 dev->cli.xfer_bytes++;
468 }
469
470}
471
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530472/**
473 * write_i2c() - Write data to I2C client.
474 * @dev: private data of I2C Driver
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530475 * @flags: message flags
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530476 *
477 * This function writes data to I2C client
478 */
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530479static int write_i2c(struct nmk_i2c_dev *dev, u16 flags)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530480{
481 u32 status = 0;
482 u32 mcr;
483 u32 irq_mask = 0;
484 int timeout;
485
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530486 mcr = load_i2c_mcr_reg(dev, flags);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530487
488 writel(mcr, dev->virtbase + I2C_MCR);
489
490 /* load the current CR value */
491 writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
492 dev->virtbase + I2C_CR);
493
494 /* enable the controller */
495 i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
496
497 init_completion(&dev->xfer_complete);
498
499 /* enable interrupts by settings the masks */
Virupax Sadashivpetimath55355342011-05-13 12:30:34 +0200500 irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
501
502 /* Fill the TX FIFO with transmit data */
503 fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
504
505 if (dev->cli.count != 0)
506 irq_mask |= I2C_IT_TXFNE;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530507
508 /*
509 * check if we want to transfer a single or multiple bytes, if so
510 * set the MTDWS bit (Master Transaction Done Without Stop)
511 * to start repeated start operation
512 */
513 if (dev->stop)
514 irq_mask |= I2C_IT_MTD;
515 else
516 irq_mask |= I2C_IT_MTDWS;
517
518 irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
519
520 writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
521 dev->virtbase + I2C_IMSCR);
522
srinidhi kasagar4b723a42011-08-09 20:17:22 +0200523 timeout = wait_for_completion_timeout(
Virupax Sadashivpetimathcd20e4fa2011-05-13 12:29:46 +0200524 &dev->xfer_complete, dev->adap.timeout);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530525
526 if (timeout < 0) {
Alessandro Rubini23560212012-06-11 22:56:38 +0200527 dev_err(&dev->adev->dev,
Linus Walleijc8d47632011-08-09 20:17:29 +0200528 "wait_for_completion_timeout "
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530529 "returned %d waiting for event\n", timeout);
530 status = timeout;
531 }
532
533 if (timeout == 0) {
Virupax Sadashivpetimath0511f642011-05-13 12:30:53 +0200534 /* Controller timed out */
Alessandro Rubini23560212012-06-11 22:56:38 +0200535 dev_err(&dev->adev->dev, "write to slave 0x%x timed out\n",
Virupax Sadashivpetimath4cb3f532011-05-13 12:29:55 +0200536 dev->cli.slave_adr);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530537 status = -ETIMEDOUT;
538 }
539
540 return status;
541}
542
543/**
Linus Walleij82a44132011-05-13 12:31:01 +0200544 * nmk_i2c_xfer_one() - transmit a single I2C message
545 * @dev: device with a message encoded into it
546 * @flags: message flags
547 */
548static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
549{
550 int status;
551
552 if (flags & I2C_M_RD) {
553 /* read operation */
554 dev->cli.operation = I2C_READ;
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530555 status = read_i2c(dev, flags);
Linus Walleij82a44132011-05-13 12:31:01 +0200556 } else {
557 /* write operation */
558 dev->cli.operation = I2C_WRITE;
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530559 status = write_i2c(dev, flags);
Linus Walleij82a44132011-05-13 12:31:01 +0200560 }
561
562 if (status || (dev->result)) {
563 u32 i2c_sr;
564 u32 cause;
565
566 i2c_sr = readl(dev->virtbase + I2C_SR);
567 /*
568 * Check if the controller I2C operation status
569 * is set to ABORT(11b).
570 */
571 if (((i2c_sr >> 2) & 0x3) == 0x3) {
572 /* get the abort cause */
573 cause = (i2c_sr >> 4) & 0x7;
Alessandro Rubini23560212012-06-11 22:56:38 +0200574 dev_err(&dev->adev->dev, "%s\n",
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200575 cause >= ARRAY_SIZE(abort_causes) ?
Linus Walleij82a44132011-05-13 12:31:01 +0200576 "unknown reason" :
577 abort_causes[cause]);
578 }
579
580 (void) init_hw(dev);
581
582 status = status ? status : dev->result;
583 }
584
585 return status;
586}
587
588/**
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530589 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
Linus Walleij1804edd2010-09-23 09:03:40 +0200590 * @i2c_adap: Adapter pointer to the controller
591 * @msgs: Pointer to data to be written.
592 * @num_msgs: Number of messages to be executed
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530593 *
594 * This is the function called by the generic kernel i2c_transfer()
595 * or i2c_smbus...() API calls. Note that this code is protected by the
596 * semaphore set in the kernel i2c_transfer() function.
597 *
598 * NOTE:
599 * READ TRANSFER : We impose a restriction of the first message to be the
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200600 * index message for any read transaction.
601 * - a no index is coded as '0',
602 * - 2byte big endian index is coded as '3'
603 * !!! msg[0].buf holds the actual index.
604 * This is compatible with generic messages of smbus emulator
605 * that send a one byte index.
606 * eg. a I2C transation to read 2 bytes from index 0
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530607 * idx = 0;
608 * msg[0].addr = client->addr;
609 * msg[0].flags = 0x0;
610 * msg[0].len = 1;
611 * msg[0].buf = &idx;
612 *
613 * msg[1].addr = client->addr;
614 * msg[1].flags = I2C_M_RD;
615 * msg[1].len = 2;
616 * msg[1].buf = rd_buff
617 * i2c_transfer(adap, msg, 2);
618 *
619 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
620 * If you want to emulate an SMBUS write transaction put the
621 * index as first byte(or first and second) in the payload.
622 * eg. a I2C transation to write 2 bytes from index 1
623 * wr_buff[0] = 0x1;
624 * wr_buff[1] = 0x23;
625 * wr_buff[2] = 0x46;
626 * msg[0].flags = 0x0;
627 * msg[0].len = 3;
628 * msg[0].buf = wr_buff;
629 * i2c_transfer(adap, msg, 1);
630 *
631 * To read or write a block of data (multiple bytes) using SMBUS emulation
632 * please use the i2c_smbus_read_i2c_block_data()
633 * or i2c_smbus_write_i2c_block_data() API
634 */
635static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
636 struct i2c_msg msgs[], int num_msgs)
637{
638 int status;
639 int i;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530640 struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200641 int j;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530642
Jonas Aberga20d2392011-05-13 12:29:02 +0200643 dev->busy = true;
644
Alessandro Rubini23560212012-06-11 22:56:38 +0200645 pm_runtime_get_sync(&dev->adev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200646
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200647 clk_enable(dev->clk);
648
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200649 status = init_hw(dev);
650 if (status)
651 goto out;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530652
Linus Walleij82a44132011-05-13 12:31:01 +0200653 /* Attempt three times to send the message queue */
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200654 for (j = 0; j < 3; j++) {
655 /* setup the i2c controller */
656 setup_i2c_controller(dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200657
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200658 for (i = 0; i < num_msgs; i++) {
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200659 dev->cli.slave_adr = msgs[i].addr;
660 dev->cli.buffer = msgs[i].buf;
661 dev->cli.count = msgs[i].len;
662 dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
663 dev->result = 0;
664
Linus Walleij82a44132011-05-13 12:31:01 +0200665 status = nmk_i2c_xfer_one(dev, msgs[i].flags);
666 if (status != 0)
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200667 break;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530668 }
Virupax Sadashivpetimathebd10e02011-05-13 12:30:23 +0200669 if (status == 0)
670 break;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530671 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200672
673out:
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200674 clk_disable(dev->clk);
Alessandro Rubini23560212012-06-11 22:56:38 +0200675 pm_runtime_put_sync(&dev->adev->dev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200676
677 dev->busy = false;
Linus Walleij8ef4f4e2010-09-23 09:03:55 +0200678
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530679 /* return the no. messages processed */
680 if (status)
681 return status;
682 else
683 return num_msgs;
684}
685
686/**
687 * disable_interrupts() - disable the interrupts
688 * @dev: private data of controller
Linus Walleij1804edd2010-09-23 09:03:40 +0200689 * @irq: interrupt number
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530690 */
691static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
692{
693 irq = IRQ_MASK(irq);
694 writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
695 dev->virtbase + I2C_IMSCR);
696 return 0;
697}
698
699/**
700 * i2c_irq_handler() - interrupt routine
701 * @irq: interrupt number
702 * @arg: data passed to the handler
703 *
704 * This is the interrupt handler for the i2c driver. Currently
705 * it handles the major interrupts like Rx & Tx FIFO management
706 * interrupts, master transaction interrupts, arbitration and
707 * bus error interrupts. The rest of the interrupts are treated as
708 * unhandled.
709 */
710static irqreturn_t i2c_irq_handler(int irq, void *arg)
711{
712 struct nmk_i2c_dev *dev = arg;
713 u32 tft, rft;
714 u32 count;
715 u32 misr;
716 u32 src = 0;
717
718 /* load Tx FIFO and Rx FIFO threshold values */
719 tft = readl(dev->virtbase + I2C_TFTR);
720 rft = readl(dev->virtbase + I2C_RFTR);
721
722 /* read interrupt status register */
723 misr = readl(dev->virtbase + I2C_MISR);
724
725 src = __ffs(misr);
726 switch ((1 << src)) {
727
728 /* Transmit FIFO nearly empty interrupt */
729 case I2C_IT_TXFNE:
730 {
731 if (dev->cli.operation == I2C_READ) {
732 /*
733 * in read operation why do we care for writing?
734 * so disable the Transmit FIFO interrupt
735 */
736 disable_interrupts(dev, I2C_IT_TXFNE);
737 } else {
Virupax Sadashivpetimath55355342011-05-13 12:30:34 +0200738 fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530739 /*
740 * if done, close the transfer by disabling the
741 * corresponding TXFNE interrupt
742 */
743 if (dev->cli.count == 0)
744 disable_interrupts(dev, I2C_IT_TXFNE);
745 }
746 }
747 break;
748
749 /*
750 * Rx FIFO nearly full interrupt.
751 * This is set when the numer of entries in Rx FIFO is
752 * greater or equal than the threshold value programmed
753 * in RFT
754 */
755 case I2C_IT_RXFNF:
756 for (count = rft; count > 0; count--) {
757 /* Read the Rx FIFO */
758 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
759 dev->cli.buffer++;
760 }
761 dev->cli.count -= rft;
762 dev->cli.xfer_bytes += rft;
763 break;
764
765 /* Rx FIFO full */
766 case I2C_IT_RXFF:
767 for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
768 *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
769 dev->cli.buffer++;
770 }
771 dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
772 dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
773 break;
774
775 /* Master Transaction Done with/without stop */
776 case I2C_IT_MTD:
777 case I2C_IT_MTDWS:
778 if (dev->cli.operation == I2C_READ) {
Rabin Vincent1df3ab12010-04-27 10:31:08 +0530779 while (!(readl(dev->virtbase + I2C_RISR)
780 & I2C_IT_RXFE)) {
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530781 if (dev->cli.count == 0)
782 break;
783 *dev->cli.buffer =
784 readb(dev->virtbase + I2C_RFR);
785 dev->cli.buffer++;
786 dev->cli.count--;
787 dev->cli.xfer_bytes++;
788 }
789 }
790
Virupax Sadashivpetimathb5e890f2011-05-13 12:30:42 +0200791 disable_all_interrupts(dev);
792 clear_all_interrupts(dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530793
794 if (dev->cli.count) {
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200795 dev->result = -EIO;
Alessandro Rubini23560212012-06-11 22:56:38 +0200796 dev_err(&dev->adev->dev,
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +0200797 "%lu bytes still remain to be xfered\n",
798 dev->cli.count);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530799 (void) init_hw(dev);
800 }
801 complete(&dev->xfer_complete);
802
803 break;
804
805 /* Master Arbitration lost interrupt */
806 case I2C_IT_MAL:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200807 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530808 (void) init_hw(dev);
809
810 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
811 complete(&dev->xfer_complete);
812
813 break;
814
815 /*
816 * Bus Error interrupt.
817 * This happens when an unexpected start/stop condition occurs
818 * during the transaction.
819 */
820 case I2C_IT_BERR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200821 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530822 /* get the status */
823 if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
824 (void) init_hw(dev);
825
826 i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
827 complete(&dev->xfer_complete);
828
829 break;
830
831 /*
832 * Tx FIFO overrun interrupt.
833 * This is set when a write operation in Tx FIFO is performed and
834 * the Tx FIFO is full.
835 */
836 case I2C_IT_TXFOVR:
Virupax Sadashivpetimath99381be2011-05-13 12:29:28 +0200837 dev->result = -EIO;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530838 (void) init_hw(dev);
839
Alessandro Rubini23560212012-06-11 22:56:38 +0200840 dev_err(&dev->adev->dev, "Tx Fifo Over run\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530841 complete(&dev->xfer_complete);
842
843 break;
844
845 /* unhandled interrupts by this driver - TODO*/
846 case I2C_IT_TXFE:
847 case I2C_IT_TXFF:
848 case I2C_IT_RXFE:
849 case I2C_IT_RFSR:
850 case I2C_IT_RFSE:
851 case I2C_IT_WTSR:
852 case I2C_IT_STD:
Alessandro Rubini23560212012-06-11 22:56:38 +0200853 dev_err(&dev->adev->dev, "unhandled Interrupt\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530854 break;
855 default:
Alessandro Rubini23560212012-06-11 22:56:38 +0200856 dev_err(&dev->adev->dev, "spurious Interrupt..\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530857 break;
858 }
859
860 return IRQ_HANDLED;
861}
862
Jonas Aberga20d2392011-05-13 12:29:02 +0200863
864#ifdef CONFIG_PM
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200865static int nmk_i2c_suspend(struct device *dev)
Jonas Aberga20d2392011-05-13 12:29:02 +0200866{
Alessandro Rubini23560212012-06-11 22:56:38 +0200867 struct amba_device *adev = to_amba_device(dev);
868 struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
Jonas Aberga20d2392011-05-13 12:29:02 +0200869
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200870 if (nmk_i2c->busy)
Jonas Aberga20d2392011-05-13 12:29:02 +0200871 return -EBUSY;
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200872
873 return 0;
874}
875
876static int nmk_i2c_resume(struct device *dev)
877{
878 return 0;
Jonas Aberga20d2392011-05-13 12:29:02 +0200879}
880#else
881#define nmk_i2c_suspend NULL
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200882#define nmk_i2c_resume NULL
Jonas Aberga20d2392011-05-13 12:29:02 +0200883#endif
884
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200885/*
886 * We use noirq so that we suspend late and resume before the wakeup interrupt
887 * to ensure that we do the !pm_runtime_suspended() check in resume before
888 * there has been a regular pm runtime resume (via pm_runtime_get_sync()).
889 */
890static const struct dev_pm_ops nmk_i2c_pm = {
891 .suspend_noirq = nmk_i2c_suspend,
892 .resume_noirq = nmk_i2c_resume,
893};
894
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530895static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
896{
Virupax Sadashivpetimath51a0c8d2012-06-25 17:56:07 +0530897 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530898}
899
900static const struct i2c_algorithm nmk_i2c_algo = {
901 .master_xfer = nmk_i2c_xfer,
902 .functionality = nmk_i2c_functionality
903};
904
Lee Jonesb007a3e2012-08-07 12:27:24 +0100905static struct nmk_i2c_controller u8500_i2c = {
906 /*
907 * Slave data setup time; 250ns, 100ns, and 10ns, which
908 * is 14, 6 and 2 respectively for a 48Mhz i2c clock.
909 */
910 .slsu = 0xe,
911 .tft = 1, /* Tx FIFO threshold */
912 .rft = 8, /* Rx FIFO threshold */
913 .clk_freq = 400000, /* fast mode operation */
914 .timeout = 200, /* Slave response timeout(ms) */
915 .sm = I2C_FREQ_MODE_FAST,
916};
917
Lee Jones43fea582012-08-06 11:09:57 +0100918static void nmk_i2c_of_probe(struct device_node *np,
919 struct nmk_i2c_controller *pdata)
920{
921 of_property_read_u32(np, "clock-frequency", &pdata->clk_freq);
922
923 /* This driver only supports 'standard' and 'fast' modes of operation. */
924 if (pdata->clk_freq <= 100000)
925 pdata->sm = I2C_FREQ_MODE_STANDARD;
926 else
927 pdata->sm = I2C_FREQ_MODE_FAST;
928}
929
Alessandro Rubini23560212012-06-11 22:56:38 +0200930static atomic_t adapter_id = ATOMIC_INIT(0);
931
932static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530933{
934 int ret = 0;
Lee Jonesb007a3e2012-08-07 12:27:24 +0100935 struct nmk_i2c_controller *pdata = adev->dev.platform_data;
Lee Jones43fea582012-08-06 11:09:57 +0100936 struct device_node *np = adev->dev.of_node;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530937 struct nmk_i2c_dev *dev;
938 struct i2c_adapter *adap;
939
Lee Jones43fea582012-08-06 11:09:57 +0100940 if (!pdata) {
941 if (np) {
942 pdata = devm_kzalloc(&adev->dev, sizeof(*pdata), GFP_KERNEL);
943 if (!pdata) {
944 ret = -ENOMEM;
945 goto err_no_mem;
946 }
947 /* Provide the default configuration as a base. */
948 memcpy(pdata, &u8500_i2c, sizeof(struct nmk_i2c_controller));
949 nmk_i2c_of_probe(np, pdata);
950 } else
951 /* No i2c configuration found, using the default. */
952 pdata = &u8500_i2c;
953 }
Lee Jonesb007a3e2012-08-07 12:27:24 +0100954
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530955 dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
956 if (!dev) {
Alessandro Rubini23560212012-06-11 22:56:38 +0200957 dev_err(&adev->dev, "cannot allocate memory\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530958 ret = -ENOMEM;
959 goto err_no_mem;
960 }
Jonas Aberga20d2392011-05-13 12:29:02 +0200961 dev->busy = false;
Alessandro Rubini23560212012-06-11 22:56:38 +0200962 dev->adev = adev;
963 amba_set_drvdata(adev, dev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530964
Alessandro Rubini23560212012-06-11 22:56:38 +0200965 dev->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530966 if (!dev->virtbase) {
967 ret = -ENOMEM;
968 goto err_no_ioremap;
969 }
970
Alessandro Rubini23560212012-06-11 22:56:38 +0200971 dev->irq = adev->irq[0];
Yong Zhang43110512011-09-21 17:28:33 +0800972 ret = request_irq(dev->irq, i2c_irq_handler, 0,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530973 DRIVER_NAME, dev);
974 if (ret) {
Alessandro Rubini23560212012-06-11 22:56:38 +0200975 dev_err(&adev->dev, "cannot claim the irq %d\n", dev->irq);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530976 goto err_irq;
977 }
978
Alessandro Rubini23560212012-06-11 22:56:38 +0200979 pm_suspend_ignore_children(&adev->dev, true);
Rabin Vincentb0e751a2011-05-13 12:30:07 +0200980
Alessandro Rubini23560212012-06-11 22:56:38 +0200981 dev->clk = clk_get(&adev->dev, NULL);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530982 if (IS_ERR(dev->clk)) {
Alessandro Rubini23560212012-06-11 22:56:38 +0200983 dev_err(&adev->dev, "could not get i2c clock\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530984 ret = PTR_ERR(dev->clk);
985 goto err_no_clk;
986 }
987
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530988 adap = &dev->adap;
Lee Jones43fea582012-08-06 11:09:57 +0100989 adap->dev.of_node = np;
Alessandro Rubini23560212012-06-11 22:56:38 +0200990 adap->dev.parent = &adev->dev;
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530991 adap->owner = THIS_MODULE;
992 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
993 adap->algo = &nmk_i2c_algo;
Lee Jones98582d92012-04-17 15:52:26 +0100994 adap->timeout = msecs_to_jiffies(pdata->timeout);
Alessandro Rubini23560212012-06-11 22:56:38 +0200995 adap->nr = atomic_read(&adapter_id);
Linus Walleij6d779a42010-11-30 16:59:29 +0100996 snprintf(adap->name, sizeof(adap->name),
Alessandro Rubini23560212012-06-11 22:56:38 +0200997 "Nomadik I2C%d at %pR", adap->nr, &adev->res);
998 atomic_inc(&adapter_id);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +0530999
1000 /* fetch the controller configuration from machine */
1001 dev->cfg.clk_freq = pdata->clk_freq;
1002 dev->cfg.slsu = pdata->slsu;
1003 dev->cfg.tft = pdata->tft;
1004 dev->cfg.rft = pdata->rft;
1005 dev->cfg.sm = pdata->sm;
1006
1007 i2c_set_adapdata(adap, dev);
1008
Alessandro Rubini23560212012-06-11 22:56:38 +02001009 dev_info(&adev->dev,
Jonas Aaberg8abf6fbb2011-10-20 18:23:01 +02001010 "initialize %s on virtual base %p\n",
1011 adap->name, dev->virtbase);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301012
1013 ret = i2c_add_numbered_adapter(adap);
1014 if (ret) {
Alessandro Rubini23560212012-06-11 22:56:38 +02001015 dev_err(&adev->dev, "failed to add adapter\n");
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301016 goto err_add_adap;
1017 }
1018
Lee Jones43fea582012-08-06 11:09:57 +01001019 of_i2c_register_devices(adap);
1020
Alessandro Rubini23560212012-06-11 22:56:38 +02001021 pm_runtime_put(&adev->dev);
1022
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301023 return 0;
1024
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301025 err_add_adap:
1026 clk_put(dev->clk);
1027 err_no_clk:
1028 free_irq(dev->irq, dev);
1029 err_irq:
1030 iounmap(dev->virtbase);
1031 err_no_ioremap:
Alessandro Rubini23560212012-06-11 22:56:38 +02001032 amba_set_drvdata(adev, NULL);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301033 kfree(dev);
1034 err_no_mem:
1035
1036 return ret;
1037}
1038
Alessandro Rubini23560212012-06-11 22:56:38 +02001039static int nmk_i2c_remove(struct amba_device *adev)
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301040{
Alessandro Rubini23560212012-06-11 22:56:38 +02001041 struct resource *res = &adev->res;
1042 struct nmk_i2c_dev *dev = amba_get_drvdata(adev);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301043
1044 i2c_del_adapter(&dev->adap);
1045 flush_i2c_fifo(dev);
1046 disable_all_interrupts(dev);
1047 clear_all_interrupts(dev);
1048 /* disable the controller */
1049 i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
1050 free_irq(dev->irq, dev);
1051 iounmap(dev->virtbase);
Rabin Vincenta1c27672010-04-27 10:31:07 +05301052 if (res)
1053 release_mem_region(res->start, resource_size(res));
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301054 clk_put(dev->clk);
Alessandro Rubini23560212012-06-11 22:56:38 +02001055 pm_runtime_disable(&adev->dev);
1056 amba_set_drvdata(adev, NULL);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301057 kfree(dev);
1058
1059 return 0;
1060}
1061
Alessandro Rubini23560212012-06-11 22:56:38 +02001062static struct amba_id nmk_i2c_ids[] = {
1063 {
1064 .id = 0x00180024,
1065 .mask = 0x00ffffff,
1066 },
1067 {
1068 .id = 0x00380024,
1069 .mask = 0x00ffffff,
1070 },
1071 {},
1072};
1073
1074MODULE_DEVICE_TABLE(amba, nmk_i2c_ids);
1075
1076static struct amba_driver nmk_i2c_driver = {
1077 .drv = {
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301078 .owner = THIS_MODULE,
1079 .name = DRIVER_NAME,
Rabin Vincentb0e751a2011-05-13 12:30:07 +02001080 .pm = &nmk_i2c_pm,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301081 },
Alessandro Rubini23560212012-06-11 22:56:38 +02001082 .id_table = nmk_i2c_ids,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301083 .probe = nmk_i2c_probe,
Alessandro Rubini23560212012-06-11 22:56:38 +02001084 .remove = nmk_i2c_remove,
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301085};
1086
1087static int __init nmk_i2c_init(void)
1088{
Alessandro Rubini23560212012-06-11 22:56:38 +02001089 return amba_driver_register(&nmk_i2c_driver);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301090}
1091
1092static void __exit nmk_i2c_exit(void)
1093{
Alessandro Rubini23560212012-06-11 22:56:38 +02001094 amba_driver_unregister(&nmk_i2c_driver);
srinidhi kasagar3f9900f2010-02-01 19:44:54 +05301095}
1096
1097subsys_initcall(nmk_i2c_init);
1098module_exit(nmk_i2c_exit);
1099
1100MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
1101MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1102MODULE_LICENSE("GPL");