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