2 * Lantiq FALC(tm) ON - I2C bus adapter
4 * Parts based on i2c-designware.c and other i2c drivers from Linux 2.6.33
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Copyright (C) 2010 Thomas Langer <thomas.langer@lantiq.com>
25 * - no high speed support
26 * - supports only master mode
27 * - ten bit mode is not tested (no slave devices)
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/i2c.h>
35 #include <linux/clk.h>
36 #include <linux/errno.h>
37 #include <linux/sched.h>
38 #include <linux/err.h>
39 #include <linux/interrupt.h>
40 #include <linux/platform_device.h>
42 #include <linux/err.h>
43 #include <linux/gpio.h>
45 #include <lantiq_soc.h>
47 /* I2C Identification Register */
49 #define I2C_ID_ID_MASK 0x0000FF00
51 #define I2C_ID_ID_OFFSET 8
53 #define I2C_ID_REV_MASK 0x000000FF
55 #define I2C_ID_REV_OFFSET 0
57 /* I2C Error Interrupt Request Source Status Register */
59 #define I2C_ERR_IRQSS_TXF_OFL 0x00000008
61 #define I2C_ERR_IRQSS_TXF_UFL 0x00000004
63 #define I2C_ERR_IRQSS_RXF_OFL 0x00000002
65 #define I2C_ERR_IRQSS_RXF_UFL 0x00000001
67 /* I2C Bus Status Register */
69 #define I2C_BUS_STAT_BS_MASK 0x00000003
70 /* I2C Bus is free. */
71 #define I2C_BUS_STAT_BS_FREE 0x00000000
73 * The device is working as master and has claimed the control
74 * on the I2C-bus (busy master).
76 #define I2C_BUS_STAT_BS_BM 0x00000002
78 /* I2C Interrupt Clear Register */
80 #define I2C_ICR_BREQ_INT_CLR 0x00000008
82 #define I2C_ICR_LBREQ_INT_CLR 0x00000004
84 /* I2C RUN Control Register */
86 #define I2C_RUN_CTRL_RUN_EN 0x00000001
88 /* I2C Kernel Clock Control Register */
90 #define I2C_CLC_RMC_OFFSET 8
92 #define I2C_IMSC_I2C_P_INT_EN 0x00000020
94 #define I2C_IMSC_I2C_ERR_INT_EN 0x00000010
96 #define I2C_IMSC_BREQ_INT_EN 0x00000008
98 #define I2C_IMSC_LBREQ_INT_EN 0x00000004
100 /* I2C Fractional Divider Configuration Register */
102 #define I2C_FDIV_CFG_INC_OFFSET 16
104 #define I2C_FDIV_CFG_DEC_OFFSET 0
106 /* I2C Fractional Divider (highspeed mode) Configuration Register */
108 #define I2C_FDIV_HIGH_CFG_INC_OFFSET 16
110 #define I2C_FDIV_HIGH_CFG_DEC_OFFSET 0
112 /* I2C Address Register */
114 #define I2C_ADDR_CFG_SOPE_EN 0x00200000
116 #define I2C_ADDR_CFG_SONA_EN 0x00100000
118 #define I2C_ADDR_CFG_MnS_EN 0x00080000
120 /* I2C Protocol Interrupt Request Source Status Register */
122 #define I2C_P_IRQSS_RX 0x00000040
124 #define I2C_P_IRQSS_TX_END 0x00000020
126 #define I2C_P_IRQSS_NACK 0x00000010
128 #define I2C_P_IRQSS_AL 0x00000008
130 /* I2C Raw Interrupt Status Register */
131 /* Read: Interrupt occurred. */
132 #define I2C_RIS_I2C_P_INT_INTOCC 0x00000020
133 /* Read: Interrupt occurred. */
134 #define I2C_RIS_I2C_ERR_INT_INTOCC 0x00000010
136 /* I2C End Data Control Register */
138 * Set End of Transmission - Note: Do not write '1' to this bit when bus is
139 * free. This will cause an abort after the first byte when a new transfer
142 #define I2C_ENDD_CTRL_SETEND 0x00000002
143 /* TX FIFO Flow Control */
144 #define I2C_FIFO_CFG_TXFC 0x00020000
145 /* RX FIFO Flow Control */
146 #define I2C_FIFO_CFG_RXFC 0x00010000
147 /* Word aligned (character alignment of four characters) */
148 #define I2C_FIFO_CFG_TXFA_TXFA2 0x00002000
149 /* Word aligned (character alignment of four characters) */
150 #define I2C_FIFO_CFG_RXFA_RXFA2 0x00000200
152 #define I2C_FIFO_CFG_TXBS_TXBS0 0x00000000
154 #define I2C_FIFO_CFG_RXBS_RXBS0 0x00000000
157 /* I2C register structure */
158 struct gpon_reg_i2c {
159 /* I2C Kernel Clock Control Register */
160 unsigned int clc; /* 0x00000000 */
162 unsigned int res_0; /* 0x00000004 */
163 /* I2C Identification Register */
164 unsigned int id; /* 0x00000008 */
166 unsigned int res_1; /* 0x0000000C */
168 * I2C RUN Control Register - This register enables and disables the I2C
169 * peripheral. Before enabling, the I2C has to be configured properly.
170 * After enabling no configuration is possible
172 unsigned int run_ctrl; /* 0x00000010 */
174 * I2C End Data Control Register - This register is used to either turn
175 * around the data transmission direction or to address another slave
176 * without sending a stop condition. Also the software can stop the
177 * slave-transmitter by sending a not-accolade when working as
178 * master-receiver or even stop data transmission immediately when
179 * operating as master-transmitter. The writing to the bits of this
180 * control register is only effective when in MASTER RECEIVES BYTES,
181 * MASTER TRANSMITS BYTES, MASTER RESTART or SLAVE RECEIVE BYTES state
183 unsigned int endd_ctrl; /* 0x00000014 */
185 * I2C Fractional Divider Configuration Register - These register is
186 * used to program the fractional divider of the I2C bus. Before the
187 * peripheral is switched on by setting the RUN-bit the two (fixed)
188 * values for the two operating frequencies are programmed into these
189 * (configuration) registers. The Register FDIV_HIGH_CFG has the same
190 * layout as I2C_FDIV_CFG.
192 unsigned int fdiv_cfg; /* 0x00000018 */
194 * I2C Fractional Divider (highspeed mode) Configuration Register
195 * These register is used to program the fractional divider of the I2C
196 * bus. Before the peripheral is switched on by setting the RUN-bit the
197 * two (fixed) values for the two operating frequencies are programmed
198 * into these (configuration) registers. The Register FDIV_CFG has the
199 * same layout as I2C_FDIV_CFG.
201 unsigned int fdiv_high_cfg; /* 0x0000001C */
202 /* I2C Address Configuration Register */
203 unsigned int addr_cfg; /* 0x00000020 */
205 * I2C Bus Status Register - This register gives a status information
206 * of the I2C. This additional information can be used by the software
207 * to start proper actions.
209 unsigned int bus_stat; /* 0x00000024 */
210 /* I2C FIFO Configuration Register */
211 unsigned int fifo_cfg; /* 0x00000028 */
212 /* I2C Maximum Received Packet Size Register */
213 unsigned int mrps_ctrl; /* 0x0000002C */
214 /* I2C Received Packet Size Status Register */
215 unsigned int rps_stat; /* 0x00000030 */
216 /* I2C Transmit Packet Size Register */
217 unsigned int tps_ctrl; /* 0x00000034 */
218 /* I2C Filled FIFO Stages Status Register */
219 unsigned int ffs_stat; /* 0x00000038 */
221 unsigned int res_2; /* 0x0000003C */
222 /* I2C Timing Configuration Register */
223 unsigned int tim_cfg; /* 0x00000040 */
225 unsigned int res_3[7]; /* 0x00000044 */
226 /* I2C Error Interrupt Request Source Mask Register */
227 unsigned int err_irqsm; /* 0x00000060 */
228 /* I2C Error Interrupt Request Source Status Register */
229 unsigned int err_irqss; /* 0x00000064 */
230 /* I2C Error Interrupt Request Source Clear Register */
231 unsigned int err_irqsc; /* 0x00000068 */
233 unsigned int res_4; /* 0x0000006C */
234 /* I2C Protocol Interrupt Request Source Mask Register */
235 unsigned int p_irqsm; /* 0x00000070 */
236 /* I2C Protocol Interrupt Request Source Status Register */
237 unsigned int p_irqss; /* 0x00000074 */
238 /* I2C Protocol Interrupt Request Source Clear Register */
239 unsigned int p_irqsc; /* 0x00000078 */
241 unsigned int res_5; /* 0x0000007C */
242 /* I2C Raw Interrupt Status Register */
243 unsigned int ris; /* 0x00000080 */
244 /* I2C Interrupt Mask Control Register */
245 unsigned int imsc; /* 0x00000084 */
246 /* I2C Masked Interrupt Status Register */
247 unsigned int mis; /* 0x00000088 */
248 /* I2C Interrupt Clear Register */
249 unsigned int icr; /* 0x0000008C */
250 /* I2C Interrupt Set Register */
251 unsigned int isr; /* 0x00000090 */
252 /* I2C DMA Enable Register */
253 unsigned int dmae; /* 0x00000094 */
255 unsigned int res_6[8154]; /* 0x00000098 */
256 /* I2C Transmit Data Register */
257 unsigned int txd; /* 0x00008000 */
259 unsigned int res_7[4095]; /* 0x00008004 */
260 /* I2C Receive Data Register */
261 unsigned int rxd; /* 0x0000C000 */
263 unsigned int res_8[4095]; /* 0x0000C004 */
266 /* mapping for access macros */
267 #define i2c ((struct gpon_reg_i2c *)priv->membase)
268 #define reg_r32(reg) __raw_readl(reg)
269 #define reg_w32(val, reg) __raw_writel(val, reg)
270 #define reg_w32_mask(clear, set, reg) \
271 reg_w32((reg_r32(reg) & ~(clear)) | (set), reg)
272 #define reg_r32_table(reg, idx) reg_r32(&((uint32_t *)®)[idx])
273 #define reg_w32_table(val, reg, idx) reg_w32(val, &((uint32_t *)®)[idx])
275 #define i2c_r32(reg) reg_r32(&i2c->reg)
276 #define i2c_w32(val, reg) reg_w32(val, &i2c->reg)
277 #define i2c_w32_mask(clear, set, reg) reg_w32_mask(clear, set, &i2c->reg)
279 #define DRV_NAME "i2c-falcon"
280 #define DRV_VERSION "1.01"
282 #define FALCON_I2C_BUSY_TIMEOUT 20 /* ms */
285 #define FALCON_I2C_XFER_TIMEOUT (25 * HZ)
287 #define FALCON_I2C_XFER_TIMEOUT HZ
289 #if defined(DEBUG) && 0
290 #define PRINTK(arg...) pr_info(arg)
292 #define PRINTK(arg...) do {} while (0)
295 #define FALCON_I2C_IMSC_DEFAULT_MASK (I2C_IMSC_I2C_P_INT_EN | \
296 I2C_IMSC_I2C_ERR_INT_EN)
298 #define FALCON_I2C_ARB_LOST (1 << 0)
299 #define FALCON_I2C_NACK (1 << 1)
300 #define FALCON_I2C_RX_UFL (1 << 2)
301 #define FALCON_I2C_RX_OFL (1 << 3)
302 #define FALCON_I2C_TX_UFL (1 << 4)
303 #define FALCON_I2C_TX_OFL (1 << 5)
309 FALCON_I2C_MODE_100 = 1,
310 FALCON_I2C_MODE_400 = 2,
311 FALCON_I2C_MODE_3400 = 3
312 } mode; /* current speed mode */
314 struct clk *clk; /* clock input for i2c hardware block */
315 struct gpon_reg_i2c __iomem *membase; /* base of mapped registers */
316 int irq_lb, irq_b, irq_err, irq_p; /* last burst, burst, error,
319 struct i2c_adapter adap;
322 struct completion cmd_complete;
324 /* message transfer data */
325 /* current message */
326 struct i2c_msg *current_msg;
327 /* number of messages to handle */
331 /* remaining length of current buffer */
333 /* error status of the current transfer */
336 /* master status codes */
339 STATUS_ADDR, /* address phase */
347 static irqreturn_t falcon_i2c_isr(int irq, void *dev_id);
349 static inline void enable_burst_irq(struct falcon_i2c *priv)
351 i2c_w32_mask(0, I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, imsc);
353 static inline void disable_burst_irq(struct falcon_i2c *priv)
355 i2c_w32_mask(I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, 0, imsc);
358 static void prepare_msg_send_addr(struct falcon_i2c *priv)
360 struct i2c_msg *msg = priv->current_msg;
361 int rd = !!(msg->flags & I2C_M_RD);
362 u16 addr = msg->addr;
365 priv->msg_buf = msg->buf;
366 priv->msg_buf_len = msg->len;
368 priv->status = STATUS_READ;
370 priv->status = STATUS_WRITE;
372 /* send slave address */
373 if (msg->flags & I2C_M_TEN) {
374 i2c_w32(0xf0 | ((addr & 0x300) >> 7) | rd, txd);
375 i2c_w32(addr & 0xff, txd);
377 i2c_w32((addr & 0x7f) << 1 | rd, txd);
380 static void set_tx_len(struct falcon_i2c *priv)
382 struct i2c_msg *msg = priv->current_msg;
383 int len = (msg->flags & I2C_M_TEN) ? 2 : 1;
385 PRINTK("set_tx_len %cX\n", (msg->flags & I2C_M_RD) ? ('R') : ('T'));
387 priv->status = STATUS_ADDR;
389 if (!(msg->flags & I2C_M_RD)) {
392 /* set maximum received packet size (before rx int!) */
393 i2c_w32(msg->len, mrps_ctrl);
395 i2c_w32(len, tps_ctrl);
396 enable_burst_irq(priv);
399 static int falcon_i2c_hw_init(struct i2c_adapter *adap)
401 struct falcon_i2c *priv = i2c_get_adapdata(adap);
404 i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
407 /* set normal operation clock divider */
408 i2c_w32(1 << I2C_CLC_RMC_OFFSET, clc);
410 /* for debugging a higher divider value! */
411 i2c_w32(0xF0 << I2C_CLC_RMC_OFFSET, clc);
415 if (priv->mode == FALCON_I2C_MODE_100) {
416 dev_dbg(priv->dev, "set standard mode (100 kHz)\n");
417 i2c_w32(0, fdiv_high_cfg);
418 i2c_w32((1 << I2C_FDIV_CFG_INC_OFFSET) |
419 (499 << I2C_FDIV_CFG_DEC_OFFSET),
421 } else if (priv->mode == FALCON_I2C_MODE_400) {
422 dev_dbg(priv->dev, "set fast mode (400 kHz)\n");
423 i2c_w32(0, fdiv_high_cfg);
424 i2c_w32((1 << I2C_FDIV_CFG_INC_OFFSET) |
425 (124 << I2C_FDIV_CFG_DEC_OFFSET),
427 } else if (priv->mode == FALCON_I2C_MODE_3400) {
428 dev_dbg(priv->dev, "set high mode (3.4 MHz)\n");
429 i2c_w32(0, fdiv_cfg);
430 /* TODO recalculate value for 100MHz input */
431 i2c_w32((41 << I2C_FDIV_HIGH_CFG_INC_OFFSET) |
432 (152 << I2C_FDIV_HIGH_CFG_DEC_OFFSET),
435 dev_warn(priv->dev, "unknown mode\n");
440 i2c_w32(I2C_FIFO_CFG_TXFC | /* tx fifo as flow controller */
441 I2C_FIFO_CFG_RXFC | /* rx fifo as flow controller */
442 I2C_FIFO_CFG_TXFA_TXFA2 | /* tx fifo 4-byte aligned */
443 I2C_FIFO_CFG_RXFA_RXFA2 | /* rx fifo 4-byte aligned */
444 I2C_FIFO_CFG_TXBS_TXBS0 | /* tx fifo burst size is 1 word */
445 I2C_FIFO_CFG_RXBS_RXBS0, /* rx fifo burst size is 1 word */
448 /* configure address */
449 i2c_w32(I2C_ADDR_CFG_SOPE_EN | /* generate stop when no more data
451 I2C_ADDR_CFG_SONA_EN | /* generate stop when NA received */
452 I2C_ADDR_CFG_MnS_EN | /* we are master device */
453 0, /* our slave address (not used!) */
457 i2c_w32_mask(0, I2C_RUN_CTRL_RUN_EN, run_ctrl);
462 static int falcon_i2c_wait_bus_not_busy(struct falcon_i2c *priv)
464 int timeout = FALCON_I2C_BUSY_TIMEOUT;
466 while ((i2c_r32(bus_stat) & I2C_BUS_STAT_BS_MASK)
467 != I2C_BUS_STAT_BS_FREE) {
469 dev_warn(priv->dev, "timeout waiting for bus ready\n");
479 static void falcon_i2c_tx(struct falcon_i2c *priv, int last)
481 if (priv->msg_buf_len && priv->msg_buf) {
482 i2c_w32(*priv->msg_buf, txd);
484 if (--priv->msg_buf_len)
487 priv->msg_buf = NULL;
492 disable_burst_irq(priv);
495 static void falcon_i2c_rx(struct falcon_i2c *priv, int last)
497 u32 fifo_stat, timeout;
498 if (priv->msg_buf_len && priv->msg_buf) {
501 fifo_stat = i2c_r32(ffs_stat);
502 } while (!fifo_stat && --timeout);
505 PRINTK("\nrx timeout\n");
509 *priv->msg_buf = i2c_r32(rxd);
510 if (--priv->msg_buf_len)
513 priv->msg_buf = NULL;
518 fifo_stat = i2c_r32(ffs_stat);
520 /* do not read more than burst size, otherwise no "last
521 burst" is generated and the transaction is blocked! */
530 disable_burst_irq(priv);
532 if (priv->status == STATUS_READ_END) {
533 /* do the STATUS_STOP and complete() here, as sometimes
534 the tx_end is already seen before this is finished */
535 priv->status = STATUS_STOP;
536 complete(&priv->cmd_complete);
538 i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
539 priv->status = STATUS_READ_END;
544 static void falcon_i2c_xfer_init(struct falcon_i2c *priv)
546 /* enable interrupts */
547 i2c_w32(FALCON_I2C_IMSC_DEFAULT_MASK, imsc);
549 /* trigger transfer of first msg */
553 static void dump_msgs(struct i2c_msg msgs[], int num, int rx)
557 pr_info("Messages %d %s\n", num, rx ? "out" : "in");
558 for (i = 0; i < num; i++) {
559 pr_info("%2d %cX Msg(%d) addr=0x%X: ", i,
560 (msgs[i].flags & I2C_M_RD) ? ('R') : ('T'),
561 msgs[i].len, msgs[i].addr);
562 if (!(msgs[i].flags & I2C_M_RD) || rx) {
563 for (j = 0; j < msgs[i].len; j++)
564 printk("%02X ", msgs[i].buf[j]);
571 static void falcon_i2c_release_bus(struct falcon_i2c *priv)
573 if ((i2c_r32(bus_stat) & I2C_BUS_STAT_BS_MASK) == I2C_BUS_STAT_BS_BM)
574 i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
577 static int falcon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
580 struct falcon_i2c *priv = i2c_get_adapdata(adap);
583 dev_dbg(priv->dev, "xfer %u messages\n", num);
584 dump_msgs(msgs, num, 0);
586 mutex_lock(&priv->mutex);
588 INIT_COMPLETION(priv->cmd_complete);
589 priv->current_msg = msgs;
590 priv->msgs_num = num;
592 priv->status = STATUS_IDLE;
594 /* wait for the bus to become ready */
595 ret = falcon_i2c_wait_bus_not_busy(priv);
599 while (priv->msgs_num) {
600 /* start the transfers */
601 falcon_i2c_xfer_init(priv);
603 /* wait for transfers to complete */
604 ret = wait_for_completion_interruptible_timeout(
605 &priv->cmd_complete, FALCON_I2C_XFER_TIMEOUT);
607 dev_err(priv->dev, "controller timed out\n");
608 falcon_i2c_hw_init(adap);
615 if (priv->msg_err & FALCON_I2C_NACK)
621 if (--priv->msgs_num)
628 falcon_i2c_release_bus(priv);
630 mutex_unlock(&priv->mutex);
633 dump_msgs(msgs, num, 1);
635 PRINTK("XFER ret %d\n", ret);
639 static irqreturn_t falcon_i2c_isr_burst(int irq, void *dev_id)
641 struct falcon_i2c *priv = dev_id;
642 struct i2c_msg *msg = priv->current_msg;
643 int last = (irq == priv->irq_lb);
650 if (msg->flags & I2C_M_RD) {
651 switch (priv->status) {
654 prepare_msg_send_addr(priv);
655 disable_burst_irq(priv);
658 case STATUS_READ_END:
660 falcon_i2c_rx(priv, last);
663 disable_burst_irq(priv);
664 PRINTK("Status R %d\n", priv->status);
668 switch (priv->status) {
671 prepare_msg_send_addr(priv);
675 falcon_i2c_tx(priv, last);
678 disable_burst_irq(priv);
679 PRINTK("Status W %d\n", priv->status);
684 i2c_w32(I2C_ICR_BREQ_INT_CLR | I2C_ICR_LBREQ_INT_CLR, icr);
688 static void falcon_i2c_isr_prot(struct falcon_i2c *priv)
690 u32 i_pro = i2c_r32(p_irqss);
694 /* not acknowledge */
695 if (i_pro & I2C_P_IRQSS_NACK) {
696 priv->msg_err |= FALCON_I2C_NACK;
700 /* arbitration lost */
701 if (i_pro & I2C_P_IRQSS_AL) {
702 priv->msg_err |= FALCON_I2C_ARB_LOST;
705 /* tx -> rx switch */
706 if (i_pro & I2C_P_IRQSS_RX)
710 if (i_pro & I2C_P_IRQSS_TX_END)
714 if (!priv->msg_err) {
715 /* tx -> rx switch */
716 if (i_pro & I2C_P_IRQSS_RX) {
717 priv->status = STATUS_READ;
718 enable_burst_irq(priv);
720 if (i_pro & I2C_P_IRQSS_TX_END) {
721 if (priv->status == STATUS_READ)
722 priv->status = STATUS_READ_END;
724 disable_burst_irq(priv);
725 priv->status = STATUS_STOP;
730 i2c_w32(i_pro, p_irqsc);
733 static irqreturn_t falcon_i2c_isr(int irq, void *dev_id)
735 u32 i_raw, i_err = 0;
736 struct falcon_i2c *priv = dev_id;
738 i_raw = i2c_r32(mis);
739 PRINTK("i_raw 0x%08X\n", i_raw);
741 /* error interrupt */
742 if (i_raw & I2C_RIS_I2C_ERR_INT_INTOCC) {
743 i_err = i2c_r32(err_irqss);
744 PRINTK("i_err 0x%08X bus_stat 0x%04X\n",
745 i_err, i2c_r32(bus_stat));
747 /* tx fifo overflow (8) */
748 if (i_err & I2C_ERR_IRQSS_TXF_OFL)
749 priv->msg_err |= FALCON_I2C_TX_OFL;
751 /* tx fifo underflow (4) */
752 if (i_err & I2C_ERR_IRQSS_TXF_UFL)
753 priv->msg_err |= FALCON_I2C_TX_UFL;
755 /* rx fifo overflow (2) */
756 if (i_err & I2C_ERR_IRQSS_RXF_OFL)
757 priv->msg_err |= FALCON_I2C_RX_OFL;
759 /* rx fifo underflow (1) */
760 if (i_err & I2C_ERR_IRQSS_RXF_UFL)
761 priv->msg_err |= FALCON_I2C_RX_UFL;
763 i2c_w32(i_err, err_irqsc);
766 /* protocol interrupt */
767 if (i_raw & I2C_RIS_I2C_P_INT_INTOCC)
768 falcon_i2c_isr_prot(priv);
770 if ((priv->msg_err) || (priv->status == STATUS_STOP))
771 complete(&priv->cmd_complete);
776 static u32 falcon_i2c_functionality(struct i2c_adapter *adap)
778 return I2C_FUNC_I2C |
779 I2C_FUNC_10BIT_ADDR |
783 static struct i2c_algorithm falcon_i2c_algorithm = {
784 .master_xfer = falcon_i2c_xfer,
785 .functionality = falcon_i2c_functionality,
788 static int __devinit falcon_i2c_probe(struct platform_device *pdev)
791 struct falcon_i2c *priv;
792 struct i2c_adapter *adap;
793 struct resource *mmres, *ioarea,
794 *irqres_lb, *irqres_b, *irqres_err, *irqres_p;
797 dev_dbg(&pdev->dev, "probing\n");
799 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
800 irqres_lb = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
802 irqres_b = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "i2c_b");
803 irqres_err = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
805 irqres_p = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "i2c_p");
807 if (!mmres || !irqres_lb || !irqres_b || !irqres_err || !irqres_p) {
808 dev_err(&pdev->dev, "no resources\n");
814 dev_err(&pdev->dev, "failed to get fpi clk\n");
818 if (clk_get_rate(clk) != 100000000) {
819 dev_err(&pdev->dev, "input clock is not 100MHz\n");
822 clk = clk_get(&pdev->dev, NULL);
824 dev_err(&pdev->dev, "failed to get i2c clk\n");
828 /* allocate private data */
829 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
831 dev_err(&pdev->dev, "can't allocate private data\n");
836 i2c_set_adapdata(adap, priv);
837 adap->owner = THIS_MODULE;
838 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
839 strlcpy(adap->name, DRV_NAME "-adapter", sizeof(adap->name));
840 adap->algo = &falcon_i2c_algorithm;
842 priv->mode = FALCON_I2C_MODE_100;
844 priv->dev = &pdev->dev;
846 init_completion(&priv->cmd_complete);
847 mutex_init(&priv->mutex);
849 if (ltq_gpio_request(&pdev->dev, 107, 0, 0, DRV_NAME":sda") ||
850 ltq_gpio_request(&pdev->dev, 108, 0, 0, DRV_NAME":scl"))
852 dev_err(&pdev->dev, "I2C gpios not available\n");
857 ioarea = request_mem_region(mmres->start, resource_size(mmres),
860 if (ioarea == NULL) {
861 dev_err(&pdev->dev, "I2C region already claimed\n");
867 priv->membase = ioremap_nocache(mmres->start & ~KSEG1,
868 resource_size(mmres));
869 if (priv->membase == NULL) {
871 goto err_release_region;
874 priv->irq_lb = irqres_lb->start;
875 ret = request_irq(priv->irq_lb, falcon_i2c_isr_burst, IRQF_DISABLED,
876 irqres_lb->name, priv);
878 dev_err(&pdev->dev, "can't get last burst IRQ %d\n",
884 priv->irq_b = irqres_b->start;
885 ret = request_irq(priv->irq_b, falcon_i2c_isr_burst, IRQF_DISABLED,
886 irqres_b->name, priv);
888 dev_err(&pdev->dev, "can't get burst IRQ %d\n",
891 goto err_free_lb_irq;
894 priv->irq_err = irqres_err->start;
895 ret = request_irq(priv->irq_err, falcon_i2c_isr, IRQF_DISABLED,
896 irqres_err->name, priv);
898 dev_err(&pdev->dev, "can't get error IRQ %d\n",
904 priv->irq_p = irqres_p->start;
905 ret = request_irq(priv->irq_p, falcon_i2c_isr, IRQF_DISABLED,
906 irqres_p->name, priv);
908 dev_err(&pdev->dev, "can't get protocol IRQ %d\n",
911 goto err_free_err_irq;
914 dev_dbg(&pdev->dev, "mapped io-space to %p\n", priv->membase);
915 dev_dbg(&pdev->dev, "use IRQs %d, %d, %d, %d\n", irqres_lb->start,
916 irqres_b->start, irqres_err->start, irqres_p->start);
918 /* add our adapter to the i2c stack */
919 ret = i2c_add_numbered_adapter(adap);
921 dev_err(&pdev->dev, "can't register I2C adapter\n");
925 platform_set_drvdata(pdev, priv);
926 i2c_set_adapdata(adap, priv);
928 /* print module version information */
929 dev_dbg(&pdev->dev, "module id=%u revision=%u\n",
930 (i2c_r32(id) & I2C_ID_ID_MASK) >> I2C_ID_ID_OFFSET,
931 (i2c_r32(id) & I2C_ID_REV_MASK) >> I2C_ID_REV_OFFSET);
934 ret = falcon_i2c_hw_init(adap);
936 dev_err(&pdev->dev, "can't configure adapter\n");
937 goto err_remove_adapter;
940 dev_info(&pdev->dev, "version %s\n", DRV_VERSION);
945 i2c_del_adapter(adap);
946 platform_set_drvdata(pdev, NULL);
949 free_irq(priv->irq_p, priv);
952 free_irq(priv->irq_err, priv);
955 free_irq(priv->irq_b, priv);
958 free_irq(priv->irq_lb, priv);
961 iounmap(priv->membase);
964 release_mem_region(mmres->start, resource_size(mmres));
976 static int __devexit falcon_i2c_remove(struct platform_device *pdev)
978 struct falcon_i2c *priv = platform_get_drvdata(pdev);
979 struct resource *mmres;
982 i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
985 platform_set_drvdata(pdev, NULL);
986 i2c_del_adapter(&priv->adap);
988 free_irq(priv->irq_lb, priv);
989 free_irq(priv->irq_b, priv);
990 free_irq(priv->irq_err, priv);
991 free_irq(priv->irq_p, priv);
993 iounmap(priv->membase);
1000 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1001 release_mem_region(mmres->start, resource_size(mmres));
1003 dev_dbg(&pdev->dev, "removed\n");
1008 static struct platform_driver falcon_i2c_driver = {
1009 .probe = falcon_i2c_probe,
1010 .remove = __devexit_p(falcon_i2c_remove),
1013 .owner = THIS_MODULE,
1017 static int __init falcon_i2c_init(void)
1021 ret = platform_driver_register(&falcon_i2c_driver);
1024 pr_debug(DRV_NAME ": can't register platform driver\n");
1029 static void __exit falcon_i2c_exit(void)
1031 platform_driver_unregister(&falcon_i2c_driver);
1034 module_init(falcon_i2c_init);
1035 module_exit(falcon_i2c_exit);
1037 MODULE_DESCRIPTION("Lantiq FALC(tm) ON - I2C bus adapter");
1038 MODULE_ALIAS("platform:" DRV_NAME);
1039 MODULE_LICENSE("GPL");
1040 MODULE_VERSION(DRV_VERSION);