summaryrefslogtreecommitdiff
path: root/drivers/i2c/busses/i2c-tegra.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/busses/i2c-tegra.c')
-rw-r--r--drivers/i2c/busses/i2c-tegra.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
index c8a26a85a938..f9a14a75563c 100644
--- a/drivers/i2c/busses/i2c-tegra.c
+++ b/drivers/i2c/busses/i2c-tegra.c
@@ -31,6 +31,7 @@
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/i2c-tegra.h>
+#include <linux/spinlock.h>
#include <asm/unaligned.h>
@@ -125,10 +126,12 @@ struct tegra_i2c_dev {
struct clk *clk;
struct resource *iomem;
struct rt_mutex dev_lock;
+ spinlock_t clk_lock;
void __iomem *base;
int cont_id;
int irq;
bool irq_disabled;
+ bool controller_enabled;
int is_dvc;
bool is_slave;
struct completion msg_complete;
@@ -385,6 +388,14 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
u32 status;
const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST | I2C_INT_TX_FIFO_OVERFLOW;
struct tegra_i2c_dev *i2c_dev = dev_id;
+ unsigned long flags;
+
+ spin_lock_irqsave(&i2c_dev->clk_lock, flags);
+ if (!i2c_dev->controller_enabled) {
+ dev_warn(i2c_dev->dev, "Controller not enabled\n");
+ spin_unlock_irqrestore(&i2c_dev->clk_lock, flags);
+ return IRQ_NONE;
+ }
status = i2c_readl(i2c_dev, I2C_INT_STATUS);
@@ -398,7 +409,6 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
i2c_dev->irq_disabled = 1;
}
- complete(&i2c_dev->msg_complete);
goto err;
}
@@ -427,8 +437,6 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
dev_warn(i2c_dev->dev, "Packet status 0x%08x\n",
i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS));
}
- complete(&i2c_dev->msg_complete);
-
goto err;
}
@@ -444,7 +452,6 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
i2c_dev->irq_disabled = 1;
}
- complete(&i2c_dev->msg_complete);
goto err;
}
@@ -465,13 +472,15 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
if (status & I2C_INT_PACKET_XFER_COMPLETE)
i2c_dev->msg_transfer_complete = 1;
- if (i2c_dev->msg_transfer_complete && !i2c_dev->msg_buf_remaining)
- complete(&i2c_dev->msg_complete);
i2c_writel(i2c_dev, status, I2C_INT_STATUS);
if (i2c_dev->is_dvc)
dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
+ if (i2c_dev->msg_transfer_complete && !i2c_dev->msg_buf_remaining)
+ complete(&i2c_dev->msg_complete);
+
+ spin_unlock_irqrestore(&i2c_dev->clk_lock, flags);
return IRQ_HANDLED;
err:
@@ -505,6 +514,8 @@ err:
if (i2c_dev->is_dvc)
dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
+ complete(&i2c_dev->msg_complete);
+ spin_unlock_irqrestore(&i2c_dev->clk_lock, flags);
return IRQ_HANDLED;
}
@@ -514,6 +525,7 @@ static int tegra_i2c_xfer_msg(struct tegra_i2c_bus *i2c_bus,
struct tegra_i2c_dev *i2c_dev = i2c_bus->dev;
u32 int_mask;
int ret;
+ unsigned long flags;
tegra_i2c_flush_fifos(i2c_dev);
@@ -570,7 +582,10 @@ static int tegra_i2c_xfer_msg(struct tegra_i2c_bus *i2c_bus,
"i2c transfer timed out, addr 0x%04x, data 0x%02x\n",
msg->addr, msg->buf[0]);
+ spin_lock_irqsave(&i2c_dev->clk_lock, flags);
+ i2c_dev->controller_enabled = false;
tegra_i2c_init(i2c_dev);
+ spin_unlock_irqrestore(&i2c_dev->clk_lock, flags);
return -ETIMEDOUT;
}
@@ -580,7 +595,11 @@ static int tegra_i2c_xfer_msg(struct tegra_i2c_bus *i2c_bus,
if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
return 0;
+ spin_lock_irqsave(&i2c_dev->clk_lock, flags);
+ i2c_dev->controller_enabled = false;
tegra_i2c_init(i2c_dev);
+ spin_unlock_irqrestore(&i2c_dev->clk_lock, flags);
+
if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
if (msg->flags & I2C_M_IGNORE_NAK)
return 0;
@@ -600,6 +619,7 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
struct tegra_i2c_dev *i2c_dev = i2c_bus->dev;
int i;
int ret = 0;
+ unsigned long flags;
if (i2c_dev->is_suspended)
return -EBUSY;
@@ -625,6 +645,11 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
if (!i2c_dev->is_clkon_always)
clk_enable(i2c_dev->clk);
+
+ spin_lock_irqsave(&i2c_dev->clk_lock, flags);
+ i2c_dev->controller_enabled = true;
+ spin_unlock_irqrestore(&i2c_dev->clk_lock, flags);
+
for (i = 0; i < num; i++) {
int stop = (i == (num - 1)) ? 1 : 0;
ret = tegra_i2c_xfer_msg(i2c_bus, &msgs[i], stop);
@@ -634,6 +659,10 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
ret = i;
out:
+ spin_lock_irqsave(&i2c_dev->clk_lock, flags);
+ i2c_dev->controller_enabled = false;
+ spin_unlock_irqrestore(&i2c_dev->clk_lock, flags);
+
if (!i2c_dev->is_clkon_always)
clk_disable(i2c_dev->clk);
@@ -731,7 +760,9 @@ static int tegra_i2c_probe(struct platform_device *pdev)
i2c_dev->last_bus_clk = plat->bus_clk_rate[0] ?: 100000;
i2c_dev->msgs = NULL;
i2c_dev->msgs_num = 0;
+ i2c_dev->controller_enabled = false;
rt_mutex_init(&i2c_dev->dev_lock);
+ spin_lock_init(&i2c_dev->clk_lock);
i2c_dev->slave_addr = plat->slave_addr;
i2c_dev->is_dvc = plat->is_dvc;