Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Sensirion SHT3x-DIS humidity and temperature sensor driver.
0003  * The SHT3x comes in many different versions, this driver is for the
0004  * I2C version only.
0005  *
0006  * Copyright (C) 2016 Sensirion AG, Switzerland
0007  * Author: David Frey <david.frey@sensirion.com>
0008  * Author: Pascal Sachs <pascal.sachs@sensirion.com>
0009  */
0010 
0011 #include <asm/page.h>
0012 #include <linux/crc8.h>
0013 #include <linux/delay.h>
0014 #include <linux/err.h>
0015 #include <linux/hwmon.h>
0016 #include <linux/hwmon-sysfs.h>
0017 #include <linux/i2c.h>
0018 #include <linux/init.h>
0019 #include <linux/kernel.h>
0020 #include <linux/module.h>
0021 #include <linux/slab.h>
0022 #include <linux/jiffies.h>
0023 #include <linux/platform_data/sht3x.h>
0024 
0025 /* commands (high precision mode) */
0026 static const unsigned char sht3x_cmd_measure_blocking_hpm[]    = { 0x2c, 0x06 };
0027 static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = { 0x24, 0x00 };
0028 
0029 /* commands (low power mode) */
0030 static const unsigned char sht3x_cmd_measure_blocking_lpm[]    = { 0x2c, 0x10 };
0031 static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = { 0x24, 0x16 };
0032 
0033 /* commands for periodic mode */
0034 static const unsigned char sht3x_cmd_measure_periodic_mode[]   = { 0xe0, 0x00 };
0035 static const unsigned char sht3x_cmd_break[]                   = { 0x30, 0x93 };
0036 
0037 /* commands for heater control */
0038 static const unsigned char sht3x_cmd_heater_on[]               = { 0x30, 0x6d };
0039 static const unsigned char sht3x_cmd_heater_off[]              = { 0x30, 0x66 };
0040 
0041 /* other commands */
0042 static const unsigned char sht3x_cmd_read_status_reg[]         = { 0xf3, 0x2d };
0043 static const unsigned char sht3x_cmd_clear_status_reg[]        = { 0x30, 0x41 };
0044 
0045 /* delays for non-blocking i2c commands, both in us */
0046 #define SHT3X_NONBLOCKING_WAIT_TIME_HPM  15000
0047 #define SHT3X_NONBLOCKING_WAIT_TIME_LPM   4000
0048 
0049 #define SHT3X_WORD_LEN         2
0050 #define SHT3X_CMD_LENGTH       2
0051 #define SHT3X_CRC8_LEN         1
0052 #define SHT3X_RESPONSE_LENGTH  6
0053 #define SHT3X_CRC8_POLYNOMIAL  0x31
0054 #define SHT3X_CRC8_INIT        0xFF
0055 #define SHT3X_MIN_TEMPERATURE  -45000
0056 #define SHT3X_MAX_TEMPERATURE  130000
0057 #define SHT3X_MIN_HUMIDITY     0
0058 #define SHT3X_MAX_HUMIDITY     100000
0059 
0060 enum sht3x_chips {
0061     sht3x,
0062     sts3x,
0063 };
0064 
0065 enum sht3x_limits {
0066     limit_max = 0,
0067     limit_max_hyst,
0068     limit_min,
0069     limit_min_hyst,
0070 };
0071 
0072 DECLARE_CRC8_TABLE(sht3x_crc8_table);
0073 
0074 /* periodic measure commands (high precision mode) */
0075 static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = {
0076     /* 0.5 measurements per second */
0077     {0x20, 0x32},
0078     /* 1 measurements per second */
0079     {0x21, 0x30},
0080     /* 2 measurements per second */
0081     {0x22, 0x36},
0082     /* 4 measurements per second */
0083     {0x23, 0x34},
0084     /* 10 measurements per second */
0085     {0x27, 0x37},
0086 };
0087 
0088 /* periodic measure commands (low power mode) */
0089 static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = {
0090     /* 0.5 measurements per second */
0091     {0x20, 0x2f},
0092     /* 1 measurements per second */
0093     {0x21, 0x2d},
0094     /* 2 measurements per second */
0095     {0x22, 0x2b},
0096     /* 4 measurements per second */
0097     {0x23, 0x29},
0098     /* 10 measurements per second */
0099     {0x27, 0x2a},
0100 };
0101 
0102 struct sht3x_limit_commands {
0103     const char read_command[SHT3X_CMD_LENGTH];
0104     const char write_command[SHT3X_CMD_LENGTH];
0105 };
0106 
0107 static const struct sht3x_limit_commands limit_commands[] = {
0108     /* temp1_max, humidity1_max */
0109     [limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} },
0110     /* temp_1_max_hyst, humidity1_max_hyst */
0111     [limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} },
0112     /* temp1_min, humidity1_min */
0113     [limit_min] = { {0xe1, 0x02}, {0x61, 0x00} },
0114     /* temp_1_min_hyst, humidity1_min_hyst */
0115     [limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} },
0116 };
0117 
0118 #define SHT3X_NUM_LIMIT_CMD  ARRAY_SIZE(limit_commands)
0119 
0120 static const u16 mode_to_update_interval[] = {
0121        0,
0122     2000,
0123     1000,
0124      500,
0125      250,
0126      100,
0127 };
0128 
0129 struct sht3x_data {
0130     struct i2c_client *client;
0131     struct mutex i2c_lock; /* lock for sending i2c commands */
0132     struct mutex data_lock; /* lock for updating driver data */
0133 
0134     u8 mode;
0135     const unsigned char *command;
0136     u32 wait_time;          /* in us*/
0137     unsigned long last_update;  /* last update in periodic mode*/
0138 
0139     struct sht3x_platform_data setup;
0140 
0141     /*
0142      * cached values for temperature and humidity and limits
0143      * the limits arrays have the following order:
0144      * max, max_hyst, min, min_hyst
0145      */
0146     int temperature;
0147     int temperature_limits[SHT3X_NUM_LIMIT_CMD];
0148     u32 humidity;
0149     u32 humidity_limits[SHT3X_NUM_LIMIT_CMD];
0150 };
0151 
0152 static u8 get_mode_from_update_interval(u16 value)
0153 {
0154     size_t index;
0155     u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval);
0156 
0157     if (value == 0)
0158         return 0;
0159 
0160     /* find next faster update interval */
0161     for (index = 1; index < number_of_modes; index++) {
0162         if (mode_to_update_interval[index] <= value)
0163             return index;
0164     }
0165 
0166     return number_of_modes - 1;
0167 }
0168 
0169 static int sht3x_read_from_command(struct i2c_client *client,
0170                    struct sht3x_data *data,
0171                    const char *command,
0172                    char *buf, int length, u32 wait_time)
0173 {
0174     int ret;
0175 
0176     mutex_lock(&data->i2c_lock);
0177     ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
0178 
0179     if (ret != SHT3X_CMD_LENGTH) {
0180         ret = ret < 0 ? ret : -EIO;
0181         goto out;
0182     }
0183 
0184     if (wait_time)
0185         usleep_range(wait_time, wait_time + 1000);
0186 
0187     ret = i2c_master_recv(client, buf, length);
0188     if (ret != length) {
0189         ret = ret < 0 ? ret : -EIO;
0190         goto out;
0191     }
0192 
0193     ret = 0;
0194 out:
0195     mutex_unlock(&data->i2c_lock);
0196     return ret;
0197 }
0198 
0199 static int sht3x_extract_temperature(u16 raw)
0200 {
0201     /*
0202      * From datasheet:
0203      * T = -45 + 175 * ST / 2^16
0204      * Adapted for integer fixed point (3 digit) arithmetic.
0205      */
0206     return ((21875 * (int)raw) >> 13) - 45000;
0207 }
0208 
0209 static u32 sht3x_extract_humidity(u16 raw)
0210 {
0211     /*
0212      * From datasheet:
0213      * RH = 100 * SRH / 2^16
0214      * Adapted for integer fixed point (3 digit) arithmetic.
0215      */
0216     return (12500 * (u32)raw) >> 13;
0217 }
0218 
0219 static struct sht3x_data *sht3x_update_client(struct device *dev)
0220 {
0221     struct sht3x_data *data = dev_get_drvdata(dev);
0222     struct i2c_client *client = data->client;
0223     u16 interval_ms = mode_to_update_interval[data->mode];
0224     unsigned long interval_jiffies = msecs_to_jiffies(interval_ms);
0225     unsigned char buf[SHT3X_RESPONSE_LENGTH];
0226     u16 val;
0227     int ret = 0;
0228 
0229     mutex_lock(&data->data_lock);
0230     /*
0231      * Only update cached readings once per update interval in periodic
0232      * mode. In single shot mode the sensor measures values on demand, so
0233      * every time the sysfs interface is called, a measurement is triggered.
0234      * In periodic mode however, the measurement process is handled
0235      * internally by the sensor and reading out sensor values only makes
0236      * sense if a new reading is available.
0237      */
0238     if (time_after(jiffies, data->last_update + interval_jiffies)) {
0239         ret = sht3x_read_from_command(client, data, data->command, buf,
0240                           sizeof(buf), data->wait_time);
0241         if (ret)
0242             goto out;
0243 
0244         val = be16_to_cpup((__be16 *)buf);
0245         data->temperature = sht3x_extract_temperature(val);
0246         val = be16_to_cpup((__be16 *)(buf + 3));
0247         data->humidity = sht3x_extract_humidity(val);
0248         data->last_update = jiffies;
0249     }
0250 
0251 out:
0252     mutex_unlock(&data->data_lock);
0253     if (ret)
0254         return ERR_PTR(ret);
0255 
0256     return data;
0257 }
0258 
0259 /* sysfs attributes */
0260 static ssize_t temp1_input_show(struct device *dev,
0261                 struct device_attribute *attr, char *buf)
0262 {
0263     struct sht3x_data *data = sht3x_update_client(dev);
0264 
0265     if (IS_ERR(data))
0266         return PTR_ERR(data);
0267 
0268     return sprintf(buf, "%d\n", data->temperature);
0269 }
0270 
0271 static ssize_t humidity1_input_show(struct device *dev,
0272                     struct device_attribute *attr, char *buf)
0273 {
0274     struct sht3x_data *data = sht3x_update_client(dev);
0275 
0276     if (IS_ERR(data))
0277         return PTR_ERR(data);
0278 
0279     return sprintf(buf, "%u\n", data->humidity);
0280 }
0281 
0282 /*
0283  * limits_update must only be called from probe or with data_lock held
0284  */
0285 static int limits_update(struct sht3x_data *data)
0286 {
0287     int ret;
0288     u8 index;
0289     int temperature;
0290     u32 humidity;
0291     u16 raw;
0292     char buffer[SHT3X_RESPONSE_LENGTH];
0293     const struct sht3x_limit_commands *commands;
0294     struct i2c_client *client = data->client;
0295 
0296     for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) {
0297         commands = &limit_commands[index];
0298         ret = sht3x_read_from_command(client, data,
0299                           commands->read_command, buffer,
0300                           SHT3X_RESPONSE_LENGTH, 0);
0301 
0302         if (ret)
0303             return ret;
0304 
0305         raw = be16_to_cpup((__be16 *)buffer);
0306         temperature = sht3x_extract_temperature((raw & 0x01ff) << 7);
0307         humidity = sht3x_extract_humidity(raw & 0xfe00);
0308         data->temperature_limits[index] = temperature;
0309         data->humidity_limits[index] = humidity;
0310     }
0311 
0312     return ret;
0313 }
0314 
0315 static ssize_t temp1_limit_show(struct device *dev,
0316                 struct device_attribute *attr,
0317                 char *buf)
0318 {
0319     struct sht3x_data *data = dev_get_drvdata(dev);
0320     u8 index = to_sensor_dev_attr(attr)->index;
0321     int temperature_limit = data->temperature_limits[index];
0322 
0323     return scnprintf(buf, PAGE_SIZE, "%d\n", temperature_limit);
0324 }
0325 
0326 static ssize_t humidity1_limit_show(struct device *dev,
0327                     struct device_attribute *attr,
0328                     char *buf)
0329 {
0330     struct sht3x_data *data = dev_get_drvdata(dev);
0331     u8 index = to_sensor_dev_attr(attr)->index;
0332     u32 humidity_limit = data->humidity_limits[index];
0333 
0334     return scnprintf(buf, PAGE_SIZE, "%u\n", humidity_limit);
0335 }
0336 
0337 /*
0338  * limit_store must only be called with data_lock held
0339  */
0340 static size_t limit_store(struct device *dev,
0341               size_t count,
0342               u8 index,
0343               int temperature,
0344               u32 humidity)
0345 {
0346     char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
0347     char *position = buffer;
0348     int ret;
0349     u16 raw;
0350     struct sht3x_data *data = dev_get_drvdata(dev);
0351     struct i2c_client *client = data->client;
0352     const struct sht3x_limit_commands *commands;
0353 
0354     commands = &limit_commands[index];
0355 
0356     memcpy(position, commands->write_command, SHT3X_CMD_LENGTH);
0357     position += SHT3X_CMD_LENGTH;
0358     /*
0359      * ST = (T + 45) / 175 * 2^16
0360      * SRH = RH / 100 * 2^16
0361      * adapted for fixed point arithmetic and packed the same as
0362      * in limit_show()
0363      */
0364     raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7);
0365     raw |= ((humidity * 42950) >> 16) & 0xfe00;
0366 
0367     *((__be16 *)position) = cpu_to_be16(raw);
0368     position += SHT3X_WORD_LEN;
0369     *position = crc8(sht3x_crc8_table,
0370              position - SHT3X_WORD_LEN,
0371              SHT3X_WORD_LEN,
0372              SHT3X_CRC8_INIT);
0373 
0374     mutex_lock(&data->i2c_lock);
0375     ret = i2c_master_send(client, buffer, sizeof(buffer));
0376     mutex_unlock(&data->i2c_lock);
0377 
0378     if (ret != sizeof(buffer))
0379         return ret < 0 ? ret : -EIO;
0380 
0381     data->temperature_limits[index] = temperature;
0382     data->humidity_limits[index] = humidity;
0383     return count;
0384 }
0385 
0386 static ssize_t temp1_limit_store(struct device *dev,
0387                  struct device_attribute *attr,
0388                  const char *buf,
0389                  size_t count)
0390 {
0391     int temperature;
0392     int ret;
0393     struct sht3x_data *data = dev_get_drvdata(dev);
0394     u8 index = to_sensor_dev_attr(attr)->index;
0395 
0396     ret = kstrtoint(buf, 0, &temperature);
0397     if (ret)
0398         return ret;
0399 
0400     temperature = clamp_val(temperature, SHT3X_MIN_TEMPERATURE,
0401                 SHT3X_MAX_TEMPERATURE);
0402     mutex_lock(&data->data_lock);
0403     ret = limit_store(dev, count, index, temperature,
0404               data->humidity_limits[index]);
0405     mutex_unlock(&data->data_lock);
0406 
0407     return ret;
0408 }
0409 
0410 static ssize_t humidity1_limit_store(struct device *dev,
0411                      struct device_attribute *attr,
0412                      const char *buf,
0413                      size_t count)
0414 {
0415     u32 humidity;
0416     int ret;
0417     struct sht3x_data *data = dev_get_drvdata(dev);
0418     u8 index = to_sensor_dev_attr(attr)->index;
0419 
0420     ret = kstrtou32(buf, 0, &humidity);
0421     if (ret)
0422         return ret;
0423 
0424     humidity = clamp_val(humidity, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY);
0425     mutex_lock(&data->data_lock);
0426     ret = limit_store(dev, count, index, data->temperature_limits[index],
0427               humidity);
0428     mutex_unlock(&data->data_lock);
0429 
0430     return ret;
0431 }
0432 
0433 static void sht3x_select_command(struct sht3x_data *data)
0434 {
0435     /*
0436      * In blocking mode (clock stretching mode) the I2C bus
0437      * is blocked for other traffic, thus the call to i2c_master_recv()
0438      * will wait until the data is ready. For non blocking mode, we
0439      * have to wait ourselves.
0440      */
0441     if (data->mode > 0) {
0442         data->command = sht3x_cmd_measure_periodic_mode;
0443         data->wait_time = 0;
0444     } else if (data->setup.blocking_io) {
0445         data->command = data->setup.high_precision ?
0446                 sht3x_cmd_measure_blocking_hpm :
0447                 sht3x_cmd_measure_blocking_lpm;
0448         data->wait_time = 0;
0449     } else {
0450         if (data->setup.high_precision) {
0451             data->command = sht3x_cmd_measure_nonblocking_hpm;
0452             data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_HPM;
0453         } else {
0454             data->command = sht3x_cmd_measure_nonblocking_lpm;
0455             data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_LPM;
0456         }
0457     }
0458 }
0459 
0460 static int status_register_read(struct device *dev,
0461                 struct device_attribute *attr,
0462                 char *buffer, int length)
0463 {
0464     int ret;
0465     struct sht3x_data *data = dev_get_drvdata(dev);
0466     struct i2c_client *client = data->client;
0467 
0468     ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg,
0469                       buffer, length, 0);
0470 
0471     return ret;
0472 }
0473 
0474 static ssize_t temp1_alarm_show(struct device *dev,
0475                 struct device_attribute *attr,
0476                 char *buf)
0477 {
0478     char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
0479     int ret;
0480 
0481     ret = status_register_read(dev, attr, buffer,
0482                    SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
0483     if (ret)
0484         return ret;
0485 
0486     return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x04));
0487 }
0488 
0489 static ssize_t humidity1_alarm_show(struct device *dev,
0490                     struct device_attribute *attr,
0491                     char *buf)
0492 {
0493     char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
0494     int ret;
0495 
0496     ret = status_register_read(dev, attr, buffer,
0497                    SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
0498     if (ret)
0499         return ret;
0500 
0501     return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x08));
0502 }
0503 
0504 static ssize_t heater_enable_show(struct device *dev,
0505                   struct device_attribute *attr,
0506                   char *buf)
0507 {
0508     char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
0509     int ret;
0510 
0511     ret = status_register_read(dev, attr, buffer,
0512                    SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
0513     if (ret)
0514         return ret;
0515 
0516     return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x20));
0517 }
0518 
0519 static ssize_t heater_enable_store(struct device *dev,
0520                    struct device_attribute *attr,
0521                    const char *buf,
0522                    size_t count)
0523 {
0524     struct sht3x_data *data = dev_get_drvdata(dev);
0525     struct i2c_client *client = data->client;
0526     int ret;
0527     bool status;
0528 
0529     ret = kstrtobool(buf, &status);
0530     if (ret)
0531         return ret;
0532 
0533     mutex_lock(&data->i2c_lock);
0534 
0535     if (status)
0536         ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on,
0537                       SHT3X_CMD_LENGTH);
0538     else
0539         ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off,
0540                       SHT3X_CMD_LENGTH);
0541 
0542     mutex_unlock(&data->i2c_lock);
0543 
0544     return ret;
0545 }
0546 
0547 static ssize_t update_interval_show(struct device *dev,
0548                     struct device_attribute *attr,
0549                     char *buf)
0550 {
0551     struct sht3x_data *data = dev_get_drvdata(dev);
0552 
0553     return scnprintf(buf, PAGE_SIZE, "%u\n",
0554              mode_to_update_interval[data->mode]);
0555 }
0556 
0557 static ssize_t update_interval_store(struct device *dev,
0558                      struct device_attribute *attr,
0559                      const char *buf,
0560                      size_t count)
0561 {
0562     u16 update_interval;
0563     u8 mode;
0564     int ret;
0565     const char *command;
0566     struct sht3x_data *data = dev_get_drvdata(dev);
0567     struct i2c_client *client = data->client;
0568 
0569     ret = kstrtou16(buf, 0, &update_interval);
0570     if (ret)
0571         return ret;
0572 
0573     mode = get_mode_from_update_interval(update_interval);
0574 
0575     mutex_lock(&data->data_lock);
0576     /* mode did not change */
0577     if (mode == data->mode) {
0578         mutex_unlock(&data->data_lock);
0579         return count;
0580     }
0581 
0582     mutex_lock(&data->i2c_lock);
0583     /*
0584      * Abort periodic measure mode.
0585      * To do any changes to the configuration while in periodic mode, we
0586      * have to send a break command to the sensor, which then falls back
0587      * to single shot (mode = 0).
0588      */
0589     if (data->mode > 0) {
0590         ret = i2c_master_send(client, sht3x_cmd_break,
0591                       SHT3X_CMD_LENGTH);
0592         if (ret != SHT3X_CMD_LENGTH)
0593             goto out;
0594         data->mode = 0;
0595     }
0596 
0597     if (mode > 0) {
0598         if (data->setup.high_precision)
0599             command = periodic_measure_commands_hpm[mode - 1];
0600         else
0601             command = periodic_measure_commands_lpm[mode - 1];
0602 
0603         /* select mode */
0604         ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
0605         if (ret != SHT3X_CMD_LENGTH)
0606             goto out;
0607     }
0608 
0609     /* select mode and command */
0610     data->mode = mode;
0611     sht3x_select_command(data);
0612 
0613 out:
0614     mutex_unlock(&data->i2c_lock);
0615     mutex_unlock(&data->data_lock);
0616     if (ret != SHT3X_CMD_LENGTH)
0617         return ret < 0 ? ret : -EIO;
0618 
0619     return count;
0620 }
0621 
0622 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp1_input, 0);
0623 static SENSOR_DEVICE_ATTR_RO(humidity1_input, humidity1_input, 0);
0624 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp1_limit, limit_max);
0625 static SENSOR_DEVICE_ATTR_RW(humidity1_max, humidity1_limit, limit_max);
0626 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp1_limit, limit_max_hyst);
0627 static SENSOR_DEVICE_ATTR_RW(humidity1_max_hyst, humidity1_limit,
0628                  limit_max_hyst);
0629 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp1_limit, limit_min);
0630 static SENSOR_DEVICE_ATTR_RW(humidity1_min, humidity1_limit, limit_min);
0631 static SENSOR_DEVICE_ATTR_RW(temp1_min_hyst, temp1_limit, limit_min_hyst);
0632 static SENSOR_DEVICE_ATTR_RW(humidity1_min_hyst, humidity1_limit,
0633                  limit_min_hyst);
0634 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, temp1_alarm, 0);
0635 static SENSOR_DEVICE_ATTR_RO(humidity1_alarm, humidity1_alarm, 0);
0636 static SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0);
0637 static SENSOR_DEVICE_ATTR_RW(update_interval, update_interval, 0);
0638 
0639 static struct attribute *sht3x_attrs[] = {
0640     &sensor_dev_attr_temp1_input.dev_attr.attr,
0641     &sensor_dev_attr_humidity1_input.dev_attr.attr,
0642     &sensor_dev_attr_temp1_max.dev_attr.attr,
0643     &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
0644     &sensor_dev_attr_humidity1_max.dev_attr.attr,
0645     &sensor_dev_attr_humidity1_max_hyst.dev_attr.attr,
0646     &sensor_dev_attr_temp1_min.dev_attr.attr,
0647     &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
0648     &sensor_dev_attr_humidity1_min.dev_attr.attr,
0649     &sensor_dev_attr_humidity1_min_hyst.dev_attr.attr,
0650     &sensor_dev_attr_temp1_alarm.dev_attr.attr,
0651     &sensor_dev_attr_humidity1_alarm.dev_attr.attr,
0652     &sensor_dev_attr_heater_enable.dev_attr.attr,
0653     &sensor_dev_attr_update_interval.dev_attr.attr,
0654     NULL
0655 };
0656 
0657 static struct attribute *sts3x_attrs[] = {
0658     &sensor_dev_attr_temp1_input.dev_attr.attr,
0659     NULL
0660 };
0661 
0662 ATTRIBUTE_GROUPS(sht3x);
0663 ATTRIBUTE_GROUPS(sts3x);
0664 
0665 static const struct i2c_device_id sht3x_ids[];
0666 
0667 static int sht3x_probe(struct i2c_client *client)
0668 {
0669     int ret;
0670     struct sht3x_data *data;
0671     struct device *hwmon_dev;
0672     struct i2c_adapter *adap = client->adapter;
0673     struct device *dev = &client->dev;
0674     const struct attribute_group **attribute_groups;
0675 
0676     /*
0677      * we require full i2c support since the sht3x uses multi-byte read and
0678      * writes as well as multi-byte commands which are not supported by
0679      * the smbus protocol
0680      */
0681     if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
0682         return -ENODEV;
0683 
0684     ret = i2c_master_send(client, sht3x_cmd_clear_status_reg,
0685                   SHT3X_CMD_LENGTH);
0686     if (ret != SHT3X_CMD_LENGTH)
0687         return ret < 0 ? ret : -ENODEV;
0688 
0689     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0690     if (!data)
0691         return -ENOMEM;
0692 
0693     data->setup.blocking_io = false;
0694     data->setup.high_precision = true;
0695     data->mode = 0;
0696     data->last_update = jiffies - msecs_to_jiffies(3000);
0697     data->client = client;
0698     crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
0699 
0700     if (client->dev.platform_data)
0701         data->setup = *(struct sht3x_platform_data *)dev->platform_data;
0702 
0703     sht3x_select_command(data);
0704 
0705     mutex_init(&data->i2c_lock);
0706     mutex_init(&data->data_lock);
0707 
0708     /*
0709      * An attempt to read limits register too early
0710      * causes a NACK response from the chip.
0711      * Waiting for an empirical delay of 500 us solves the issue.
0712      */
0713     usleep_range(500, 600);
0714 
0715     ret = limits_update(data);
0716     if (ret)
0717         return ret;
0718 
0719     if (i2c_match_id(sht3x_ids, client)->driver_data == sts3x)
0720         attribute_groups = sts3x_groups;
0721     else
0722         attribute_groups = sht3x_groups;
0723 
0724     hwmon_dev = devm_hwmon_device_register_with_groups(dev,
0725                                client->name,
0726                                data,
0727                                attribute_groups);
0728 
0729     if (IS_ERR(hwmon_dev))
0730         dev_dbg(dev, "unable to register hwmon device\n");
0731 
0732     return PTR_ERR_OR_ZERO(hwmon_dev);
0733 }
0734 
0735 /* device ID table */
0736 static const struct i2c_device_id sht3x_ids[] = {
0737     {"sht3x", sht3x},
0738     {"sts3x", sts3x},
0739     {}
0740 };
0741 
0742 MODULE_DEVICE_TABLE(i2c, sht3x_ids);
0743 
0744 static struct i2c_driver sht3x_i2c_driver = {
0745     .driver.name = "sht3x",
0746     .probe_new   = sht3x_probe,
0747     .id_table    = sht3x_ids,
0748 };
0749 
0750 module_i2c_driver(sht3x_i2c_driver);
0751 
0752 MODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
0753 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
0754 MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
0755 MODULE_LICENSE("GPL");