The configuration register does not change on its own. Yet, it is read
in various locations, modified, and written back. Simplify and optimize
the code by caching its value and by only writing it back when needed.
Cc: Boyang Yu <byu@arista.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
unsigned int update_interval; /* in milliseconds */
unsigned int update_interval; /* in milliseconds */
+ u8 config; /* Current configuration register value */
u8 config_orig; /* Original configuration register value */
u8 convrate_orig; /* Original conversion rate register value */
u16 alert_alarms; /* Which alarm bits trigger ALERT# */
u8 config_orig; /* Original configuration register value */
u8 convrate_orig; /* Original conversion rate register value */
u16 alert_alarms; /* Which alarm bits trigger ALERT# */
struct lm90_data *data,
int channel)
{
struct lm90_data *data,
int channel)
{
if (data->kind == max6696) {
if (data->kind == max6696) {
- config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
- if (config < 0)
- return config;
- config &= ~0x08;
+ u8 config = data->config & ~0x08;
+ int err;
+
if (channel)
config |= 0x08;
if (channel)
config |= 0x08;
- i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
- config);
+ if (data->config != config) {
+ err = i2c_smbus_write_byte_data(client,
+ LM90_REG_W_CONFIG1,
+ config);
+ if (err)
+ return err;
+ data->config = config;
+ }
static int lm90_write_convrate(struct i2c_client *client,
struct lm90_data *data, int val)
{
static int lm90_write_convrate(struct i2c_client *client,
struct lm90_data *data, int val)
{
+ u8 config = data->config;
- int config_orig, config_stop;
/* Save config and pause conversion */
if (data->flags & LM90_PAUSE_FOR_CONFIG) {
/* Save config and pause conversion */
if (data->flags & LM90_PAUSE_FOR_CONFIG) {
- config_orig = lm90_read_reg(client, LM90_REG_R_CONFIG1);
- if (config_orig < 0)
- return config_orig;
- config_stop = config_orig | 0x40;
- if (config_orig != config_stop) {
+ config |= 0x40;
+ if (data->config != config) {
err = i2c_smbus_write_byte_data(client,
LM90_REG_W_CONFIG1,
err = i2c_smbus_write_byte_data(client,
LM90_REG_W_CONFIG1,
if (err < 0)
return err;
}
if (err < 0)
return err;
}
err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, val);
/* Revert change to config */
err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, val);
/* Revert change to config */
- if (data->flags & LM90_PAUSE_FOR_CONFIG && config_orig != config_stop)
+ if (data->config != config)
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
*/
if (!(data->config_orig & 0x80) &&
!(data->alarms & data->alert_alarms)) {
*/
if (!(data->config_orig & 0x80) &&
!(data->alarms & data->alert_alarms)) {
- val = lm90_read_reg(client, LM90_REG_R_CONFIG1);
- if (val < 0)
- return val;
-
- if (val & 0x80) {
+ if (data->config & 0x80) {
dev_dbg(&client->dev, "Re-enabling ALERT#\n");
dev_dbg(&client->dev, "Re-enabling ALERT#\n");
i2c_smbus_write_byte_data(client,
LM90_REG_W_CONFIG1,
i2c_smbus_write_byte_data(client,
LM90_REG_W_CONFIG1,
if (config < 0)
return config;
data->config_orig = config;
if (config < 0)
return config;
data->config_orig = config;
lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
config &= ~0x08;
config &= 0xBF; /* run */
config &= ~0x08;
config &= 0xBF; /* run */
- if (config != data->config_orig) /* Only write if changed */
+ if (config != data->config) { /* Only write if changed */
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
+ data->config = config;
+ }
return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
}
return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
}
if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
(alarms & data->alert_alarms)) {
if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
(alarms & data->alert_alarms)) {
dev_dbg(&client->dev, "Disabling ALERT#\n");
dev_dbg(&client->dev, "Disabling ALERT#\n");
- config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
- if (config >= 0)
- i2c_smbus_write_byte_data(client,
- LM90_REG_W_CONFIG1,
- config | 0x80);
+ data->config |= 0x80;
+ i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
+ data->config);
}
} else {
dev_info(&client->dev, "Everything OK\n");
}
} else {
dev_info(&client->dev, "Everything OK\n");