| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Sensirion SCD4X carbon dioxide sensor i2c driver |
| 4 |
* |
| 5 |
* Copyright (C) 2021 Protonic Holland |
| 6 |
* Author: Roan van Dijk <[email protected]> |
| 7 |
* |
| 8 |
* I2C slave address: 0x62 |
| 9 |
* |
| 10 |
* Datasheets: |
| 11 |
* https://www.sensirion.com/file/datasheet_scd4x |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <linux/unaligned.h> |
| 15 |
#include <linux/crc8.h> |
| 16 |
#include <linux/delay.h> |
| 17 |
#include <linux/device.h> |
| 18 |
#include <linux/i2c.h> |
| 19 |
#include <linux/iio/buffer.h> |
| 20 |
#include <linux/iio/iio.h> |
| 21 |
#include <linux/iio/sysfs.h> |
| 22 |
#include <linux/iio/trigger.h> |
| 23 |
#include <linux/iio/trigger_consumer.h> |
| 24 |
#include <linux/iio/triggered_buffer.h> |
| 25 |
#include <linux/iio/types.h> |
| 26 |
#include <linux/kernel.h> |
| 27 |
#include <linux/mutex.h> |
| 28 |
#include <linux/string.h> |
| 29 |
#include <linux/sysfs.h> |
| 30 |
#include <linux/types.h> |
| 31 |
|
| 32 |
#define SCD4X_CRC8_POLYNOMIAL 0x31 |
| 33 |
#define SCD4X_TIMEOUT_ERR 1000 |
| 34 |
#define SCD4X_READ_BUF_SIZE 9 |
| 35 |
#define SCD4X_COMMAND_BUF_SIZE 2 |
| 36 |
#define SCD4X_WRITE_BUF_SIZE 5 |
| 37 |
#define SCD4X_FRC_MIN_PPM 0 |
| 38 |
#define SCD4X_FRC_MAX_PPM 2000 |
| 39 |
#define SCD4X_PRESSURE_COMP_MIN_MBAR 700 |
| 40 |
#define SCD4X_PRESSURE_COMP_MAX_MBAR 1200 |
| 41 |
#define SCD4X_READY_MASK 0x01 |
| 42 |
|
| 43 |
/*Commands SCD4X*/ |
| 44 |
enum scd4x_cmd { |
| 45 |
CMD_START_MEAS = 0x21b1, |
| 46 |
CMD_READ_MEAS = 0xec05, |
| 47 |
CMD_STOP_MEAS = 0x3f86, |
| 48 |
CMD_SET_TEMP_OFFSET = 0x241d, |
| 49 |
CMD_GET_TEMP_OFFSET = 0x2318, |
| 50 |
CMD_SET_AMB_PRESSURE = 0xe000, |
| 51 |
CMD_GET_AMB_PRESSURE = 0xe000, |
| 52 |
CMD_FRC = 0x362f, |
| 53 |
CMD_SET_ASC = 0x2416, |
| 54 |
CMD_GET_ASC = 0x2313, |
| 55 |
CMD_GET_DATA_READY = 0xe4b8, |
| 56 |
}; |
| 57 |
|
| 58 |
enum scd4x_channel_idx { |
| 59 |
SCD4X_CO2, |
| 60 |
SCD4X_TEMP, |
| 61 |
SCD4X_HR, |
| 62 |
/* kernel timestamp, at the end of buffer */ |
| 63 |
SCD4X_TS, |
| 64 |
}; |
| 65 |
|
| 66 |
struct scd4x_state { |
| 67 |
struct i2c_client *client; |
| 68 |
/* maintain access to device, to prevent concurrent reads/writes */ |
| 69 |
struct mutex lock; |
| 70 |
struct regulator *vdd; |
| 71 |
}; |
| 72 |
|
| 73 |
DECLARE_CRC8_TABLE(scd4x_crc8_table); |
| 74 |
|
| 75 |
static int scd4x_i2c_xfer(struct scd4x_state *state, char *txbuf, int txsize, |
| 76 |
char *rxbuf, int rxsize) |
| 77 |
{ |
| 78 |
struct i2c_client *client = state->client; |
| 79 |
int ret; |
| 80 |
|
| 81 |
ret = i2c_master_send(client, txbuf, txsize); |
| 82 |
|
| 83 |
if (ret < 0) |
| 84 |
return ret; |
| 85 |
if (ret != txsize) |
| 86 |
return -EIO; |
| 87 |
|
| 88 |
if (rxsize == 0) |
| 89 |
return 0; |
| 90 |
|
| 91 |
ret = i2c_master_recv(client, rxbuf, rxsize); |
| 92 |
if (ret < 0) |
| 93 |
return ret; |
| 94 |
if (ret != rxsize) |
| 95 |
return -EIO; |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int scd4x_send_command(struct scd4x_state *state, enum scd4x_cmd cmd) |
| 101 |
{ |
| 102 |
char buf[SCD4X_COMMAND_BUF_SIZE]; |
| 103 |
int ret; |
| 104 |
|
| 105 |
/* |
| 106 |
* Measurement needs to be stopped before sending commands. |
| 107 |
* Except stop and start command. |
| 108 |
*/ |
| 109 |
if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) { |
| 110 |
|
| 111 |
ret = scd4x_send_command(state, CMD_STOP_MEAS); |
| 112 |
if (ret) |
| 113 |
return ret; |
| 114 |
|
| 115 |
/* execution time for stopping measurement */ |
| 116 |
msleep_interruptible(500); |
| 117 |
} |
| 118 |
|
| 119 |
put_unaligned_be16(cmd, buf); |
| 120 |
ret = scd4x_i2c_xfer(state, buf, 2, buf, 0); |
| 121 |
if (ret) |
| 122 |
return ret; |
| 123 |
|
| 124 |
if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) { |
| 125 |
ret = scd4x_send_command(state, CMD_START_MEAS); |
| 126 |
if (ret) |
| 127 |
return ret; |
| 128 |
} |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
static int scd4x_read(struct scd4x_state *state, enum scd4x_cmd cmd, |
| 134 |
void *response, int response_sz) |
| 135 |
{ |
| 136 |
struct i2c_client *client = state->client; |
| 137 |
char buf[SCD4X_READ_BUF_SIZE]; |
| 138 |
char *rsp = response; |
| 139 |
int i, ret; |
| 140 |
char crc; |
| 141 |
|
| 142 |
/* |
| 143 |
* Measurement needs to be stopped before sending commands. |
| 144 |
* Except for reading measurement and data ready command. |
| 145 |
*/ |
| 146 |
if ((cmd != CMD_GET_DATA_READY) && (cmd != CMD_READ_MEAS) && |
| 147 |
(cmd != CMD_GET_AMB_PRESSURE)) { |
| 148 |
ret = scd4x_send_command(state, CMD_STOP_MEAS); |
| 149 |
if (ret) |
| 150 |
return ret; |
| 151 |
|
| 152 |
/* execution time for stopping measurement */ |
| 153 |
msleep_interruptible(500); |
| 154 |
} |
| 155 |
|
| 156 |
/* CRC byte for every 2 bytes of data */ |
| 157 |
response_sz += response_sz / 2; |
| 158 |
|
| 159 |
put_unaligned_be16(cmd, buf); |
| 160 |
ret = scd4x_i2c_xfer(state, buf, 2, buf, response_sz); |
| 161 |
if (ret) |
| 162 |
return ret; |
| 163 |
|
| 164 |
for (i = 0; i < response_sz; i += 3) { |
| 165 |
crc = crc8(scd4x_crc8_table, buf + i, 2, CRC8_INIT_VALUE); |
| 166 |
if (crc != buf[i + 2]) { |
| 167 |
dev_err(&client->dev, "CRC error\n"); |
| 168 |
return -EIO; |
| 169 |
} |
| 170 |
|
| 171 |
*rsp++ = buf[i]; |
| 172 |
*rsp++ = buf[i + 1]; |
| 173 |
} |
| 174 |
|
| 175 |
/* start measurement */ |
| 176 |
if ((cmd != CMD_GET_DATA_READY) && (cmd != CMD_READ_MEAS) && |
| 177 |
(cmd != CMD_GET_AMB_PRESSURE)) { |
| 178 |
ret = scd4x_send_command(state, CMD_START_MEAS); |
| 179 |
if (ret) |
| 180 |
return ret; |
| 181 |
} |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
static int scd4x_write(struct scd4x_state *state, enum scd4x_cmd cmd, uint16_t arg) |
| 187 |
{ |
| 188 |
char buf[SCD4X_WRITE_BUF_SIZE]; |
| 189 |
int ret; |
| 190 |
char crc; |
| 191 |
|
| 192 |
put_unaligned_be16(cmd, buf); |
| 193 |
put_unaligned_be16(arg, buf + 2); |
| 194 |
|
| 195 |
crc = crc8(scd4x_crc8_table, buf + 2, 2, CRC8_INIT_VALUE); |
| 196 |
buf[4] = crc; |
| 197 |
|
| 198 |
/* measurement needs to be stopped before sending commands */ |
| 199 |
if (cmd != CMD_SET_AMB_PRESSURE) { |
| 200 |
ret = scd4x_send_command(state, CMD_STOP_MEAS); |
| 201 |
if (ret) |
| 202 |
return ret; |
| 203 |
} |
| 204 |
|
| 205 |
/* execution time */ |
| 206 |
msleep_interruptible(500); |
| 207 |
|
| 208 |
ret = scd4x_i2c_xfer(state, buf, SCD4X_WRITE_BUF_SIZE, buf, 0); |
| 209 |
if (ret) |
| 210 |
return ret; |
| 211 |
|
| 212 |
/* start measurement, except for forced calibration command */ |
| 213 |
if ((cmd != CMD_FRC) && (cmd != CMD_SET_AMB_PRESSURE)) { |
| 214 |
ret = scd4x_send_command(state, CMD_START_MEAS); |
| 215 |
if (ret) |
| 216 |
return ret; |
| 217 |
} |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static int scd4x_write_and_fetch(struct scd4x_state *state, enum scd4x_cmd cmd, |
| 223 |
uint16_t arg, void *response, int response_sz) |
| 224 |
{ |
| 225 |
struct i2c_client *client = state->client; |
| 226 |
char buf[SCD4X_READ_BUF_SIZE]; |
| 227 |
char *rsp = response; |
| 228 |
int i, ret; |
| 229 |
char crc; |
| 230 |
|
| 231 |
ret = scd4x_write(state, CMD_FRC, arg); |
| 232 |
if (ret) |
| 233 |
goto err; |
| 234 |
|
| 235 |
/* execution time */ |
| 236 |
msleep_interruptible(400); |
| 237 |
|
| 238 |
/* CRC byte for every 2 bytes of data */ |
| 239 |
response_sz += response_sz / 2; |
| 240 |
|
| 241 |
ret = i2c_master_recv(client, buf, response_sz); |
| 242 |
if (ret < 0) |
| 243 |
goto err; |
| 244 |
if (ret != response_sz) { |
| 245 |
ret = -EIO; |
| 246 |
goto err; |
| 247 |
} |
| 248 |
|
| 249 |
for (i = 0; i < response_sz; i += 3) { |
| 250 |
crc = crc8(scd4x_crc8_table, buf + i, 2, CRC8_INIT_VALUE); |
| 251 |
if (crc != buf[i + 2]) { |
| 252 |
dev_err(&client->dev, "CRC error\n"); |
| 253 |
ret = -EIO; |
| 254 |
goto err; |
| 255 |
} |
| 256 |
|
| 257 |
*rsp++ = buf[i]; |
| 258 |
*rsp++ = buf[i + 1]; |
| 259 |
} |
| 260 |
|
| 261 |
return scd4x_send_command(state, CMD_START_MEAS); |
| 262 |
|
| 263 |
err: |
| 264 |
/* |
| 265 |
* on error try to start the measurement, |
| 266 |
* puts sensor back into continuous measurement |
| 267 |
*/ |
| 268 |
scd4x_send_command(state, CMD_START_MEAS); |
| 269 |
|
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
static int scd4x_read_meas(struct scd4x_state *state, uint16_t *meas) |
| 274 |
{ |
| 275 |
int i, ret; |
| 276 |
__be16 buf[3]; |
| 277 |
|
| 278 |
ret = scd4x_read(state, CMD_READ_MEAS, buf, sizeof(buf)); |
| 279 |
if (ret) |
| 280 |
return ret; |
| 281 |
|
| 282 |
for (i = 0; i < ARRAY_SIZE(buf); i++) |
| 283 |
meas[i] = be16_to_cpu(buf[i]); |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static int scd4x_wait_meas_poll(struct scd4x_state *state) |
| 289 |
{ |
| 290 |
struct i2c_client *client = state->client; |
| 291 |
int tries = 6; |
| 292 |
int ret; |
| 293 |
|
| 294 |
do { |
| 295 |
__be16 bval; |
| 296 |
uint16_t val; |
| 297 |
|
| 298 |
ret = scd4x_read(state, CMD_GET_DATA_READY, &bval, sizeof(bval)); |
| 299 |
if (ret) |
| 300 |
return -EIO; |
| 301 |
val = be16_to_cpu(bval); |
| 302 |
|
| 303 |
/* new measurement available */ |
| 304 |
if (val & 0x7FF) |
| 305 |
return 0; |
| 306 |
|
| 307 |
msleep_interruptible(1000); |
| 308 |
} while (--tries); |
| 309 |
|
| 310 |
/* try to start sensor on timeout */ |
| 311 |
ret = scd4x_send_command(state, CMD_START_MEAS); |
| 312 |
if (ret) |
| 313 |
dev_err(&client->dev, "failed to start measurement: %d\n", ret); |
| 314 |
|
| 315 |
return -ETIMEDOUT; |
| 316 |
} |
| 317 |
|
| 318 |
static int scd4x_read_poll(struct scd4x_state *state, uint16_t *buf) |
| 319 |
{ |
| 320 |
int ret; |
| 321 |
|
| 322 |
ret = scd4x_wait_meas_poll(state); |
| 323 |
if (ret) |
| 324 |
return ret; |
| 325 |
|
| 326 |
return scd4x_read_meas(state, buf); |
| 327 |
} |
| 328 |
|
| 329 |
static int scd4x_read_channel(struct scd4x_state *state, int chan) |
| 330 |
{ |
| 331 |
int ret; |
| 332 |
uint16_t buf[3]; |
| 333 |
|
| 334 |
ret = scd4x_read_poll(state, buf); |
| 335 |
if (ret) |
| 336 |
return ret; |
| 337 |
|
| 338 |
return buf[chan]; |
| 339 |
} |
| 340 |
|
| 341 |
static int scd4x_read_raw(struct iio_dev *indio_dev, |
| 342 |
struct iio_chan_spec const *chan, int *val, |
| 343 |
int *val2, long mask) |
| 344 |
{ |
| 345 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 346 |
int ret; |
| 347 |
__be16 tmp; |
| 348 |
|
| 349 |
switch (mask) { |
| 350 |
case IIO_CHAN_INFO_RAW: |
| 351 |
if (chan->output) { |
| 352 |
mutex_lock(&state->lock); |
| 353 |
ret = scd4x_read(state, CMD_GET_AMB_PRESSURE, &tmp, sizeof(tmp)); |
| 354 |
mutex_unlock(&state->lock); |
| 355 |
|
| 356 |
if (ret) |
| 357 |
return ret; |
| 358 |
|
| 359 |
*val = be16_to_cpu(tmp); |
| 360 |
return IIO_VAL_INT; |
| 361 |
} |
| 362 |
|
| 363 |
if (!iio_device_claim_direct(indio_dev)) |
| 364 |
return -EBUSY; |
| 365 |
|
| 366 |
mutex_lock(&state->lock); |
| 367 |
ret = scd4x_read_channel(state, chan->address); |
| 368 |
mutex_unlock(&state->lock); |
| 369 |
|
| 370 |
iio_device_release_direct(indio_dev); |
| 371 |
if (ret < 0) |
| 372 |
return ret; |
| 373 |
|
| 374 |
*val = ret; |
| 375 |
return IIO_VAL_INT; |
| 376 |
case IIO_CHAN_INFO_SCALE: |
| 377 |
if (chan->type == IIO_CONCENTRATION) { |
| 378 |
*val = 0; |
| 379 |
*val2 = 100; |
| 380 |
return IIO_VAL_INT_PLUS_MICRO; |
| 381 |
} else if (chan->type == IIO_TEMP) { |
| 382 |
*val = 175000; |
| 383 |
*val2 = 65536; |
| 384 |
return IIO_VAL_FRACTIONAL; |
| 385 |
} else if (chan->type == IIO_HUMIDITYRELATIVE) { |
| 386 |
*val = 100000; |
| 387 |
*val2 = 65536; |
| 388 |
return IIO_VAL_FRACTIONAL; |
| 389 |
} |
| 390 |
return -EINVAL; |
| 391 |
case IIO_CHAN_INFO_OFFSET: |
| 392 |
*val = -16852; |
| 393 |
*val2 = 114286; |
| 394 |
return IIO_VAL_INT_PLUS_MICRO; |
| 395 |
case IIO_CHAN_INFO_CALIBBIAS: |
| 396 |
mutex_lock(&state->lock); |
| 397 |
ret = scd4x_read(state, CMD_GET_TEMP_OFFSET, &tmp, sizeof(tmp)); |
| 398 |
mutex_unlock(&state->lock); |
| 399 |
if (ret) |
| 400 |
return ret; |
| 401 |
|
| 402 |
*val = be16_to_cpu(tmp); |
| 403 |
|
| 404 |
return IIO_VAL_INT; |
| 405 |
default: |
| 406 |
return -EINVAL; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static const int scd4x_pressure_calibbias_available[] = { |
| 411 |
SCD4X_PRESSURE_COMP_MIN_MBAR, 1, SCD4X_PRESSURE_COMP_MAX_MBAR, |
| 412 |
}; |
| 413 |
|
| 414 |
static int scd4x_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, |
| 415 |
const int **vals, int *type, int *length, long mask) |
| 416 |
{ |
| 417 |
switch (mask) { |
| 418 |
case IIO_CHAN_INFO_RAW: |
| 419 |
*vals = scd4x_pressure_calibbias_available; |
| 420 |
*type = IIO_VAL_INT; |
| 421 |
|
| 422 |
return IIO_AVAIL_RANGE; |
| 423 |
} |
| 424 |
|
| 425 |
return -EINVAL; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
static int scd4x_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, |
| 430 |
int val, int val2, long mask) |
| 431 |
{ |
| 432 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 433 |
int ret = 0; |
| 434 |
|
| 435 |
switch (mask) { |
| 436 |
case IIO_CHAN_INFO_CALIBBIAS: |
| 437 |
mutex_lock(&state->lock); |
| 438 |
ret = scd4x_write(state, CMD_SET_TEMP_OFFSET, val); |
| 439 |
mutex_unlock(&state->lock); |
| 440 |
|
| 441 |
return ret; |
| 442 |
case IIO_CHAN_INFO_RAW: |
| 443 |
switch (chan->type) { |
| 444 |
case IIO_PRESSURE: |
| 445 |
if (val < SCD4X_PRESSURE_COMP_MIN_MBAR || |
| 446 |
val > SCD4X_PRESSURE_COMP_MAX_MBAR) |
| 447 |
return -EINVAL; |
| 448 |
|
| 449 |
mutex_lock(&state->lock); |
| 450 |
ret = scd4x_write(state, CMD_SET_AMB_PRESSURE, val); |
| 451 |
mutex_unlock(&state->lock); |
| 452 |
|
| 453 |
return ret; |
| 454 |
default: |
| 455 |
return -EINVAL; |
| 456 |
} |
| 457 |
default: |
| 458 |
return -EINVAL; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
static ssize_t calibration_auto_enable_show(struct device *dev, |
| 463 |
struct device_attribute *attr, char *buf) |
| 464 |
{ |
| 465 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 466 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 467 |
int ret; |
| 468 |
__be16 bval; |
| 469 |
u16 val; |
| 470 |
|
| 471 |
mutex_lock(&state->lock); |
| 472 |
ret = scd4x_read(state, CMD_GET_ASC, &bval, sizeof(bval)); |
| 473 |
mutex_unlock(&state->lock); |
| 474 |
if (ret) { |
| 475 |
dev_err(dev, "failed to read automatic calibration"); |
| 476 |
return ret; |
| 477 |
} |
| 478 |
|
| 479 |
val = (be16_to_cpu(bval) & SCD4X_READY_MASK) ? 1 : 0; |
| 480 |
|
| 481 |
return sysfs_emit(buf, "%d\n", val); |
| 482 |
} |
| 483 |
|
| 484 |
static ssize_t calibration_auto_enable_store(struct device *dev, |
| 485 |
struct device_attribute *attr, |
| 486 |
const char *buf, size_t len) |
| 487 |
{ |
| 488 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 489 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 490 |
bool val; |
| 491 |
int ret; |
| 492 |
uint16_t value; |
| 493 |
|
| 494 |
ret = kstrtobool(buf, &val); |
| 495 |
if (ret) |
| 496 |
return ret; |
| 497 |
|
| 498 |
value = val; |
| 499 |
|
| 500 |
mutex_lock(&state->lock); |
| 501 |
ret = scd4x_write(state, CMD_SET_ASC, value); |
| 502 |
mutex_unlock(&state->lock); |
| 503 |
if (ret) |
| 504 |
dev_err(dev, "failed to set automatic calibration"); |
| 505 |
|
| 506 |
return ret ?: len; |
| 507 |
} |
| 508 |
|
| 509 |
static ssize_t calibration_forced_value_store(struct device *dev, |
| 510 |
struct device_attribute *attr, |
| 511 |
const char *buf, size_t len) |
| 512 |
{ |
| 513 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 514 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 515 |
uint16_t val, arg; |
| 516 |
int ret; |
| 517 |
|
| 518 |
ret = kstrtou16(buf, 0, &arg); |
| 519 |
if (ret) |
| 520 |
return ret; |
| 521 |
|
| 522 |
if (arg < SCD4X_FRC_MIN_PPM || arg > SCD4X_FRC_MAX_PPM) |
| 523 |
return -EINVAL; |
| 524 |
|
| 525 |
mutex_lock(&state->lock); |
| 526 |
ret = scd4x_write_and_fetch(state, CMD_FRC, arg, &val, sizeof(val)); |
| 527 |
mutex_unlock(&state->lock); |
| 528 |
|
| 529 |
if (ret) |
| 530 |
return ret; |
| 531 |
|
| 532 |
if (val == 0xff) { |
| 533 |
dev_err(dev, "forced calibration has failed"); |
| 534 |
return -EINVAL; |
| 535 |
} |
| 536 |
|
| 537 |
return len; |
| 538 |
} |
| 539 |
|
| 540 |
static IIO_DEVICE_ATTR_RW(calibration_auto_enable, 0); |
| 541 |
static IIO_DEVICE_ATTR_WO(calibration_forced_value, 0); |
| 542 |
|
| 543 |
static IIO_CONST_ATTR(calibration_forced_value_available, |
| 544 |
__stringify([SCD4X_FRC_MIN_PPM 1 SCD4X_FRC_MAX_PPM])); |
| 545 |
|
| 546 |
static struct attribute *scd4x_attrs[] = { |
| 547 |
&iio_dev_attr_calibration_auto_enable.dev_attr.attr, |
| 548 |
&iio_dev_attr_calibration_forced_value.dev_attr.attr, |
| 549 |
&iio_const_attr_calibration_forced_value_available.dev_attr.attr, |
| 550 |
NULL |
| 551 |
}; |
| 552 |
|
| 553 |
static const struct attribute_group scd4x_attr_group = { |
| 554 |
.attrs = scd4x_attrs, |
| 555 |
}; |
| 556 |
|
| 557 |
static const struct iio_info scd4x_info = { |
| 558 |
.attrs = &scd4x_attr_group, |
| 559 |
.read_raw = scd4x_read_raw, |
| 560 |
.write_raw = scd4x_write_raw, |
| 561 |
.read_avail = scd4x_read_avail, |
| 562 |
}; |
| 563 |
|
| 564 |
static const struct iio_chan_spec scd4x_channels[] = { |
| 565 |
{ |
| 566 |
/* |
| 567 |
* this channel is special in a sense we are pretending that |
| 568 |
* sensor is able to change measurement chamber pressure but in |
| 569 |
* fact we're just setting pressure compensation value |
| 570 |
*/ |
| 571 |
.type = IIO_PRESSURE, |
| 572 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 573 |
.info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW), |
| 574 |
.output = 1, |
| 575 |
.scan_index = -1, |
| 576 |
}, |
| 577 |
{ |
| 578 |
.type = IIO_CONCENTRATION, |
| 579 |
.channel2 = IIO_MOD_CO2, |
| 580 |
.modified = 1, |
| 581 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 582 |
BIT(IIO_CHAN_INFO_SCALE), |
| 583 |
.address = SCD4X_CO2, |
| 584 |
.scan_index = SCD4X_CO2, |
| 585 |
.scan_type = { |
| 586 |
.sign = 'u', |
| 587 |
.realbits = 16, |
| 588 |
.storagebits = 16, |
| 589 |
.endianness = IIO_CPU, |
| 590 |
}, |
| 591 |
}, |
| 592 |
{ |
| 593 |
.type = IIO_TEMP, |
| 594 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 595 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 596 |
BIT(IIO_CHAN_INFO_OFFSET) | |
| 597 |
BIT(IIO_CHAN_INFO_CALIBBIAS), |
| 598 |
.address = SCD4X_TEMP, |
| 599 |
.scan_index = SCD4X_TEMP, |
| 600 |
.scan_type = { |
| 601 |
.sign = 'u', |
| 602 |
.realbits = 16, |
| 603 |
.storagebits = 16, |
| 604 |
.endianness = IIO_CPU, |
| 605 |
}, |
| 606 |
}, |
| 607 |
{ |
| 608 |
.type = IIO_HUMIDITYRELATIVE, |
| 609 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 610 |
BIT(IIO_CHAN_INFO_SCALE), |
| 611 |
.address = SCD4X_HR, |
| 612 |
.scan_index = SCD4X_HR, |
| 613 |
.scan_type = { |
| 614 |
.sign = 'u', |
| 615 |
.realbits = 16, |
| 616 |
.storagebits = 16, |
| 617 |
.endianness = IIO_CPU, |
| 618 |
}, |
| 619 |
}, |
| 620 |
IIO_CHAN_SOFT_TIMESTAMP(SCD4X_TS), |
| 621 |
}; |
| 622 |
|
| 623 |
static int scd4x_suspend(struct device *dev) |
| 624 |
{ |
| 625 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 626 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 627 |
int ret; |
| 628 |
|
| 629 |
ret = scd4x_send_command(state, CMD_STOP_MEAS); |
| 630 |
if (ret) |
| 631 |
return ret; |
| 632 |
|
| 633 |
return regulator_disable(state->vdd); |
| 634 |
} |
| 635 |
|
| 636 |
static int scd4x_resume(struct device *dev) |
| 637 |
{ |
| 638 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 639 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 640 |
int ret; |
| 641 |
|
| 642 |
ret = regulator_enable(state->vdd); |
| 643 |
if (ret) |
| 644 |
return ret; |
| 645 |
|
| 646 |
return scd4x_send_command(state, CMD_START_MEAS); |
| 647 |
} |
| 648 |
|
| 649 |
static DEFINE_SIMPLE_DEV_PM_OPS(scd4x_pm_ops, scd4x_suspend, scd4x_resume); |
| 650 |
|
| 651 |
static void scd4x_stop_meas(void *state) |
| 652 |
{ |
| 653 |
scd4x_send_command(state, CMD_STOP_MEAS); |
| 654 |
} |
| 655 |
|
| 656 |
static void scd4x_disable_regulator(void *data) |
| 657 |
{ |
| 658 |
struct scd4x_state *state = data; |
| 659 |
|
| 660 |
regulator_disable(state->vdd); |
| 661 |
} |
| 662 |
|
| 663 |
static irqreturn_t scd4x_trigger_handler(int irq, void *p) |
| 664 |
{ |
| 665 |
struct iio_poll_func *pf = p; |
| 666 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 667 |
struct scd4x_state *state = iio_priv(indio_dev); |
| 668 |
struct { |
| 669 |
uint16_t data[3]; |
| 670 |
aligned_s64 ts; |
| 671 |
} scan = { }; |
| 672 |
int ret; |
| 673 |
|
| 674 |
mutex_lock(&state->lock); |
| 675 |
ret = scd4x_read_poll(state, scan.data); |
| 676 |
mutex_unlock(&state->lock); |
| 677 |
if (ret) |
| 678 |
goto out; |
| 679 |
|
| 680 |
iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), |
| 681 |
iio_get_time_ns(indio_dev)); |
| 682 |
out: |
| 683 |
iio_trigger_notify_done(indio_dev->trig); |
| 684 |
return IRQ_HANDLED; |
| 685 |
} |
| 686 |
|
| 687 |
static int scd4x_probe(struct i2c_client *client) |
| 688 |
{ |
| 689 |
static const unsigned long scd4x_scan_masks[] = { 0x07, 0x00 }; |
| 690 |
struct device *dev = &client->dev; |
| 691 |
struct iio_dev *indio_dev; |
| 692 |
struct scd4x_state *state; |
| 693 |
int ret; |
| 694 |
|
| 695 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); |
| 696 |
if (!indio_dev) |
| 697 |
return -ENOMEM; |
| 698 |
|
| 699 |
state = iio_priv(indio_dev); |
| 700 |
mutex_init(&state->lock); |
| 701 |
state->client = client; |
| 702 |
crc8_populate_msb(scd4x_crc8_table, SCD4X_CRC8_POLYNOMIAL); |
| 703 |
|
| 704 |
indio_dev->info = &scd4x_info; |
| 705 |
indio_dev->name = client->name; |
| 706 |
indio_dev->channels = scd4x_channels; |
| 707 |
indio_dev->num_channels = ARRAY_SIZE(scd4x_channels); |
| 708 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 709 |
indio_dev->available_scan_masks = scd4x_scan_masks; |
| 710 |
|
| 711 |
state->vdd = devm_regulator_get(dev, "vdd"); |
| 712 |
if (IS_ERR(state->vdd)) |
| 713 |
return dev_err_probe(dev, PTR_ERR(state->vdd), "failed to get regulator\n"); |
| 714 |
|
| 715 |
ret = regulator_enable(state->vdd); |
| 716 |
if (ret) |
| 717 |
return ret; |
| 718 |
|
| 719 |
ret = devm_add_action_or_reset(dev, scd4x_disable_regulator, state); |
| 720 |
if (ret) |
| 721 |
return ret; |
| 722 |
|
| 723 |
ret = scd4x_send_command(state, CMD_STOP_MEAS); |
| 724 |
if (ret) { |
| 725 |
dev_err(dev, "failed to stop measurement: %d\n", ret); |
| 726 |
return ret; |
| 727 |
} |
| 728 |
|
| 729 |
/* execution time */ |
| 730 |
msleep_interruptible(500); |
| 731 |
|
| 732 |
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, scd4x_trigger_handler, NULL); |
| 733 |
if (ret) |
| 734 |
return ret; |
| 735 |
|
| 736 |
ret = scd4x_send_command(state, CMD_START_MEAS); |
| 737 |
if (ret) { |
| 738 |
dev_err(dev, "failed to start measurement: %d\n", ret); |
| 739 |
return ret; |
| 740 |
} |
| 741 |
|
| 742 |
ret = devm_add_action_or_reset(dev, scd4x_stop_meas, state); |
| 743 |
if (ret) |
| 744 |
return ret; |
| 745 |
|
| 746 |
return devm_iio_device_register(dev, indio_dev); |
| 747 |
} |
| 748 |
|
| 749 |
static const struct of_device_id scd4x_dt_ids[] = { |
| 750 |
{ .compatible = "sensirion,scd40" }, |
| 751 |
{ .compatible = "sensirion,scd41" }, |
| 752 |
{ } |
| 753 |
}; |
| 754 |
MODULE_DEVICE_TABLE(of, scd4x_dt_ids); |
| 755 |
|
| 756 |
static struct i2c_driver scd4x_i2c_driver = { |
| 757 |
.driver = { |
| 758 |
.name = KBUILD_MODNAME, |
| 759 |
.of_match_table = scd4x_dt_ids, |
| 760 |
.pm = pm_sleep_ptr(&scd4x_pm_ops), |
| 761 |
}, |
| 762 |
.probe = scd4x_probe, |
| 763 |
}; |
| 764 |
module_i2c_driver(scd4x_i2c_driver); |
| 765 |
|
| 766 |
MODULE_AUTHOR("Roan van Dijk <[email protected]>"); |
| 767 |
MODULE_DESCRIPTION("Sensirion SCD4X carbon dioxide sensor core driver"); |
| 768 |
MODULE_LICENSE("GPL v2"); |