mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-02-27 15:15:42 -05:00
i2c: lpi2c: use readl_poll_timeout() for register polling
Replaces polling loops with the readl_poll_timeout() helper macro. Signed-off-by: Emanuele Ghidoli <emanuele.ghidoli@toradex.com> Signed-off-by: Francesco Dolcini <francesco.dolcini@toradex.com> Reviewed-by: Carlos Song <carlos.song@nxp.com> Tested-by: Primoz Fiser <primoz.fiser@norik.com> Signed-off-by: Andi Shyti <andi.shyti@kernel.org> Link: https://lore.kernel.org/r/20250718133429.67219-2-francesco@dolcini.it
This commit is contained in:
committed by
Andi Shyti
parent
f8fd855d5b
commit
8336f9de21
@@ -16,6 +16,7 @@
|
||||
#include <linux/init.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/io.h>
|
||||
#include <linux/iopoll.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/of.h>
|
||||
@@ -187,6 +188,10 @@ struct lpi2c_imx_struct {
|
||||
struct i2c_client *target;
|
||||
};
|
||||
|
||||
#define lpi2c_imx_read_msr_poll_timeout(val, cond) \
|
||||
readl_poll_timeout(lpi2c_imx->base + LPI2C_MSR, val, cond, \
|
||||
0, 500000)
|
||||
|
||||
static void lpi2c_imx_intctrl(struct lpi2c_imx_struct *lpi2c_imx,
|
||||
unsigned int enable)
|
||||
{
|
||||
@@ -195,33 +200,34 @@ static void lpi2c_imx_intctrl(struct lpi2c_imx_struct *lpi2c_imx,
|
||||
|
||||
static int lpi2c_imx_bus_busy(struct lpi2c_imx_struct *lpi2c_imx)
|
||||
{
|
||||
unsigned long orig_jiffies = jiffies;
|
||||
unsigned int temp;
|
||||
int err;
|
||||
|
||||
while (1) {
|
||||
temp = readl(lpi2c_imx->base + LPI2C_MSR);
|
||||
err = lpi2c_imx_read_msr_poll_timeout(temp,
|
||||
temp & (MSR_ALF | MSR_BBF | MSR_MBF));
|
||||
|
||||
/* check for arbitration lost, clear if set */
|
||||
if (temp & MSR_ALF) {
|
||||
writel(temp, lpi2c_imx->base + LPI2C_MSR);
|
||||
return -EAGAIN;
|
||||
}
|
||||
/* check for arbitration lost, clear if set */
|
||||
if (temp & MSR_ALF) {
|
||||
writel(temp, lpi2c_imx->base + LPI2C_MSR);
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
if (temp & (MSR_BBF | MSR_MBF))
|
||||
break;
|
||||
|
||||
if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "bus not work\n");
|
||||
if (lpi2c_imx->adapter.bus_recovery_info)
|
||||
i2c_recover_bus(&lpi2c_imx->adapter);
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
schedule();
|
||||
/* check for bus not busy */
|
||||
if (err) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "bus not work\n");
|
||||
if (lpi2c_imx->adapter.bus_recovery_info)
|
||||
i2c_recover_bus(&lpi2c_imx->adapter);
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 lpi2c_imx_txfifo_cnt(struct lpi2c_imx_struct *lpi2c_imx)
|
||||
{
|
||||
return readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff;
|
||||
}
|
||||
|
||||
static void lpi2c_imx_set_mode(struct lpi2c_imx_struct *lpi2c_imx)
|
||||
{
|
||||
unsigned int bitrate = lpi2c_imx->bitrate;
|
||||
@@ -259,25 +265,18 @@ static int lpi2c_imx_start(struct lpi2c_imx_struct *lpi2c_imx,
|
||||
|
||||
static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx)
|
||||
{
|
||||
unsigned long orig_jiffies = jiffies;
|
||||
unsigned int temp;
|
||||
int err;
|
||||
|
||||
writel(GEN_STOP << 8, lpi2c_imx->base + LPI2C_MTDR);
|
||||
|
||||
do {
|
||||
temp = readl(lpi2c_imx->base + LPI2C_MSR);
|
||||
if (temp & MSR_SDF)
|
||||
break;
|
||||
err = lpi2c_imx_read_msr_poll_timeout(temp, temp & MSR_SDF);
|
||||
|
||||
if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "stop timeout\n");
|
||||
if (lpi2c_imx->adapter.bus_recovery_info)
|
||||
i2c_recover_bus(&lpi2c_imx->adapter);
|
||||
break;
|
||||
}
|
||||
schedule();
|
||||
|
||||
} while (1);
|
||||
if (err) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "stop timeout\n");
|
||||
if (lpi2c_imx->adapter.bus_recovery_info)
|
||||
i2c_recover_bus(&lpi2c_imx->adapter);
|
||||
}
|
||||
}
|
||||
|
||||
/* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */
|
||||
@@ -393,26 +392,23 @@ static int lpi2c_imx_pio_msg_complete(struct lpi2c_imx_struct *lpi2c_imx)
|
||||
|
||||
static int lpi2c_imx_txfifo_empty(struct lpi2c_imx_struct *lpi2c_imx)
|
||||
{
|
||||
unsigned long orig_jiffies = jiffies;
|
||||
u32 txcnt;
|
||||
unsigned int temp;
|
||||
int err;
|
||||
|
||||
do {
|
||||
txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff;
|
||||
err = lpi2c_imx_read_msr_poll_timeout(temp,
|
||||
(temp & MSR_NDF) || !lpi2c_imx_txfifo_cnt(lpi2c_imx));
|
||||
|
||||
if (readl(lpi2c_imx->base + LPI2C_MSR) & MSR_NDF) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "NDF detected\n");
|
||||
return -EIO;
|
||||
}
|
||||
if (temp & MSR_NDF) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "NDF detected\n");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "txfifo empty timeout\n");
|
||||
if (lpi2c_imx->adapter.bus_recovery_info)
|
||||
i2c_recover_bus(&lpi2c_imx->adapter);
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
schedule();
|
||||
|
||||
} while (txcnt);
|
||||
if (err) {
|
||||
dev_dbg(&lpi2c_imx->adapter.dev, "txfifo empty timeout\n");
|
||||
if (lpi2c_imx->adapter.bus_recovery_info)
|
||||
i2c_recover_bus(&lpi2c_imx->adapter);
|
||||
return -ETIMEDOUT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user