Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 2 | /* Sensirion SHTC1 humidity and temperature sensor driver |
| 3 | * |
| 4 | * Copyright (C) 2014 Sensirion AG, Switzerland |
| 5 | * Author: Johannes Winkelmann <johannes.winkelmann@sensirion.com> |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/hwmon.h> |
| 13 | #include <linux/hwmon-sysfs.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/platform_data/shtc1.h> |
| 17 | |
| 18 | /* commands (high precision mode) */ |
| 19 | static const unsigned char shtc1_cmd_measure_blocking_hpm[] = { 0x7C, 0xA2 }; |
| 20 | static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 }; |
| 21 | |
| 22 | /* commands (low precision mode) */ |
| 23 | static const unsigned char shtc1_cmd_measure_blocking_lpm[] = { 0x64, 0x58 }; |
| 24 | static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c }; |
| 25 | |
| 26 | /* command for reading the ID register */ |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 27 | static const unsigned char shtc1_cmd_read_id_reg[] = { 0xef, 0xc8 }; |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 28 | |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 29 | /* |
| 30 | * constants for reading the ID register |
| 31 | * SHTC1: 0x0007 with mask 0x003f |
| 32 | * SHTW1: 0x0007 with mask 0x003f |
| 33 | * SHTC3: 0x0807 with mask 0x083f |
| 34 | */ |
| 35 | #define SHTC3_ID 0x0807 |
| 36 | #define SHTC3_ID_MASK 0x083f |
| 37 | #define SHTC1_ID 0x0007 |
| 38 | #define SHTC1_ID_MASK 0x003f |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 39 | |
| 40 | /* delays for non-blocking i2c commands, both in us */ |
| 41 | #define SHTC1_NONBLOCKING_WAIT_TIME_HPM 14400 |
| 42 | #define SHTC1_NONBLOCKING_WAIT_TIME_LPM 1000 |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 43 | #define SHTC3_NONBLOCKING_WAIT_TIME_HPM 12100 |
| 44 | #define SHTC3_NONBLOCKING_WAIT_TIME_LPM 800 |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 45 | |
| 46 | #define SHTC1_CMD_LENGTH 2 |
| 47 | #define SHTC1_RESPONSE_LENGTH 6 |
| 48 | |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 49 | enum shtcx_chips { |
| 50 | shtc1, |
| 51 | shtc3, |
| 52 | }; |
| 53 | |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 54 | struct shtc1_data { |
| 55 | struct i2c_client *client; |
| 56 | struct mutex update_lock; |
| 57 | bool valid; |
| 58 | unsigned long last_updated; /* in jiffies */ |
| 59 | |
| 60 | const unsigned char *command; |
| 61 | unsigned int nonblocking_wait_time; /* in us */ |
| 62 | |
| 63 | struct shtc1_platform_data setup; |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 64 | enum shtcx_chips chip; |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 65 | |
| 66 | int temperature; /* 1000 * temperature in dgr C */ |
| 67 | int humidity; /* 1000 * relative humidity in %RH */ |
| 68 | }; |
| 69 | |
| 70 | static int shtc1_update_values(struct i2c_client *client, |
| 71 | struct shtc1_data *data, |
| 72 | char *buf, int bufsize) |
| 73 | { |
| 74 | int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH); |
| 75 | if (ret != SHTC1_CMD_LENGTH) { |
| 76 | dev_err(&client->dev, "failed to send command: %d\n", ret); |
| 77 | return ret < 0 ? ret : -EIO; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * In blocking mode (clock stretching mode) the I2C bus |
| 82 | * is blocked for other traffic, thus the call to i2c_master_recv() |
| 83 | * will wait until the data is ready. For non blocking mode, we |
| 84 | * have to wait ourselves. |
| 85 | */ |
| 86 | if (!data->setup.blocking_io) |
| 87 | usleep_range(data->nonblocking_wait_time, |
| 88 | data->nonblocking_wait_time + 1000); |
| 89 | |
| 90 | ret = i2c_master_recv(client, buf, bufsize); |
| 91 | if (ret != bufsize) { |
| 92 | dev_err(&client->dev, "failed to read values: %d\n", ret); |
| 93 | return ret < 0 ? ret : -EIO; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* sysfs attributes */ |
| 100 | static struct shtc1_data *shtc1_update_client(struct device *dev) |
| 101 | { |
| 102 | struct shtc1_data *data = dev_get_drvdata(dev); |
| 103 | struct i2c_client *client = data->client; |
| 104 | unsigned char buf[SHTC1_RESPONSE_LENGTH]; |
| 105 | int val; |
| 106 | int ret = 0; |
| 107 | |
| 108 | mutex_lock(&data->update_lock); |
| 109 | |
| 110 | if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) { |
| 111 | ret = shtc1_update_values(client, data, buf, sizeof(buf)); |
| 112 | if (ret) |
| 113 | goto out; |
| 114 | |
| 115 | /* |
| 116 | * From datasheet: |
| 117 | * T = -45 + 175 * ST / 2^16 |
| 118 | * RH = 100 * SRH / 2^16 |
| 119 | * |
| 120 | * Adapted for integer fixed point (3 digit) arithmetic. |
| 121 | */ |
| 122 | val = be16_to_cpup((__be16 *)buf); |
| 123 | data->temperature = ((21875 * val) >> 13) - 45000; |
| 124 | val = be16_to_cpup((__be16 *)(buf + 3)); |
| 125 | data->humidity = ((12500 * val) >> 13); |
| 126 | |
| 127 | data->last_updated = jiffies; |
| 128 | data->valid = true; |
| 129 | } |
| 130 | |
| 131 | out: |
| 132 | mutex_unlock(&data->update_lock); |
| 133 | |
| 134 | return ret == 0 ? data : ERR_PTR(ret); |
| 135 | } |
| 136 | |
| 137 | static ssize_t temp1_input_show(struct device *dev, |
| 138 | struct device_attribute *attr, |
| 139 | char *buf) |
| 140 | { |
| 141 | struct shtc1_data *data = shtc1_update_client(dev); |
| 142 | if (IS_ERR(data)) |
| 143 | return PTR_ERR(data); |
| 144 | |
| 145 | return sprintf(buf, "%d\n", data->temperature); |
| 146 | } |
| 147 | |
| 148 | static ssize_t humidity1_input_show(struct device *dev, |
| 149 | struct device_attribute *attr, char *buf) |
| 150 | { |
| 151 | struct shtc1_data *data = shtc1_update_client(dev); |
| 152 | if (IS_ERR(data)) |
| 153 | return PTR_ERR(data); |
| 154 | |
| 155 | return sprintf(buf, "%d\n", data->humidity); |
| 156 | } |
| 157 | |
| 158 | static DEVICE_ATTR_RO(temp1_input); |
| 159 | static DEVICE_ATTR_RO(humidity1_input); |
| 160 | |
| 161 | static struct attribute *shtc1_attrs[] = { |
| 162 | &dev_attr_temp1_input.attr, |
| 163 | &dev_attr_humidity1_input.attr, |
| 164 | NULL |
| 165 | }; |
| 166 | |
| 167 | ATTRIBUTE_GROUPS(shtc1); |
| 168 | |
| 169 | static void shtc1_select_command(struct shtc1_data *data) |
| 170 | { |
| 171 | if (data->setup.high_precision) { |
| 172 | data->command = data->setup.blocking_io ? |
| 173 | shtc1_cmd_measure_blocking_hpm : |
| 174 | shtc1_cmd_measure_nonblocking_hpm; |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 175 | data->nonblocking_wait_time = (data->chip == shtc1) ? |
| 176 | SHTC1_NONBLOCKING_WAIT_TIME_HPM : |
| 177 | SHTC3_NONBLOCKING_WAIT_TIME_HPM; |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 178 | } else { |
| 179 | data->command = data->setup.blocking_io ? |
| 180 | shtc1_cmd_measure_blocking_lpm : |
| 181 | shtc1_cmd_measure_nonblocking_lpm; |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 182 | data->nonblocking_wait_time = (data->chip == shtc1) ? |
| 183 | SHTC1_NONBLOCKING_WAIT_TIME_LPM : |
| 184 | SHTC3_NONBLOCKING_WAIT_TIME_LPM; |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 188 | static const struct i2c_device_id shtc1_id[]; |
| 189 | |
| 190 | static int shtc1_probe(struct i2c_client *client) |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 191 | { |
| 192 | int ret; |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 193 | u16 id_reg; |
| 194 | char id_reg_buf[2]; |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 195 | struct shtc1_data *data; |
| 196 | struct device *hwmon_dev; |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 197 | enum shtcx_chips chip = i2c_match_id(shtc1_id, client)->driver_data; |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 198 | struct i2c_adapter *adap = client->adapter; |
| 199 | struct device *dev = &client->dev; |
| 200 | |
| 201 | if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) { |
| 202 | dev_err(dev, "plain i2c transactions not supported\n"); |
| 203 | return -ENODEV; |
| 204 | } |
| 205 | |
| 206 | ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH); |
| 207 | if (ret != SHTC1_CMD_LENGTH) { |
| 208 | dev_err(dev, "could not send read_id_reg command: %d\n", ret); |
| 209 | return ret < 0 ? ret : -ENODEV; |
| 210 | } |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 211 | ret = i2c_master_recv(client, id_reg_buf, sizeof(id_reg_buf)); |
| 212 | if (ret != sizeof(id_reg_buf)) { |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 213 | dev_err(dev, "could not read ID register: %d\n", ret); |
| 214 | return -ENODEV; |
| 215 | } |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 216 | |
| 217 | id_reg = be16_to_cpup((__be16 *)id_reg_buf); |
| 218 | if (chip == shtc3) { |
| 219 | if ((id_reg & SHTC3_ID_MASK) != SHTC3_ID) { |
| 220 | dev_err(dev, "SHTC3 ID register does not match\n"); |
| 221 | return -ENODEV; |
| 222 | } |
| 223 | } else if ((id_reg & SHTC1_ID_MASK) != SHTC1_ID) { |
| 224 | dev_err(dev, "SHTC1 ID register does not match\n"); |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 225 | return -ENODEV; |
| 226 | } |
| 227 | |
| 228 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 229 | if (!data) |
| 230 | return -ENOMEM; |
| 231 | |
| 232 | data->setup.blocking_io = false; |
| 233 | data->setup.high_precision = true; |
| 234 | data->client = client; |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 235 | data->chip = chip; |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 236 | |
| 237 | if (client->dev.platform_data) |
| 238 | data->setup = *(struct shtc1_platform_data *)dev->platform_data; |
| 239 | shtc1_select_command(data); |
| 240 | mutex_init(&data->update_lock); |
| 241 | |
| 242 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, |
| 243 | client->name, |
| 244 | data, |
| 245 | shtc1_groups); |
| 246 | if (IS_ERR(hwmon_dev)) |
| 247 | dev_dbg(dev, "unable to register hwmon device\n"); |
| 248 | |
| 249 | return PTR_ERR_OR_ZERO(hwmon_dev); |
| 250 | } |
| 251 | |
| 252 | /* device ID table */ |
| 253 | static const struct i2c_device_id shtc1_id[] = { |
Dan Robertson | ffd9686 | 2019-09-05 01:45:53 +0000 | [diff] [blame] | 254 | { "shtc1", shtc1 }, |
| 255 | { "shtw1", shtc1 }, |
| 256 | { "shtc3", shtc3 }, |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 257 | { } |
| 258 | }; |
| 259 | MODULE_DEVICE_TABLE(i2c, shtc1_id); |
| 260 | |
| 261 | static struct i2c_driver shtc1_i2c_driver = { |
| 262 | .driver.name = "shtc1", |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 263 | .probe_new = shtc1_probe, |
Tomas Pop | 1a539d3 | 2014-06-05 15:24:19 -0700 | [diff] [blame] | 264 | .id_table = shtc1_id, |
| 265 | }; |
| 266 | |
| 267 | module_i2c_driver(shtc1_i2c_driver); |
| 268 | |
| 269 | MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>"); |
| 270 | MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver"); |
| 271 | MODULE_LICENSE("GPL"); |