1 // SPDX-License-Identifier: GPL-2.0
3 * Sensirion SCD30 carbon dioxide sensor core driver
5 * Copyright (c) 2020 Tomasz Duszynski <tomasz.duszynski@octakon.com>
7 #include <linux/bits.h>
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/export.h>
13 #include <linux/iio/buffer.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include <linux/iio/types.h>
20 #include <linux/interrupt.h>
21 #include <linux/irqreturn.h>
22 #include <linux/jiffies.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/string.h>
28 #include <linux/sysfs.h>
29 #include <linux/types.h>
30 #include <asm/byteorder.h>
34 #define SCD30_PRESSURE_COMP_MIN_MBAR 700
35 #define SCD30_PRESSURE_COMP_MAX_MBAR 1400
36 #define SCD30_PRESSURE_COMP_DEFAULT 1013
37 #define SCD30_MEAS_INTERVAL_MIN_S 2
38 #define SCD30_MEAS_INTERVAL_MAX_S 1800
39 #define SCD30_MEAS_INTERVAL_DEFAULT SCD30_MEAS_INTERVAL_MIN_S
40 #define SCD30_FRC_MIN_PPM 400
41 #define SCD30_FRC_MAX_PPM 2000
42 #define SCD30_TEMP_OFFSET_MAX 655360
43 #define SCD30_EXTRA_TIMEOUT_PER_S 250
51 static int scd30_command_write(struct scd30_state
*state
, enum scd30_cmd cmd
, u16 arg
)
53 return state
->command(state
, cmd
, arg
, NULL
, 0);
56 static int scd30_command_read(struct scd30_state
*state
, enum scd30_cmd cmd
, u16
*val
)
61 ret
= state
->command(state
, cmd
, 0, &tmp
, sizeof(tmp
));
62 *val
= be16_to_cpup(&tmp
);
67 static int scd30_reset(struct scd30_state
*state
)
72 ret
= scd30_command_write(state
, CMD_RESET
, 0);
76 /* sensor boots up within 2 secs */
79 * Power-on-reset causes sensor to produce some glitch on i2c bus and
80 * some controllers end up in error state. Try to recover by placing
81 * any data on the bus.
83 scd30_command_read(state
, CMD_MEAS_READY
, &val
);
88 /* simplified float to fixed point conversion with a scaling factor of 0.01 */
89 static int scd30_float_to_fp(int float32
)
92 mantissa
= float32
& GENMASK(22, 0),
93 sign
= (float32
& BIT(31)) ? -1 : 1,
94 exp
= (float32
& ~BIT(31)) >> 23;
97 if (!exp
&& !mantissa
)
103 /* return values ranging from 1 to 99 */
104 return sign
* ((((BIT(23) + mantissa
) * 100) >> 23) >> exp
);
107 /* return values starting at 100 */
109 float32
= BIT(exp
) + (mantissa
>> shift
);
110 fraction
= mantissa
& GENMASK(shift
- 1, 0);
112 return sign
* (float32
* 100 + ((fraction
* 100) >> shift
));
115 static int scd30_read_meas(struct scd30_state
*state
)
119 ret
= state
->command(state
, CMD_READ_MEAS
, 0, state
->meas
, sizeof(state
->meas
));
123 be32_to_cpu_array(state
->meas
, (__be32
*)state
->meas
, ARRAY_SIZE(state
->meas
));
125 for (i
= 0; i
< ARRAY_SIZE(state
->meas
); i
++)
126 state
->meas
[i
] = scd30_float_to_fp(state
->meas
[i
]);
129 * co2 is left unprocessed while temperature and humidity are scaled
130 * to milli deg C and milli percent respectively.
132 state
->meas
[SCD30_TEMP
] *= 10;
133 state
->meas
[SCD30_HR
] *= 10;
138 static int scd30_wait_meas_irq(struct scd30_state
*state
)
142 reinit_completion(&state
->meas_ready
);
143 enable_irq(state
->irq
);
144 timeout
= msecs_to_jiffies(state
->meas_interval
* (1000 + SCD30_EXTRA_TIMEOUT_PER_S
));
145 ret
= wait_for_completion_interruptible_timeout(&state
->meas_ready
, timeout
);
151 disable_irq(state
->irq
);
156 static int scd30_wait_meas_poll(struct scd30_state
*state
)
158 int timeout
= state
->meas_interval
* SCD30_EXTRA_TIMEOUT_PER_S
, tries
= 5;
164 ret
= scd30_command_read(state
, CMD_MEAS_READY
, &val
);
168 /* new measurement available */
172 msleep_interruptible(timeout
);
175 return tries
? 0 : -ETIMEDOUT
;
178 static int scd30_read_poll(struct scd30_state
*state
)
182 ret
= scd30_wait_meas_poll(state
);
186 return scd30_read_meas(state
);
189 static int scd30_read(struct scd30_state
*state
)
192 return scd30_wait_meas_irq(state
);
194 return scd30_read_poll(state
);
197 static int scd30_read_raw(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
198 int *val
, int *val2
, long mask
)
200 struct scd30_state
*state
= iio_priv(indio_dev
);
204 mutex_lock(&state
->lock
);
206 case IIO_CHAN_INFO_RAW
:
207 case IIO_CHAN_INFO_PROCESSED
:
209 *val
= state
->pressure_comp
;
214 ret
= iio_device_claim_direct_mode(indio_dev
);
218 ret
= scd30_read(state
);
220 iio_device_release_direct_mode(indio_dev
);
224 *val
= state
->meas
[chan
->address
];
225 iio_device_release_direct_mode(indio_dev
);
228 case IIO_CHAN_INFO_SCALE
:
231 ret
= IIO_VAL_INT_PLUS_MICRO
;
233 case IIO_CHAN_INFO_SAMP_FREQ
:
234 ret
= scd30_command_read(state
, CMD_MEAS_INTERVAL
, &tmp
);
239 *val2
= 1000000000 / tmp
;
240 ret
= IIO_VAL_INT_PLUS_NANO
;
242 case IIO_CHAN_INFO_CALIBBIAS
:
243 ret
= scd30_command_read(state
, CMD_TEMP_OFFSET
, &tmp
);
251 mutex_unlock(&state
->lock
);
256 static int scd30_write_raw(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
257 int val
, int val2
, long mask
)
259 struct scd30_state
*state
= iio_priv(indio_dev
);
262 mutex_lock(&state
->lock
);
264 case IIO_CHAN_INFO_SAMP_FREQ
:
268 val
= 1000000000 / val2
;
269 if (val
< SCD30_MEAS_INTERVAL_MIN_S
|| val
> SCD30_MEAS_INTERVAL_MAX_S
)
272 ret
= scd30_command_write(state
, CMD_MEAS_INTERVAL
, val
);
276 state
->meas_interval
= val
;
278 case IIO_CHAN_INFO_RAW
:
279 switch (chan
->type
) {
281 if (val
< SCD30_PRESSURE_COMP_MIN_MBAR
||
282 val
> SCD30_PRESSURE_COMP_MAX_MBAR
)
285 ret
= scd30_command_write(state
, CMD_START_MEAS
, val
);
289 state
->pressure_comp
= val
;
295 case IIO_CHAN_INFO_CALIBBIAS
:
296 if (val
< 0 || val
> SCD30_TEMP_OFFSET_MAX
)
299 * Manufacturer does not explicitly specify min/max sensible
300 * values hence check is omitted for simplicity.
302 ret
= scd30_command_write(state
, CMD_TEMP_OFFSET
/ 10, val
);
304 mutex_unlock(&state
->lock
);
309 static int scd30_write_raw_get_fmt(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
313 case IIO_CHAN_INFO_SAMP_FREQ
:
314 return IIO_VAL_INT_PLUS_NANO
;
315 case IIO_CHAN_INFO_RAW
:
316 case IIO_CHAN_INFO_CALIBBIAS
:
323 static const int scd30_pressure_raw_available
[] = {
324 SCD30_PRESSURE_COMP_MIN_MBAR
, 1, SCD30_PRESSURE_COMP_MAX_MBAR
,
327 static const int scd30_temp_calibbias_available
[] = {
328 0, 10, SCD30_TEMP_OFFSET_MAX
,
331 static int scd30_read_avail(struct iio_dev
*indio_dev
, struct iio_chan_spec
const *chan
,
332 const int **vals
, int *type
, int *length
, long mask
)
335 case IIO_CHAN_INFO_RAW
:
336 *vals
= scd30_pressure_raw_available
;
339 return IIO_AVAIL_RANGE
;
340 case IIO_CHAN_INFO_CALIBBIAS
:
341 *vals
= scd30_temp_calibbias_available
;
344 return IIO_AVAIL_RANGE
;
350 static ssize_t
sampling_frequency_available_show(struct device
*dev
, struct device_attribute
*attr
,
353 int i
= SCD30_MEAS_INTERVAL_MIN_S
;
357 len
+= sysfs_emit_at(buf
, len
, "0.%09u ", 1000000000 / i
);
359 * Not all values fit PAGE_SIZE buffer hence print every 6th
360 * (each frequency differs by 6s in time domain from the
361 * adjacent). Unlisted but valid ones are still accepted.
364 } while (i
<= SCD30_MEAS_INTERVAL_MAX_S
);
371 static ssize_t
calibration_auto_enable_show(struct device
*dev
, struct device_attribute
*attr
,
374 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
375 struct scd30_state
*state
= iio_priv(indio_dev
);
379 mutex_lock(&state
->lock
);
380 ret
= scd30_command_read(state
, CMD_ASC
, &val
);
381 mutex_unlock(&state
->lock
);
383 return ret
?: sysfs_emit(buf
, "%d\n", val
);
386 static ssize_t
calibration_auto_enable_store(struct device
*dev
, struct device_attribute
*attr
,
387 const char *buf
, size_t len
)
389 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
390 struct scd30_state
*state
= iio_priv(indio_dev
);
394 ret
= kstrtobool(buf
, &val
);
398 mutex_lock(&state
->lock
);
399 ret
= scd30_command_write(state
, CMD_ASC
, val
);
400 mutex_unlock(&state
->lock
);
405 static ssize_t
calibration_forced_value_show(struct device
*dev
, struct device_attribute
*attr
,
408 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
409 struct scd30_state
*state
= iio_priv(indio_dev
);
413 mutex_lock(&state
->lock
);
414 ret
= scd30_command_read(state
, CMD_FRC
, &val
);
415 mutex_unlock(&state
->lock
);
417 return ret
?: sysfs_emit(buf
, "%d\n", val
);
420 static ssize_t
calibration_forced_value_store(struct device
*dev
, struct device_attribute
*attr
,
421 const char *buf
, size_t len
)
423 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
424 struct scd30_state
*state
= iio_priv(indio_dev
);
428 ret
= kstrtou16(buf
, 0, &val
);
432 if (val
< SCD30_FRC_MIN_PPM
|| val
> SCD30_FRC_MAX_PPM
)
435 mutex_lock(&state
->lock
);
436 ret
= scd30_command_write(state
, CMD_FRC
, val
);
437 mutex_unlock(&state
->lock
);
442 static IIO_DEVICE_ATTR_RO(sampling_frequency_available
, 0);
443 static IIO_DEVICE_ATTR_RW(calibration_auto_enable
, 0);
444 static IIO_DEVICE_ATTR_RW(calibration_forced_value
, 0);
446 static struct attribute
*scd30_attrs
[] = {
447 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
448 &iio_dev_attr_calibration_auto_enable
.dev_attr
.attr
,
449 &iio_dev_attr_calibration_forced_value
.dev_attr
.attr
,
453 static const struct attribute_group scd30_attr_group
= {
454 .attrs
= scd30_attrs
,
457 static const struct iio_info scd30_info
= {
458 .attrs
= &scd30_attr_group
,
459 .read_raw
= scd30_read_raw
,
460 .write_raw
= scd30_write_raw
,
461 .write_raw_get_fmt
= scd30_write_raw_get_fmt
,
462 .read_avail
= scd30_read_avail
,
465 #define SCD30_CHAN_SCAN_TYPE(_sign, _realbits) .scan_type = { \
467 .realbits = _realbits, \
469 .endianness = IIO_CPU, \
472 static const struct iio_chan_spec scd30_channels
[] = {
475 * this channel is special in a sense we are pretending that
476 * sensor is able to change measurement chamber pressure but in
477 * fact we're just setting pressure compensation value
479 .type
= IIO_PRESSURE
,
480 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
481 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_RAW
),
486 .type
= IIO_CONCENTRATION
,
487 .channel2
= IIO_MOD_CO2
,
488 .address
= SCD30_CONC
,
489 .scan_index
= SCD30_CONC
,
490 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
491 BIT(IIO_CHAN_INFO_SCALE
),
492 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
495 SCD30_CHAN_SCAN_TYPE('u', 20),
499 .address
= SCD30_TEMP
,
500 .scan_index
= SCD30_TEMP
,
501 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
502 BIT(IIO_CHAN_INFO_CALIBBIAS
),
503 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_CALIBBIAS
),
504 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
506 SCD30_CHAN_SCAN_TYPE('s', 18),
509 .type
= IIO_HUMIDITYRELATIVE
,
511 .scan_index
= SCD30_HR
,
512 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
513 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
515 SCD30_CHAN_SCAN_TYPE('u', 17),
517 IIO_CHAN_SOFT_TIMESTAMP(3),
520 static int scd30_suspend(struct device
*dev
)
522 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
523 struct scd30_state
*state
= iio_priv(indio_dev
);
526 ret
= scd30_command_write(state
, CMD_STOP_MEAS
, 0);
530 return regulator_disable(state
->vdd
);
533 static int scd30_resume(struct device
*dev
)
535 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
536 struct scd30_state
*state
= iio_priv(indio_dev
);
539 ret
= regulator_enable(state
->vdd
);
543 return scd30_command_write(state
, CMD_START_MEAS
, state
->pressure_comp
);
546 EXPORT_NS_SIMPLE_DEV_PM_OPS(scd30_pm_ops
, scd30_suspend
, scd30_resume
, IIO_SCD30
);
548 static void scd30_stop_meas(void *data
)
550 struct scd30_state
*state
= data
;
552 scd30_command_write(state
, CMD_STOP_MEAS
, 0);
555 static void scd30_disable_regulator(void *data
)
557 struct scd30_state
*state
= data
;
559 regulator_disable(state
->vdd
);
562 static irqreturn_t
scd30_irq_handler(int irq
, void *priv
)
564 struct iio_dev
*indio_dev
= priv
;
566 if (iio_buffer_enabled(indio_dev
)) {
567 iio_trigger_poll(indio_dev
->trig
);
572 return IRQ_WAKE_THREAD
;
575 static irqreturn_t
scd30_irq_thread_handler(int irq
, void *priv
)
577 struct iio_dev
*indio_dev
= priv
;
578 struct scd30_state
*state
= iio_priv(indio_dev
);
581 ret
= scd30_read_meas(state
);
585 complete_all(&state
->meas_ready
);
590 static irqreturn_t
scd30_trigger_handler(int irq
, void *p
)
592 struct iio_poll_func
*pf
= p
;
593 struct iio_dev
*indio_dev
= pf
->indio_dev
;
594 struct scd30_state
*state
= iio_priv(indio_dev
);
596 int data
[SCD30_MEAS_COUNT
];
601 mutex_lock(&state
->lock
);
602 if (!iio_trigger_using_own(indio_dev
))
603 ret
= scd30_read_poll(state
);
605 ret
= scd30_read_meas(state
);
606 memset(&scan
, 0, sizeof(scan
));
607 memcpy(scan
.data
, state
->meas
, sizeof(state
->meas
));
608 mutex_unlock(&state
->lock
);
612 iio_push_to_buffers_with_timestamp(indio_dev
, &scan
, iio_get_time_ns(indio_dev
));
614 iio_trigger_notify_done(indio_dev
->trig
);
618 static int scd30_set_trigger_state(struct iio_trigger
*trig
, bool state
)
620 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
621 struct scd30_state
*st
= iio_priv(indio_dev
);
626 disable_irq(st
->irq
);
631 static const struct iio_trigger_ops scd30_trigger_ops
= {
632 .set_trigger_state
= scd30_set_trigger_state
,
633 .validate_device
= iio_trigger_validate_own_device
,
636 static int scd30_setup_trigger(struct iio_dev
*indio_dev
)
638 struct scd30_state
*state
= iio_priv(indio_dev
);
639 struct device
*dev
= indio_dev
->dev
.parent
;
640 struct iio_trigger
*trig
;
643 trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d", indio_dev
->name
,
644 iio_device_id(indio_dev
));
646 return dev_err_probe(dev
, -ENOMEM
, "failed to allocate trigger\n");
648 trig
->ops
= &scd30_trigger_ops
;
649 iio_trigger_set_drvdata(trig
, indio_dev
);
651 ret
= devm_iio_trigger_register(dev
, trig
);
655 indio_dev
->trig
= iio_trigger_get(trig
);
658 * Interrupt is enabled just before taking a fresh measurement
659 * and disabled afterwards. This means we need to ensure it is not
660 * enabled here to keep calls to enable/disable balanced.
662 ret
= devm_request_threaded_irq(dev
, state
->irq
, scd30_irq_handler
,
663 scd30_irq_thread_handler
,
664 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
|
666 indio_dev
->name
, indio_dev
);
668 return dev_err_probe(dev
, ret
, "failed to request irq\n");
673 int scd30_probe(struct device
*dev
, int irq
, const char *name
, void *priv
,
674 scd30_command_t command
)
676 static const unsigned long scd30_scan_masks
[] = { 0x07, 0x00 };
677 struct scd30_state
*state
;
678 struct iio_dev
*indio_dev
;
682 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*state
));
686 state
= iio_priv(indio_dev
);
690 state
->pressure_comp
= SCD30_PRESSURE_COMP_DEFAULT
;
691 state
->meas_interval
= SCD30_MEAS_INTERVAL_DEFAULT
;
692 state
->command
= command
;
693 mutex_init(&state
->lock
);
694 init_completion(&state
->meas_ready
);
696 dev_set_drvdata(dev
, indio_dev
);
698 indio_dev
->info
= &scd30_info
;
699 indio_dev
->name
= name
;
700 indio_dev
->channels
= scd30_channels
;
701 indio_dev
->num_channels
= ARRAY_SIZE(scd30_channels
);
702 indio_dev
->modes
= INDIO_DIRECT_MODE
;
703 indio_dev
->available_scan_masks
= scd30_scan_masks
;
705 state
->vdd
= devm_regulator_get(dev
, "vdd");
706 if (IS_ERR(state
->vdd
))
707 return dev_err_probe(dev
, PTR_ERR(state
->vdd
), "failed to get regulator\n");
709 ret
= regulator_enable(state
->vdd
);
713 ret
= devm_add_action_or_reset(dev
, scd30_disable_regulator
, state
);
717 ret
= scd30_reset(state
);
719 return dev_err_probe(dev
, ret
, "failed to reset device\n");
721 if (state
->irq
> 0) {
722 ret
= scd30_setup_trigger(indio_dev
);
724 return dev_err_probe(dev
, ret
, "failed to setup trigger\n");
727 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
, scd30_trigger_handler
, NULL
);
731 ret
= scd30_command_read(state
, CMD_FW_VERSION
, &val
);
733 return dev_err_probe(dev
, ret
, "failed to read firmware version\n");
734 dev_info(dev
, "firmware version: %d.%d\n", val
>> 8, (char)val
);
736 ret
= scd30_command_write(state
, CMD_MEAS_INTERVAL
, state
->meas_interval
);
738 return dev_err_probe(dev
, ret
, "failed to set measurement interval\n");
740 ret
= scd30_command_write(state
, CMD_START_MEAS
, state
->pressure_comp
);
742 return dev_err_probe(dev
, ret
, "failed to start measurement\n");
744 ret
= devm_add_action_or_reset(dev
, scd30_stop_meas
, state
);
748 return devm_iio_device_register(dev
, indio_dev
);
750 EXPORT_SYMBOL_NS(scd30_probe
, "IIO_SCD30");
752 MODULE_AUTHOR("Tomasz Duszynski <tomasz.duszynski@octakon.com>");
753 MODULE_DESCRIPTION("Sensirion SCD30 carbon dioxide sensor core driver");
754 MODULE_LICENSE("GPL v2");