2 * TI OMAP I2C master mode driver
4 * Copyright (C) 2003 MontaVista Software, Inc.
5 * Copyright (C) 2005 Nokia Corporation
6 * Copyright (C) 2004 - 2007 Texas Instruments.
8 * Originally written by MontaVista Software, Inc.
9 * Additional contributions by:
10 * Tony Lindgren <tony@atomide.com>
11 * Imre Deak <imre.deak@nokia.com>
12 * Juha Yrjölä <juha.yrjola@nokia.com>
13 * Syed Khasim <x0khasim@ti.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <linux/i2c.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/completion.h>
36 #include <linux/platform_device.h>
37 #include <linux/clk.h>
41 /* Hack to enable zero length transfers and smbus quick until clean fix
45 /* timeout waiting for the controller to respond */
46 #define OMAP_I2C_TIMEOUT (msecs_to_jiffies(1000))
48 #define OMAP_I2C_REV_REG 0x00
49 #define OMAP_I2C_IE_REG 0x04
50 #define OMAP_I2C_STAT_REG 0x08
51 #define OMAP_I2C_IV_REG 0x0c
52 #define OMAP_I2C_SYSS_REG 0x10
53 #define OMAP_I2C_BUF_REG 0x14
54 #define OMAP_I2C_CNT_REG 0x18
55 #define OMAP_I2C_DATA_REG 0x1c
56 #define OMAP_I2C_SYSC_REG 0x20
57 #define OMAP_I2C_CON_REG 0x24
58 #define OMAP_I2C_OA_REG 0x28
59 #define OMAP_I2C_SA_REG 0x2c
60 #define OMAP_I2C_PSC_REG 0x30
61 #define OMAP_I2C_SCLL_REG 0x34
62 #define OMAP_I2C_SCLH_REG 0x38
63 #define OMAP_I2C_SYSTEST_REG 0x3c
64 #define OMAP_I2C_BUFSTAT_REG 0x40
66 /* I2C Interrupt Enable Register (OMAP_I2C_IE): */
67 #define OMAP_I2C_IE_XDR (1 << 14) /* TX Buffer draining int enable */
68 #define OMAP_I2C_IE_RDR (1 << 13) /* RX Buffer draining int enable */
69 #define OMAP_I2C_IE_XRDY (1 << 4) /* TX data ready int enable */
70 #define OMAP_I2C_IE_RRDY (1 << 3) /* RX data ready int enable */
71 #define OMAP_I2C_IE_ARDY (1 << 2) /* Access ready int enable */
72 #define OMAP_I2C_IE_NACK (1 << 1) /* No ack interrupt enable */
73 #define OMAP_I2C_IE_AL (1 << 0) /* Arbitration lost int ena */
75 /* I2C Status Register (OMAP_I2C_STAT): */
76 #define OMAP_I2C_STAT_XDR (1 << 14) /* TX Buffer draining */
77 #define OMAP_I2C_STAT_RDR (1 << 13) /* RX Buffer draining */
78 #define OMAP_I2C_STAT_BB (1 << 12) /* Bus busy */
79 #define OMAP_I2C_STAT_ROVR (1 << 11) /* Receive overrun */
80 #define OMAP_I2C_STAT_XUDF (1 << 10) /* Transmit underflow */
81 #define OMAP_I2C_STAT_AAS (1 << 9) /* Address as slave */
82 #define OMAP_I2C_STAT_AD0 (1 << 8) /* Address zero */
83 #define OMAP_I2C_STAT_XRDY (1 << 4) /* Transmit data ready */
84 #define OMAP_I2C_STAT_RRDY (1 << 3) /* Receive data ready */
85 #define OMAP_I2C_STAT_ARDY (1 << 2) /* Register access ready */
86 #define OMAP_I2C_STAT_NACK (1 << 1) /* No ack interrupt enable */
87 #define OMAP_I2C_STAT_AL (1 << 0) /* Arbitration lost int ena */
89 /* I2C Buffer Configuration Register (OMAP_I2C_BUF): */
90 #define OMAP_I2C_BUF_RDMA_EN (1 << 15) /* RX DMA channel enable */
91 #define OMAP_I2C_BUF_RXFIF_CLR (1 << 14) /* RX FIFO Clear */
92 #define OMAP_I2C_BUF_XDMA_EN (1 << 7) /* TX DMA channel enable */
93 #define OMAP_I2C_BUF_TXFIF_CLR (1 << 6) /* TX FIFO Clear */
95 /* I2C Configuration Register (OMAP_I2C_CON): */
96 #define OMAP_I2C_CON_EN (1 << 15) /* I2C module enable */
97 #define OMAP_I2C_CON_BE (1 << 14) /* Big endian mode */
98 #define OMAP_I2C_CON_OPMODE_HS (1 << 12) /* High Speed support */
99 #define OMAP_I2C_CON_STB (1 << 11) /* Start byte mode (master) */
100 #define OMAP_I2C_CON_MST (1 << 10) /* Master/slave mode */
101 #define OMAP_I2C_CON_TRX (1 << 9) /* TX/RX mode (master only) */
102 #define OMAP_I2C_CON_XA (1 << 8) /* Expand address */
103 #define OMAP_I2C_CON_RM (1 << 2) /* Repeat mode (master only) */
104 #define OMAP_I2C_CON_STP (1 << 1) /* Stop cond (master only) */
105 #define OMAP_I2C_CON_STT (1 << 0) /* Start condition (master) */
107 /* I2C SCL time value when Master */
108 #define OMAP_I2C_SCLL_HSSCLL 8
109 #define OMAP_I2C_SCLH_HSSCLH 8
111 /* I2C System Test Register (OMAP_I2C_SYSTEST): */
113 #define OMAP_I2C_SYSTEST_ST_EN (1 << 15) /* System test enable */
114 #define OMAP_I2C_SYSTEST_FREE (1 << 14) /* Free running mode */
115 #define OMAP_I2C_SYSTEST_TMODE_MASK (3 << 12) /* Test mode select */
116 #define OMAP_I2C_SYSTEST_TMODE_SHIFT (12) /* Test mode select */
117 #define OMAP_I2C_SYSTEST_SCL_I (1 << 3) /* SCL line sense in */
118 #define OMAP_I2C_SYSTEST_SCL_O (1 << 2) /* SCL line drive out */
119 #define OMAP_I2C_SYSTEST_SDA_I (1 << 1) /* SDA line sense in */
120 #define OMAP_I2C_SYSTEST_SDA_O (1 << 0) /* SDA line drive out */
123 /* I2C System Status register (OMAP_I2C_SYSS): */
124 #define OMAP_I2C_SYSS_RDONE (1 << 0) /* Reset Done */
126 /* I2C System Configuration Register (OMAP_I2C_SYSC): */
127 #define OMAP_I2C_SYSC_SRST (1 << 1) /* Soft Reset */
129 struct omap_i2c_dev {
131 void __iomem *base; /* virtual */
133 struct clk *iclk; /* Interface clock */
134 struct clk *fclk; /* Functional clock */
135 struct completion cmd_complete;
136 struct resource *ioarea;
137 u32 speed; /* Speed of bus in Khz */
141 struct i2c_adapter adapter;
142 u8 fifo_size; /* use as flag and value
143 * fifo_size==0 implies no fifo
144 * if set, should be trsh+1
147 unsigned b_hw:1; /* bad h/w fixes */
149 u16 iestate; /* Saved interrupt register */
152 static inline void omap_i2c_write_reg(struct omap_i2c_dev *i2c_dev,
155 __raw_writew(val, i2c_dev->base + reg);
158 static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *i2c_dev, int reg)
160 return __raw_readw(i2c_dev->base + reg);
163 static int omap_i2c_get_clocks(struct omap_i2c_dev *dev)
165 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
166 dev->iclk = clk_get(dev->dev, "i2c_ick");
167 if (IS_ERR(dev->iclk)) {
172 /* For I2C operations on 2430 we need 96Mhz clock */
173 if (cpu_is_omap2430()) {
174 dev->fclk = clk_get(dev->dev, "i2chs_fck");
175 if (IS_ERR(dev->fclk)) {
176 if (dev->iclk != NULL) {
184 dev->fclk = clk_get(dev->dev, "i2c_fck");
185 if (IS_ERR(dev->fclk)) {
186 if (dev->iclk != NULL) {
197 static void omap_i2c_put_clocks(struct omap_i2c_dev *dev)
201 if (dev->iclk != NULL) {
207 static void omap_i2c_unidle(struct omap_i2c_dev *dev)
209 if (dev->iclk != NULL)
210 clk_enable(dev->iclk);
211 clk_enable(dev->fclk);
213 omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, dev->iestate);
217 static void omap_i2c_idle(struct omap_i2c_dev *dev)
222 dev->iestate = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
223 omap_i2c_write_reg(dev, OMAP_I2C_IE_REG, 0);
225 iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
227 omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, dev->iestate);
228 clk_disable(dev->fclk);
229 if (dev->iclk != NULL)
230 clk_disable(dev->iclk);
233 static int omap_i2c_init(struct omap_i2c_dev *dev)
235 u16 psc = 0, scll = 0, sclh = 0;
236 u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;
237 unsigned long fclk_rate = 12000000;
238 unsigned long timeout;
239 unsigned long internal_clk = 0;
242 omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, OMAP_I2C_SYSC_SRST);
243 /* For some reason we need to set the EN bit before the
244 * reset done bit gets set. */
245 timeout = jiffies + OMAP_I2C_TIMEOUT;
246 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
247 while (!(omap_i2c_read_reg(dev, OMAP_I2C_SYSS_REG) &
248 OMAP_I2C_SYSS_RDONE)) {
249 if (time_after(jiffies, timeout)) {
250 dev_warn(dev->dev, "timeout waiting "
251 "for controller reset\n");
257 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
259 if (cpu_class_is_omap1()) {
260 struct clk *armxor_ck;
262 armxor_ck = clk_get(NULL, "armxor_ck");
263 if (IS_ERR(armxor_ck))
264 dev_warn(dev->dev, "Could not get armxor_ck\n");
266 fclk_rate = clk_get_rate(armxor_ck);
269 /* TRM for 5912 says the I2C clock must be prescaled to be
270 * between 7 - 12 MHz. The XOR input clock is typically
271 * 12, 13 or 19.2 MHz. So we should have code that produces:
273 * XOR MHz Divider Prescaler
278 if (fclk_rate > 12000000)
279 psc = fclk_rate / 12000000;
282 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
284 /* HSI2C controller internal clk rate should be 19.2 Mhz */
285 internal_clk = 19200;
286 fclk_rate = clk_get_rate(dev->fclk) / 1000;
288 /* Compute prescaler divisor */
289 psc = fclk_rate / internal_clk;
292 /* If configured for High Speed */
293 if (dev->speed > 400) {
294 /* For first phase of HS mode */
295 fsscll = internal_clk / (400 * 2) - 6;
296 fssclh = internal_clk / (400 * 2) - 6;
298 /* For second phase of HS mode */
299 hsscll = fclk_rate / (dev->speed * 2) - 6;
300 hssclh = fclk_rate / (dev->speed * 2) - 6;
302 /* To handle F/S modes */
303 fsscll = internal_clk / (dev->speed * 2) - 6;
304 fssclh = internal_clk / (dev->speed * 2) - 6;
306 scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
307 sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;
309 /* Program desired operating rate */
310 fclk_rate /= (psc + 1) * 1000;
313 scll = fclk_rate / (dev->speed * 2) - 7 + psc;
314 sclh = fclk_rate / (dev->speed * 2) - 7 + psc;
317 /* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
318 omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, psc);
320 /* SCL low and high time values */
321 omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, scll);
322 omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, sclh);
325 /* Note: setup required fifo size - 1 */
326 omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG,
327 (dev->fifo_size - 1) << 8 | /* RTRSH */
328 OMAP_I2C_BUF_RXFIF_CLR |
329 (dev->fifo_size - 1) | /* XTRSH */
330 OMAP_I2C_BUF_TXFIF_CLR);
332 /* Take the I2C module out of reset: */
333 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
335 /* Enable interrupts */
336 omap_i2c_write_reg(dev, OMAP_I2C_IE_REG,
337 (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
338 OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
339 OMAP_I2C_IE_AL) | ((dev->fifo_size) ?
340 (OMAP_I2C_IE_RDR | OMAP_I2C_IE_XDR) : 0));
345 * Waiting on Bus Busy
347 static int omap_i2c_wait_for_bb(struct omap_i2c_dev *dev)
349 unsigned long timeout;
351 timeout = jiffies + OMAP_I2C_TIMEOUT;
352 while (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG) & OMAP_I2C_STAT_BB) {
353 if (time_after(jiffies, timeout)) {
354 dev_warn(dev->dev, "timeout waiting for bus ready\n");
364 * Low level master read/write transaction.
366 static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
367 struct i2c_msg *msg, int stop)
369 struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
376 dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
377 msg->addr, msg->len, msg->flags, stop);
383 omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
385 /* REVISIT: Could the STB bit of I2C_CON be used with probing? */
387 dev->buf_len = msg->len;
391 omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
392 /* REVISIT: Remove this hack when we can get I2C chips from board-*.c
394 * Sigh, seems we can't do zero length transactions. Thus, we
395 * can't probe for devices w/o actually sending/receiving at least
396 * a single byte. So we'll set count to 1 for the zero length
397 * transaction case and hope we don't cause grief for some
398 * arbitrary device due to random byte write/read during
402 dev->buf = &zero_byte;
406 dev->buf_len = msg->len;
410 omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);
412 /* Clear the FIFO Buffers */
413 w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
414 w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
415 omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);
417 init_completion(&dev->cmd_complete);
420 w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
422 /* High speed configuration */
423 if (dev->speed > 400)
424 w |= OMAP_I2C_CON_OPMODE_HS;
426 if (msg->flags & I2C_M_TEN)
427 w |= OMAP_I2C_CON_XA;
428 if (!(msg->flags & I2C_M_RD))
429 w |= OMAP_I2C_CON_TRX;
431 if (!dev->b_hw && stop)
432 w |= OMAP_I2C_CON_STP;
434 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
436 if (dev->b_hw && stop) {
437 /* H/w behavior: dont write stt and stp together.. */
438 while (omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) & OMAP_I2C_CON_STT) {
439 /* Dont do anything - this will come in a couple of loops at max*/
441 w |= OMAP_I2C_CON_STP;
442 w &= ~OMAP_I2C_CON_STT;
443 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
445 r = wait_for_completion_timeout(&dev->cmd_complete,
451 dev_err(dev->dev, "controller timed out\n");
456 if (likely(!dev->cmd_err))
459 /* We have an error */
460 if (dev->cmd_err & (OMAP_I2C_STAT_AL | OMAP_I2C_STAT_ROVR |
461 OMAP_I2C_STAT_XUDF)) {
466 if (dev->cmd_err & OMAP_I2C_STAT_NACK) {
467 if (msg->flags & I2C_M_IGNORE_NAK)
470 w = omap_i2c_read_reg(dev, OMAP_I2C_CON_REG);
471 w |= OMAP_I2C_CON_STP;
472 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
481 * Prepare controller for a transaction and call omap_i2c_xfer_msg
482 * to do the work during IRQ processing.
485 omap_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
487 struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
491 omap_i2c_unidle(dev);
493 if ((r = omap_i2c_wait_for_bb(dev)) < 0)
496 for (i = 0; i < num; i++) {
497 r = omap_i2c_xfer_msg(adap, &msgs[i], (i == (num - 1)));
510 omap_i2c_func(struct i2c_adapter *adap)
513 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
515 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
520 omap_i2c_complete_cmd(struct omap_i2c_dev *dev, u16 err)
523 complete(&dev->cmd_complete);
527 omap_i2c_ack_stat(struct omap_i2c_dev *dev, u16 stat)
529 omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat);
533 omap_i2c_rev1_isr(int this_irq, void *dev_id)
535 struct omap_i2c_dev *dev = dev_id;
541 iv = omap_i2c_read_reg(dev, OMAP_I2C_IV_REG);
543 case 0x00: /* None */
545 case 0x01: /* Arbitration lost */
546 dev_err(dev->dev, "Arbitration lost\n");
547 omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_AL);
549 case 0x02: /* No acknowledgement */
550 omap_i2c_complete_cmd(dev, OMAP_I2C_STAT_NACK);
551 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_STP);
553 case 0x03: /* Register access ready */
554 omap_i2c_complete_cmd(dev, 0);
556 case 0x04: /* Receive data ready */
558 w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
562 *dev->buf++ = w >> 8;
566 dev_err(dev->dev, "RRDY IRQ while no data requested\n");
568 case 0x05: /* Transmit data ready */
573 w |= *dev->buf++ << 8;
576 omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
578 dev_err(dev->dev, "XRDY IRQ while no data to send\n");
588 omap_i2c_isr(int this_irq, void *dev_id)
590 struct omap_i2c_dev *dev = dev_id;
598 bits = omap_i2c_read_reg(dev, OMAP_I2C_IE_REG);
599 while ((stat = (omap_i2c_read_reg(dev, OMAP_I2C_STAT_REG))) & bits) {
600 dev_dbg(dev->dev, "IRQ (ISR = 0x%04x)\n", stat);
601 if (count++ == 100) {
602 dev_warn(dev->dev, "Too much work in one IRQ\n");
606 omap_i2c_write_reg(dev, OMAP_I2C_STAT_REG, stat);
609 if (stat & OMAP_I2C_STAT_NACK) {
610 err |= OMAP_I2C_STAT_NACK;
611 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG,
614 if (stat & OMAP_I2C_STAT_AL) {
615 dev_err(dev->dev, "Arbitration lost\n");
616 err |= OMAP_I2C_STAT_AL;
618 if (stat & (OMAP_I2C_STAT_ARDY | OMAP_I2C_STAT_NACK |
620 omap_i2c_complete_cmd(dev, err);
621 if (stat & (OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR)) {
623 if (dev->fifo_size) {
624 num_bytes = (stat & OMAP_I2C_STAT_RRDY) ? dev->fifo_size :
625 omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG);
629 w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
633 /* Data reg from 2430 is 8 bit wide */
634 if (!cpu_is_omap2430() &&
635 !cpu_is_omap34xx()) {
637 *dev->buf++ = w >> 8;
642 if (stat & OMAP_I2C_STAT_RRDY)
643 dev_err(dev->dev, "RRDY IRQ while no data "
645 if (stat & OMAP_I2C_STAT_RDR)
646 dev_err(dev->dev, "RDR IRQ while no data "
651 omap_i2c_ack_stat(dev, stat & (OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR));
653 if (stat & (OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR)) {
655 if (dev->fifo_size) {
656 num_bytes = (stat & OMAP_I2C_STAT_XRDY) ? dev->fifo_size :
657 omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG);
665 /* Data reg from 2430 is 8 bit wide */
666 if (!cpu_is_omap2430() &&
667 !cpu_is_omap34xx()) {
669 w |= *dev->buf++ << 8;
674 if (stat & OMAP_I2C_STAT_XRDY)
675 dev_err(dev->dev, "XRDY IRQ while no "
677 if (stat & OMAP_I2C_STAT_XDR)
678 dev_err(dev->dev, "XDR IRQ while no "
682 omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
684 omap_i2c_ack_stat(dev, stat & (OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR));
686 if (stat & OMAP_I2C_STAT_ROVR) {
687 dev_err(dev->dev, "Receive overrun\n");
688 dev->cmd_err |= OMAP_I2C_STAT_ROVR;
690 if (stat & OMAP_I2C_STAT_XUDF) {
691 dev_err(dev->dev, "Transmit overflow\n");
692 dev->cmd_err |= OMAP_I2C_STAT_XUDF;
696 return count ? IRQ_HANDLED : IRQ_NONE;
699 static const struct i2c_algorithm omap_i2c_algo = {
700 .master_xfer = omap_i2c_xfer,
701 .functionality = omap_i2c_func,
705 omap_i2c_probe(struct platform_device *pdev)
707 struct omap_i2c_dev *dev;
708 struct i2c_adapter *adap;
709 struct resource *mem, *irq, *ioarea;
713 /* NOTE: driver uses the static register mapping */
714 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
716 dev_err(&pdev->dev, "no mem resource?\n");
719 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
721 dev_err(&pdev->dev, "no irq resource?\n");
725 ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
728 dev_err(&pdev->dev, "I2C region already claimed\n");
732 dev = kzalloc(sizeof(struct omap_i2c_dev), GFP_KERNEL);
735 goto err_release_region;
738 if (pdev->dev.platform_data != NULL)
739 speed = (u32 *) pdev->dev.platform_data;
741 *speed = 100; /* Defualt speed */
744 dev->dev = &pdev->dev;
745 dev->irq = irq->start;
746 dev->base = (void __iomem *) IO_ADDRESS(mem->start);
747 platform_set_drvdata(pdev, dev);
749 if ((r = omap_i2c_get_clocks(dev)) != 0)
752 omap_i2c_unidle(dev);
754 if (cpu_is_omap15xx())
755 dev->rev1 = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) < 0x20;
757 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
758 /* Set up the fifo size - Get total size */
759 dev->fifo_size = 0x8 <<
760 ((omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG) >> 14) & 0x3);
762 * Set up notification threshold as half the total available size
763 * This is to ensure that we can handle the status on int call back
766 dev->fifo_size = (dev->fifo_size / 2);
767 dev->b_hw = 1; /* Enable hardware fixes */
770 /* reset ASAP, clearing any IRQs */
773 r = request_irq(dev->irq, dev->rev1 ? omap_i2c_rev1_isr : omap_i2c_isr,
777 dev_err(dev->dev, "failure requesting irq %i\n", dev->irq);
778 goto err_unuse_clocks;
780 r = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) & 0xff;
781 dev_info(dev->dev, "bus %d rev%d.%d at %d kHz\n",
782 pdev->id, r >> 4, r & 0xf, dev->speed);
784 adap = &dev->adapter;
785 i2c_set_adapdata(adap, dev);
786 adap->owner = THIS_MODULE;
787 adap->class = I2C_CLASS_HWMON;
788 strncpy(adap->name, "OMAP I2C adapter", sizeof(adap->name));
789 adap->algo = &omap_i2c_algo;
790 adap->dev.parent = &pdev->dev;
792 /* i2c device drivers may be active on return from add_adapter() */
794 r = i2c_add_numbered_adapter(adap);
796 dev_err(dev->dev, "failure adding adapter\n");
805 free_irq(dev->irq, dev);
807 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
809 omap_i2c_put_clocks(dev);
811 platform_set_drvdata(pdev, NULL);
814 release_mem_region(mem->start, (mem->end - mem->start) + 1);
820 omap_i2c_remove(struct platform_device *pdev)
822 struct omap_i2c_dev *dev = platform_get_drvdata(pdev);
823 struct resource *mem;
825 platform_set_drvdata(pdev, NULL);
827 free_irq(dev->irq, dev);
828 i2c_del_adapter(&dev->adapter);
829 omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, 0);
830 omap_i2c_put_clocks(dev);
832 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
833 release_mem_region(mem->start, (mem->end - mem->start) + 1);
837 static struct platform_driver omap_i2c_driver = {
838 .probe = omap_i2c_probe,
839 .remove = omap_i2c_remove,
842 .owner = THIS_MODULE,
846 /* I2C may be needed to bring up other drivers */
848 omap_i2c_init_driver(void)
850 return platform_driver_register(&omap_i2c_driver);
852 subsys_initcall(omap_i2c_init_driver);
854 static void __exit omap_i2c_exit_driver(void)
856 platform_driver_unregister(&omap_i2c_driver);
858 module_exit(omap_i2c_exit_driver);
860 MODULE_AUTHOR("MontaVista Software, Inc. (and others)");
861 MODULE_DESCRIPTION("TI OMAP I2C bus adapter");
862 MODULE_LICENSE("GPL");
863 MODULE_ALIAS("platform:i2c_omap");