1 From f2ac37c0a5297ca4663da9e4328c77736504b484 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 20 May 2012 00:42:39 +0200
4 Subject: [PATCH 113/123] I2C: MIPS: lantiq: add FALC-ON i2c bus master
6 This patch adds the driver needed to make the I2C bus work on FALC-ON SoCs.
8 Signed-off-by: Thomas Langer <thomas.langer@lantiq.com>
9 Signed-off-by: John Crispin <blogic@openwrt.org>
11 drivers/i2c/busses/Kconfig | 10 +
12 drivers/i2c/busses/Makefile | 1 +
13 drivers/i2c/busses/i2c-lantiq.c | 747 +++++++++++++++++++++++++++++++++++++++
14 drivers/i2c/busses/i2c-lantiq.h | 234 ++++++++++++
15 4 files changed, 992 insertions(+)
16 create mode 100644 drivers/i2c/busses/i2c-lantiq.c
17 create mode 100644 drivers/i2c/busses/i2c-lantiq.h
19 diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
20 index e9df461..e03f821 100644
21 --- a/drivers/i2c/busses/Kconfig
22 +++ b/drivers/i2c/busses/Kconfig
23 @@ -460,6 +460,16 @@ config I2C_IOP3XX
24 This driver can also be built as a module. If so, the module
25 will be called i2c-iop3xx.
28 + tristate "Lantiq I2C interface"
29 + depends on LANTIQ && SOC_FALCON
31 + If you say yes to this option, support will be included for the
34 + This driver can also be built as a module. If so, the module
35 + will be called i2c-lantiq.
38 tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx"
40 diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
41 index 395b516..74a789a 100644
42 --- a/drivers/i2c/busses/Makefile
43 +++ b/drivers/i2c/busses/Makefile
44 @@ -45,6 +45,7 @@ obj-$(CONFIG_I2C_IBM_IIC) += i2c-ibm_iic.o
45 obj-$(CONFIG_I2C_IMX) += i2c-imx.o
46 obj-$(CONFIG_I2C_INTEL_MID) += i2c-intel-mid.o
47 obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o
48 +obj-$(CONFIG_I2C_LANTIQ) += i2c-lantiq.o
49 obj-$(CONFIG_I2C_MPC) += i2c-mpc.o
50 obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o
51 obj-$(CONFIG_I2C_MXS) += i2c-mxs.o
52 diff --git a/drivers/i2c/busses/i2c-lantiq.c b/drivers/i2c/busses/i2c-lantiq.c
54 index 0000000..9a5f58b
56 +++ b/drivers/i2c/busses/i2c-lantiq.c
60 + * Lantiq I2C bus adapter
62 + * Parts based on i2c-designware.c and other i2c drivers from Linux 2.6.33
64 + * This program is free software; you can redistribute it and/or modify
65 + * it under the terms of the GNU General Public License as published by
66 + * the Free Software Foundation; either version 2 of the License, or
67 + * (at your option) any later version.
69 + * This program is distributed in the hope that it will be useful,
70 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
71 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 + * GNU General Public License for more details.
74 + * You should have received a copy of the GNU General Public License
75 + * along with this program; if not, write to the Free Software
76 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
78 + * Copyright (C) 2012 Thomas Langer <thomas.langer@lantiq.com>
81 +#include <linux/kernel.h>
82 +#include <linux/module.h>
83 +#include <linux/delay.h>
84 +#include <linux/slab.h> /* for kzalloc, kfree */
85 +#include <linux/i2c.h>
86 +#include <linux/errno.h>
87 +#include <linux/completion.h>
88 +#include <linux/interrupt.h>
89 +#include <linux/platform_device.h>
90 +#include <linux/io.h>
91 +#include <linux/of_irq.h>
92 +#include <linux/of_i2c.h>
94 +#include <lantiq_soc.h>
95 +#include "i2c-lantiq.h"
99 + * - no high speed support
100 + * - ten bit mode is not tested (no slave devices)
104 +#define i2c_r32(reg) \
105 + __raw_readl(&(priv->membase)->reg)
106 +#define i2c_w32(val, reg) \
107 + __raw_writel(val, &(priv->membase)->reg)
108 +#define i2c_w32_mask(clear, set, reg) \
109 + i2c_w32((i2c_r32(reg) & ~(clear)) | (set), reg)
111 +#define DRV_NAME "i2c-lantiq"
112 +#define DRV_VERSION "1.00"
114 +#define LTQ_I2C_BUSY_TIMEOUT 20 /* ms */
117 +#define LTQ_I2C_XFER_TIMEOUT (25*HZ)
119 +#define LTQ_I2C_XFER_TIMEOUT HZ
122 +#define LTQ_I2C_IMSC_DEFAULT_MASK (I2C_IMSC_I2C_P_INT_EN | \
123 + I2C_IMSC_I2C_ERR_INT_EN)
125 +#define LTQ_I2C_ARB_LOST (1 << 0)
126 +#define LTQ_I2C_NACK (1 << 1)
127 +#define LTQ_I2C_RX_UFL (1 << 2)
128 +#define LTQ_I2C_RX_OFL (1 << 3)
129 +#define LTQ_I2C_TX_UFL (1 << 4)
130 +#define LTQ_I2C_TX_OFL (1 << 5)
133 + struct mutex mutex;
136 + /* active clock settings */
137 + unsigned int input_clock; /* clock input for i2c hardware block */
138 + unsigned int i2c_clock; /* approximated bus clock in kHz */
140 + struct clk *clk_gate;
141 + struct clk *clk_input;
144 + /* resources (memory and interrupts) */
145 + int irq_lb; /* last burst irq */
147 + struct lantiq_reg_i2c __iomem *membase; /* base of mapped registers */
149 + struct i2c_adapter adap;
150 + struct device *dev;
152 + struct completion cmd_complete;
155 + /* message transfer data */
156 + struct i2c_msg *current_msg; /* current message */
157 + int msgs_num; /* number of messages to handle */
158 + u8 *msg_buf; /* current buffer */
159 + u32 msg_buf_len; /* remaining length of current buffer */
160 + int msg_err; /* error status of the current transfer */
163 + /* master status codes */
166 + STATUS_ADDR, /* address phase */
174 +static irqreturn_t ltq_i2c_isr(int irq, void *dev_id);
176 +static inline void enable_burst_irq(struct ltq_i2c *priv)
178 + i2c_w32_mask(0, I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, imsc);
180 +static inline void disable_burst_irq(struct ltq_i2c *priv)
182 + i2c_w32_mask(I2C_IMSC_LBREQ_INT_EN | I2C_IMSC_BREQ_INT_EN, 0, imsc);
185 +static void prepare_msg_send_addr(struct ltq_i2c *priv)
187 + struct i2c_msg *msg = priv->current_msg;
188 + int rd = !!(msg->flags & I2C_M_RD); /* extends to 0 or 1 */
189 + u16 addr = msg->addr;
192 + priv->msg_buf = msg->buf;
193 + priv->msg_buf_len = msg->len;
195 + priv->status = STATUS_READ;
197 + priv->status = STATUS_WRITE;
199 + /* send slave address */
200 + if (msg->flags & I2C_M_TEN) {
201 + i2c_w32(0xf0 | ((addr & 0x300) >> 7) | rd, txd);
202 + i2c_w32(addr & 0xff, txd);
204 + i2c_w32((addr & 0x7f) << 1 | rd, txd);
208 +static void ltq_i2c_set_tx_len(struct ltq_i2c *priv)
210 + struct i2c_msg *msg = priv->current_msg;
211 + int len = (msg->flags & I2C_M_TEN) ? 2 : 1;
213 + pr_debug("set_tx_len %cX\n", (msg->flags & I2C_M_RD) ? 'R' : 'T');
215 + priv->status = STATUS_ADDR;
217 + if (!(msg->flags & I2C_M_RD))
220 + /* set maximum received packet size (before rx int!) */
221 + i2c_w32(msg->len, mrps_ctrl);
222 + i2c_w32(len, tps_ctrl);
223 + enable_burst_irq(priv);
226 +static int ltq_i2c_hw_set_clock(struct i2c_adapter *adap)
228 + struct ltq_i2c *priv = i2c_get_adapdata(adap);
229 + unsigned int input_clock = clk_get_rate(priv->clk_input);
232 + /* clock changed? */
233 + if (priv->input_clock == input_clock)
237 + * this formula is only an approximation, found by the recommended
238 + * values in the "I2C Architecture Specification 1.7.1"
240 + dec = input_clock / (priv->i2c_clock * 2);
244 + i2c_w32(0, fdiv_high_cfg);
245 + i2c_w32((inc << I2C_FDIV_CFG_INC_OFFSET) |
246 + (dec << I2C_FDIV_CFG_DEC_OFFSET),
249 + dev_info(priv->dev, "setup clocks (in %d kHz, bus %d kHz, dec=%d)\n",
250 + input_clock, priv->i2c_clock, dec);
252 + priv->input_clock = input_clock;
256 +static int ltq_i2c_hw_init(struct i2c_adapter *adap)
259 + struct ltq_i2c *priv = i2c_get_adapdata(adap);
262 + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
265 + /* set normal operation clock divider */
266 + i2c_w32(1 << I2C_CLC_RMC_OFFSET, clc);
268 + /* for debugging a higher divider value! */
269 + i2c_w32(0xF0 << I2C_CLC_RMC_OFFSET, clc);
273 + ret = ltq_i2c_hw_set_clock(adap);
275 + dev_warn(priv->dev, "invalid clock settings\n");
279 + /* configure fifo */
280 + i2c_w32(I2C_FIFO_CFG_TXFC | /* tx fifo as flow controller */
281 + I2C_FIFO_CFG_RXFC | /* rx fifo as flow controller */
282 + I2C_FIFO_CFG_TXFA_TXFA2 | /* tx fifo 4-byte aligned */
283 + I2C_FIFO_CFG_RXFA_RXFA2 | /* rx fifo 4-byte aligned */
284 + I2C_FIFO_CFG_TXBS_TXBS0 | /* tx fifo burst size is 1 word */
285 + I2C_FIFO_CFG_RXBS_RXBS0, /* rx fifo burst size is 1 word */
288 + /* configure address */
289 + i2c_w32(I2C_ADDR_CFG_SOPE_EN | /* generate stop when no more data in
291 + I2C_ADDR_CFG_SONA_EN | /* generate stop when NA received */
292 + I2C_ADDR_CFG_MnS_EN | /* we are master device */
293 + 0, /* our slave address (not used!) */
297 + i2c_w32_mask(0, I2C_RUN_CTRL_RUN_EN, run_ctrl);
302 +static int ltq_i2c_wait_bus_not_busy(struct ltq_i2c *priv)
304 + unsigned long timeout;
306 + timeout = jiffies + msecs_to_jiffies(LTQ_I2C_BUSY_TIMEOUT);
309 + u32 stat = i2c_r32(bus_stat);
311 + if ((stat & I2C_BUS_STAT_BS_MASK) == I2C_BUS_STAT_BS_FREE)
315 + } while (!time_after_eq(jiffies, timeout));
317 + dev_err(priv->dev, "timeout waiting for bus ready\n");
321 +static void ltq_i2c_tx(struct ltq_i2c *priv, int last)
323 + if (priv->msg_buf_len && priv->msg_buf) {
324 + i2c_w32(*priv->msg_buf, txd);
326 + if (--priv->msg_buf_len)
329 + priv->msg_buf = NULL;
335 + disable_burst_irq(priv);
338 +static void ltq_i2c_rx(struct ltq_i2c *priv, int last)
340 + u32 fifo_stat, timeout;
341 + if (priv->msg_buf_len && priv->msg_buf) {
344 + fifo_stat = i2c_r32(ffs_stat);
345 + } while (!fifo_stat && --timeout);
348 + pr_debug("\nrx timeout\n");
351 + while (fifo_stat) {
352 + *priv->msg_buf = i2c_r32(rxd);
353 + if (--priv->msg_buf_len) {
356 + priv->msg_buf = NULL;
361 + * do not read more than burst size, otherwise no "last
362 + * burst" is generated and the transaction is blocked!
371 + disable_burst_irq(priv);
373 + if (priv->status == STATUS_READ_END) {
375 + * do the STATUS_STOP and complete() here, as sometimes
376 + * the tx_end is already seen before this is finished
378 + priv->status = STATUS_STOP;
379 + complete(&priv->cmd_complete);
381 + i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
382 + priv->status = STATUS_READ_END;
387 +static void ltq_i2c_xfer_init(struct ltq_i2c *priv)
389 + /* enable interrupts */
390 + i2c_w32(LTQ_I2C_IMSC_DEFAULT_MASK, imsc);
392 + /* trigger transfer of first msg */
393 + ltq_i2c_set_tx_len(priv);
396 +static void dump_msgs(struct i2c_msg msgs[], int num, int rx)
400 + pr_debug("Messages %d %s\n", num, rx ? "out" : "in");
401 + for (i = 0; i < num; i++) {
402 + pr_debug("%2d %cX Msg(%d) addr=0x%X: ", i,
403 + (msgs[i].flags & I2C_M_RD) ? 'R' : 'T',
404 + msgs[i].len, msgs[i].addr);
405 + if (!(msgs[i].flags & I2C_M_RD) || rx) {
406 + for (j = 0; j < msgs[i].len; j++)
407 + pr_debug("%02X ", msgs[i].buf[j]);
414 +static void ltq_i2c_release_bus(struct ltq_i2c *priv)
416 + if ((i2c_r32(bus_stat) & I2C_BUS_STAT_BS_MASK) == I2C_BUS_STAT_BS_BM)
417 + i2c_w32(I2C_ENDD_CTRL_SETEND, endd_ctrl);
420 +static int ltq_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
423 + struct ltq_i2c *priv = i2c_get_adapdata(adap);
426 + dev_dbg(priv->dev, "xfer %u messages\n", num);
427 + dump_msgs(msgs, num, 0);
429 + mutex_lock(&priv->mutex);
431 + INIT_COMPLETION(priv->cmd_complete);
432 + priv->current_msg = msgs;
433 + priv->msgs_num = num;
435 + priv->status = STATUS_IDLE;
437 + /* wait for the bus to become ready */
438 + ret = ltq_i2c_wait_bus_not_busy(priv);
442 + while (priv->msgs_num) {
443 + /* start the transfers */
444 + ltq_i2c_xfer_init(priv);
446 + /* wait for transfers to complete */
447 + ret = wait_for_completion_interruptible_timeout(
448 + &priv->cmd_complete, LTQ_I2C_XFER_TIMEOUT);
450 + dev_err(priv->dev, "controller timed out\n");
451 + ltq_i2c_hw_init(adap);
454 + } else if (ret < 0)
457 + if (priv->msg_err) {
458 + if (priv->msg_err & LTQ_I2C_NACK)
464 + if (--priv->msgs_num)
465 + priv->current_msg++;
471 + ltq_i2c_release_bus(priv);
473 + mutex_unlock(&priv->mutex);
476 + dump_msgs(msgs, num, 1);
478 + pr_debug("XFER ret %d\n", ret);
482 +static irqreturn_t ltq_i2c_isr_burst(int irq, void *dev_id)
484 + struct ltq_i2c *priv = dev_id;
485 + struct i2c_msg *msg = priv->current_msg;
486 + int last = (irq == priv->irq_lb);
493 + if (msg->flags & I2C_M_RD) {
494 + switch (priv->status) {
497 + prepare_msg_send_addr(priv);
498 + disable_burst_irq(priv);
501 + case STATUS_READ_END:
503 + ltq_i2c_rx(priv, last);
506 + disable_burst_irq(priv);
507 + pr_warn("Status R %d\n", priv->status);
511 + switch (priv->status) {
514 + prepare_msg_send_addr(priv);
518 + ltq_i2c_tx(priv, last);
521 + disable_burst_irq(priv);
522 + pr_warn("Status W %d\n", priv->status);
527 + i2c_w32(I2C_ICR_BREQ_INT_CLR | I2C_ICR_LBREQ_INT_CLR, icr);
528 + return IRQ_HANDLED;
531 +static void ltq_i2c_isr_prot(struct ltq_i2c *priv)
533 + u32 i_pro = i2c_r32(p_irqss);
537 + /* not acknowledge */
538 + if (i_pro & I2C_P_IRQSS_NACK) {
539 + priv->msg_err |= LTQ_I2C_NACK;
543 + /* arbitration lost */
544 + if (i_pro & I2C_P_IRQSS_AL) {
545 + priv->msg_err |= LTQ_I2C_ARB_LOST;
546 + pr_debug(" arb-lost");
548 + /* tx -> rx switch */
549 + if (i_pro & I2C_P_IRQSS_RX)
553 + if (i_pro & I2C_P_IRQSS_TX_END)
554 + pr_debug(" txend");
557 + if (!priv->msg_err) {
558 + /* tx -> rx switch */
559 + if (i_pro & I2C_P_IRQSS_RX) {
560 + priv->status = STATUS_READ;
561 + enable_burst_irq(priv);
563 + if (i_pro & I2C_P_IRQSS_TX_END) {
564 + if (priv->status == STATUS_READ)
565 + priv->status = STATUS_READ_END;
567 + disable_burst_irq(priv);
568 + priv->status = STATUS_STOP;
573 + i2c_w32(i_pro, p_irqsc);
576 +static irqreturn_t ltq_i2c_isr(int irq, void *dev_id)
578 + u32 i_raw, i_err = 0;
579 + struct ltq_i2c *priv = dev_id;
581 + i_raw = i2c_r32(mis);
582 + pr_debug("i_raw 0x%08X\n", i_raw);
584 + /* error interrupt */
585 + if (i_raw & I2C_RIS_I2C_ERR_INT_INTOCC) {
586 + i_err = i2c_r32(err_irqss);
587 + pr_debug("i_err 0x%08X bus_stat 0x%04X\n",
588 + i_err, i2c_r32(bus_stat));
590 + /* tx fifo overflow (8) */
591 + if (i_err & I2C_ERR_IRQSS_TXF_OFL)
592 + priv->msg_err |= LTQ_I2C_TX_OFL;
594 + /* tx fifo underflow (4) */
595 + if (i_err & I2C_ERR_IRQSS_TXF_UFL)
596 + priv->msg_err |= LTQ_I2C_TX_UFL;
598 + /* rx fifo overflow (2) */
599 + if (i_err & I2C_ERR_IRQSS_RXF_OFL)
600 + priv->msg_err |= LTQ_I2C_RX_OFL;
602 + /* rx fifo underflow (1) */
603 + if (i_err & I2C_ERR_IRQSS_RXF_UFL)
604 + priv->msg_err |= LTQ_I2C_RX_UFL;
606 + i2c_w32(i_err, err_irqsc);
609 + /* protocol interrupt */
610 + if (i_raw & I2C_RIS_I2C_P_INT_INTOCC)
611 + ltq_i2c_isr_prot(priv);
613 + if ((priv->msg_err) || (priv->status == STATUS_STOP))
614 + complete(&priv->cmd_complete);
616 + return IRQ_HANDLED;
619 +static u32 ltq_i2c_functionality(struct i2c_adapter *adap)
621 + return I2C_FUNC_I2C |
622 + I2C_FUNC_10BIT_ADDR |
623 + I2C_FUNC_SMBUS_EMUL;
626 +static struct i2c_algorithm ltq_i2c_algorithm = {
627 + .master_xfer = ltq_i2c_xfer,
628 + .functionality = ltq_i2c_functionality,
631 +static int __devinit ltq_i2c_probe(struct platform_device *pdev)
633 + struct device_node *node = pdev->dev.of_node;
634 + struct ltq_i2c *priv;
635 + struct i2c_adapter *adap;
636 + struct resource *mmres, irqres[4];
639 + dev_dbg(&pdev->dev, "probing\n");
641 + mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
642 + ret = of_irq_to_resource_table(node, irqres, 4);
643 + if (!mmres || (ret != 4)) {
644 + dev_err(&pdev->dev, "no resources\n");
648 + /* allocate private data */
649 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
651 + dev_err(&pdev->dev, "can't allocate private data\n");
655 + adap = &priv->adap;
656 + i2c_set_adapdata(adap, priv);
657 + adap->owner = THIS_MODULE;
658 + adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
659 + strlcpy(adap->name, DRV_NAME "-adapter", sizeof(adap->name));
660 + adap->algo = <q_i2c_algorithm;
662 + if (of_property_read_u32(node, "clock-frequency", &priv->i2c_clock)) {
663 + dev_warn(&pdev->dev, "No I2C speed selected, using 100kHz\n");
664 + priv->i2c_clock = 100000;
667 + init_completion(&priv->cmd_complete);
668 + mutex_init(&priv->mutex);
670 + priv->membase = devm_request_and_ioremap(&pdev->dev, mmres);
671 + if (priv->membase == NULL)
674 + priv->dev = &pdev->dev;
675 + priv->irq_lb = irqres[0].start;
677 + ret = devm_request_irq(&pdev->dev, irqres[0].start, ltq_i2c_isr_burst,
678 + IRQF_DISABLED, "i2c lb", priv);
680 + dev_err(&pdev->dev, "can't get last burst IRQ %d\n",
685 + ret = devm_request_irq(&pdev->dev, irqres[1].start, ltq_i2c_isr_burst,
686 + IRQF_DISABLED, "i2c b", priv);
688 + dev_err(&pdev->dev, "can't get burst IRQ %d\n",
693 + ret = devm_request_irq(&pdev->dev, irqres[2].start, ltq_i2c_isr,
694 + IRQF_DISABLED, "i2c err", priv);
696 + dev_err(&pdev->dev, "can't get error IRQ %d\n",
701 + ret = devm_request_irq(&pdev->dev, irqres[3].start, ltq_i2c_isr,
702 + IRQF_DISABLED, "i2c p", priv);
704 + dev_err(&pdev->dev, "can't get protocol IRQ %d\n",
709 + dev_dbg(&pdev->dev, "mapped io-space to %p\n", priv->membase);
710 + dev_dbg(&pdev->dev, "use IRQs %d, %d, %d, %d\n", irqres[0].start,
711 + irqres[1].start, irqres[2].start, irqres[3].start);
713 + priv->clk_gate = devm_clk_get(&pdev->dev, NULL);
714 + if (IS_ERR(priv->clk_gate)) {
715 + dev_err(&pdev->dev, "failed to get i2c clk\n");
719 + /* this is a static clock, which has no refcounting */
720 + priv->clk_input = clk_get_fpi();
721 + if (IS_ERR(priv->clk_input)) {
722 + dev_err(&pdev->dev, "failed to get fpi clk\n");
726 + clk_activate(priv->clk_gate);
728 + /* add our adapter to the i2c stack */
729 + ret = i2c_add_numbered_adapter(adap);
731 + dev_err(&pdev->dev, "can't register I2C adapter\n");
735 + platform_set_drvdata(pdev, priv);
736 + i2c_set_adapdata(adap, priv);
738 + /* print module version information */
739 + dev_dbg(&pdev->dev, "module id=%u revision=%u\n",
740 + (i2c_r32(id) & I2C_ID_ID_MASK) >> I2C_ID_ID_OFFSET,
741 + (i2c_r32(id) & I2C_ID_REV_MASK) >> I2C_ID_REV_OFFSET);
743 + /* initialize HW */
744 + ret = ltq_i2c_hw_init(adap);
746 + dev_err(&pdev->dev, "can't configure adapter\n");
747 + i2c_del_adapter(adap);
748 + platform_set_drvdata(pdev, NULL);
750 + dev_info(&pdev->dev, "version %s\n", DRV_VERSION);
753 + of_i2c_register_devices(adap);
756 + /* if init failed, we need to deactivate the clock gate */
758 + clk_deactivate(priv->clk_gate);
763 +static int __devexit ltq_i2c_remove(struct platform_device *pdev)
765 + struct ltq_i2c *priv = platform_get_drvdata(pdev);
768 + i2c_w32_mask(I2C_RUN_CTRL_RUN_EN, 0, run_ctrl);
770 + /* power down the core */
771 + clk_deactivate(priv->clk_gate);
773 + /* remove driver */
774 + i2c_del_adapter(&priv->adap);
777 + dev_dbg(&pdev->dev, "removed\n");
778 + platform_set_drvdata(pdev, NULL);
782 +static const struct of_device_id ltq_i2c_match[] = {
783 + { .compatible = "lantiq,lantiq-i2c" },
786 +MODULE_DEVICE_TABLE(of, ltq_i2c_match);
788 +static struct platform_driver ltq_i2c_driver = {
789 + .probe = ltq_i2c_probe,
790 + .remove = __devexit_p(ltq_i2c_remove),
793 + .owner = THIS_MODULE,
794 + .of_match_table = ltq_i2c_match,
798 +module_platform_driver(ltq_i2c_driver);
800 +MODULE_DESCRIPTION("Lantiq I2C bus adapter");
801 +MODULE_AUTHOR("Thomas Langer <thomas.langer@lantiq.com>");
802 +MODULE_ALIAS("platform:" DRV_NAME);
803 +MODULE_LICENSE("GPL");
804 +MODULE_VERSION(DRV_VERSION);
805 diff --git a/drivers/i2c/busses/i2c-lantiq.h b/drivers/i2c/busses/i2c-lantiq.h
807 index 0000000..7a86b89
809 +++ b/drivers/i2c/busses/i2c-lantiq.h
811 +#ifndef I2C_LANTIQ_H
812 +#define I2C_LANTIQ_H
814 +/* I2C register structure */
815 +struct lantiq_reg_i2c {
816 + /* I2C Kernel Clock Control Register */
817 + unsigned int clc; /* 0x00000000 */
819 + unsigned int res_0; /* 0x00000004 */
820 + /* I2C Identification Register */
821 + unsigned int id; /* 0x00000008 */
823 + unsigned int res_1; /* 0x0000000C */
825 + * I2C RUN Control Register
826 + * This register enables and disables the I2C peripheral. Before
827 + * enabling, the I2C has to be configured properly. After enabling
828 + * no configuration is possible
830 + unsigned int run_ctrl; /* 0x00000010 */
832 + * I2C End Data Control Register
833 + * This register is used to either turn around the data transmission
834 + * direction or to address another slave without sending a stop
835 + * condition. Also the software can stop the slave-transmitter by
836 + * sending a not-accolade when working as master-receiver or even
837 + * stop data transmission immediately when operating as
838 + * master-transmitter. The writing to the bits of this control
839 + * register is only effective when in MASTER RECEIVES BYTES, MASTER
840 + * TRANSMITS BYTES, MASTER RESTART or SLAVE RECEIVE BYTES state
842 + unsigned int endd_ctrl; /* 0x00000014 */
844 + * I2C Fractional Divider Configuration Register
845 + * These register is used to program the fractional divider of the I2C
846 + * bus. Before the peripheral is switched on by setting the RUN-bit the
847 + * two (fixed) values for the two operating frequencies are programmed
848 + * into these (configuration) registers. The Register FDIV_HIGH_CFG has
849 + * the same layout as I2C_FDIV_CFG.
851 + unsigned int fdiv_cfg; /* 0x00000018 */
853 + * I2C Fractional Divider (highspeed mode) Configuration Register
854 + * These register is used to program the fractional divider of the I2C
855 + * bus. Before the peripheral is switched on by setting the RUN-bit the
856 + * two (fixed) values for the two operating frequencies are programmed
857 + * into these (configuration) registers. The Register FDIV_CFG has the
858 + * same layout as I2C_FDIV_CFG.
860 + unsigned int fdiv_high_cfg; /* 0x0000001C */
861 + /* I2C Address Configuration Register */
862 + unsigned int addr_cfg; /* 0x00000020 */
863 + /* I2C Bus Status Register
864 + * This register gives a status information of the I2C. This additional
865 + * information can be used by the software to start proper actions.
867 + unsigned int bus_stat; /* 0x00000024 */
868 + /* I2C FIFO Configuration Register */
869 + unsigned int fifo_cfg; /* 0x00000028 */
870 + /* I2C Maximum Received Packet Size Register */
871 + unsigned int mrps_ctrl; /* 0x0000002C */
872 + /* I2C Received Packet Size Status Register */
873 + unsigned int rps_stat; /* 0x00000030 */
874 + /* I2C Transmit Packet Size Register */
875 + unsigned int tps_ctrl; /* 0x00000034 */
876 + /* I2C Filled FIFO Stages Status Register */
877 + unsigned int ffs_stat; /* 0x00000038 */
879 + unsigned int res_2; /* 0x0000003C */
880 + /* I2C Timing Configuration Register */
881 + unsigned int tim_cfg; /* 0x00000040 */
883 + unsigned int res_3[7]; /* 0x00000044 */
884 + /* I2C Error Interrupt Request Source Mask Register */
885 + unsigned int err_irqsm; /* 0x00000060 */
886 + /* I2C Error Interrupt Request Source Status Register */
887 + unsigned int err_irqss; /* 0x00000064 */
888 + /* I2C Error Interrupt Request Source Clear Register */
889 + unsigned int err_irqsc; /* 0x00000068 */
891 + unsigned int res_4; /* 0x0000006C */
892 + /* I2C Protocol Interrupt Request Source Mask Register */
893 + unsigned int p_irqsm; /* 0x00000070 */
894 + /* I2C Protocol Interrupt Request Source Status Register */
895 + unsigned int p_irqss; /* 0x00000074 */
896 + /* I2C Protocol Interrupt Request Source Clear Register */
897 + unsigned int p_irqsc; /* 0x00000078 */
899 + unsigned int res_5; /* 0x0000007C */
900 + /* I2C Raw Interrupt Status Register */
901 + unsigned int ris; /* 0x00000080 */
902 + /* I2C Interrupt Mask Control Register */
903 + unsigned int imsc; /* 0x00000084 */
904 + /* I2C Masked Interrupt Status Register */
905 + unsigned int mis; /* 0x00000088 */
906 + /* I2C Interrupt Clear Register */
907 + unsigned int icr; /* 0x0000008C */
908 + /* I2C Interrupt Set Register */
909 + unsigned int isr; /* 0x00000090 */
910 + /* I2C DMA Enable Register */
911 + unsigned int dmae; /* 0x00000094 */
913 + unsigned int res_6[8154]; /* 0x00000098 */
914 + /* I2C Transmit Data Register */
915 + unsigned int txd; /* 0x00008000 */
917 + unsigned int res_7[4095]; /* 0x00008004 */
918 + /* I2C Receive Data Register */
919 + unsigned int rxd; /* 0x0000C000 */
921 + unsigned int res_8[4095]; /* 0x0000C004 */
925 + * Clock Divider for Normal Run Mode
926 + * Max 8-bit divider value. IF RMC is 0 the module is disabled. Note: As long
927 + * as the new divider value RMC is not valid, the register returns 0x0000 00xx
930 +#define I2C_CLC_RMC_MASK 0x0000FF00
932 +#define I2C_CLC_RMC_OFFSET 8
934 +/* Fields of "I2C Identification Register" */
936 +#define I2C_ID_ID_MASK 0x0000FF00
938 +#define I2C_ID_ID_OFFSET 8
940 +#define I2C_ID_REV_MASK 0x000000FF
942 +#define I2C_ID_REV_OFFSET 0
944 +/* Fields of "I2C Interrupt Mask Control Register" */
946 +#define I2C_IMSC_BREQ_INT_EN 0x00000008
948 +#define I2C_IMSC_LBREQ_INT_EN 0x00000004
950 +/* Fields of "I2C Fractional Divider Configuration Register" */
952 +#define I2C_FDIV_CFG_INC_OFFSET 16
954 +/* Fields of "I2C Interrupt Mask Control Register" */
956 +#define I2C_IMSC_I2C_P_INT_EN 0x00000020
958 +#define I2C_IMSC_I2C_ERR_INT_EN 0x00000010
960 +/* Fields of "I2C Error Interrupt Request Source Status Register" */
962 +#define I2C_ERR_IRQSS_TXF_OFL 0x00000008
964 +#define I2C_ERR_IRQSS_TXF_UFL 0x00000004
966 +#define I2C_ERR_IRQSS_RXF_OFL 0x00000002
968 +#define I2C_ERR_IRQSS_RXF_UFL 0x00000001
970 +/* Fields of "I2C Raw Interrupt Status Register" */
971 +/* Read: Interrupt occurred. */
972 +#define I2C_RIS_I2C_ERR_INT_INTOCC 0x00000010
973 +/* Read: Interrupt occurred. */
974 +#define I2C_RIS_I2C_P_INT_INTOCC 0x00000020
976 +/* Fields of "I2C FIFO Configuration Register" */
977 +/* TX FIFO Flow Control */
978 +#define I2C_FIFO_CFG_TXFC 0x00020000
979 +/* RX FIFO Flow Control */
980 +#define I2C_FIFO_CFG_RXFC 0x00010000
981 +/* Word aligned (character alignment of four characters) */
982 +#define I2C_FIFO_CFG_TXFA_TXFA2 0x00002000
983 +/* Word aligned (character alignment of four characters) */
984 +#define I2C_FIFO_CFG_RXFA_RXFA2 0x00000200
986 +#define I2C_FIFO_CFG_TXBS_TXBS0 0x00000000
988 +/* Fields of "I2C FIFO Configuration Register" */
990 +#define I2C_FIFO_CFG_RXBS_RXBS0 0x00000000
991 +/* Stop on Packet End Enable */
992 +#define I2C_ADDR_CFG_SOPE_EN 0x00200000
993 +/* Stop on Not Acknowledge Enable */
994 +#define I2C_ADDR_CFG_SONA_EN 0x00100000
996 +#define I2C_ADDR_CFG_MnS_EN 0x00080000
998 +/* Fields of "I2C Interrupt Clear Register" */
1000 +#define I2C_ICR_BREQ_INT_CLR 0x00000008
1002 +#define I2C_ICR_LBREQ_INT_CLR 0x00000004
1004 +/* Fields of "I2C Fractional Divider Configuration Register" */
1006 +#define I2C_FDIV_CFG_DEC_OFFSET 0
1008 +/* Fields of "I2C Bus Status Register" */
1010 +#define I2C_BUS_STAT_BS_MASK 0x00000003
1011 +/* Read from I2C Bus. */
1012 +#define I2C_BUS_STAT_RNW_READ 0x00000004
1013 +/* I2C Bus is free. */
1014 +#define I2C_BUS_STAT_BS_FREE 0x00000000
1016 + * The device is working as master and has claimed the control on the
1017 + * I2C-bus (busy master).
1019 +#define I2C_BUS_STAT_BS_BM 0x00000002
1021 +/* Fields of "I2C RUN Control Register" */
1023 +#define I2C_RUN_CTRL_RUN_EN 0x00000001
1025 +/* Fields of "I2C End Data Control Register" */
1027 + * Set End of Transmission
1028 + * Note:Do not write '1' to this bit when bus is free. This will cause an
1029 + * abort after the first byte when a new transfer is started.
1031 +#define I2C_ENDD_CTRL_SETEND 0x00000002
1033 +/* Fields of "I2C Protocol Interrupt Request Source Status Register" */
1035 +#define I2C_P_IRQSS_NACK 0x00000010
1037 +#define I2C_P_IRQSS_AL 0x00000008
1039 +#define I2C_P_IRQSS_RX 0x00000040
1041 +#define I2C_P_IRQSS_TX_END 0x00000020
1044 +#endif /* I2C_LANTIQ_H */