1 // SPDX-License-Identifier: GPL-2.0
3 * Sensirion SCD4X carbon dioxide sensor i2c driver
5 * Copyright (C) 2021 Protonic Holland
6 * Author: Roan van Dijk <roan@protonic.nl>
8 * I2C slave address: 0x62
11 * https://www.sensirion.com/file/datasheet_scd4x
14 #include <asm/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>
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_READY_MASK 0x01
43 CMD_START_MEAS = 0x21b1,
44 CMD_READ_MEAS = 0xec05,
45 CMD_STOP_MEAS = 0x3f86,
46 CMD_SET_TEMP_OFFSET = 0x241d,
47 CMD_GET_TEMP_OFFSET = 0x2318,
51 CMD_GET_DATA_READY = 0xe4b8,
54 enum scd4x_channel_idx {
61 struct i2c_client *client;
62 /* maintain access to device, to prevent concurrent reads/writes */
64 struct regulator *vdd;
67 DECLARE_CRC8_TABLE(scd4x_crc8_table);
69 static int scd4x_i2c_xfer(struct scd4x_state *state, char *txbuf, int txsize,
70 char *rxbuf, int rxsize)
72 struct i2c_client *client = state->client;
75 ret = i2c_master_send(client, txbuf, txsize);
85 ret = i2c_master_recv(client, rxbuf, rxsize);
94 static int scd4x_send_command(struct scd4x_state *state, enum scd4x_cmd cmd)
96 char buf[SCD4X_COMMAND_BUF_SIZE];
100 * Measurement needs to be stopped before sending commands.
101 * Except stop and start command.
103 if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) {
105 ret = scd4x_send_command(state, CMD_STOP_MEAS);
109 /* execution time for stopping measurement */
110 msleep_interruptible(500);
113 put_unaligned_be16(cmd, buf);
114 ret = scd4x_i2c_xfer(state, buf, 2, buf, 0);
118 if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) {
119 ret = scd4x_send_command(state, CMD_START_MEAS);
127 static int scd4x_read(struct scd4x_state *state, enum scd4x_cmd cmd,
128 void *response, int response_sz)
130 struct i2c_client *client = state->client;
131 char buf[SCD4X_READ_BUF_SIZE];
132 char *rsp = response;
137 * Measurement needs to be stopped before sending commands.
138 * Except for reading measurement and data ready command.
140 if ((cmd != CMD_GET_DATA_READY) && (cmd != CMD_READ_MEAS)) {
141 ret = scd4x_send_command(state, CMD_STOP_MEAS);
145 /* execution time for stopping measurement */
146 msleep_interruptible(500);
149 /* CRC byte for every 2 bytes of data */
150 response_sz += response_sz / 2;
152 put_unaligned_be16(cmd, buf);
153 ret = scd4x_i2c_xfer(state, buf, 2, buf, response_sz);
157 for (i = 0; i < response_sz; i += 3) {
158 crc = crc8(scd4x_crc8_table, buf + i, 2, CRC8_INIT_VALUE);
159 if (crc != buf[i + 2]) {
160 dev_err(&client->dev, "CRC error\n");
168 /* start measurement */
169 if ((cmd != CMD_GET_DATA_READY) && (cmd != CMD_READ_MEAS)) {
170 ret = scd4x_send_command(state, CMD_START_MEAS);
178 static int scd4x_write(struct scd4x_state *state, enum scd4x_cmd cmd, uint16_t arg)
180 char buf[SCD4X_WRITE_BUF_SIZE];
184 put_unaligned_be16(cmd, buf);
185 put_unaligned_be16(arg, buf + 2);
187 crc = crc8(scd4x_crc8_table, buf + 2, 2, CRC8_INIT_VALUE);
190 /* measurement needs to be stopped before sending commands */
191 ret = scd4x_send_command(state, CMD_STOP_MEAS);
196 msleep_interruptible(500);
198 ret = scd4x_i2c_xfer(state, buf, SCD4X_WRITE_BUF_SIZE, buf, 0);
202 /* start measurement, except for forced calibration command */
203 if (cmd != CMD_FRC) {
204 ret = scd4x_send_command(state, CMD_START_MEAS);
212 static int scd4x_write_and_fetch(struct scd4x_state *state, enum scd4x_cmd cmd,
213 uint16_t arg, void *response, int response_sz)
215 struct i2c_client *client = state->client;
216 char buf[SCD4X_READ_BUF_SIZE];
217 char *rsp = response;
221 ret = scd4x_write(state, CMD_FRC, arg);
226 msleep_interruptible(400);
228 /* CRC byte for every 2 bytes of data */
229 response_sz += response_sz / 2;
231 ret = i2c_master_recv(client, buf, response_sz);
234 if (ret != response_sz) {
239 for (i = 0; i < response_sz; i += 3) {
240 crc = crc8(scd4x_crc8_table, buf + i, 2, CRC8_INIT_VALUE);
241 if (crc != buf[i + 2]) {
242 dev_err(&client->dev, "CRC error\n");
251 return scd4x_send_command(state, CMD_START_MEAS);
255 * on error try to start the measurement,
256 * puts sensor back into continuous measurement
258 scd4x_send_command(state, CMD_START_MEAS);
263 static int scd4x_read_meas(struct scd4x_state *state, uint16_t *meas)
268 ret = scd4x_read(state, CMD_READ_MEAS, buf, sizeof(buf));
272 for (i = 0; i < ARRAY_SIZE(buf); i++)
273 meas[i] = be16_to_cpu(buf[i]);
278 static int scd4x_wait_meas_poll(struct scd4x_state *state)
280 struct i2c_client *client = state->client;
288 ret = scd4x_read(state, CMD_GET_DATA_READY, &bval, sizeof(bval));
291 val = be16_to_cpu(bval);
293 /* new measurement available */
297 msleep_interruptible(1000);
300 /* try to start sensor on timeout */
301 ret = scd4x_send_command(state, CMD_START_MEAS);
303 dev_err(&client->dev, "failed to start measurement: %d\n", ret);
308 static int scd4x_read_poll(struct scd4x_state *state, uint16_t *buf)
312 ret = scd4x_wait_meas_poll(state);
316 return scd4x_read_meas(state, buf);
319 static int scd4x_read_channel(struct scd4x_state *state, int chan)
324 ret = scd4x_read_poll(state, buf);
331 static int scd4x_read_raw(struct iio_dev *indio_dev,
332 struct iio_chan_spec const *chan, int *val,
333 int *val2, long mask)
335 struct scd4x_state *state = iio_priv(indio_dev);
340 case IIO_CHAN_INFO_RAW:
341 ret = iio_device_claim_direct_mode(indio_dev);
345 mutex_lock(&state->lock);
346 ret = scd4x_read_channel(state, chan->address);
347 mutex_unlock(&state->lock);
349 iio_device_release_direct_mode(indio_dev);
355 case IIO_CHAN_INFO_SCALE:
356 if (chan->type == IIO_CONCENTRATION) {
359 return IIO_VAL_INT_PLUS_MICRO;
360 } else if (chan->type == IIO_TEMP) {
363 return IIO_VAL_FRACTIONAL;
364 } else if (chan->type == IIO_HUMIDITYRELATIVE) {
367 return IIO_VAL_FRACTIONAL;
370 case IIO_CHAN_INFO_OFFSET:
373 return IIO_VAL_INT_PLUS_MICRO;
374 case IIO_CHAN_INFO_CALIBBIAS:
375 mutex_lock(&state->lock);
376 ret = scd4x_read(state, CMD_GET_TEMP_OFFSET, &tmp, sizeof(tmp));
377 mutex_unlock(&state->lock);
381 *val = be16_to_cpu(tmp);
389 static int scd4x_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
390 int val, int val2, long mask)
392 struct scd4x_state *state = iio_priv(indio_dev);
396 case IIO_CHAN_INFO_CALIBBIAS:
397 mutex_lock(&state->lock);
398 ret = scd4x_write(state, CMD_SET_TEMP_OFFSET, val);
399 mutex_unlock(&state->lock);
407 static ssize_t calibration_auto_enable_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
410 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
411 struct scd4x_state *state = iio_priv(indio_dev);
416 mutex_lock(&state->lock);
417 ret = scd4x_read(state, CMD_GET_ASC, &bval, sizeof(bval));
418 mutex_unlock(&state->lock);
420 dev_err(dev, "failed to read automatic calibration");
424 val = (be16_to_cpu(bval) & SCD4X_READY_MASK) ? 1 : 0;
426 return sprintf(buf, "%d\n", val);
429 static ssize_t calibration_auto_enable_store(struct device *dev,
430 struct device_attribute *attr,
431 const char *buf, size_t len)
433 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
434 struct scd4x_state *state = iio_priv(indio_dev);
439 ret = kstrtobool(buf, &val);
445 mutex_lock(&state->lock);
446 ret = scd4x_write(state, CMD_SET_ASC, value);
447 mutex_unlock(&state->lock);
449 dev_err(dev, "failed to set automatic calibration");
454 static ssize_t calibration_forced_value_store(struct device *dev,
455 struct device_attribute *attr,
456 const char *buf, size_t len)
458 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
459 struct scd4x_state *state = iio_priv(indio_dev);
463 ret = kstrtou16(buf, 0, &arg);
467 if (arg < SCD4X_FRC_MIN_PPM || arg > SCD4X_FRC_MAX_PPM)
470 mutex_lock(&state->lock);
471 ret = scd4x_write_and_fetch(state, CMD_FRC, arg, &val, sizeof(val));
472 mutex_unlock(&state->lock);
475 dev_err(dev, "forced calibration has failed");
482 static IIO_DEVICE_ATTR_RW(calibration_auto_enable, 0);
483 static IIO_DEVICE_ATTR_WO(calibration_forced_value, 0);
485 static IIO_CONST_ATTR(calibration_forced_value_available,
486 __stringify([SCD4X_FRC_MIN_PPM 1 SCD4X_FRC_MAX_PPM]));
488 static struct attribute *scd4x_attrs[] = {
489 &iio_dev_attr_calibration_auto_enable.dev_attr.attr,
490 &iio_dev_attr_calibration_forced_value.dev_attr.attr,
491 &iio_const_attr_calibration_forced_value_available.dev_attr.attr,
495 static const struct attribute_group scd4x_attr_group = {
496 .attrs = scd4x_attrs,
499 static const struct iio_info scd4x_info = {
500 .attrs = &scd4x_attr_group,
501 .read_raw = scd4x_read_raw,
502 .write_raw = scd4x_write_raw,
505 static const struct iio_chan_spec scd4x_channels[] = {
507 .type = IIO_CONCENTRATION,
508 .channel2 = IIO_MOD_CO2,
510 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
511 BIT(IIO_CHAN_INFO_SCALE),
512 .address = SCD4X_CO2,
513 .scan_index = SCD4X_CO2,
518 .endianness = IIO_BE,
523 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
524 BIT(IIO_CHAN_INFO_SCALE) |
525 BIT(IIO_CHAN_INFO_OFFSET) |
526 BIT(IIO_CHAN_INFO_CALIBBIAS),
527 .address = SCD4X_TEMP,
528 .scan_index = SCD4X_TEMP,
533 .endianness = IIO_BE,
537 .type = IIO_HUMIDITYRELATIVE,
538 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
539 BIT(IIO_CHAN_INFO_SCALE),
541 .scan_index = SCD4X_HR,
546 .endianness = IIO_BE,
551 static int __maybe_unused scd4x_suspend(struct device *dev)
553 struct iio_dev *indio_dev = dev_get_drvdata(dev);
554 struct scd4x_state *state = iio_priv(indio_dev);
557 ret = scd4x_send_command(state, CMD_STOP_MEAS);
561 return regulator_disable(state->vdd);
564 static int __maybe_unused scd4x_resume(struct device *dev)
566 struct iio_dev *indio_dev = dev_get_drvdata(dev);
567 struct scd4x_state *state = iio_priv(indio_dev);
570 ret = regulator_enable(state->vdd);
574 return scd4x_send_command(state, CMD_START_MEAS);
577 static __maybe_unused SIMPLE_DEV_PM_OPS(scd4x_pm_ops, scd4x_suspend, scd4x_resume);
579 static void scd4x_stop_meas(void *state)
581 scd4x_send_command(state, CMD_STOP_MEAS);
584 static void scd4x_disable_regulator(void *data)
586 struct scd4x_state *state = data;
588 regulator_disable(state->vdd);
591 static irqreturn_t scd4x_trigger_handler(int irq, void *p)
593 struct iio_poll_func *pf = p;
594 struct iio_dev *indio_dev = pf->indio_dev;
595 struct scd4x_state *state = iio_priv(indio_dev);
598 int64_t ts __aligned(8);
602 memset(&scan, 0, sizeof(scan));
603 mutex_lock(&state->lock);
604 ret = scd4x_read_poll(state, scan.data);
605 mutex_unlock(&state->lock);
609 iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
611 iio_trigger_notify_done(indio_dev->trig);
615 static int scd4x_probe(struct i2c_client *client, const struct i2c_device_id *id)
617 static const unsigned long scd4x_scan_masks[] = { 0x07, 0x00 };
618 struct device *dev = &client->dev;
619 struct iio_dev *indio_dev;
620 struct scd4x_state *state;
623 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
627 state = iio_priv(indio_dev);
628 mutex_init(&state->lock);
629 state->client = client;
630 crc8_populate_msb(scd4x_crc8_table, SCD4X_CRC8_POLYNOMIAL);
632 indio_dev->info = &scd4x_info;
633 indio_dev->name = client->name;
634 indio_dev->channels = scd4x_channels;
635 indio_dev->num_channels = ARRAY_SIZE(scd4x_channels);
636 indio_dev->modes = INDIO_DIRECT_MODE;
637 indio_dev->available_scan_masks = scd4x_scan_masks;
639 state->vdd = devm_regulator_get(dev, "vdd");
640 if (IS_ERR(state->vdd))
641 return dev_err_probe(dev, PTR_ERR(state->vdd), "failed to get regulator\n");
643 ret = regulator_enable(state->vdd);
647 ret = devm_add_action_or_reset(dev, scd4x_disable_regulator, state);
651 ret = scd4x_send_command(state, CMD_STOP_MEAS);
653 dev_err(dev, "failed to stop measurement: %d\n", ret);
658 msleep_interruptible(500);
660 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, scd4x_trigger_handler, NULL);
664 ret = scd4x_send_command(state, CMD_START_MEAS);
666 dev_err(dev, "failed to start measurement: %d\n", ret);
670 ret = devm_add_action_or_reset(dev, scd4x_stop_meas, state);
674 return devm_iio_device_register(dev, indio_dev);
677 static const struct of_device_id scd4x_dt_ids[] = {
678 { .compatible = "sensirion,scd40" },
679 { .compatible = "sensirion,scd41" },
682 MODULE_DEVICE_TABLE(of, scd4x_dt_ids);
684 static struct i2c_driver scd4x_i2c_driver = {
686 .name = KBUILD_MODNAME,
687 .of_match_table = scd4x_dt_ids,
690 .probe = scd4x_probe,
692 module_i2c_driver(scd4x_i2c_driver);
694 MODULE_AUTHOR("Roan van Dijk <roan@protonic.nl>");
695 MODULE_DESCRIPTION("Sensirion SCD4X carbon dioxide sensor core driver");
696 MODULE_LICENSE("GPL v2");