Commit 3a2a8cc3 authored by Guenter Roeck's avatar Guenter Roeck

hwmon: (max6697) Convert to use regmap

Use regmap for register caching, and use regmap API for bit operations
to simplify the code.

This patch reduces object file size by approximately 10%.
Reviewed-by: default avatarTzung-Bi Shih <tzungbi@kernel.org>
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent c02b25dc
...@@ -13,10 +13,10 @@ ...@@ -13,10 +13,10 @@
#include <linux/hwmon-sysfs.h> #include <linux/hwmon-sysfs.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/regmap.h>
#include <linux/slab.h> #include <linux/slab.h>
enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694, enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694,
...@@ -82,20 +82,15 @@ struct max6697_chip_data { ...@@ -82,20 +82,15 @@ struct max6697_chip_data {
}; };
struct max6697_data { struct max6697_data {
struct i2c_client *client; struct regmap *regmap;
enum chips type; enum chips type;
const struct max6697_chip_data *chip; const struct max6697_chip_data *chip;
int update_interval; /* in milli-seconds */
int temp_offset; /* in degrees C */ int temp_offset; /* in degrees C */
struct mutex update_lock; struct mutex update_lock;
unsigned long last_updated; /* In jiffies */
bool valid; /* true if following fields are valid */
/* 1x local and up to 7x remote */
u8 temp[8][4]; /* [nr][0]=temp [1]=ext [2]=max [3]=crit */
#define MAX6697_TEMP_INPUT 0 #define MAX6697_TEMP_INPUT 0
#define MAX6697_TEMP_EXT 1 #define MAX6697_TEMP_EXT 1
#define MAX6697_TEMP_MAX 2 #define MAX6697_TEMP_MAX 2
...@@ -189,88 +184,22 @@ static inline int max6581_offset_to_millic(int val) ...@@ -189,88 +184,22 @@ static inline int max6581_offset_to_millic(int val)
return sign_extend32(val, 7) * 250; return sign_extend32(val, 7) * 250;
} }
static struct max6697_data *max6697_update_device(struct device *dev)
{
struct max6697_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
struct max6697_data *ret = data;
int val;
int i;
u32 alarms;
mutex_lock(&data->update_lock);
if (data->valid &&
!time_after(jiffies, data->last_updated
+ msecs_to_jiffies(data->update_interval)))
goto abort;
for (i = 0; i < data->chip->channels; i++) {
if (data->chip->have_ext & BIT(i)) {
val = i2c_smbus_read_byte_data(client,
MAX6697_REG_TEMP_EXT[i]);
if (unlikely(val < 0)) {
ret = ERR_PTR(val);
goto abort;
}
data->temp[i][MAX6697_TEMP_EXT] = val;
}
val = i2c_smbus_read_byte_data(client, MAX6697_REG_TEMP[i]);
if (unlikely(val < 0)) {
ret = ERR_PTR(val);
goto abort;
}
data->temp[i][MAX6697_TEMP_INPUT] = val;
val = i2c_smbus_read_byte_data(client, MAX6697_REG_MAX[i]);
if (unlikely(val < 0)) {
ret = ERR_PTR(val);
goto abort;
}
data->temp[i][MAX6697_TEMP_MAX] = val;
if (data->chip->have_crit & BIT(i)) {
val = i2c_smbus_read_byte_data(client,
MAX6697_REG_CRIT[i]);
if (unlikely(val < 0)) {
ret = ERR_PTR(val);
goto abort;
}
data->temp[i][MAX6697_TEMP_CRIT] = val;
}
}
alarms = 0;
for (i = 0; i < 3; i++) {
val = i2c_smbus_read_byte_data(client, MAX6697_REG_STAT(i));
if (unlikely(val < 0)) {
ret = ERR_PTR(val);
goto abort;
}
alarms = (alarms << 8) | val;
}
data->alarms = alarms;
data->last_updated = jiffies;
data->valid = true;
abort:
mutex_unlock(&data->update_lock);
return ret;
}
static ssize_t temp_input_show(struct device *dev, static ssize_t temp_input_show(struct device *dev,
struct device_attribute *devattr, char *buf) struct device_attribute *devattr, char *buf)
{ {
struct max6697_data *data = dev_get_drvdata(dev);
int index = to_sensor_dev_attr(devattr)->index; int index = to_sensor_dev_attr(devattr)->index;
struct max6697_data *data = max6697_update_device(dev); unsigned int regs[2] = { MAX6697_REG_TEMP[index],
int temp; MAX6697_REG_TEMP_EXT[index] };
u8 regdata[2] = { };
int temp, ret;
if (IS_ERR(data)) ret = regmap_multi_reg_read(data->regmap, regs, regdata,
return PTR_ERR(data); data->chip->have_ext & BIT(index) ? 2 : 1);
if (ret)
return ret;
temp = (data->temp[index][MAX6697_TEMP_INPUT] - data->temp_offset) << 3; temp = ((regdata[0] - data->temp_offset) << 3) | (regdata[1] >> 5);
temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5;
return sprintf(buf, "%d\n", temp * 125); return sprintf(buf, "%d\n", temp * 125);
} }
...@@ -278,33 +207,41 @@ static ssize_t temp_input_show(struct device *dev, ...@@ -278,33 +207,41 @@ static ssize_t temp_input_show(struct device *dev,
static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
char *buf) char *buf)
{ {
int nr = to_sensor_dev_attr_2(devattr)->nr; struct max6697_data *data = dev_get_drvdata(dev);
int index = to_sensor_dev_attr_2(devattr)->index; int index = to_sensor_dev_attr_2(devattr)->index;
struct max6697_data *data = max6697_update_device(dev); int nr = to_sensor_dev_attr_2(devattr)->nr;
int temp; unsigned int temp;
int reg, ret;
if (IS_ERR(data)) if (index == MAX6697_TEMP_MAX)
return PTR_ERR(data); reg = MAX6697_REG_MAX[nr];
else
reg = MAX6697_REG_CRIT[nr];
temp = data->temp[nr][index]; ret = regmap_read(data->regmap, reg, &temp);
temp -= data->temp_offset; if (ret)
return ret;
return sprintf(buf, "%d\n", temp * 1000); return sprintf(buf, "%d\n", ((int)temp - data->temp_offset) * 1000);
} }
static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct max6697_data *data = dev_get_drvdata(dev);
int index = to_sensor_dev_attr(attr)->index; int index = to_sensor_dev_attr(attr)->index;
struct max6697_data *data = max6697_update_device(dev); unsigned int alarms;
int reg, ret;
if (IS_ERR(data))
return PTR_ERR(data);
if (data->chip->alarm_map) if (data->chip->alarm_map)
index = data->chip->alarm_map[index]; index = data->chip->alarm_map[index];
return sprintf(buf, "%u\n", !!(data->alarms & BIT(index))); reg = MAX6697_REG_STAT(2 - (index / 8));
ret = regmap_read(data->regmap, reg, &alarms);
if (ret)
return ret;
return sprintf(buf, "%u\n", !!(alarms & BIT(index & 7)));
} }
static ssize_t temp_store(struct device *dev, static ssize_t temp_store(struct device *dev,
...@@ -321,82 +258,63 @@ static ssize_t temp_store(struct device *dev, ...@@ -321,82 +258,63 @@ static ssize_t temp_store(struct device *dev,
if (ret < 0) if (ret < 0)
return ret; return ret;
mutex_lock(&data->update_lock);
temp = clamp_val(temp, -1000000, 1000000); /* prevent underflow */ temp = clamp_val(temp, -1000000, 1000000); /* prevent underflow */
temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset; temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset;
temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127); temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127);
data->temp[nr][index] = temp; ret = regmap_write(data->regmap,
ret = i2c_smbus_write_byte_data(data->client,
index == 2 ? MAX6697_REG_MAX[nr] index == 2 ? MAX6697_REG_MAX[nr]
: MAX6697_REG_CRIT[nr], : MAX6697_REG_CRIT[nr],
temp); temp);
mutex_unlock(&data->update_lock);
return ret < 0 ? ret : count; return ret ? : count;
} }
static ssize_t offset_store(struct device *dev, struct device_attribute *devattr, const char *buf, static ssize_t offset_store(struct device *dev, struct device_attribute *devattr, const char *buf,
size_t count) size_t count)
{ {
int val, ret, index, select; struct max6697_data *data = dev_get_drvdata(dev);
struct max6697_data *data; int index = to_sensor_dev_attr(devattr)->index;
bool channel_enabled; struct regmap *regmap = data->regmap;
long temp; long temp;
int ret;
index = to_sensor_dev_attr(devattr)->index;
data = dev_get_drvdata(dev);
ret = kstrtol(buf, 10, &temp); ret = kstrtol(buf, 10, &temp);
if (ret < 0) if (ret < 0)
return ret; return ret;
mutex_lock(&data->update_lock); mutex_lock(&data->update_lock);
select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT);
if (select < 0) {
ret = select;
goto abort;
}
channel_enabled = (select & BIT(index - 1));
temp = clamp_val(temp, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX); temp = clamp_val(temp, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX);
val = DIV_ROUND_CLOSEST(temp, 250); temp = DIV_ROUND_CLOSEST(temp, 250);
/* disable the offset for channel if the new offset is 0 */ if (!temp) { /* disable this (and only this) channel */
if (val == 0) { ret = regmap_clear_bits(regmap, MAX6581_REG_OFFSET_SELECT, BIT(index - 1));
if (channel_enabled) goto unlock;
ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT,
select & ~BIT(index - 1));
ret = ret < 0 ? ret : count;
goto abort;
}
if (!channel_enabled) {
ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT,
select | BIT(index - 1));
if (ret < 0)
goto abort;
} }
ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET, val); /* enable channel, and update offset */
ret = ret < 0 ? ret : count; ret = regmap_set_bits(regmap, MAX6581_REG_OFFSET_SELECT, BIT(index - 1));
if (ret)
abort: goto unlock;
ret = regmap_write(regmap, MAX6581_REG_OFFSET, temp);
unlock:
mutex_unlock(&data->update_lock); mutex_unlock(&data->update_lock);
return ret; return ret ? : count;
} }
static ssize_t offset_show(struct device *dev, struct device_attribute *devattr, char *buf) static ssize_t offset_show(struct device *dev, struct device_attribute *devattr, char *buf)
{ {
struct max6697_data *data; unsigned int regs[2] = { MAX6581_REG_OFFSET_SELECT, MAX6581_REG_OFFSET };
int select, ret, index; struct max6697_data *data = dev_get_drvdata(dev);
int index = to_sensor_dev_attr(devattr)->index;
u8 regdata[2];
int ret;
index = to_sensor_dev_attr(devattr)->index; ret = regmap_multi_reg_read(data->regmap, regs, regdata, 2);
data = dev_get_drvdata(dev); if (ret)
mutex_lock(&data->update_lock); return ret;
select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT);
if (select < 0) if (!(regdata[0] & BIT(index - 1)))
ret = select; regdata[1] = 0;
else if (select & BIT(index - 1))
ret = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET); return sprintf(buf, "%d\n", max6581_offset_to_millic(regdata[1]));
else
ret = 0;
mutex_unlock(&data->update_lock);
return ret < 0 ? ret : sprintf(buf, "%d\n", max6581_offset_to_millic(ret));
} }
static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
...@@ -569,12 +487,11 @@ static const struct attribute_group max6697_group = { ...@@ -569,12 +487,11 @@ static const struct attribute_group max6697_group = {
}; };
__ATTRIBUTE_GROUPS(max6697); __ATTRIBUTE_GROUPS(max6697);
static int max6697_config_of(struct max6697_data *data, struct i2c_client *client) static int max6697_config_of(struct device_node *node, struct max6697_data *data)
{ {
const struct max6697_chip_data *chip = data->chip; const struct max6697_chip_data *chip = data->chip;
struct device_node *node = client->dev.of_node; struct regmap *regmap = data->regmap;
int ret, confreg; int ret, confreg;
int factor = 0;
u32 vals[2]; u32 vals[2];
confreg = 0; confreg = 0;
...@@ -594,14 +511,14 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien ...@@ -594,14 +511,14 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien
if (of_property_read_u32(node, "alert-mask", vals)) if (of_property_read_u32(node, "alert-mask", vals))
vals[0] = 0; vals[0] = 0;
ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK, ret = regmap_write(regmap, MAX6697_REG_ALERT_MASK,
MAX6697_ALERT_MAP_BITS(vals[0])); MAX6697_ALERT_MAP_BITS(vals[0]));
if (ret) if (ret)
return ret; return ret;
if (of_property_read_u32(node, "over-temperature-mask", vals)) if (of_property_read_u32(node, "over-temperature-mask", vals))
vals[0] = 0; vals[0] = 0;
ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK, ret = regmap_write(regmap, MAX6697_REG_OVERT_MASK,
MAX6697_OVERT_MAP_BITS(vals[0])); MAX6697_OVERT_MAP_BITS(vals[0]));
if (ret) if (ret)
return ret; return ret;
...@@ -610,7 +527,6 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien ...@@ -610,7 +527,6 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien
if (of_property_read_bool(node, "resistance-cancellation") && if (of_property_read_bool(node, "resistance-cancellation") &&
chip->valid_conf & MAX6697_CONF_RESISTANCE) { chip->valid_conf & MAX6697_CONF_RESISTANCE) {
confreg |= MAX6697_CONF_RESISTANCE; confreg |= MAX6697_CONF_RESISTANCE;
factor = 1;
} }
} else { } else {
if (of_property_read_u32(node, "resistance-cancellation", &vals[0])) { if (of_property_read_u32(node, "resistance-cancellation", &vals[0])) {
...@@ -621,9 +537,7 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien ...@@ -621,9 +537,7 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien
} }
vals[0] &= 0xfe; vals[0] &= 0xfe;
factor = hweight8(vals[0]); ret = regmap_write(regmap, MAX6581_REG_RESISTANCE, vals[0] >> 1);
ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE,
vals[0] >> 1);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -632,81 +546,92 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien ...@@ -632,81 +546,92 @@ static int max6697_config_of(struct max6697_data *data, struct i2c_client *clien
vals[1] = 0; vals[1] = 0;
} }
ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY, ret = regmap_write(regmap, MAX6581_REG_IDEALITY, vals[1]);
vals[1]);
if (ret < 0) if (ret < 0)
return ret; return ret;
ret = i2c_smbus_write_byte_data(client, ret = regmap_write(regmap, MAX6581_REG_IDEALITY_SELECT,
MAX6581_REG_IDEALITY_SELECT,
(vals[0] & 0xfe) >> 1); (vals[0] & 0xfe) >> 1);
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, confreg); return regmap_write(regmap, MAX6697_REG_CONFIG, confreg);
if (ret < 0)
return ret;
return factor;
} }
static int max6697_init_chip(struct max6697_data *data, static int max6697_init_chip(struct device_node *np, struct max6697_data *data)
struct i2c_client *client)
{ {
const struct max6697_chip_data *chip = data->chip; unsigned int reg;
int factor = chip->channels; int ret;
int ret, reg;
/* /*
* Don't touch configuration if there is no devicetree configuration. * Don't touch configuration if there is no devicetree configuration.
* If that is the case, use the current chip configuration. * If that is the case, use the current chip configuration.
*/ */
if (!client->dev.of_node) { if (!np) {
reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG); struct regmap *regmap = data->regmap;
if (reg < 0)
return reg; ret = regmap_read(regmap, MAX6697_REG_CONFIG, &reg);
if (ret < 0)
return ret;
if (data->type == max6581) { if (data->type == max6581) {
if (reg & MAX6581_CONF_EXTENDED) if (reg & MAX6581_CONF_EXTENDED)
data->temp_offset = 64; data->temp_offset = 64;
reg = i2c_smbus_read_byte_data(client, ret = regmap_read(regmap, MAX6581_REG_RESISTANCE, &reg);
MAX6581_REG_RESISTANCE);
if (reg < 0)
return reg;
factor += hweight8(reg);
} else {
if (reg & MAX6697_CONF_RESISTANCE)
factor++;
} }
data->update_interval = factor * MAX6697_CONV_TIME;
} else { } else {
ret = max6697_config_of(data, client); ret = max6697_config_of(np, data);
if (ret < 0) }
return ret; return ret;
data->update_interval = (factor + ret) * MAX6697_CONV_TIME; }
static bool max6697_volatile_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0x00 ... 0x09: /* temperature high bytes */
case 0x44 ... 0x47: /* status */
case 0x51 ... 0x58: /* temperature low bytes */
return true;
default:
return false;
} }
}
return 0; static bool max6697_writeable_reg(struct device *dev, unsigned int reg)
{
return reg != 0x0a && reg != 0x0f && !max6697_volatile_reg(dev, reg);
} }
static const struct regmap_config max6697_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = 0x58,
.writeable_reg = max6697_writeable_reg,
.volatile_reg = max6697_volatile_reg,
.cache_type = REGCACHE_MAPLE,
};
static int max6697_probe(struct i2c_client *client) static int max6697_probe(struct i2c_client *client)
{ {
struct i2c_adapter *adapter = client->adapter;
struct device *dev = &client->dev; struct device *dev = &client->dev;
struct max6697_data *data; struct max6697_data *data;
struct device *hwmon_dev; struct device *hwmon_dev;
struct regmap *regmap;
int err; int err;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) regmap = regmap_init_i2c(client, &max6697_regmap_config);
return -ENODEV; if (IS_ERR(regmap))
return PTR_ERR(regmap);
data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL); data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
data->regmap = regmap;
data->type = (uintptr_t)i2c_get_match_data(client); data->type = (uintptr_t)i2c_get_match_data(client);
data->chip = &max6697_chip_data[data->type]; data->chip = &max6697_chip_data[data->type];
data->client = client;
mutex_init(&data->update_lock); mutex_init(&data->update_lock);
err = max6697_init_chip(data, client); err = max6697_init_chip(client->dev.of_node, data);
if (err) if (err)
return err; return err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment