Skip to content

Commit 40027b2

Browse files
petegriffinWolfram Sang
authored andcommitted
i2c: st: Implement bus clear
>From I2C specifications: http://www.nxp.com/documents/user_manual/UM10204.pdf Chapter 3.1.16, when the i2c device held the SDA line low, the master should send 9 clocks pulses to try to recover. Signed-off-by: Frederic Pillon <frederic.pillon@st.com> Signed-off-by: Peter Griffin <peter.griffin@linaro.org> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
1 parent 3a9ddaf commit 40027b2

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

drivers/i2c/busses/i2c-st.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,42 @@ static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev)
337337
writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT);
338338
}
339339

340+
static int st_i2c_recover_bus(struct i2c_adapter *i2c_adap)
341+
{
342+
struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
343+
u32 ctl;
344+
345+
dev_dbg(i2c_dev->dev, "Trying to recover bus\n");
346+
347+
/*
348+
* SSP IP is dual role SPI/I2C to generate 9 clock pulses
349+
* we switch to SPI node, 9 bit words and write a 0. This
350+
* has been validate with a oscilloscope and is easier
351+
* than switching to GPIO mode.
352+
*/
353+
354+
/* Disable interrupts */
355+
writel_relaxed(0, i2c_dev->base + SSC_IEN);
356+
357+
st_i2c_hw_config(i2c_dev);
358+
359+
ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
360+
st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
361+
362+
st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
363+
usleep_range(8000, 10000);
364+
365+
writel_relaxed(0, i2c_dev->base + SSC_TBUF);
366+
usleep_range(2000, 4000);
367+
st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
368+
369+
return 0;
370+
}
371+
340372
static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev)
341373
{
342374
u32 sta;
343-
int i;
375+
int i, ret;
344376

345377
for (i = 0; i < 10; i++) {
346378
sta = readl_relaxed(i2c_dev->base + SSC_STA);
@@ -352,6 +384,12 @@ static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev)
352384

353385
dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta);
354386

387+
ret = i2c_recover_bus(&i2c_dev->adap);
388+
if (ret) {
389+
dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret);
390+
return ret;
391+
}
392+
355393
return -EBUSY;
356394
}
357395

@@ -743,6 +781,10 @@ static struct i2c_algorithm st_i2c_algo = {
743781
.functionality = st_i2c_func,
744782
};
745783

784+
static struct i2c_bus_recovery_info st_i2c_recovery_info = {
785+
.recover_bus = st_i2c_recover_bus,
786+
};
787+
746788
static int st_i2c_of_get_deglitch(struct device_node *np,
747789
struct st_i2c_dev *i2c_dev)
748790
{
@@ -825,6 +867,7 @@ static int st_i2c_probe(struct platform_device *pdev)
825867
adap->timeout = 2 * HZ;
826868
adap->retries = 0;
827869
adap->algo = &st_i2c_algo;
870+
adap->bus_recovery_info = &st_i2c_recovery_info;
828871
adap->dev.parent = &pdev->dev;
829872
adap->dev.of_node = pdev->dev.of_node;
830873

0 commit comments

Comments
 (0)