Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * STTS751 sensor driver
0004  *
0005  * Copyright (C) 2016-2017 Istituto Italiano di Tecnologia - RBCS - EDL
0006  * Robotics, Brain and Cognitive Sciences department
0007  * Electronic Design Laboratory
0008  *
0009  * Written by Andrea Merello <andrea.merello@gmail.com>
0010  *
0011  * Based on  LM95241 driver and LM90 driver
0012  */
0013 
0014 #include <linux/bitops.h>
0015 #include <linux/err.h>
0016 #include <linux/hwmon.h>
0017 #include <linux/hwmon-sysfs.h>
0018 #include <linux/i2c.h>
0019 #include <linux/init.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/jiffies.h>
0022 #include <linux/module.h>
0023 #include <linux/mutex.h>
0024 #include <linux/property.h>
0025 #include <linux/slab.h>
0026 #include <linux/sysfs.h>
0027 #include <linux/util_macros.h>
0028 
0029 #define DEVNAME "stts751"
0030 
0031 static const unsigned short normal_i2c[] = {
0032     0x48, 0x49, 0x38, 0x39,  /* STTS751-0 */
0033     0x4A, 0x4B, 0x3A, 0x3B,  /* STTS751-1 */
0034     I2C_CLIENT_END };
0035 
0036 #define STTS751_REG_TEMP_H  0x00
0037 #define STTS751_REG_STATUS  0x01
0038 #define STTS751_STATUS_TRIPT    BIT(0)
0039 #define STTS751_STATUS_TRIPL    BIT(5)
0040 #define STTS751_STATUS_TRIPH    BIT(6)
0041 #define STTS751_REG_TEMP_L  0x02
0042 #define STTS751_REG_CONF    0x03
0043 #define STTS751_CONF_RES_MASK   0x0C
0044 #define STTS751_CONF_RES_SHIFT  2
0045 #define STTS751_CONF_EVENT_DIS  BIT(7)
0046 #define STTS751_CONF_STOP   BIT(6)
0047 #define STTS751_REG_RATE    0x04
0048 #define STTS751_REG_HLIM_H  0x05
0049 #define STTS751_REG_HLIM_L  0x06
0050 #define STTS751_REG_LLIM_H  0x07
0051 #define STTS751_REG_LLIM_L  0x08
0052 #define STTS751_REG_TLIM    0x20
0053 #define STTS751_REG_HYST    0x21
0054 #define STTS751_REG_SMBUS_TO    0x22
0055 
0056 #define STTS751_REG_PROD_ID 0xFD
0057 #define STTS751_REG_MAN_ID  0xFE
0058 #define STTS751_REG_REV_ID  0xFF
0059 
0060 #define STTS751_0_PROD_ID   0x00
0061 #define STTS751_1_PROD_ID   0x01
0062 #define ST_MAN_ID       0x53
0063 
0064 /*
0065  * Possible update intervals are (in mS):
0066  * 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62.5, 31.25
0067  * However we are not going to complicate things too much and we stick to the
0068  * approx value in mS.
0069  */
0070 static const int stts751_intervals[] = {
0071     16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 31
0072 };
0073 
0074 static const struct i2c_device_id stts751_id[] = {
0075     { "stts751", 0 },
0076     { }
0077 };
0078 
0079 static const struct of_device_id __maybe_unused stts751_of_match[] = {
0080     { .compatible = "stts751" },
0081     { },
0082 };
0083 MODULE_DEVICE_TABLE(of, stts751_of_match);
0084 
0085 struct stts751_priv {
0086     struct device *dev;
0087     struct i2c_client *client;
0088     struct mutex access_lock;
0089     u8 interval;
0090     int res;
0091     int event_max, event_min;
0092     int therm;
0093     int hyst;
0094     bool smbus_timeout;
0095     int temp;
0096     unsigned long last_update, last_alert_update;
0097     u8 config;
0098     bool min_alert, max_alert, therm_trip;
0099     bool data_valid, alert_valid;
0100     bool notify_max, notify_min;
0101 };
0102 
0103 /*
0104  * These functions converts temperature from HW format to integer format and
0105  * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
0106  */
0107 static int stts751_to_deg(s16 hw_val)
0108 {
0109     return hw_val * 125 / 32;
0110 }
0111 
0112 static s32 stts751_to_hw(int val)
0113 {
0114     return DIV_ROUND_CLOSEST(val, 125) * 32;
0115 }
0116 
0117 static int stts751_adjust_resolution(struct stts751_priv *priv)
0118 {
0119     u8 res;
0120 
0121     switch (priv->interval) {
0122     case 9:
0123         /* 10 bits */
0124         res = 0;
0125         break;
0126     case 8:
0127         /* 11 bits */
0128         res = 1;
0129         break;
0130     default:
0131         /* 12 bits */
0132         res = 3;
0133         break;
0134     }
0135 
0136     if (priv->res == res)
0137         return 0;
0138 
0139     priv->config &= ~STTS751_CONF_RES_MASK;
0140     priv->config |= res << STTS751_CONF_RES_SHIFT;
0141     dev_dbg(&priv->client->dev, "setting res %d. config %x",
0142         res, priv->config);
0143     priv->res = res;
0144 
0145     return i2c_smbus_write_byte_data(priv->client,
0146                 STTS751_REG_CONF, priv->config);
0147 }
0148 
0149 static int stts751_update_temp(struct stts751_priv *priv)
0150 {
0151     s32 integer1, integer2, frac;
0152 
0153     /*
0154      * There is a trick here, like in the lm90 driver. We have to read two
0155      * registers to get the sensor temperature, but we have to beware a
0156      * conversion could occur between the readings. We could use the
0157      * one-shot conversion register, but we don't want to do this (disables
0158      * hardware monitoring). So the solution used here is to read the high
0159      * byte once, then the low byte, then the high byte again. If the new
0160      * high byte matches the old one, then we have a valid reading. Else we
0161      * have to read the low byte again, and now we believe we have a correct
0162      * reading.
0163      */
0164     integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
0165     if (integer1 < 0) {
0166         dev_dbg(&priv->client->dev,
0167             "I2C read failed (temp H). ret: %x\n", integer1);
0168         return integer1;
0169     }
0170 
0171     frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L);
0172     if (frac < 0) {
0173         dev_dbg(&priv->client->dev,
0174             "I2C read failed (temp L). ret: %x\n", frac);
0175         return frac;
0176     }
0177 
0178     integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
0179     if (integer2 < 0) {
0180         dev_dbg(&priv->client->dev,
0181             "I2C 2nd read failed (temp H). ret: %x\n", integer2);
0182         return integer2;
0183     }
0184 
0185     if (integer1 != integer2) {
0186         frac = i2c_smbus_read_byte_data(priv->client,
0187                         STTS751_REG_TEMP_L);
0188         if (frac < 0) {
0189             dev_dbg(&priv->client->dev,
0190                 "I2C 2nd read failed (temp L). ret: %x\n",
0191                 frac);
0192             return frac;
0193         }
0194     }
0195 
0196     priv->temp = stts751_to_deg((integer1 << 8) | frac);
0197     return 0;
0198 }
0199 
0200 static int stts751_set_temp_reg16(struct stts751_priv *priv, int temp,
0201                   u8 hreg, u8 lreg)
0202 {
0203     s32 hwval;
0204     int ret;
0205 
0206     hwval = stts751_to_hw(temp);
0207 
0208     ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8);
0209     if (ret)
0210         return ret;
0211 
0212     return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff);
0213 }
0214 
0215 static int stts751_set_temp_reg8(struct stts751_priv *priv, int temp, u8 reg)
0216 {
0217     s32 hwval;
0218 
0219     hwval = stts751_to_hw(temp);
0220     return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8);
0221 }
0222 
0223 static int stts751_read_reg16(struct stts751_priv *priv, int *temp,
0224                   u8 hreg, u8 lreg)
0225 {
0226     int integer, frac;
0227 
0228     integer = i2c_smbus_read_byte_data(priv->client, hreg);
0229     if (integer < 0)
0230         return integer;
0231 
0232     frac = i2c_smbus_read_byte_data(priv->client, lreg);
0233     if (frac < 0)
0234         return frac;
0235 
0236     *temp = stts751_to_deg((integer << 8) | frac);
0237 
0238     return 0;
0239 }
0240 
0241 static int stts751_read_reg8(struct stts751_priv *priv, int *temp, u8 reg)
0242 {
0243     int integer;
0244 
0245     integer = i2c_smbus_read_byte_data(priv->client, reg);
0246     if (integer < 0)
0247         return integer;
0248 
0249     *temp = stts751_to_deg(integer << 8);
0250 
0251     return 0;
0252 }
0253 
0254 /*
0255  * Update alert flags without waiting for cache to expire. We detects alerts
0256  * immediately for the sake of the alert handler; we still need to deal with
0257  * caching to workaround the fact that alarm flags int the status register,
0258  * despite what the datasheet claims, gets always cleared on read.
0259  */
0260 static int stts751_update_alert(struct stts751_priv *priv)
0261 {
0262     int ret;
0263     bool conv_done;
0264     int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
0265 
0266     /*
0267      * Add another 10% because if we run faster than the HW conversion
0268      * rate we will end up in reporting incorrectly alarms.
0269      */
0270     cache_time += cache_time / 10;
0271 
0272     ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS);
0273     if (ret < 0)
0274         return ret;
0275 
0276     dev_dbg(&priv->client->dev, "status reg %x\n", ret);
0277     conv_done = ret & (STTS751_STATUS_TRIPH | STTS751_STATUS_TRIPL);
0278     /*
0279      * Reset the cache if the cache time expired, or if we are sure
0280      * we have valid data from a device conversion, or if we know
0281      * our cache has been never written.
0282      *
0283      * Note that when the cache has been never written the point is
0284      * to correctly initialize the timestamp, rather than clearing
0285      * the cache values.
0286      *
0287      * Note that updating the cache timestamp when we get an alarm flag
0288      * is required, otherwise we could incorrectly report alarms to be zero.
0289      */
0290     if (time_after(jiffies, priv->last_alert_update + cache_time) ||
0291         conv_done || !priv->alert_valid) {
0292         priv->max_alert = false;
0293         priv->min_alert = false;
0294         priv->alert_valid = true;
0295         priv->last_alert_update = jiffies;
0296         dev_dbg(&priv->client->dev, "invalidating alert cache\n");
0297     }
0298 
0299     priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH);
0300     priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL);
0301     priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT);
0302 
0303     dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n",
0304         priv->max_alert, priv->min_alert, priv->therm_trip);
0305 
0306     return 0;
0307 }
0308 
0309 static void stts751_alert(struct i2c_client *client,
0310               enum i2c_alert_protocol type, unsigned int data)
0311 {
0312     int ret;
0313     struct stts751_priv *priv = i2c_get_clientdata(client);
0314 
0315     if (type != I2C_PROTOCOL_SMBUS_ALERT)
0316         return;
0317 
0318     dev_dbg(&client->dev, "alert!");
0319 
0320     mutex_lock(&priv->access_lock);
0321     ret = stts751_update_alert(priv);
0322     if (ret < 0) {
0323         /* default to worst case */
0324         priv->max_alert = true;
0325         priv->min_alert = true;
0326 
0327         dev_warn(priv->dev,
0328              "Alert received, but can't communicate to the device. Triggering all alarms!");
0329     }
0330 
0331     if (priv->max_alert) {
0332         if (priv->notify_max)
0333             dev_notice(priv->dev, "got alert for HIGH temperature");
0334         priv->notify_max = false;
0335 
0336         /* unblock alert poll */
0337         sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm");
0338     }
0339 
0340     if (priv->min_alert) {
0341         if (priv->notify_min)
0342             dev_notice(priv->dev, "got alert for LOW temperature");
0343         priv->notify_min = false;
0344 
0345         /* unblock alert poll */
0346         sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm");
0347     }
0348 
0349     if (priv->min_alert || priv->max_alert)
0350         kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE);
0351 
0352     mutex_unlock(&priv->access_lock);
0353 }
0354 
0355 static int stts751_update(struct stts751_priv *priv)
0356 {
0357     int ret;
0358     int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
0359 
0360     if (time_after(jiffies, priv->last_update + cache_time) ||
0361         !priv->data_valid) {
0362         ret = stts751_update_temp(priv);
0363         if (ret)
0364             return ret;
0365 
0366         ret = stts751_update_alert(priv);
0367         if (ret)
0368             return ret;
0369         priv->data_valid = true;
0370         priv->last_update = jiffies;
0371     }
0372 
0373     return 0;
0374 }
0375 
0376 static ssize_t max_alarm_show(struct device *dev,
0377                   struct device_attribute *attr, char *buf)
0378 {
0379     int ret;
0380     struct stts751_priv *priv = dev_get_drvdata(dev);
0381 
0382     mutex_lock(&priv->access_lock);
0383     ret = stts751_update(priv);
0384     if (!ret)
0385         priv->notify_max = true;
0386     mutex_unlock(&priv->access_lock);
0387     if (ret < 0)
0388         return ret;
0389 
0390     return sysfs_emit(buf, "%d\n", priv->max_alert);
0391 }
0392 
0393 static ssize_t min_alarm_show(struct device *dev,
0394                   struct device_attribute *attr, char *buf)
0395 {
0396     int ret;
0397     struct stts751_priv *priv = dev_get_drvdata(dev);
0398 
0399     mutex_lock(&priv->access_lock);
0400     ret = stts751_update(priv);
0401     if (!ret)
0402         priv->notify_min = true;
0403     mutex_unlock(&priv->access_lock);
0404     if (ret < 0)
0405         return ret;
0406 
0407     return sysfs_emit(buf, "%d\n", priv->min_alert);
0408 }
0409 
0410 static ssize_t input_show(struct device *dev, struct device_attribute *attr,
0411               char *buf)
0412 {
0413     int ret;
0414     struct stts751_priv *priv = dev_get_drvdata(dev);
0415 
0416     mutex_lock(&priv->access_lock);
0417     ret = stts751_update(priv);
0418     mutex_unlock(&priv->access_lock);
0419     if (ret < 0)
0420         return ret;
0421 
0422     return sysfs_emit(buf, "%d\n", priv->temp);
0423 }
0424 
0425 static ssize_t therm_show(struct device *dev, struct device_attribute *attr,
0426               char *buf)
0427 {
0428     struct stts751_priv *priv = dev_get_drvdata(dev);
0429 
0430     return sysfs_emit(buf, "%d\n", priv->therm);
0431 }
0432 
0433 static ssize_t therm_store(struct device *dev, struct device_attribute *attr,
0434                const char *buf, size_t count)
0435 {
0436     int ret;
0437     long temp;
0438     struct stts751_priv *priv = dev_get_drvdata(dev);
0439 
0440     if (kstrtol(buf, 10, &temp) < 0)
0441         return -EINVAL;
0442 
0443     /* HW works in range -64C to +127.937C */
0444     temp = clamp_val(temp, -64000, 127937);
0445     mutex_lock(&priv->access_lock);
0446     ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_TLIM);
0447     if (ret)
0448         goto exit;
0449 
0450     dev_dbg(&priv->client->dev, "setting therm %ld", temp);
0451 
0452     /*
0453      * hysteresis reg is relative to therm, so the HW does not need to be
0454      * adjusted, we need to update our local copy only.
0455      */
0456     priv->hyst = temp - (priv->therm - priv->hyst);
0457     priv->therm = temp;
0458 
0459 exit:
0460     mutex_unlock(&priv->access_lock);
0461     if (ret)
0462         return ret;
0463 
0464     return count;
0465 }
0466 
0467 static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
0468              char *buf)
0469 {
0470     struct stts751_priv *priv = dev_get_drvdata(dev);
0471 
0472     return sysfs_emit(buf, "%d\n", priv->hyst);
0473 }
0474 
0475 static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
0476               const char *buf, size_t count)
0477 {
0478     int ret;
0479     long temp;
0480 
0481     struct stts751_priv *priv = dev_get_drvdata(dev);
0482 
0483     if (kstrtol(buf, 10, &temp) < 0)
0484         return -EINVAL;
0485 
0486     mutex_lock(&priv->access_lock);
0487     /* HW works in range -64C to +127.937C */
0488     temp = clamp_val(temp, -64000, priv->therm);
0489     priv->hyst = temp;
0490     dev_dbg(&priv->client->dev, "setting hyst %ld", temp);
0491     temp = priv->therm - temp;
0492     ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_HYST);
0493     mutex_unlock(&priv->access_lock);
0494     if (ret)
0495         return ret;
0496 
0497     return count;
0498 }
0499 
0500 static ssize_t therm_trip_show(struct device *dev,
0501                    struct device_attribute *attr, char *buf)
0502 {
0503     int ret;
0504     struct stts751_priv *priv = dev_get_drvdata(dev);
0505 
0506     mutex_lock(&priv->access_lock);
0507     ret = stts751_update(priv);
0508     mutex_unlock(&priv->access_lock);
0509     if (ret < 0)
0510         return ret;
0511 
0512     return sysfs_emit(buf, "%d\n", priv->therm_trip);
0513 }
0514 
0515 static ssize_t max_show(struct device *dev, struct device_attribute *attr,
0516             char *buf)
0517 {
0518     struct stts751_priv *priv = dev_get_drvdata(dev);
0519 
0520     return sysfs_emit(buf, "%d\n", priv->event_max);
0521 }
0522 
0523 static ssize_t max_store(struct device *dev, struct device_attribute *attr,
0524              const char *buf, size_t count)
0525 {
0526     int ret;
0527     long temp;
0528     struct stts751_priv *priv = dev_get_drvdata(dev);
0529 
0530     if (kstrtol(buf, 10, &temp) < 0)
0531         return -EINVAL;
0532 
0533     mutex_lock(&priv->access_lock);
0534     /* HW works in range -64C to +127.937C */
0535     temp = clamp_val(temp, priv->event_min, 127937);
0536     ret = stts751_set_temp_reg16(priv, temp,
0537                      STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
0538     if (ret)
0539         goto exit;
0540 
0541     dev_dbg(&priv->client->dev, "setting event max %ld", temp);
0542     priv->event_max = temp;
0543     ret = count;
0544 exit:
0545     mutex_unlock(&priv->access_lock);
0546     return ret;
0547 }
0548 
0549 static ssize_t min_show(struct device *dev, struct device_attribute *attr,
0550             char *buf)
0551 {
0552     struct stts751_priv *priv = dev_get_drvdata(dev);
0553 
0554     return sysfs_emit(buf, "%d\n", priv->event_min);
0555 }
0556 
0557 static ssize_t min_store(struct device *dev, struct device_attribute *attr,
0558              const char *buf, size_t count)
0559 {
0560     int ret;
0561     long temp;
0562     struct stts751_priv *priv = dev_get_drvdata(dev);
0563 
0564     if (kstrtol(buf, 10, &temp) < 0)
0565         return -EINVAL;
0566 
0567     mutex_lock(&priv->access_lock);
0568     /* HW works in range -64C to +127.937C */
0569     temp = clamp_val(temp, -64000, priv->event_max);
0570     ret = stts751_set_temp_reg16(priv, temp,
0571                      STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
0572     if (ret)
0573         goto exit;
0574 
0575     dev_dbg(&priv->client->dev, "setting event min %ld", temp);
0576     priv->event_min = temp;
0577     ret = count;
0578 exit:
0579     mutex_unlock(&priv->access_lock);
0580     return ret;
0581 }
0582 
0583 static ssize_t interval_show(struct device *dev,
0584                  struct device_attribute *attr, char *buf)
0585 {
0586     struct stts751_priv *priv = dev_get_drvdata(dev);
0587 
0588     return sysfs_emit(buf, "%d\n",
0589               stts751_intervals[priv->interval]);
0590 }
0591 
0592 static ssize_t interval_store(struct device *dev,
0593                   struct device_attribute *attr, const char *buf,
0594                   size_t count)
0595 {
0596     unsigned long val;
0597     int idx;
0598     int ret = count;
0599     struct stts751_priv *priv = dev_get_drvdata(dev);
0600 
0601     if (kstrtoul(buf, 10, &val) < 0)
0602         return -EINVAL;
0603 
0604     idx = find_closest_descending(val, stts751_intervals,
0605                       ARRAY_SIZE(stts751_intervals));
0606 
0607     dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d",
0608         val, idx, stts751_intervals[idx]);
0609 
0610     mutex_lock(&priv->access_lock);
0611     if (priv->interval == idx)
0612         goto exit;
0613 
0614     /*
0615      * In early development stages I've become suspicious about the chip
0616      * starting to misbehave if I ever set, even briefly, an invalid
0617      * configuration. While I'm not sure this is really needed, be
0618      * conservative and set rate/resolution in such an order that avoids
0619      * passing through an invalid configuration.
0620      */
0621 
0622     /* speed up: lower the resolution, then modify convrate */
0623     if (priv->interval < idx) {
0624         dev_dbg(&priv->client->dev, "lower resolution, then modify convrate");
0625         priv->interval = idx;
0626         ret = stts751_adjust_resolution(priv);
0627         if (ret)
0628             goto exit;
0629     }
0630 
0631     ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx);
0632     if (ret)
0633         goto exit;
0634     /* slow down: modify convrate, then raise resolution */
0635     if (priv->interval != idx) {
0636         dev_dbg(&priv->client->dev, "modify convrate, then raise resolution");
0637         priv->interval = idx;
0638         ret = stts751_adjust_resolution(priv);
0639         if (ret)
0640             goto exit;
0641     }
0642     ret = count;
0643 exit:
0644     mutex_unlock(&priv->access_lock);
0645 
0646     return ret;
0647 }
0648 
0649 static int stts751_detect(struct i2c_client *new_client,
0650               struct i2c_board_info *info)
0651 {
0652     struct i2c_adapter *adapter = new_client->adapter;
0653     const char *name;
0654     int tmp;
0655 
0656     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0657         return -ENODEV;
0658 
0659     tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_MAN_ID);
0660     if (tmp != ST_MAN_ID)
0661         return -ENODEV;
0662 
0663     /* lower temperaure registers always have bits 0-3 set to zero */
0664     tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_TEMP_L);
0665     if (tmp & 0xf)
0666         return -ENODEV;
0667 
0668     tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_HLIM_L);
0669     if (tmp & 0xf)
0670         return -ENODEV;
0671 
0672     tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_LLIM_L);
0673     if (tmp & 0xf)
0674         return -ENODEV;
0675 
0676     /* smbus timeout register always have bits 0-7 set to zero */
0677     tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_SMBUS_TO);
0678     if (tmp & 0x7f)
0679         return -ENODEV;
0680 
0681     tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_PROD_ID);
0682 
0683     switch (tmp) {
0684     case STTS751_0_PROD_ID:
0685         name = "STTS751-0";
0686         break;
0687     case STTS751_1_PROD_ID:
0688         name = "STTS751-1";
0689         break;
0690     default:
0691         return -ENODEV;
0692     }
0693     dev_dbg(&new_client->dev, "Chip %s detected", name);
0694 
0695     strlcpy(info->type, stts751_id[0].name, I2C_NAME_SIZE);
0696     return 0;
0697 }
0698 
0699 static int stts751_read_chip_config(struct stts751_priv *priv)
0700 {
0701     int ret;
0702     int tmp;
0703 
0704     ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF);
0705     if (ret < 0)
0706         return ret;
0707     priv->config = ret;
0708     priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT;
0709 
0710     ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE);
0711     if (ret < 0)
0712         return ret;
0713     if (ret >= ARRAY_SIZE(stts751_intervals)) {
0714         dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret);
0715         return -ENODEV;
0716     }
0717     priv->interval = ret;
0718 
0719     ret = stts751_read_reg16(priv, &priv->event_max,
0720                  STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
0721     if (ret)
0722         return ret;
0723 
0724     ret = stts751_read_reg16(priv, &priv->event_min,
0725                  STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
0726     if (ret)
0727         return ret;
0728 
0729     ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM);
0730     if (ret)
0731         return ret;
0732 
0733     ret = stts751_read_reg8(priv, &tmp, STTS751_REG_HYST);
0734     if (ret)
0735         return ret;
0736     priv->hyst = priv->therm - tmp;
0737 
0738     return 0;
0739 }
0740 
0741 static SENSOR_DEVICE_ATTR_RO(temp1_input, input, 0);
0742 static SENSOR_DEVICE_ATTR_RW(temp1_min, min, 0);
0743 static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
0744 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, min_alarm, 0);
0745 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, max_alarm, 0);
0746 static SENSOR_DEVICE_ATTR_RW(temp1_crit, therm, 0);
0747 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0);
0748 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, therm_trip, 0);
0749 static SENSOR_DEVICE_ATTR_RW(update_interval, interval, 0);
0750 
0751 static struct attribute *stts751_attrs[] = {
0752     &sensor_dev_attr_temp1_input.dev_attr.attr,
0753     &sensor_dev_attr_temp1_min.dev_attr.attr,
0754     &sensor_dev_attr_temp1_max.dev_attr.attr,
0755     &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
0756     &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
0757     &sensor_dev_attr_temp1_crit.dev_attr.attr,
0758     &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
0759     &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
0760     &sensor_dev_attr_update_interval.dev_attr.attr,
0761     NULL
0762 };
0763 ATTRIBUTE_GROUPS(stts751);
0764 
0765 static int stts751_probe(struct i2c_client *client)
0766 {
0767     struct stts751_priv *priv;
0768     int ret;
0769     bool smbus_nto;
0770     int rev_id;
0771 
0772     priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
0773     if (!priv)
0774         return -ENOMEM;
0775 
0776     priv->client = client;
0777     priv->notify_max = true;
0778     priv->notify_min = true;
0779     i2c_set_clientdata(client, priv);
0780     mutex_init(&priv->access_lock);
0781 
0782     if (device_property_present(&client->dev,
0783                     "smbus-timeout-disable")) {
0784         smbus_nto = device_property_read_bool(&client->dev,
0785                               "smbus-timeout-disable");
0786 
0787         ret = i2c_smbus_write_byte_data(client, STTS751_REG_SMBUS_TO,
0788                         smbus_nto ? 0 : 0x80);
0789         if (ret)
0790             return ret;
0791     }
0792 
0793     rev_id = i2c_smbus_read_byte_data(client, STTS751_REG_REV_ID);
0794     if (rev_id < 0)
0795         return -ENODEV;
0796     if (rev_id != 0x1) {
0797         dev_dbg(&client->dev, "Chip revision 0x%x is untested\n",
0798             rev_id);
0799     }
0800 
0801     ret = stts751_read_chip_config(priv);
0802     if (ret)
0803         return ret;
0804 
0805     priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS);
0806     ret = i2c_smbus_write_byte_data(client, STTS751_REG_CONF, priv->config);
0807     if (ret)
0808         return ret;
0809 
0810     priv->dev = devm_hwmon_device_register_with_groups(&client->dev,
0811                             client->name, priv,
0812                             stts751_groups);
0813     return PTR_ERR_OR_ZERO(priv->dev);
0814 }
0815 
0816 MODULE_DEVICE_TABLE(i2c, stts751_id);
0817 
0818 static struct i2c_driver stts751_driver = {
0819     .class      = I2C_CLASS_HWMON,
0820     .driver = {
0821         .name   = DEVNAME,
0822         .of_match_table = of_match_ptr(stts751_of_match),
0823     },
0824     .probe_new  = stts751_probe,
0825     .id_table   = stts751_id,
0826     .detect     = stts751_detect,
0827     .alert      = stts751_alert,
0828     .address_list   = normal_i2c,
0829 };
0830 
0831 module_i2c_driver(stts751_driver);
0832 
0833 MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
0834 MODULE_DESCRIPTION("STTS751 sensor driver");
0835 MODULE_LICENSE("GPL");