Skip to content

Commit 2378bc0

Browse files
Wolfram SangJean Delvare
authored andcommitted
i2c-algo-pca: Use timeout for checking the state machine
We now timeout also if the state machine does not change within the given time. For that, the driver-specific completion-functions are extended to return true or false depending on the timeout. This then gets checked in the algorithm. Signed-off-by: Wolfram Sang <w.sang@pengutronix.de> Signed-off-by: Jean Delvare <khali@linux-fr.org>
1 parent 8e99ada commit 2378bc0

3 files changed

Lines changed: 46 additions & 33 deletions

File tree

‎drivers/i2c/algos/i2c-algo-pca.c‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,29 @@ static void pca9665_reset(void *pd)
6060
*
6161
* returns after the start condition has occurred
6262
*/
63-
static void pca_start(struct i2c_algo_pca_data *adap)
63+
static int pca_start(struct i2c_algo_pca_data *adap)
6464
{
6565
int sta = pca_get_con(adap);
6666
DEB2("=== START\n");
6767
sta |= I2C_PCA_CON_STA;
6868
sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
6969
pca_set_con(adap, sta);
70-
pca_wait(adap);
70+
return pca_wait(adap);
7171
}
7272

7373
/*
7474
* Generate a repeated start condition on the i2c bus
7575
*
7676
* return after the repeated start condition has occurred
7777
*/
78-
static void pca_repeated_start(struct i2c_algo_pca_data *adap)
78+
static int pca_repeated_start(struct i2c_algo_pca_data *adap)
7979
{
8080
int sta = pca_get_con(adap);
8181
DEB2("=== REPEATED START\n");
8282
sta |= I2C_PCA_CON_STA;
8383
sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
8484
pca_set_con(adap, sta);
85-
pca_wait(adap);
85+
return pca_wait(adap);
8686
}
8787

