Commit 4cab259f authored by Guenter Roeck's avatar Guenter Roeck

hwmon: (emc1403) Convert to use regmap

Convert to regmap to be able to use its register caching mechanism.
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent 57d60b1b
...@@ -18,9 +18,6 @@ ...@@ -18,9 +18,6 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* TODO
* - cache alarm and critical limit registers
*/ */
#include <linux/module.h> #include <linux/module.h>
...@@ -32,7 +29,7 @@ ...@@ -32,7 +29,7 @@
#include <linux/err.h> #include <linux/err.h>
#include <linux/sysfs.h> #include <linux/sysfs.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/jiffies.h> #include <linux/regmap.h>
#define THERMAL_PID_REG 0xfd #define THERMAL_PID_REG 0xfd
#define THERMAL_SMSC_ID_REG 0xfe #define THERMAL_SMSC_ID_REG 0xfe
...@@ -41,15 +38,9 @@ ...@@ -41,15 +38,9 @@
enum emc1403_chip { emc1402, emc1403, emc1404 }; enum emc1403_chip { emc1402, emc1403, emc1404 };
struct thermal_data { struct thermal_data {
struct i2c_client *client; struct regmap *regmap;
const struct attribute_group *groups[4];
struct mutex mutex; struct mutex mutex;
/* const struct attribute_group *groups[4];
* Cache the hyst value so we don't keep re-reading it. In theory
* we could cache it forever as nobody else should be writing it.
*/
u8 cached_hyst;
unsigned long hyst_valid;
}; };
static ssize_t show_temp(struct device *dev, static ssize_t show_temp(struct device *dev,
...@@ -57,12 +48,13 @@ static ssize_t show_temp(struct device *dev, ...@@ -57,12 +48,13 @@ static ssize_t show_temp(struct device *dev,
{ {
struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
struct thermal_data *data = dev_get_drvdata(dev); struct thermal_data *data = dev_get_drvdata(dev);
unsigned int val;
int retval; int retval;
retval = i2c_smbus_read_byte_data(data->client, sda->index); retval = regmap_read(data->regmap, sda->index, &val);
if (retval < 0) if (retval < 0)
return retval; return retval;
return sprintf(buf, "%d000\n", retval); return sprintf(buf, "%d000\n", val);
} }
static ssize_t show_bit(struct device *dev, static ssize_t show_bit(struct device *dev,
...@@ -70,12 +62,13 @@ static ssize_t show_bit(struct device *dev, ...@@ -70,12 +62,13 @@ static ssize_t show_bit(struct device *dev,
{ {
struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
struct thermal_data *data = dev_get_drvdata(dev); struct thermal_data *data = dev_get_drvdata(dev);
unsigned int val;
int retval; int retval;
retval = i2c_smbus_read_byte_data(data->client, sda->nr); retval = regmap_read(data->regmap, sda->nr, &val);
if (retval < 0) if (retval < 0)
return retval; return retval;
return sprintf(buf, "%d\n", !!(retval & sda->index)); return sprintf(buf, "%d\n", !!(val & sda->index));
} }
static ssize_t store_temp(struct device *dev, static ssize_t store_temp(struct device *dev,
...@@ -88,8 +81,8 @@ static ssize_t store_temp(struct device *dev, ...@@ -88,8 +81,8 @@ static ssize_t store_temp(struct device *dev,
if (kstrtoul(buf, 10, &val)) if (kstrtoul(buf, 10, &val))
return -EINVAL; return -EINVAL;
retval = i2c_smbus_write_byte_data(data->client, sda->index, retval = regmap_write(data->regmap, sda->index,
DIV_ROUND_CLOSEST(val, 1000)); DIV_ROUND_CLOSEST(val, 1000));
if (retval < 0) if (retval < 0)
return retval; return retval;
return count; return count;
...@@ -100,28 +93,17 @@ static ssize_t store_bit(struct device *dev, ...@@ -100,28 +93,17 @@ static ssize_t store_bit(struct device *dev,
{ {
struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
struct thermal_data *data = dev_get_drvdata(dev); struct thermal_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
unsigned long val; unsigned long val;
int retval; int retval;
if (kstrtoul(buf, 10, &val)) if (kstrtoul(buf, 10, &val))
return -EINVAL; return -EINVAL;
mutex_lock(&data->mutex); retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
retval = i2c_smbus_read_byte_data(client, sda->nr); val ? sda->index : 0);
if (retval < 0) if (retval < 0)
goto fail; return retval;
return count;
retval &= ~sda->index;
if (val)
retval |= sda->index;
retval = i2c_smbus_write_byte_data(client, sda->index, retval);
if (retval == 0)
retval = count;
fail:
mutex_unlock(&data->mutex);
return retval;
} }
static ssize_t show_hyst(struct device *dev, static ssize_t show_hyst(struct device *dev,
...@@ -129,22 +111,20 @@ static ssize_t show_hyst(struct device *dev, ...@@ -129,22 +111,20 @@ static ssize_t show_hyst(struct device *dev,
{ {
struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
struct thermal_data *data = dev_get_drvdata(dev); struct thermal_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client; struct regmap *regmap = data->regmap;
unsigned int limit;
unsigned int hyst;
int retval; int retval;
int hyst;
retval = i2c_smbus_read_byte_data(client, sda->index); retval = regmap_read(regmap, sda->index, &limit);
if (retval < 0) if (retval < 0)
return retval; return retval;
if (time_after(jiffies, data->hyst_valid)) { retval = regmap_read(regmap, 0x21, &hyst);
hyst = i2c_smbus_read_byte_data(client, 0x21); if (retval < 0)
if (hyst < 0) return retval;
return retval;
data->cached_hyst = hyst; return sprintf(buf, "%d000\n", limit - hyst);
data->hyst_valid = jiffies + HZ;
}
return sprintf(buf, "%d000\n", retval - data->cached_hyst);
} }
static ssize_t store_hyst(struct device *dev, static ssize_t store_hyst(struct device *dev,
...@@ -152,7 +132,8 @@ static ssize_t store_hyst(struct device *dev, ...@@ -152,7 +132,8 @@ static ssize_t store_hyst(struct device *dev,
{ {
struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
struct thermal_data *data = dev_get_drvdata(dev); struct thermal_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client; struct regmap *regmap = data->regmap;
unsigned int limit;
int retval; int retval;
int hyst; int hyst;
unsigned long val; unsigned long val;
...@@ -161,23 +142,20 @@ static ssize_t store_hyst(struct device *dev, ...@@ -161,23 +142,20 @@ static ssize_t store_hyst(struct device *dev,
return -EINVAL; return -EINVAL;
mutex_lock(&data->mutex); mutex_lock(&data->mutex);
retval = i2c_smbus_read_byte_data(client, sda->index); retval = regmap_read(regmap, sda->index, &limit);
if (retval < 0) if (retval < 0)
goto fail; goto fail;
hyst = retval * 1000 - val; hyst = limit * 1000 - val;
hyst = DIV_ROUND_CLOSEST(hyst, 1000); hyst = DIV_ROUND_CLOSEST(hyst, 1000);
if (hyst < 0 || hyst > 255) { if (hyst < 0 || hyst > 255) {
retval = -ERANGE; retval = -ERANGE;
goto fail; goto fail;
} }
retval = i2c_smbus_write_byte_data(client, 0x21, hyst); retval = regmap_write(regmap, 0x21, hyst);
if (retval == 0) { if (retval == 0)
retval = count; retval = count;
data->cached_hyst = hyst;
data->hyst_valid = jiffies + HZ;
}
fail: fail:
mutex_unlock(&data->mutex); mutex_unlock(&data->mutex);
return retval; return retval;
...@@ -356,6 +334,35 @@ static int emc1403_detect(struct i2c_client *client, ...@@ -356,6 +334,35 @@ static int emc1403_detect(struct i2c_client *client,
return 0; return 0;
} }
static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0x00: /* internal diode high byte */
case 0x01: /* external diode 1 high byte */
case 0x02: /* status */
case 0x10: /* external diode 1 low byte */
case 0x1b: /* external diode fault */
case 0x23: /* external diode 2 high byte */
case 0x24: /* external diode 2 low byte */
case 0x29: /* internal diode low byte */
case 0x2a: /* externl diode 3 high byte */
case 0x2b: /* external diode 3 low byte */
case 0x35: /* high limit status */
case 0x36: /* low limit status */
case 0x37: /* therm limit status */
return true;
default:
return false;
}
}
static struct regmap_config emc1403_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.cache_type = REGCACHE_RBTREE,
.volatile_reg = emc1403_regmap_is_volatile,
};
static int emc1403_probe(struct i2c_client *client, static int emc1403_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
...@@ -367,9 +374,11 @@ static int emc1403_probe(struct i2c_client *client, ...@@ -367,9 +374,11 @@ static int emc1403_probe(struct i2c_client *client,
if (data == NULL) if (data == NULL)
return -ENOMEM; return -ENOMEM;
data->client = client; data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
if (IS_ERR(data->regmap))
return PTR_ERR(data->regmap);
mutex_init(&data->mutex); mutex_init(&data->mutex);
data->hyst_valid = jiffies - 1; /* Expired */
switch (id->driver_data) { switch (id->driver_data) {
case emc1404: case emc1404:
......
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