Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Sensirion SHTC1 humidity and temperature sensor driver
0003  *
0004  * Copyright (C) 2014 Sensirion AG, Switzerland
0005  * Author: Johannes Winkelmann <johannes.winkelmann@sensirion.com>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/slab.h>
0011 #include <linux/i2c.h>
0012 #include <linux/hwmon.h>
0013 #include <linux/hwmon-sysfs.h>
0014 #include <linux/err.h>
0015 #include <linux/delay.h>
0016 #include <linux/platform_data/shtc1.h>
0017 #include <linux/of.h>
0018 
0019 /* commands (high precision mode) */
0020 static const unsigned char shtc1_cmd_measure_blocking_hpm[]    = { 0x7C, 0xA2 };
0021 static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 };
0022 
0023 /* commands (low precision mode) */
0024 static const unsigned char shtc1_cmd_measure_blocking_lpm[]    = { 0x64, 0x58 };
0025 static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c };
0026 
0027 /* command for reading the ID register */
0028 static const unsigned char shtc1_cmd_read_id_reg[]             = { 0xef, 0xc8 };
0029 
0030 /*
0031  * constants for reading the ID register
0032  * SHTC1: 0x0007 with mask 0x003f
0033  * SHTW1: 0x0007 with mask 0x003f
0034  * SHTC3: 0x0807 with mask 0x083f
0035  */
0036 #define SHTC3_ID      0x0807
0037 #define SHTC3_ID_MASK 0x083f
0038 #define SHTC1_ID      0x0007
0039 #define SHTC1_ID_MASK 0x003f
0040 
0041 /* delays for non-blocking i2c commands, both in us */
0042 #define SHTC1_NONBLOCKING_WAIT_TIME_HPM  14400
0043 #define SHTC1_NONBLOCKING_WAIT_TIME_LPM   1000
0044 #define SHTC3_NONBLOCKING_WAIT_TIME_HPM  12100
0045 #define SHTC3_NONBLOCKING_WAIT_TIME_LPM    800
0046 
0047 #define SHTC1_CMD_LENGTH      2
0048 #define SHTC1_RESPONSE_LENGTH 6
0049 
0050 enum shtcx_chips {
0051     shtc1,
0052     shtc3,
0053 };
0054 
0055 struct shtc1_data {
0056     struct i2c_client *client;
0057     struct mutex update_lock;
0058     bool valid;
0059     unsigned long last_updated; /* in jiffies */
0060 
0061     const unsigned char *command;
0062     unsigned int nonblocking_wait_time; /* in us */
0063 
0064     struct shtc1_platform_data setup;
0065     enum shtcx_chips chip;
0066 
0067     int temperature; /* 1000 * temperature in dgr C */
0068     int humidity; /* 1000 * relative humidity in %RH */
0069 };
0070 
0071 static int shtc1_update_values(struct i2c_client *client,
0072                    struct shtc1_data *data,
0073                    char *buf, int bufsize)
0074 {
0075     int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
0076     if (ret != SHTC1_CMD_LENGTH) {
0077         dev_err(&client->dev, "failed to send command: %d\n", ret);
0078         return ret < 0 ? ret : -EIO;
0079     }
0080 
0081     /*
0082      * In blocking mode (clock stretching mode) the I2C bus
0083      * is blocked for other traffic, thus the call to i2c_master_recv()
0084      * will wait until the data is ready. For non blocking mode, we
0085      * have to wait ourselves.
0086      */
0087     if (!data->setup.blocking_io)
0088         usleep_range(data->nonblocking_wait_time,
0089                  data->nonblocking_wait_time + 1000);
0090 
0091     ret = i2c_master_recv(client, buf, bufsize);
0092     if (ret != bufsize) {
0093         dev_err(&client->dev, "failed to read values: %d\n", ret);
0094         return ret < 0 ? ret : -EIO;
0095     }
0096 
0097     return 0;
0098 }
0099 
0100 /* sysfs attributes */
0101 static struct shtc1_data *shtc1_update_client(struct device *dev)
0102 {
0103     struct shtc1_data *data = dev_get_drvdata(dev);
0104     struct i2c_client *client = data->client;
0105     unsigned char buf[SHTC1_RESPONSE_LENGTH];
0106     int val;
0107     int ret = 0;
0108 
0109     mutex_lock(&data->update_lock);
0110 
0111     if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
0112         ret = shtc1_update_values(client, data, buf, sizeof(buf));
0113         if (ret)
0114             goto out;
0115 
0116         /*
0117          * From datasheet:
0118          * T = -45 + 175 * ST / 2^16
0119          * RH = 100 * SRH / 2^16
0120          *
0121          * Adapted for integer fixed point (3 digit) arithmetic.
0122          */
0123         val = be16_to_cpup((__be16 *)buf);
0124         data->temperature = ((21875 * val) >> 13) - 45000;
0125         val = be16_to_cpup((__be16 *)(buf + 3));
0126         data->humidity = ((12500 * val) >> 13);
0127 
0128         data->last_updated = jiffies;
0129         data->valid = true;
0130     }
0131 
0132 out:
0133     mutex_unlock(&data->update_lock);
0134 
0135     return ret == 0 ? data : ERR_PTR(ret);
0136 }
0137 
0138 static ssize_t temp1_input_show(struct device *dev,
0139                 struct device_attribute *attr,
0140                 char *buf)
0141 {
0142     struct shtc1_data *data = shtc1_update_client(dev);
0143     if (IS_ERR(data))
0144         return PTR_ERR(data);
0145 
0146     return sprintf(buf, "%d\n", data->temperature);
0147 }
0148 
0149 static ssize_t humidity1_input_show(struct device *dev,
0150                     struct device_attribute *attr, char *buf)
0151 {
0152     struct shtc1_data *data = shtc1_update_client(dev);
0153     if (IS_ERR(data))
0154         return PTR_ERR(data);
0155 
0156     return sprintf(buf, "%d\n", data->humidity);
0157 }
0158 
0159 static DEVICE_ATTR_RO(temp1_input);
0160 static DEVICE_ATTR_RO(humidity1_input);
0161 
0162 static struct attribute *shtc1_attrs[] = {
0163     &dev_attr_temp1_input.attr,
0164     &dev_attr_humidity1_input.attr,
0165     NULL
0166 };
0167 
0168 ATTRIBUTE_GROUPS(shtc1);
0169 
0170 static void shtc1_select_command(struct shtc1_data *data)
0171 {
0172     if (data->setup.high_precision) {
0173         data->command = data->setup.blocking_io ?
0174                 shtc1_cmd_measure_blocking_hpm :
0175                 shtc1_cmd_measure_nonblocking_hpm;
0176         data->nonblocking_wait_time = (data->chip == shtc1) ?
0177                 SHTC1_NONBLOCKING_WAIT_TIME_HPM :
0178                 SHTC3_NONBLOCKING_WAIT_TIME_HPM;
0179     } else {
0180         data->command = data->setup.blocking_io ?
0181                 shtc1_cmd_measure_blocking_lpm :
0182                 shtc1_cmd_measure_nonblocking_lpm;
0183         data->nonblocking_wait_time = (data->chip == shtc1) ?
0184                 SHTC1_NONBLOCKING_WAIT_TIME_LPM :
0185                 SHTC3_NONBLOCKING_WAIT_TIME_LPM;
0186     }
0187 }
0188 
0189 static const struct i2c_device_id shtc1_id[];
0190 
0191 static int shtc1_probe(struct i2c_client *client)
0192 {
0193     int ret;
0194     u16 id_reg;
0195     char id_reg_buf[2];
0196     struct shtc1_data *data;
0197     struct device *hwmon_dev;
0198     enum shtcx_chips chip = i2c_match_id(shtc1_id, client)->driver_data;
0199     struct i2c_adapter *adap = client->adapter;
0200     struct device *dev = &client->dev;
0201     struct device_node *np = dev->of_node;
0202 
0203     if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) {
0204         dev_err(dev, "plain i2c transactions not supported\n");
0205         return -ENODEV;
0206     }
0207 
0208     ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH);
0209     if (ret != SHTC1_CMD_LENGTH) {
0210         dev_err(dev, "could not send read_id_reg command: %d\n", ret);
0211         return ret < 0 ? ret : -ENODEV;
0212     }
0213     ret = i2c_master_recv(client, id_reg_buf, sizeof(id_reg_buf));
0214     if (ret != sizeof(id_reg_buf)) {
0215         dev_err(dev, "could not read ID register: %d\n", ret);
0216         return -ENODEV;
0217     }
0218 
0219     id_reg = be16_to_cpup((__be16 *)id_reg_buf);
0220     if (chip == shtc3) {
0221         if ((id_reg & SHTC3_ID_MASK) != SHTC3_ID) {
0222             dev_err(dev, "SHTC3 ID register does not match\n");
0223             return -ENODEV;
0224         }
0225     } else if ((id_reg & SHTC1_ID_MASK) != SHTC1_ID) {
0226         dev_err(dev, "SHTC1 ID register does not match\n");
0227         return -ENODEV;
0228     }
0229 
0230     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0231     if (!data)
0232         return -ENOMEM;
0233 
0234     data->setup.blocking_io = false;
0235     data->setup.high_precision = true;
0236     data->client = client;
0237     data->chip = chip;
0238 
0239     if (np) {
0240         data->setup.blocking_io = of_property_read_bool(np, "sensirion,blocking-io");
0241         data->setup.high_precision = !of_property_read_bool(np, "sensicon,low-precision");
0242     } else {
0243         if (client->dev.platform_data)
0244             data->setup = *(struct shtc1_platform_data *)dev->platform_data;
0245     }
0246 
0247     shtc1_select_command(data);
0248     mutex_init(&data->update_lock);
0249 
0250     hwmon_dev = devm_hwmon_device_register_with_groups(dev,
0251                                client->name,
0252                                data,
0253                                shtc1_groups);
0254     if (IS_ERR(hwmon_dev))
0255         dev_dbg(dev, "unable to register hwmon device\n");
0256 
0257     return PTR_ERR_OR_ZERO(hwmon_dev);
0258 }
0259 
0260 /* device ID table */
0261 static const struct i2c_device_id shtc1_id[] = {
0262     { "shtc1", shtc1 },
0263     { "shtw1", shtc1 },
0264     { "shtc3", shtc3 },
0265     { }
0266 };
0267 MODULE_DEVICE_TABLE(i2c, shtc1_id);
0268 
0269 static const struct of_device_id shtc1_of_match[] = {
0270     { .compatible = "sensirion,shtc1" },
0271     { .compatible = "sensirion,shtw1" },
0272     { .compatible = "sensirion,shtc3" },
0273     { }
0274 };
0275 MODULE_DEVICE_TABLE(of, shtc1_of_match);
0276 
0277 static struct i2c_driver shtc1_i2c_driver = {
0278     .driver = {
0279         .name = "shtc1",
0280         .of_match_table = shtc1_of_match,
0281     },
0282     .probe_new    = shtc1_probe,
0283     .id_table     = shtc1_id,
0284 };
0285 
0286 module_i2c_driver(shtc1_i2c_driver);
0287 
0288 MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>");
0289 MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver");
0290 MODULE_LICENSE("GPL");