From 4f91cc159fbc7f48579757831486b9315e7386c8 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 1 Feb 2004 22:52:07 -0800 Subject: [PATCH] [PATCH] I2C: Handle read errors in w83l785ts Here is a patch that adds proper read error handling in the w83l785ts driver. This is needed for this driver because on many Asus boards the BIOS or something accesses the chip in our back and causes collisions or something similar that causes many read errors. For now, these errors make the driver return temperature values of -1 from times to times. I have been working with James Bolt on this. See the thread on the lm_sensors mailing list for details. The patch is fairly well tested. It is against 2.6.2-rc3 + your current stack of patches as I know it. The "retry until it works" method is the best I could think of, since we have no information from Asus and I don't expect to get any. If it still doesn't work after an arbitrary number (5) of tries it returns the previously known value and generates an error in the logs. James' tests showed that it will probably never happen though (highest retry count was 3 and happened once out of 3000 reads) unless we lower the arbitrary number (but we don't want to, do we?) I inserted incremental delays as reads fail, I felt like it should help avoid collisions with whatever-is-bugging-us. Seems to work OK, but it's not perfect (since we sometimes need 3 retries) and I didn't test with a different policy (no delay, constant delay or different increment). --- drivers/i2c/chips/w83l785ts.c | 55 ++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/drivers/i2c/chips/w83l785ts.c b/drivers/i2c/chips/w83l785ts.c index ad3dd7bc9b2a7..db2907f07b3d0 100644 --- a/drivers/i2c/chips/w83l785ts.c +++ b/drivers/i2c/chips/w83l785ts.c @@ -38,6 +38,9 @@ #include #include +/* How many retries on register read error */ +#define MAX_RETRIES 5 + /* * Address to scan * Address is fully defined internally and cannot be changed. @@ -82,6 +85,7 @@ static int w83l785ts_attach_adapter(struct i2c_adapter *adapter); static int w83l785ts_detect(struct i2c_adapter *adapter, int address, int kind); static int w83l785ts_detach_client(struct i2c_client *client); +static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval); static void w83l785ts_update_client(struct i2c_client *client); /* @@ -196,10 +200,10 @@ static int w83l785ts_detect(struct i2c_adapter *adapter, int address, int kind) * are skipped. */ if (kind < 0) { /* detection */ - if (((i2c_smbus_read_byte_data(new_client, - W83L785TS_REG_CONFIG) & 0x80) != 0x00) - || ((i2c_smbus_read_byte_data(new_client, - W83L785TS_REG_TYPE) & 0xFC) != 0x00)) { + if (((w83l785ts_read_value(new_client, + W83L785TS_REG_CONFIG, 0) & 0x80) != 0x00) + || ((w83l785ts_read_value(new_client, + W83L785TS_REG_TYPE, 0) & 0xFC) != 0x00)) { dev_dbg(&adapter->dev, "W83L785TS-S detection failed at 0x%02x.\n", address); @@ -211,12 +215,12 @@ static int w83l785ts_detect(struct i2c_adapter *adapter, int address, int kind) u16 man_id; u8 chip_id; - man_id = (i2c_smbus_read_byte_data(new_client, - W83L785TS_REG_MAN_ID1) << 8) + - i2c_smbus_read_byte_data(new_client, - W83L785TS_REG_MAN_ID2); - chip_id = i2c_smbus_read_byte_data(new_client, - W83L785TS_REG_CHIP_ID); + man_id = (w83l785ts_read_value(new_client, + W83L785TS_REG_MAN_ID1, 0) << 8) + + w83l785ts_read_value(new_client, + W83L785TS_REG_MAN_ID2, 0); + chip_id = w83l785ts_read_value(new_client, + W83L785TS_REG_CHIP_ID, 0); if (man_id == 0x5CA3) { /* Winbond */ if (chip_id == 0x70) { /* W83L785TS-S */ @@ -239,6 +243,9 @@ static int w83l785ts_detect(struct i2c_adapter *adapter, int address, int kind) data->valid = 0; init_MUTEX(&data->update_lock); + /* Default values in case the first read fails (unlikely). */ + data->temp_over = data->temp = 0; + /* Tell the I2C layer a new client has arrived. */ if ((err = i2c_attach_client(new_client))) goto exit_free; @@ -274,6 +281,26 @@ static int w83l785ts_detach_client(struct i2c_client *client) return 0; } +static u8 w83l785ts_read_value(struct i2c_client *client, u8 reg, u8 defval) +{ + int value, i; + + /* Frequent read errors have been reported on Asus boards, so we + * retry on read errors. If it still fails (unlikely), return the + * default value requested by the caller. */ + for (i = 1; i <= MAX_RETRIES; i++) { + value = i2c_smbus_read_byte_data(client, reg); + if (value >= 0) + return value; + dev_dbg(&client->dev, "Read failed, will retry in %d.\n", i); + i2c_delay(i); + } + + dev_err(&client->dev, "Couldn't read value from register. " + "Please report.\n"); + return defval; +} + static void w83l785ts_update_client(struct i2c_client *client) { struct w83l785ts_data *data = i2c_get_clientdata(client); @@ -284,10 +311,10 @@ static void w83l785ts_update_client(struct i2c_client *client) || (jiffies - data->last_updated > HZ * 2) || (jiffies < data->last_updated)) { dev_dbg(&client->dev, "Updating w83l785ts data.\n"); - data->temp = i2c_smbus_read_byte_data(client, - W83L785TS_REG_TEMP); - data->temp_over = i2c_smbus_read_byte_data(client, - W83L785TS_REG_TEMP_OVER); + data->temp = w83l785ts_read_value(client, + W83L785TS_REG_TEMP, data->temp); + data->temp_over = w83l785ts_read_value(client, + W83L785TS_REG_TEMP_OVER, data->temp_over); data->last_updated = jiffies; data->valid = 1; -- 2.53.0