1 // SPDX-License-Identifier: GPL-2.0-only 2 3 /* 4 * Copyright (c) Linumiz 2021 5 * 6 * sht4x.c - Linux hwmon driver for SHT4x Temperature and Humidity sensor 7 * 8 * Author: Navin Sankar Velliangiri <navin@linumiz.com> 9 */ 10 11 #include <linux/crc8.h> 12 #include <linux/delay.h> 13 #include <linux/hwmon.h> 14 #include <linux/hwmon-sysfs.h> 15 #include <linux/i2c.h> 16 #include <linux/jiffies.h> 17 #include <linux/module.h> 18 19 /* 20 * Poll intervals (in milliseconds) 21 */ 22 #define SHT4X_MIN_POLL_INTERVAL 2000 23 24 /* 25 * I2C command delays (in microseconds) 26 */ 27 #define SHT4X_MEAS_DELAY_HPM 8200 /* see t_MEAS,h in datasheet */ 28 #define SHT4X_DELAY_EXTRA 10000 29 30 /* 31 * Command Bytes 32 */ 33 #define SHT4X_CMD_MEASURE_HPM 0b11111101 34 #define SHT4X_CMD_RESET 0b10010100 35 #define SHT4X_CMD_HEATER_20_1 0b00011110 36 #define SHT4X_CMD_HEATER_20_01 0b00010101 37 #define SHT4X_CMD_HEATER_110_1 0b00101111 38 #define SHT4X_CMD_HEATER_110_01 0b00100100 39 #define SHT4X_CMD_HEATER_200_1 0b00111001 40 #define SHT4X_CMD_HEATER_200_01 0b00110010 41 42 #define SHT4X_CMD_LEN 1 43 #define SHT4X_CRC8_LEN 1 44 #define SHT4X_WORD_LEN 2 45 #define SHT4X_RESPONSE_LENGTH 6 46 #define STS4X_RESPONSE_LENGTH 3 47 #define SHT4X_CRC8_POLYNOMIAL 0x31 48 #define SHT4X_CRC8_INIT 0xff 49 #define SHT4X_MIN_TEMPERATURE -45000 50 #define SHT4X_MAX_TEMPERATURE 125000 51 #define SHT4X_MIN_HUMIDITY 0 52 #define SHT4X_MAX_HUMIDITY 100000 53 54 DECLARE_CRC8_TABLE(sht4x_crc8_table); 55 56 enum sht4x_chips { 57 sht4x, 58 sts4x, 59 }; 60 61 /** 62 * struct sht4x_data - All the data required to operate an SHT4X chip 63 * @client: the i2c client associated with the SHT4X 64 * @chip_id: the chip type (sht4x or sts4x) 65 * @heating_complete: the time that the last heating finished 66 * @data_pending: true if and only if there are measurements to retrieve after heating 67 * @heater_power: the power at which the heater will be started 68 * @heater_time: the time for which the heater will remain turned on 69 * @valid: validity of fields below 70 * @update_interval: the minimum poll interval 71 * @last_updated: the previous time that the SHT4X was polled 72 * @temperature: the latest temperature value received from the SHT4X 73 * @humidity: the latest humidity value received from the SHT4X 74 */ 75 struct sht4x_data { 76 struct i2c_client *client; 77 enum sht4x_chips chip_id; 78 u64 heating_complete; /* in jiffies */ 79 bool data_pending; 80 u32 heater_power; /* in milli-watts */ 81 u32 heater_time; /* in milli-seconds */ 82 bool valid; /* validity of fields below */ 83 long update_interval; /* in milli-seconds */ 84 long last_updated; /* in jiffies */ 85 s32 temperature; 86 s32 humidity; 87 }; 88 89 /** 90 * sht4x_read_values() - read and parse the raw data from the SHT4X 91 * @data: the struct sht4x_data to use for the lock 92 * Return: 0 if successful, -ERRNO if not 93 */ 94 static int sht4x_read_values(struct sht4x_data *data) 95 { 96 int ret; 97 u16 t_ticks, rh_ticks; 98 unsigned long next_update; 99 struct i2c_client *client = data->client; 100 u8 crc; 101 u8 cmd[SHT4X_CMD_LEN] = {SHT4X_CMD_MEASURE_HPM}; 102 u8 raw_data[SHT4X_RESPONSE_LENGTH]; 103 size_t response_length = data->chip_id == sts4x ? 104 STS4X_RESPONSE_LENGTH : SHT4X_RESPONSE_LENGTH; 105 106 if (data->chip_id != sts4x) { 107 u64 curr_jiffies = get_jiffies_64(); 108 109 if (time_before64(curr_jiffies, data->heating_complete)) 110 msleep(jiffies_to_msecs(data->heating_complete - curr_jiffies)); 111 } 112 113 if (data->data_pending && 114 time_before64(get_jiffies_64(), 115 data->heating_complete + 116 msecs_to_jiffies(data->update_interval))) { 117 data->data_pending = false; 118 } else { 119 next_update = data->last_updated + 120 msecs_to_jiffies(data->update_interval); 121 122 if (data->valid && time_before_eq(jiffies, next_update)) 123 return 0; 124 125 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN); 126 if (ret < 0) 127 return ret; 128 129 usleep_range(SHT4X_MEAS_DELAY_HPM, SHT4X_MEAS_DELAY_HPM + SHT4X_DELAY_EXTRA); 130 } 131 132 ret = i2c_master_recv(client, raw_data, response_length); 133 if (ret != response_length) { 134 if (ret >= 0) 135 ret = -ENODATA; 136 return ret; 137 } 138 139 t_ticks = raw_data[0] << 8 | raw_data[1]; 140 141 crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE); 142 if (crc != raw_data[2]) { 143 dev_err(&client->dev, "data integrity check failed\n"); 144 return -EIO; 145 } 146 147 data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000; 148 149 if (data->chip_id != sts4x) { 150 rh_ticks = raw_data[3] << 8 | raw_data[4]; 151 crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE); 152 if (crc != raw_data[5]) { 153 dev_err(&client->dev, "data integrity check failed\n"); 154 return -EIO; 155 } 156 157 data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000; 158 } 159 160 data->last_updated = jiffies; 161 data->valid = true; 162 return 0; 163 } 164 165 static ssize_t sht4x_interval_write(struct sht4x_data *data, long val) 166 { 167 data->update_interval = clamp_val(val, SHT4X_MIN_POLL_INTERVAL, INT_MAX); 168 169 return 0; 170 } 171 172 /* sht4x_interval_read() - read the minimum poll interval in milliseconds */ 173 static size_t sht4x_interval_read(struct sht4x_data *data, long *val) 174 { 175 *val = data->update_interval; 176 return 0; 177 } 178 179 /* sht4x_temperature1_read() - read the temperature in millidegrees */ 180 static int sht4x_temperature1_read(struct sht4x_data *data, long *val) 181 { 182 int ret; 183 184 ret = sht4x_read_values(data); 185 if (ret < 0) 186 return ret; 187 188 *val = data->temperature; 189 190 return 0; 191 } 192 193 /* sht4x_humidity1_read() - read a relative humidity in millipercent */ 194 static int sht4x_humidity1_read(struct sht4x_data *data, long *val) 195 { 196 int ret; 197 198 ret = sht4x_read_values(data); 199 if (ret < 0) 200 return ret; 201 202 *val = data->humidity; 203 204 return 0; 205 } 206 207 static umode_t sht4x_hwmon_visible(const void *data, 208 enum hwmon_sensor_types type, 209 u32 attr, int channel) 210 { 211 const struct sht4x_data *chip_data = data; 212 213 switch (type) { 214 case hwmon_temp: 215 return 0444; 216 case hwmon_humidity: 217 if (chip_data->chip_id == sts4x) 218 return 0; 219 return 0444; 220 case hwmon_chip: 221 return 0644; 222 default: 223 return 0; 224 } 225 } 226 227 static int sht4x_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 228 u32 attr, int channel, long *val) 229 { 230 struct sht4x_data *data = dev_get_drvdata(dev); 231 232 switch (type) { 233 case hwmon_temp: 234 return sht4x_temperature1_read(data, val); 235 case hwmon_humidity: 236 return sht4x_humidity1_read(data, val); 237 case hwmon_chip: 238 return sht4x_interval_read(data, val); 239 default: 240 return -EOPNOTSUPP; 241 } 242 } 243 244 static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type, 245 u32 attr, int channel, long val) 246 { 247 struct sht4x_data *data = dev_get_drvdata(dev); 248 249 switch (type) { 250 case hwmon_chip: 251 return sht4x_interval_write(data, val); 252 default: 253 return -EOPNOTSUPP; 254 } 255 } 256 257 static ssize_t heater_enable_show(struct device *dev, 258 struct device_attribute *attr, 259 char *buf) 260 { 261 struct sht4x_data *data = dev_get_drvdata(dev); 262 263 guard(hwmon_lock)(dev); 264 265 return sysfs_emit(buf, "%u\n", time_before64(get_jiffies_64(), data->heating_complete)); 266 } 267 268 static ssize_t heater_enable_store(struct device *dev, 269 struct device_attribute *attr, 270 const char *buf, 271 size_t count) 272 { 273 struct sht4x_data *data = dev_get_drvdata(dev); 274 bool status; 275 ssize_t ret; 276 u8 cmd; 277 u32 heating_time_bound; 278 279 ret = kstrtobool(buf, &status); 280 if (ret) 281 return ret; 282 if (!status) 283 return -EINVAL; 284 285 if (data->heater_time == 100) { 286 if (data->heater_power == 20) 287 cmd = SHT4X_CMD_HEATER_20_01; 288 else if (data->heater_power == 110) 289 cmd = SHT4X_CMD_HEATER_110_01; 290 else /* data->heater_power == 200 */ 291 cmd = SHT4X_CMD_HEATER_200_01; 292 293 heating_time_bound = 110; 294 } else { /* data->heater_time == 1000 */ 295 if (data->heater_power == 20) 296 cmd = SHT4X_CMD_HEATER_20_1; 297 else if (data->heater_power == 110) 298 cmd = SHT4X_CMD_HEATER_110_1; 299 else /* data->heater_power == 200 */ 300 cmd = SHT4X_CMD_HEATER_200_1; 301 302 heating_time_bound = 1100; 303 } 304 305 guard(hwmon_lock)(dev); 306 307 if (time_before64(get_jiffies_64(), data->heating_complete)) 308 return -EBUSY; 309 310 ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN); 311 if (ret < 0) 312 return ret; 313 if (ret != SHT4X_CMD_LEN) 314 return -EIO; 315 316 data->heating_complete = get_jiffies_64() + msecs_to_jiffies(heating_time_bound); 317 data->data_pending = true; 318 return count; 319 } 320 321 static ssize_t heater_power_show(struct device *dev, 322 struct device_attribute *attr, 323 char *buf) 324 { 325 struct sht4x_data *data = dev_get_drvdata(dev); 326 327 return sysfs_emit(buf, "%u\n", data->heater_power); 328 } 329 330 static ssize_t heater_power_store(struct device *dev, 331 struct device_attribute *attr, 332 const char *buf, 333 size_t count) 334 { 335 struct sht4x_data *data = dev_get_drvdata(dev); 336 u32 power; 337 ssize_t ret; 338 339 ret = kstrtou32(buf, 10, &power); 340 if (ret) 341 return ret; 342 343 if (power != 20 && power != 110 && power != 200) 344 return -EINVAL; 345 346 guard(hwmon_lock)(dev); 347 348 data->heater_power = power; 349 350 return count; 351 } 352 353 static ssize_t heater_time_show(struct device *dev, 354 struct device_attribute *attr, 355 char *buf) 356 { 357 struct sht4x_data *data = dev_get_drvdata(dev); 358 359 return sysfs_emit(buf, "%u\n", data->heater_time); 360 } 361 362 static ssize_t heater_time_store(struct device *dev, 363 struct device_attribute *attr, 364 const char *buf, 365 size_t count) 366 { 367 struct sht4x_data *data = dev_get_drvdata(dev); 368 u32 time; 369 ssize_t ret; 370 371 ret = kstrtou32(buf, 10, &time); 372 if (ret) 373 return ret; 374 375 if (time != 100 && time != 1000) 376 return -EINVAL; 377 378 guard(hwmon_lock)(dev); 379 380 data->heater_time = time; 381 382 return count; 383 } 384 385 static DEVICE_ATTR_RW(heater_enable); 386 static DEVICE_ATTR_RW(heater_power); 387 static DEVICE_ATTR_RW(heater_time); 388 389 static struct attribute *sht4x_attrs[] = { 390 &dev_attr_heater_enable.attr, 391 &dev_attr_heater_power.attr, 392 &dev_attr_heater_time.attr, 393 NULL 394 }; 395 396 ATTRIBUTE_GROUPS(sht4x); 397 398 static const struct hwmon_channel_info * const sht4x_info[] = { 399 HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL), 400 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT), 401 HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT), 402 NULL, 403 }; 404 405 static const struct hwmon_ops sht4x_hwmon_ops = { 406 .is_visible = sht4x_hwmon_visible, 407 .read = sht4x_hwmon_read, 408 .write = sht4x_hwmon_write, 409 }; 410 411 static const struct hwmon_chip_info sht4x_chip_info = { 412 .ops = &sht4x_hwmon_ops, 413 .info = sht4x_info, 414 }; 415 416 static int sht4x_probe(struct i2c_client *client) 417 { 418 const struct attribute_group **groups = NULL; 419 struct device *device = &client->dev; 420 struct device *hwmon_dev; 421 struct sht4x_data *data; 422 u8 cmd[] = {SHT4X_CMD_RESET}; 423 int ret; 424 425 /* 426 * we require full i2c support since the sht4x uses multi-byte read and 427 * writes as well as multi-byte commands which are not supported by 428 * the smbus protocol 429 */ 430 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 431 return -EOPNOTSUPP; 432 433 data = devm_kzalloc_obj(device, *data); 434 if (!data) 435 return -ENOMEM; 436 437 data->chip_id = (uintptr_t)i2c_get_match_data(client); 438 data->update_interval = SHT4X_MIN_POLL_INTERVAL; 439 data->client = client; 440 if (data->chip_id != sts4x) { 441 data->heater_power = 200; 442 data->heater_time = 1000; 443 data->heating_complete = get_jiffies_64(); 444 groups = sht4x_groups; 445 } 446 447 crc8_populate_msb(sht4x_crc8_table, SHT4X_CRC8_POLYNOMIAL); 448 449 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN); 450 if (ret < 0) 451 return ret; 452 if (ret != SHT4X_CMD_LEN) 453 return -EIO; 454 455 hwmon_dev = devm_hwmon_device_register_with_info(device, 456 client->name, 457 data, 458 &sht4x_chip_info, 459 groups); 460 461 return PTR_ERR_OR_ZERO(hwmon_dev); 462 } 463 464 static const struct i2c_device_id sht4x_id[] = { 465 { .name = "sht4x", .driver_data = sht4x }, 466 { .name = "sts4x", .driver_data = sts4x }, 467 { } 468 }; 469 MODULE_DEVICE_TABLE(i2c, sht4x_id); 470 471 static const struct of_device_id sht4x_of_match[] = { 472 { .compatible = "sensirion,sht4x", .data = (void *)sht4x }, 473 { .compatible = "sensirion,sts40", .data = (void *)sts4x }, 474 { } 475 }; 476 MODULE_DEVICE_TABLE(of, sht4x_of_match); 477 478 static struct i2c_driver sht4x_driver = { 479 .driver = { 480 .name = "sht4x", 481 .of_match_table = sht4x_of_match, 482 }, 483 .probe = sht4x_probe, 484 .id_table = sht4x_id, 485 }; 486 487 module_i2c_driver(sht4x_driver); 488 489 MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>"); 490 MODULE_DESCRIPTION("Sensirion SHT4x humidity and temperature sensor driver"); 491 MODULE_LICENSE("GPL v2"); 492