hwmon: (lm95241) Add support for fault attributes
[cascardo/linux.git] / drivers / hwmon / lm95241.c
index cdf19ad..e4e7bf1 100644 (file)
@@ -59,6 +59,7 @@ static const unsigned short normal_i2c[] = {
 #define CFG_CR0182 0x10
 #define CFG_CR1000 0x20
 #define CFG_CR2700 0x30
+#define CFG_CRMASK 0x30
 #define R1MS_SHIFT 0
 #define R2MS_SHIFT 2
 #define R1MS_MASK (0x01 << (R1MS_SHIFT))
@@ -69,6 +70,8 @@ static const unsigned short normal_i2c[] = {
 #define R2DF_MASK (0x01 << (R2DF_SHIFT))
 #define R1FE_MASK 0x01
 #define R2FE_MASK 0x05
+#define R1DM 0x01
+#define R2DM 0x02
 #define TT1_SHIFT 0
 #define TT2_SHIFT 4
 #define TT_OFF 0
@@ -91,11 +94,12 @@ static const u8 lm95241_reg_address[] = {
 struct lm95241_data {
        struct i2c_client *client;
        struct mutex update_lock;
-       unsigned long last_updated, interval;   /* in jiffies */
+       unsigned long last_updated;     /* in jiffies */
+       unsigned long interval;         /* in milli-seconds */
        char valid;             /* zero until following fields are valid */
        /* registers values */
        u8 temp[ARRAY_SIZE(lm95241_reg_address)];
-       u8 config, model, trutherm;
+       u8 status, config, model, trutherm;
 };
 
 /* Conversions */
@@ -118,7 +122,8 @@ static struct lm95241_data *lm95241_update_device(struct device *dev)
 
        mutex_lock(&data->update_lock);
 
-       if (time_after(jiffies, data->last_updated + data->interval) ||
+       if (time_after(jiffies, data->last_updated
+                      + msecs_to_jiffies(data->interval)) ||
            !data->valid) {
                int i;
 
@@ -127,6 +132,9 @@ static struct lm95241_data *lm95241_update_device(struct device *dev)
                        data->temp[i]
                          = i2c_smbus_read_byte_data(client,
                                                     lm95241_reg_address[i]);
+
+               data->status = i2c_smbus_read_byte_data(client,
+                                                       LM95241_REG_R_STATUS);
                data->last_updated = jiffies;
                data->valid = 1;
        }
@@ -271,13 +279,21 @@ static ssize_t set_max(struct device *dev, struct device_attribute *attr,
        return count;
 }
 
+static ssize_t show_fault(struct device *dev, struct device_attribute *attr,
+                         char *buf)
+{
+       struct lm95241_data *data = lm95241_update_device(dev);
+
+       return snprintf(buf, PAGE_SIZE - 1, "%d",
+                       !!(data->status & to_sensor_dev_attr(attr)->index));
+}
+
 static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
                             char *buf)
 {
        struct lm95241_data *data = lm95241_update_device(dev);
 
-       return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval
-                       / HZ);
+       return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval);
 }
 
 static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
@@ -285,11 +301,35 @@ static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
 {
        struct lm95241_data *data = dev_get_drvdata(dev);
        unsigned long val;
+       int convrate;
+       u8 config;
 
        if (kstrtoul(buf, 10, &val) < 0)
                return -EINVAL;
 
-       data->interval = val * HZ / 1000;
+       mutex_lock(&data->update_lock);
+
+       config = data->config & ~CFG_CRMASK;
+
+       if (val < 130) {
+               convrate = 76;
+               config |= CFG_CR0076;
+       } else if (val < 590) {
+               convrate = 182;
+               config |= CFG_CR0182;
+       } else if (val < 1850) {
+               convrate = 1000;
+               config |= CFG_CR1000;
+       } else {
+               convrate = 2700;
+               config |= CFG_CR2700;
+       }
+
+       data->interval = convrate;
+       data->config = config;
+       i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
+                                 config);
+       mutex_unlock(&data->update_lock);
 
        return count;
 }
@@ -309,6 +349,8 @@ static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
                          R1DF_MASK);
 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
                          R2DF_MASK);
+static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, R1DM);
+static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, R2DM);
 static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
                   set_interval);
 
@@ -322,6 +364,8 @@ static struct attribute *lm95241_attrs[] = {
        &sensor_dev_attr_temp3_min.dev_attr.attr,
        &sensor_dev_attr_temp2_max.dev_attr.attr,
        &sensor_dev_attr_temp3_max.dev_attr.attr,
+       &sensor_dev_attr_temp2_fault.dev_attr.attr,
+       &sensor_dev_attr_temp3_fault.dev_attr.attr,
        &dev_attr_update_interval.attr,
        NULL
 };
@@ -362,8 +406,8 @@ static int lm95241_detect(struct i2c_client *new_client,
 static void lm95241_init_client(struct i2c_client *client,
                                struct lm95241_data *data)
 {
-       data->interval = HZ;    /* 1 sec default */
-       data->config = CFG_CR0076;
+       data->interval = 1000;
+       data->config = CFG_CR1000;
        data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
 
        i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);