| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* Sensirion SHT3x-DIS humidity and temperature sensor driver. |
| 3 | * The SHT3x comes in many different versions, this driver is for the |
| 4 | * I2C version only. |
| 5 | * |
| 6 | * Copyright (C) 2016 Sensirion AG, Switzerland |
| 7 | * Author: David Frey <david.frey@sensirion.com> |
| 8 | * Author: Pascal Sachs <pascal.sachs@sensirion.com> |
| 9 | */ |
| 10 | |
| 11 | #include <asm/page.h> |
| 12 | #include <linux/crc8.h> |
| 13 | #include <linux/debugfs.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/hwmon.h> |
| 17 | #include <linux/hwmon-sysfs.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | |
| 25 | /* commands (high repeatability mode) */ |
| 26 | static const unsigned char sht3x_cmd_measure_single_hpm[] = { 0x24, 0x00 }; |
| 27 | |
| 28 | /* commands (medium repeatability mode) */ |
| 29 | static const unsigned char sht3x_cmd_measure_single_mpm[] = { 0x24, 0x0b }; |
| 30 | |
| 31 | /* commands (low repeatability mode) */ |
| 32 | static const unsigned char sht3x_cmd_measure_single_lpm[] = { 0x24, 0x16 }; |
| 33 | |
| 34 | /* commands for periodic mode */ |
| 35 | static const unsigned char sht3x_cmd_measure_periodic_mode[] = { 0xe0, 0x00 }; |
| 36 | static const unsigned char sht3x_cmd_break[] = { 0x30, 0x93 }; |
| 37 | |
| 38 | /* commands for heater control */ |
| 39 | static const unsigned char sht3x_cmd_heater_on[] = { 0x30, 0x6d }; |
| 40 | static const unsigned char sht3x_cmd_heater_off[] = { 0x30, 0x66 }; |
| 41 | |
| 42 | /* other commands */ |
| 43 | static const unsigned char sht3x_cmd_read_status_reg[] = { 0xf3, 0x2d }; |
| 44 | static const unsigned char sht3x_cmd_clear_status_reg[] = { 0x30, 0x41 }; |
| 45 | static const unsigned char sht3x_cmd_read_serial_number[] = { 0x37, 0x80 }; |
| 46 | |
| 47 | /* delays for single-shot mode i2c commands, both in us */ |
| 48 | #define SHT3X_SINGLE_WAIT_TIME_HPM 15000 |
| 49 | #define SHT3X_SINGLE_WAIT_TIME_MPM 6000 |
| 50 | #define SHT3X_SINGLE_WAIT_TIME_LPM 4000 |
| 51 | |
| 52 | #define SHT3X_WORD_LEN 2 |
| 53 | #define SHT3X_CMD_LENGTH 2 |
| 54 | #define SHT3X_CRC8_LEN 1 |
| 55 | #define SHT3X_RESPONSE_LENGTH 6 |
| 56 | #define SHT3X_CRC8_POLYNOMIAL 0x31 |
| 57 | #define SHT3X_CRC8_INIT 0xFF |
| 58 | #define SHT3X_MIN_TEMPERATURE -45000 |
| 59 | #define SHT3X_MAX_TEMPERATURE 130000 |
| 60 | #define SHT3X_MIN_HUMIDITY 0 |
| 61 | #define SHT3X_MAX_HUMIDITY 100000 |
| 62 | |
| 63 | enum sht3x_chips { |
| 64 | sht3x, |
| 65 | sts3x, |
| 66 | }; |
| 67 | |
| 68 | enum sht3x_limits { |
| 69 | limit_max = 0, |
| 70 | limit_max_hyst, |
| 71 | limit_min, |
| 72 | limit_min_hyst, |
| 73 | }; |
| 74 | |
| 75 | enum sht3x_repeatability { |
| 76 | low_repeatability, |
| 77 | medium_repeatability, |
| 78 | high_repeatability, |
| 79 | }; |
| 80 | |
| 81 | DECLARE_CRC8_TABLE(sht3x_crc8_table); |
| 82 | |
| 83 | /* periodic measure commands (high repeatability mode) */ |
| 84 | static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = { |
| 85 | /* 0.5 measurements per second */ |
| 86 | {0x20, 0x32}, |
| 87 | /* 1 measurements per second */ |
| 88 | {0x21, 0x30}, |
| 89 | /* 2 measurements per second */ |
| 90 | {0x22, 0x36}, |
| 91 | /* 4 measurements per second */ |
| 92 | {0x23, 0x34}, |
| 93 | /* 10 measurements per second */ |
| 94 | {0x27, 0x37}, |
| 95 | }; |
| 96 | |
| 97 | /* periodic measure commands (medium repeatability) */ |
| 98 | static const char periodic_measure_commands_mpm[][SHT3X_CMD_LENGTH] = { |
| 99 | /* 0.5 measurements per second */ |
| 100 | {0x20, 0x24}, |
| 101 | /* 1 measurements per second */ |
| 102 | {0x21, 0x26}, |
| 103 | /* 2 measurements per second */ |
| 104 | {0x22, 0x20}, |
| 105 | /* 4 measurements per second */ |
| 106 | {0x23, 0x22}, |
| 107 | /* 10 measurements per second */ |
| 108 | {0x27, 0x21}, |
| 109 | }; |
| 110 | |
| 111 | /* periodic measure commands (low repeatability mode) */ |
| 112 | static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = { |
| 113 | /* 0.5 measurements per second */ |
| 114 | {0x20, 0x2f}, |
| 115 | /* 1 measurements per second */ |
| 116 | {0x21, 0x2d}, |
| 117 | /* 2 measurements per second */ |
| 118 | {0x22, 0x2b}, |
| 119 | /* 4 measurements per second */ |
| 120 | {0x23, 0x29}, |
| 121 | /* 10 measurements per second */ |
| 122 | {0x27, 0x2a}, |
| 123 | }; |
| 124 | |
| 125 | struct sht3x_limit_commands { |
| 126 | const char read_command[SHT3X_CMD_LENGTH]; |
| 127 | const char write_command[SHT3X_CMD_LENGTH]; |
| 128 | }; |
| 129 | |
| 130 | static const struct sht3x_limit_commands limit_commands[] = { |
| 131 | /* temp1_max, humidity1_max */ |
| 132 | [limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} }, |
| 133 | /* temp_1_max_hyst, humidity1_max_hyst */ |
| 134 | [limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} }, |
| 135 | /* temp1_min, humidity1_min */ |
| 136 | [limit_min] = { {0xe1, 0x02}, {0x61, 0x00} }, |
| 137 | /* temp_1_min_hyst, humidity1_min_hyst */ |
| 138 | [limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} }, |
| 139 | }; |
| 140 | |
| 141 | #define SHT3X_NUM_LIMIT_CMD ARRAY_SIZE(limit_commands) |
| 142 | |
| 143 | static const u16 mode_to_update_interval[] = { |
| 144 | 0, |
| 145 | 2000, |
| 146 | 1000, |
| 147 | 500, |
| 148 | 250, |
| 149 | 100, |
| 150 | }; |
| 151 | |
| 152 | static const struct hwmon_channel_info * const sht3x_channel_info[] = { |
| 153 | HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL), |
| 154 | HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MIN | |
| 155 | HWMON_T_MIN_HYST | HWMON_T_MAX | |
| 156 | HWMON_T_MAX_HYST | HWMON_T_ALARM), |
| 157 | HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT | HWMON_H_MIN | |
| 158 | HWMON_H_MIN_HYST | HWMON_H_MAX | |
| 159 | HWMON_H_MAX_HYST | HWMON_H_ALARM), |
| 160 | NULL, |
| 161 | }; |
| 162 | |
| 163 | struct sht3x_data { |
| 164 | struct i2c_client *client; |
| 165 | enum sht3x_chips chip_id; |
| 166 | struct mutex i2c_lock; /* lock for sending i2c commands */ |
| 167 | struct mutex data_lock; /* lock for updating driver data */ |
| 168 | |
| 169 | u8 mode; |
| 170 | const unsigned char *command; |
| 171 | u32 wait_time; /* in us*/ |
| 172 | unsigned long last_update; /* last update in periodic mode*/ |
| 173 | enum sht3x_repeatability repeatability; |
| 174 | u32 serial_number; |
| 175 | |
| 176 | /* |
| 177 | * cached values for temperature and humidity and limits |
| 178 | * the limits arrays have the following order: |
| 179 | * max, max_hyst, min, min_hyst |
| 180 | */ |
| 181 | int temperature; |
| 182 | int temperature_limits[SHT3X_NUM_LIMIT_CMD]; |
| 183 | u32 humidity; |
| 184 | u32 humidity_limits[SHT3X_NUM_LIMIT_CMD]; |
| 185 | }; |
| 186 | |
| 187 | static u8 get_mode_from_update_interval(u16 value) |
| 188 | { |
| 189 | size_t index; |
| 190 | u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval); |
| 191 | |
| 192 | if (value == 0) |
| 193 | return 0; |
| 194 | |
| 195 | /* find next faster update interval */ |
| 196 | for (index = 1; index < number_of_modes; index++) { |
| 197 | if (mode_to_update_interval[index] <= value) |
| 198 | return index; |
| 199 | } |
| 200 | |
| 201 | return number_of_modes - 1; |
| 202 | } |
| 203 | |
| 204 | static int sht3x_read_from_command(struct i2c_client *client, |
| 205 | struct sht3x_data *data, |
| 206 | const char *command, |
| 207 | char *buf, int length, u32 wait_time) |
| 208 | { |
| 209 | int ret; |
| 210 | |
| 211 | mutex_lock(&data->i2c_lock); |
| 212 | ret = i2c_master_send(client, buf: command, SHT3X_CMD_LENGTH); |
| 213 | |
| 214 | if (ret != SHT3X_CMD_LENGTH) { |
| 215 | ret = ret < 0 ? ret : -EIO; |
| 216 | goto out; |
| 217 | } |
| 218 | |
| 219 | if (wait_time) |
| 220 | usleep_range(min: wait_time, max: wait_time + 1000); |
| 221 | |
| 222 | ret = i2c_master_recv(client, buf, count: length); |
| 223 | if (ret != length) { |
| 224 | ret = ret < 0 ? ret : -EIO; |
| 225 | goto out; |
| 226 | } |
| 227 | |
| 228 | ret = 0; |
| 229 | out: |
| 230 | mutex_unlock(lock: &data->i2c_lock); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int (u16 raw) |
| 235 | { |
| 236 | /* |
| 237 | * From datasheet: |
| 238 | * T = -45 + 175 * ST / 2^16 |
| 239 | * Adapted for integer fixed point (3 digit) arithmetic. |
| 240 | */ |
| 241 | return ((21875 * (int)raw) >> 13) - 45000; |
| 242 | } |
| 243 | |
| 244 | static u32 (u16 raw) |
| 245 | { |
| 246 | /* |
| 247 | * From datasheet: |
| 248 | * RH = 100 * SRH / 2^16 |
| 249 | * Adapted for integer fixed point (3 digit) arithmetic. |
| 250 | */ |
| 251 | return (12500 * (u32)raw) >> 13; |
| 252 | } |
| 253 | |
| 254 | static struct sht3x_data *sht3x_update_client(struct device *dev) |
| 255 | { |
| 256 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 257 | struct i2c_client *client = data->client; |
| 258 | u16 interval_ms = mode_to_update_interval[data->mode]; |
| 259 | unsigned long interval_jiffies = msecs_to_jiffies(m: interval_ms); |
| 260 | unsigned char buf[SHT3X_RESPONSE_LENGTH]; |
| 261 | u16 val; |
| 262 | int ret = 0; |
| 263 | |
| 264 | mutex_lock(&data->data_lock); |
| 265 | /* |
| 266 | * Only update cached readings once per update interval in periodic |
| 267 | * mode. In single shot mode the sensor measures values on demand, so |
| 268 | * every time the sysfs interface is called, a measurement is triggered. |
| 269 | * In periodic mode however, the measurement process is handled |
| 270 | * internally by the sensor and reading out sensor values only makes |
| 271 | * sense if a new reading is available. |
| 272 | */ |
| 273 | if (time_after(jiffies, data->last_update + interval_jiffies)) { |
| 274 | ret = sht3x_read_from_command(client, data, command: data->command, buf, |
| 275 | length: sizeof(buf), wait_time: data->wait_time); |
| 276 | if (ret) |
| 277 | goto out; |
| 278 | |
| 279 | val = be16_to_cpup(p: (__be16 *)buf); |
| 280 | data->temperature = sht3x_extract_temperature(raw: val); |
| 281 | val = be16_to_cpup(p: (__be16 *)(buf + 3)); |
| 282 | data->humidity = sht3x_extract_humidity(raw: val); |
| 283 | data->last_update = jiffies; |
| 284 | } |
| 285 | |
| 286 | out: |
| 287 | mutex_unlock(lock: &data->data_lock); |
| 288 | if (ret) |
| 289 | return ERR_PTR(error: ret); |
| 290 | |
| 291 | return data; |
| 292 | } |
| 293 | |
| 294 | static int temp1_input_read(struct device *dev, long *temp) |
| 295 | { |
| 296 | struct sht3x_data *data = sht3x_update_client(dev); |
| 297 | |
| 298 | if (IS_ERR(ptr: data)) |
| 299 | return PTR_ERR(ptr: data); |
| 300 | |
| 301 | *temp = data->temperature; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int humidity1_input_read(struct device *dev, long *humidity) |
| 306 | { |
| 307 | struct sht3x_data *data = sht3x_update_client(dev); |
| 308 | |
| 309 | if (IS_ERR(ptr: data)) |
| 310 | return PTR_ERR(ptr: data); |
| 311 | |
| 312 | *humidity = data->humidity; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * limits_update must only be called from probe or with data_lock held |
| 318 | */ |
| 319 | static int limits_update(struct sht3x_data *data) |
| 320 | { |
| 321 | int ret; |
| 322 | u8 index; |
| 323 | int temperature; |
| 324 | u32 humidity; |
| 325 | u16 raw; |
| 326 | char buffer[SHT3X_RESPONSE_LENGTH]; |
| 327 | const struct sht3x_limit_commands *commands; |
| 328 | struct i2c_client *client = data->client; |
| 329 | |
| 330 | for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) { |
| 331 | commands = &limit_commands[index]; |
| 332 | ret = sht3x_read_from_command(client, data, |
| 333 | command: commands->read_command, buf: buffer, |
| 334 | SHT3X_RESPONSE_LENGTH, wait_time: 0); |
| 335 | |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | |
| 339 | raw = be16_to_cpup(p: (__be16 *)buffer); |
| 340 | temperature = sht3x_extract_temperature(raw: (raw & 0x01ff) << 7); |
| 341 | humidity = sht3x_extract_humidity(raw: raw & 0xfe00); |
| 342 | data->temperature_limits[index] = temperature; |
| 343 | data->humidity_limits[index] = humidity; |
| 344 | } |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | static int temp1_limit_read(struct device *dev, int index) |
| 350 | { |
| 351 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 352 | |
| 353 | return data->temperature_limits[index]; |
| 354 | } |
| 355 | |
| 356 | static int humidity1_limit_read(struct device *dev, int index) |
| 357 | { |
| 358 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 359 | |
| 360 | return data->humidity_limits[index]; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * limit_write must only be called with data_lock held |
| 365 | */ |
| 366 | static size_t limit_write(struct device *dev, |
| 367 | u8 index, |
| 368 | int temperature, |
| 369 | u32 humidity) |
| 370 | { |
| 371 | char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; |
| 372 | char *position = buffer; |
| 373 | int ret; |
| 374 | u16 raw; |
| 375 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 376 | struct i2c_client *client = data->client; |
| 377 | const struct sht3x_limit_commands *commands; |
| 378 | |
| 379 | commands = &limit_commands[index]; |
| 380 | |
| 381 | memcpy(position, commands->write_command, SHT3X_CMD_LENGTH); |
| 382 | position += SHT3X_CMD_LENGTH; |
| 383 | /* |
| 384 | * ST = (T + 45) / 175 * 2^16 |
| 385 | * SRH = RH / 100 * 2^16 |
| 386 | * adapted for fixed point arithmetic and packed the same as |
| 387 | * in limit_read() |
| 388 | */ |
| 389 | raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7); |
| 390 | raw |= ((humidity * 42950) >> 16) & 0xfe00; |
| 391 | |
| 392 | *((__be16 *)position) = cpu_to_be16(raw); |
| 393 | position += SHT3X_WORD_LEN; |
| 394 | *position = crc8(table: sht3x_crc8_table, |
| 395 | pdata: position - SHT3X_WORD_LEN, |
| 396 | SHT3X_WORD_LEN, |
| 397 | SHT3X_CRC8_INIT); |
| 398 | |
| 399 | mutex_lock(&data->i2c_lock); |
| 400 | ret = i2c_master_send(client, buf: buffer, count: sizeof(buffer)); |
| 401 | mutex_unlock(lock: &data->i2c_lock); |
| 402 | |
| 403 | if (ret != sizeof(buffer)) |
| 404 | return ret < 0 ? ret : -EIO; |
| 405 | |
| 406 | data->temperature_limits[index] = temperature; |
| 407 | data->humidity_limits[index] = humidity; |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int temp1_limit_write(struct device *dev, int index, int val) |
| 413 | { |
| 414 | int temperature; |
| 415 | int ret; |
| 416 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 417 | |
| 418 | temperature = clamp_val(val, SHT3X_MIN_TEMPERATURE, |
| 419 | SHT3X_MAX_TEMPERATURE); |
| 420 | mutex_lock(&data->data_lock); |
| 421 | ret = limit_write(dev, index, temperature, |
| 422 | humidity: data->humidity_limits[index]); |
| 423 | mutex_unlock(lock: &data->data_lock); |
| 424 | |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | static int humidity1_limit_write(struct device *dev, int index, int val) |
| 429 | { |
| 430 | u32 humidity; |
| 431 | int ret; |
| 432 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 433 | |
| 434 | humidity = clamp_val(val, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY); |
| 435 | mutex_lock(&data->data_lock); |
| 436 | ret = limit_write(dev, index, temperature: data->temperature_limits[index], |
| 437 | humidity); |
| 438 | mutex_unlock(lock: &data->data_lock); |
| 439 | |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | static void sht3x_select_command(struct sht3x_data *data) |
| 444 | { |
| 445 | /* |
| 446 | * For single-shot mode, only non blocking mode is support, |
| 447 | * we have to wait ourselves for result. |
| 448 | */ |
| 449 | if (data->mode > 0) { |
| 450 | data->command = sht3x_cmd_measure_periodic_mode; |
| 451 | data->wait_time = 0; |
| 452 | } else { |
| 453 | if (data->repeatability == high_repeatability) { |
| 454 | data->command = sht3x_cmd_measure_single_hpm; |
| 455 | data->wait_time = SHT3X_SINGLE_WAIT_TIME_HPM; |
| 456 | } else if (data->repeatability == medium_repeatability) { |
| 457 | data->command = sht3x_cmd_measure_single_mpm; |
| 458 | data->wait_time = SHT3X_SINGLE_WAIT_TIME_MPM; |
| 459 | } else { |
| 460 | data->command = sht3x_cmd_measure_single_lpm; |
| 461 | data->wait_time = SHT3X_SINGLE_WAIT_TIME_LPM; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static int status_register_read(struct device *dev, |
| 467 | char *buffer, int length) |
| 468 | { |
| 469 | int ret; |
| 470 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 471 | struct i2c_client *client = data->client; |
| 472 | |
| 473 | ret = sht3x_read_from_command(client, data, command: sht3x_cmd_read_status_reg, |
| 474 | buf: buffer, length, wait_time: 0); |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | static int temp1_alarm_read(struct device *dev) |
| 480 | { |
| 481 | char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; |
| 482 | int ret; |
| 483 | |
| 484 | ret = status_register_read(dev, buffer, |
| 485 | SHT3X_WORD_LEN + SHT3X_CRC8_LEN); |
| 486 | if (ret) |
| 487 | return ret; |
| 488 | |
| 489 | return !!(buffer[0] & 0x04); |
| 490 | } |
| 491 | |
| 492 | static int humidity1_alarm_read(struct device *dev) |
| 493 | { |
| 494 | char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; |
| 495 | int ret; |
| 496 | |
| 497 | ret = status_register_read(dev, buffer, |
| 498 | SHT3X_WORD_LEN + SHT3X_CRC8_LEN); |
| 499 | if (ret) |
| 500 | return ret; |
| 501 | |
| 502 | return !!(buffer[0] & 0x08); |
| 503 | } |
| 504 | |
| 505 | static ssize_t heater_enable_show(struct device *dev, |
| 506 | struct device_attribute *attr, |
| 507 | char *buf) |
| 508 | { |
| 509 | char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN]; |
| 510 | int ret; |
| 511 | |
| 512 | ret = status_register_read(dev, buffer, |
| 513 | SHT3X_WORD_LEN + SHT3X_CRC8_LEN); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | |
| 517 | return sysfs_emit(buf, fmt: "%d\n" , !!(buffer[0] & 0x20)); |
| 518 | } |
| 519 | |
| 520 | static ssize_t heater_enable_store(struct device *dev, |
| 521 | struct device_attribute *attr, |
| 522 | const char *buf, |
| 523 | size_t count) |
| 524 | { |
| 525 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 526 | struct i2c_client *client = data->client; |
| 527 | int ret; |
| 528 | bool status; |
| 529 | |
| 530 | ret = kstrtobool(s: buf, res: &status); |
| 531 | if (ret) |
| 532 | return ret; |
| 533 | |
| 534 | mutex_lock(&data->i2c_lock); |
| 535 | |
| 536 | if (status) |
| 537 | ret = i2c_master_send(client, buf: (char *)&sht3x_cmd_heater_on, |
| 538 | SHT3X_CMD_LENGTH); |
| 539 | else |
| 540 | ret = i2c_master_send(client, buf: (char *)&sht3x_cmd_heater_off, |
| 541 | SHT3X_CMD_LENGTH); |
| 542 | |
| 543 | mutex_unlock(lock: &data->i2c_lock); |
| 544 | |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | static int update_interval_read(struct device *dev) |
| 549 | { |
| 550 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 551 | |
| 552 | return mode_to_update_interval[data->mode]; |
| 553 | } |
| 554 | |
| 555 | static int update_interval_write(struct device *dev, int val) |
| 556 | { |
| 557 | u8 mode; |
| 558 | int ret; |
| 559 | const char *command; |
| 560 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 561 | struct i2c_client *client = data->client; |
| 562 | |
| 563 | mode = get_mode_from_update_interval(value: val); |
| 564 | |
| 565 | mutex_lock(&data->data_lock); |
| 566 | /* mode did not change */ |
| 567 | if (mode == data->mode) { |
| 568 | mutex_unlock(lock: &data->data_lock); |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | mutex_lock(&data->i2c_lock); |
| 573 | /* |
| 574 | * Abort periodic measure mode. |
| 575 | * To do any changes to the configuration while in periodic mode, we |
| 576 | * have to send a break command to the sensor, which then falls back |
| 577 | * to single shot (mode = 0). |
| 578 | */ |
| 579 | if (data->mode > 0) { |
| 580 | ret = i2c_master_send(client, buf: sht3x_cmd_break, |
| 581 | SHT3X_CMD_LENGTH); |
| 582 | if (ret != SHT3X_CMD_LENGTH) |
| 583 | goto out; |
| 584 | data->mode = 0; |
| 585 | } |
| 586 | |
| 587 | if (mode > 0) { |
| 588 | if (data->repeatability == high_repeatability) |
| 589 | command = periodic_measure_commands_hpm[mode - 1]; |
| 590 | else if (data->repeatability == medium_repeatability) |
| 591 | command = periodic_measure_commands_mpm[mode - 1]; |
| 592 | else |
| 593 | command = periodic_measure_commands_lpm[mode - 1]; |
| 594 | |
| 595 | /* select mode */ |
| 596 | ret = i2c_master_send(client, buf: command, SHT3X_CMD_LENGTH); |
| 597 | if (ret != SHT3X_CMD_LENGTH) |
| 598 | goto out; |
| 599 | } |
| 600 | |
| 601 | /* select mode and command */ |
| 602 | data->mode = mode; |
| 603 | sht3x_select_command(data); |
| 604 | |
| 605 | out: |
| 606 | mutex_unlock(lock: &data->i2c_lock); |
| 607 | mutex_unlock(lock: &data->data_lock); |
| 608 | if (ret != SHT3X_CMD_LENGTH) |
| 609 | return ret < 0 ? ret : -EIO; |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static ssize_t repeatability_show(struct device *dev, |
| 615 | struct device_attribute *attr, |
| 616 | char *buf) |
| 617 | { |
| 618 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 619 | |
| 620 | return sysfs_emit(buf, fmt: "%d\n" , data->repeatability); |
| 621 | } |
| 622 | |
| 623 | static ssize_t repeatability_store(struct device *dev, |
| 624 | struct device_attribute *attr, |
| 625 | const char *buf, |
| 626 | size_t count) |
| 627 | { |
| 628 | int ret; |
| 629 | u8 val; |
| 630 | |
| 631 | struct sht3x_data *data = dev_get_drvdata(dev); |
| 632 | |
| 633 | ret = kstrtou8(s: buf, base: 0, res: &val); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | |
| 637 | if (val > 2) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | data->repeatability = val; |
| 641 | |
| 642 | return count; |
| 643 | } |
| 644 | |
| 645 | static SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0); |
| 646 | static SENSOR_DEVICE_ATTR_RW(repeatability, repeatability, 0); |
| 647 | |
| 648 | static struct attribute *sht3x_attrs[] = { |
| 649 | &sensor_dev_attr_heater_enable.dev_attr.attr, |
| 650 | &sensor_dev_attr_repeatability.dev_attr.attr, |
| 651 | NULL |
| 652 | }; |
| 653 | |
| 654 | ATTRIBUTE_GROUPS(sht3x); |
| 655 | |
| 656 | static umode_t sht3x_is_visible(const void *data, enum hwmon_sensor_types type, |
| 657 | u32 attr, int channel) |
| 658 | { |
| 659 | const struct sht3x_data *chip_data = data; |
| 660 | |
| 661 | switch (type) { |
| 662 | case hwmon_chip: |
| 663 | switch (attr) { |
| 664 | case hwmon_chip_update_interval: |
| 665 | return 0644; |
| 666 | default: |
| 667 | break; |
| 668 | } |
| 669 | break; |
| 670 | case hwmon_temp: |
| 671 | switch (attr) { |
| 672 | case hwmon_temp_input: |
| 673 | case hwmon_temp_alarm: |
| 674 | return 0444; |
| 675 | case hwmon_temp_max: |
| 676 | case hwmon_temp_max_hyst: |
| 677 | case hwmon_temp_min: |
| 678 | case hwmon_temp_min_hyst: |
| 679 | return 0644; |
| 680 | default: |
| 681 | break; |
| 682 | } |
| 683 | break; |
| 684 | case hwmon_humidity: |
| 685 | if (chip_data->chip_id == sts3x) |
| 686 | break; |
| 687 | switch (attr) { |
| 688 | case hwmon_humidity_input: |
| 689 | case hwmon_humidity_alarm: |
| 690 | return 0444; |
| 691 | case hwmon_humidity_max: |
| 692 | case hwmon_humidity_max_hyst: |
| 693 | case hwmon_humidity_min: |
| 694 | case hwmon_humidity_min_hyst: |
| 695 | return 0644; |
| 696 | default: |
| 697 | break; |
| 698 | } |
| 699 | break; |
| 700 | default: |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int sht3x_read(struct device *dev, enum hwmon_sensor_types type, |
| 708 | u32 attr, int channel, long *val) |
| 709 | { |
| 710 | enum sht3x_limits index; |
| 711 | int ret; |
| 712 | |
| 713 | switch (type) { |
| 714 | case hwmon_chip: |
| 715 | switch (attr) { |
| 716 | case hwmon_chip_update_interval: |
| 717 | *val = update_interval_read(dev); |
| 718 | break; |
| 719 | default: |
| 720 | return -EOPNOTSUPP; |
| 721 | } |
| 722 | break; |
| 723 | case hwmon_temp: |
| 724 | switch (attr) { |
| 725 | case hwmon_temp_input: |
| 726 | return temp1_input_read(dev, temp: val); |
| 727 | case hwmon_temp_alarm: |
| 728 | ret = temp1_alarm_read(dev); |
| 729 | if (ret < 0) |
| 730 | return ret; |
| 731 | *val = ret; |
| 732 | break; |
| 733 | case hwmon_temp_max: |
| 734 | index = limit_max; |
| 735 | *val = temp1_limit_read(dev, index); |
| 736 | break; |
| 737 | case hwmon_temp_max_hyst: |
| 738 | index = limit_max_hyst; |
| 739 | *val = temp1_limit_read(dev, index); |
| 740 | break; |
| 741 | case hwmon_temp_min: |
| 742 | index = limit_min; |
| 743 | *val = temp1_limit_read(dev, index); |
| 744 | break; |
| 745 | case hwmon_temp_min_hyst: |
| 746 | index = limit_min_hyst; |
| 747 | *val = temp1_limit_read(dev, index); |
| 748 | break; |
| 749 | default: |
| 750 | return -EOPNOTSUPP; |
| 751 | } |
| 752 | break; |
| 753 | case hwmon_humidity: |
| 754 | switch (attr) { |
| 755 | case hwmon_humidity_input: |
| 756 | return humidity1_input_read(dev, humidity: val); |
| 757 | case hwmon_humidity_alarm: |
| 758 | ret = humidity1_alarm_read(dev); |
| 759 | if (ret < 0) |
| 760 | return ret; |
| 761 | *val = ret; |
| 762 | break; |
| 763 | case hwmon_humidity_max: |
| 764 | index = limit_max; |
| 765 | *val = humidity1_limit_read(dev, index); |
| 766 | break; |
| 767 | case hwmon_humidity_max_hyst: |
| 768 | index = limit_max_hyst; |
| 769 | *val = humidity1_limit_read(dev, index); |
| 770 | break; |
| 771 | case hwmon_humidity_min: |
| 772 | index = limit_min; |
| 773 | *val = humidity1_limit_read(dev, index); |
| 774 | break; |
| 775 | case hwmon_humidity_min_hyst: |
| 776 | index = limit_min_hyst; |
| 777 | *val = humidity1_limit_read(dev, index); |
| 778 | break; |
| 779 | default: |
| 780 | return -EOPNOTSUPP; |
| 781 | } |
| 782 | break; |
| 783 | default: |
| 784 | return -EOPNOTSUPP; |
| 785 | } |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static int sht3x_write(struct device *dev, enum hwmon_sensor_types type, |
| 791 | u32 attr, int channel, long val) |
| 792 | { |
| 793 | enum sht3x_limits index; |
| 794 | |
| 795 | switch (type) { |
| 796 | case hwmon_chip: |
| 797 | switch (attr) { |
| 798 | case hwmon_chip_update_interval: |
| 799 | return update_interval_write(dev, val); |
| 800 | default: |
| 801 | return -EOPNOTSUPP; |
| 802 | } |
| 803 | case hwmon_temp: |
| 804 | switch (attr) { |
| 805 | case hwmon_temp_max: |
| 806 | index = limit_max; |
| 807 | break; |
| 808 | case hwmon_temp_max_hyst: |
| 809 | index = limit_max_hyst; |
| 810 | break; |
| 811 | case hwmon_temp_min: |
| 812 | index = limit_min; |
| 813 | break; |
| 814 | case hwmon_temp_min_hyst: |
| 815 | index = limit_min_hyst; |
| 816 | break; |
| 817 | default: |
| 818 | return -EOPNOTSUPP; |
| 819 | } |
| 820 | return temp1_limit_write(dev, index, val); |
| 821 | case hwmon_humidity: |
| 822 | switch (attr) { |
| 823 | case hwmon_humidity_max: |
| 824 | index = limit_max; |
| 825 | break; |
| 826 | case hwmon_humidity_max_hyst: |
| 827 | index = limit_max_hyst; |
| 828 | break; |
| 829 | case hwmon_humidity_min: |
| 830 | index = limit_min; |
| 831 | break; |
| 832 | case hwmon_humidity_min_hyst: |
| 833 | index = limit_min_hyst; |
| 834 | break; |
| 835 | default: |
| 836 | return -EOPNOTSUPP; |
| 837 | } |
| 838 | return humidity1_limit_write(dev, index, val); |
| 839 | default: |
| 840 | return -EOPNOTSUPP; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | static void sht3x_serial_number_read(struct sht3x_data *data) |
| 845 | { |
| 846 | int ret; |
| 847 | char buffer[SHT3X_RESPONSE_LENGTH]; |
| 848 | struct i2c_client *client = data->client; |
| 849 | |
| 850 | ret = sht3x_read_from_command(client, data, |
| 851 | command: sht3x_cmd_read_serial_number, |
| 852 | buf: buffer, |
| 853 | SHT3X_RESPONSE_LENGTH, wait_time: 0); |
| 854 | if (ret) |
| 855 | return; |
| 856 | |
| 857 | data->serial_number = (buffer[0] << 24) | (buffer[1] << 16) | |
| 858 | (buffer[3] << 8) | buffer[4]; |
| 859 | |
| 860 | debugfs_create_u32(name: "serial_number" , mode: 0444, parent: client->debugfs, value: &data->serial_number); |
| 861 | } |
| 862 | |
| 863 | static const struct hwmon_ops sht3x_ops = { |
| 864 | .is_visible = sht3x_is_visible, |
| 865 | .read = sht3x_read, |
| 866 | .write = sht3x_write, |
| 867 | }; |
| 868 | |
| 869 | static const struct hwmon_chip_info sht3x_chip_info = { |
| 870 | .ops = &sht3x_ops, |
| 871 | .info = sht3x_channel_info, |
| 872 | }; |
| 873 | |
| 874 | static int sht3x_probe(struct i2c_client *client) |
| 875 | { |
| 876 | int ret; |
| 877 | struct sht3x_data *data; |
| 878 | struct device *hwmon_dev; |
| 879 | struct i2c_adapter *adap = client->adapter; |
| 880 | struct device *dev = &client->dev; |
| 881 | |
| 882 | /* |
| 883 | * we require full i2c support since the sht3x uses multi-byte read and |
| 884 | * writes as well as multi-byte commands which are not supported by |
| 885 | * the smbus protocol |
| 886 | */ |
| 887 | if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) |
| 888 | return -ENODEV; |
| 889 | |
| 890 | ret = i2c_master_send(client, buf: sht3x_cmd_clear_status_reg, |
| 891 | SHT3X_CMD_LENGTH); |
| 892 | if (ret != SHT3X_CMD_LENGTH) |
| 893 | return ret < 0 ? ret : -ENODEV; |
| 894 | |
| 895 | data = devm_kzalloc(dev, size: sizeof(*data), GFP_KERNEL); |
| 896 | if (!data) |
| 897 | return -ENOMEM; |
| 898 | |
| 899 | data->repeatability = high_repeatability; |
| 900 | data->mode = 0; |
| 901 | data->last_update = jiffies - msecs_to_jiffies(m: 3000); |
| 902 | data->client = client; |
| 903 | data->chip_id = (uintptr_t)i2c_get_match_data(client); |
| 904 | crc8_populate_msb(table: sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL); |
| 905 | |
| 906 | sht3x_select_command(data); |
| 907 | |
| 908 | mutex_init(&data->i2c_lock); |
| 909 | mutex_init(&data->data_lock); |
| 910 | |
| 911 | /* |
| 912 | * An attempt to read limits register too early |
| 913 | * causes a NACK response from the chip. |
| 914 | * Waiting for an empirical delay of 500 us solves the issue. |
| 915 | */ |
| 916 | usleep_range(min: 500, max: 600); |
| 917 | |
| 918 | ret = limits_update(data); |
| 919 | if (ret) |
| 920 | return ret; |
| 921 | |
| 922 | hwmon_dev = devm_hwmon_device_register_with_info(dev, name: client->name, drvdata: data, |
| 923 | info: &sht3x_chip_info, extra_groups: sht3x_groups); |
| 924 | if (IS_ERR(ptr: hwmon_dev)) |
| 925 | return PTR_ERR(ptr: hwmon_dev); |
| 926 | |
| 927 | sht3x_serial_number_read(data); |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | /* device ID table */ |
| 933 | static const struct i2c_device_id sht3x_ids[] = { |
| 934 | {"sht3x" , sht3x}, |
| 935 | {"sts3x" , sts3x}, |
| 936 | {} |
| 937 | }; |
| 938 | |
| 939 | MODULE_DEVICE_TABLE(i2c, sht3x_ids); |
| 940 | |
| 941 | static struct i2c_driver sht3x_i2c_driver = { |
| 942 | .driver.name = "sht3x" , |
| 943 | .probe = sht3x_probe, |
| 944 | .id_table = sht3x_ids, |
| 945 | }; |
| 946 | module_i2c_driver(sht3x_i2c_driver); |
| 947 | |
| 948 | MODULE_AUTHOR("David Frey <david.frey@sensirion.com>" ); |
| 949 | MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>" ); |
| 950 | MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver" ); |
| 951 | MODULE_LICENSE("GPL" ); |
| 952 | |