8888
/*
@@ -108,7 +108,7 @@ static void pca_stop(struct i2c_algo_pca_data *adap)
108108
*
109109
* returns after the address has been sent
110110
*/
111-
static void pca_address(struct i2c_algo_pca_data *adap,
111+
static int pca_address(struct i2c_algo_pca_data *adap,
112112
struct i2c_msg *msg)
113113
{
114114
int sta = pca_get_con(adap);
@@ -125,15 +125,15 @@ static void pca_address(struct i2c_algo_pca_data *adap,
125125
sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
126126
pca_set_con(adap, sta);
127127

128-
pca_wait(adap);
128+
return pca_wait(adap);
129129
}
130130

131131
/*
132132
* Transmit a byte.
133133
*
134134
* Returns after the byte has been transmitted
135135
*/
136-
static void pca_tx_byte(struct i2c_algo_pca_data *adap,
136+
static int pca_tx_byte(struct i2c_algo_pca_data *adap,
137137
__u8 b)
138138
{
139139
int sta = pca_get_con(adap);
@@ -143,7 +143,7 @@ static void pca_tx_byte(struct i2c_algo_pca_data *adap,
143143
sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
144144
pca_set_con(adap, sta);
145145

146-
pca_wait(adap);
146+
return pca_wait(adap);
147147
}
148148

149149
/*
@@ -163,7 +163,7 @@ static void pca_rx_byte(struct i2c_algo_pca_data *adap,
163163
*
164164
* Returns after next byte has arrived.
165165
*/
166-
static void pca_rx_ack(struct i2c_algo_pca_data *adap,
166+
static int pca_rx_ack(struct i2c_algo_pca_data *adap,
167167
int ack)
168168
{
169169
int sta = pca_get_con(adap);
@@ -174,7 +174,7 @@ static void pca_rx_ack(struct i2c_algo_pca_data *adap,
174174
sta |= I2C_PCA_CON_AA;
175175

176176
pca_set_con(adap, sta);
177-
pca_wait(adap);
177+
return pca_wait(adap);
178178
}
179179

180180
static int pca_xfer(struct i2c_adapter *i2c_adap,
@@ -187,6 +187,7 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
187187
int numbytes = 0;
188188
int state;
189189
int ret;
190+
int completed = 1;
190191
unsigned long timeout = jiffies + i2c_adap->timeout;
191192

192193
while (pca_status(adap) != 0xf8) {
@@ -232,26 +233,27 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
232233

233234
switch (state) {
234235
case 0xf8: /* On reset or stop the bus is idle */
235-
pca_start(adap);
236+
completed = pca_start(adap);
236237
break;
237238

238239
case 0x08: /* A START condition has been transmitted */
239240
case 0x10: /* A repeated start condition has been transmitted */
240-
pca_address(adap, msg);
241+
completed = pca_address(adap, msg);
241242
break;
242243

243244
case 0x18: /* SLA+W has been transmitted; ACK has been received */
244245
case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
245246
if (numbytes < msg->len) {
246-
pca_tx_byte(adap, msg->buf[numbytes]);
247+
completed = pca_tx_byte(adap,
248+
msg->buf[numbytes]);
247249
numbytes++;
248250
break;
249251
}
250252
curmsg++; numbytes = 0;
251253
if (curmsg == num)
252254
pca_stop(adap);
253255
else
254-
pca_repeated_start(adap);
256+
completed = pca_repeated_start(adap);
255257
break;
256258

257259
case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
@@ -260,21 +262,22 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
260262
goto out;
261263

262264
case 0x40: /* SLA+R has been transmitted; ACK has been received */
263-
pca_rx_ack(adap, msg->len > 1);
265+
completed = pca_rx_ack(adap, msg->len > 1);
264266
break;
265267

266268
case 0x50: /* Data bytes has been received; ACK has been returned */
267269
if (numbytes < msg->len) {
268270
pca_rx_byte(adap, &msg->buf[numbytes], 1);
269271
numbytes++;
270-
pca_rx_ack(adap, numbytes < msg->len - 1);
272+
completed = pca_rx_ack(adap,
273+
numbytes < msg->len - 1);
271274
break;
272275
}
273276
curmsg++; numbytes = 0;
274277
if (curmsg == num)
275278
pca_stop(adap);
276279
else
277-
pca_repeated_start(adap);
280+
completed = pca_repeated_start(adap);
278281
break;
279282

280283
case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
@@ -297,7 +300,7 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
297300
if (curmsg == num)
298301
pca_stop(adap);
299302
else
300-
pca_repeated_start(adap);
303+
completed = pca_repeated_start(adap);
301304
} else {
302305
DEB2("NOT ACK sent after data byte received. "
303306
"Not final byte. numbytes %d. len %d\n",
@@ -323,6 +326,8 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
323326
break;
324327
}
325328

329+
if (!completed)
330+
goto out;
326331
}
327332

328333
ret = curmsg;

‎drivers/i2c/busses/i2c-pca-isa.c‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/module.h>
2424
#include <linux/moduleparam.h>
2525
#include <linux/delay.h>
26+
#include <linux/jiffies.h>
2627
#include <linux/init.h>
2728
#include <linux/interrupt.h>
2829
#include <linux/wait.h>
@@ -43,6 +44,7 @@ static int irq = -1;
4344
* in the actual clock rate */
4445
static int clock = 59000;
4546

47+
static struct i2c_adapter pca_isa_ops;
4648
static wait_queue_head_t pca_wait;
4749

4850
static void pca_isa_writebyte(void *pd, int reg, int val)
@@ -69,16 +71,22 @@ static int pca_isa_readbyte(void *pd, int reg)
6971

7072
static int pca_isa_waitforcompletion(void *pd)
7173
{
72-
int ret = 0;
74+
long ret = ~0;
75+
unsigned long timeout;
7376

7477
if (irq > -1) {
75-
ret = wait_event_interruptible(pca_wait,
76-
pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI);
78+
ret = wait_event_interruptible_timeout(pca_wait,
79+
pca_isa_readbyte(pd, I2C_PCA_CON)
80+
& I2C_PCA_CON_SI, pca_isa_ops.timeout);
7781
} else {
78-
while ((pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
82+
/* Do polling */
83+
timeout = jiffies + pca_isa_ops.timeout;
84+
while (((pca_isa_readbyte(pd, I2C_PCA_CON)
85+
& I2C_PCA_CON_SI) == 0)
86+
&& (ret = time_before(jiffies, timeout)))
7987
udelay(100);
8088
}
81-
return ret;
89+
return ret > 0;
8290
}
8391

8492
static void pca_isa_resetchip(void *pd)

‎drivers/i2c/busses/i2c-pca-platform.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/init.h>
1616
#include <linux/slab.h>
1717
#include <linux/delay.h>
18+
#include <linux/jiffies.h>
1819
#include <linux/errno.h>
1920
#include <linux/i2c.h>
2021
#include <linux/interrupt.h>
@@ -81,24 +82,23 @@ static void i2c_pca_pf_writebyte32(void *pd, int reg, int val)
8182
static int i2c_pca_pf_waitforcompletion(void *pd)
8283
{
8384
struct i2c_pca_pf_data *i2c = pd;
84-
int ret = 0;
85+
long ret = ~0;
86+
unsigned long timeout;
8587

8688
if (i2c->irq) {
87-
ret = wait_event_interruptible(i2c->wait,
89+
ret = wait_event_interruptible_timeout(i2c->wait,
8890
i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
89-
& I2C_PCA_CON_SI);
91+
& I2C_PCA_CON_SI, i2c->adap.timeout);
9092
} else {
91-
/*
92-
* Do polling...
93-
* XXX: Could get stuck in extreme cases!
94-
* Maybe add timeout, but using irqs is preferred anyhow.
95-
*/
96-
while ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
93+
/* Do polling */
94+
timeout = jiffies + i2c->adap.timeout;
95+
while (((i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
9796
& I2C_PCA_CON_SI) == 0)
97+
&& (ret = time_before(jiffies, timeout)))
9898
udelay(100);
9999
}
100100

101-
return ret;
101+
return ret > 0;
102102
}
103103

104104
static void i2c_pca_pf_dummyreset(void *pd)

0 commit comments

Comments
 (0)