module: Convert symbol namespace to string literal
[linux.git] / drivers / iio / chemical / scd30_core.c
blobac3080929f0b1c472c0339b151f9d866df13753a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Sensirion SCD30 carbon dioxide sensor core driver
5 * Copyright (c) 2020 Tomasz Duszynski <tomasz.duszynski@octakon.com>
6 */
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>
32 #include "scd30.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
45 enum {
46 SCD30_CONC,
47 SCD30_TEMP,
48 SCD30_HR,
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)
58 __be16 tmp;
59 int ret;
61 ret = state->command(state, cmd, 0, &tmp, sizeof(tmp));
62 *val = be16_to_cpup(&tmp);
64 return ret;
67 static int scd30_reset(struct scd30_state *state)
69 int ret;
70 u16 val;
72 ret = scd30_command_write(state, CMD_RESET, 0);
73 if (ret)
74 return ret;
76 /* sensor boots up within 2 secs */
77 msleep(2000);
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);
85 return 0;
88 /* simplified float to fixed point conversion with a scaling factor of 0.01 */
89 static int scd30_float_to_fp(int float32)
91 int fraction, shift,
92 mantissa = float32 & GENMASK(22, 0),
93 sign = (float32 & BIT(31)) ? -1 : 1,
94 exp = (float32 & ~BIT(31)) >> 23;
96 /* special case 0 */
97 if (!exp && !mantissa)
98 return 0;
100 exp -= 127;
101 if (exp < 0) {
102 exp = -exp;
103 /* return values ranging from 1 to 99 */
104 return sign * ((((BIT(23) + mantissa) * 100) >> 23) >> exp);
107 /* return values starting at 100 */
108 shift = 23 - exp;
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)
117 int i, ret;
119 ret = state->command(state, CMD_READ_MEAS, 0, state->meas, sizeof(state->meas));
120 if (ret)
121 return ret;
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;
135 return 0;
138 static int scd30_wait_meas_irq(struct scd30_state *state)
140 int ret, timeout;
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);
146 if (ret > 0)
147 ret = 0;
148 else if (!ret)
149 ret = -ETIMEDOUT;
151 disable_irq(state->irq);
153 return ret;
156 static int scd30_wait_meas_poll(struct scd30_state *state)
158 int timeout = state->meas_interval * SCD30_EXTRA_TIMEOUT_PER_S, tries = 5;
160 do {
161 int ret;
162 u16 val;
164 ret = scd30_command_read(state, CMD_MEAS_READY, &val);
165 if (ret)
166 return -EIO;
168 /* new measurement available */
169 if (val)
170 break;
172 msleep_interruptible(timeout);
173 } while (--tries);
175 return tries ? 0 : -ETIMEDOUT;
178 static int scd30_read_poll(struct scd30_state *state)
180 int ret;
182 ret = scd30_wait_meas_poll(state);
183 if (ret)
184 return ret;
186 return scd30_read_meas(state);
189 static int scd30_read(struct scd30_state *state)
191 if (state->irq > 0)
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);
201 int ret = -EINVAL;
202 u16 tmp;
204 mutex_lock(&state->lock);
205 switch (mask) {
206 case IIO_CHAN_INFO_RAW:
207 case IIO_CHAN_INFO_PROCESSED:
208 if (chan->output) {
209 *val = state->pressure_comp;
210 ret = IIO_VAL_INT;
211 break;
214 ret = iio_device_claim_direct_mode(indio_dev);
215 if (ret)
216 break;
218 ret = scd30_read(state);
219 if (ret) {
220 iio_device_release_direct_mode(indio_dev);
221 break;
224 *val = state->meas[chan->address];
225 iio_device_release_direct_mode(indio_dev);
226 ret = IIO_VAL_INT;
227 break;
228 case IIO_CHAN_INFO_SCALE:
229 *val = 0;
230 *val2 = 1;
231 ret = IIO_VAL_INT_PLUS_MICRO;
232 break;
233 case IIO_CHAN_INFO_SAMP_FREQ:
234 ret = scd30_command_read(state, CMD_MEAS_INTERVAL, &tmp);
235 if (ret)
236 break;
238 *val = 0;
239 *val2 = 1000000000 / tmp;
240 ret = IIO_VAL_INT_PLUS_NANO;
241 break;
242 case IIO_CHAN_INFO_CALIBBIAS:
243 ret = scd30_command_read(state, CMD_TEMP_OFFSET, &tmp);
244 if (ret)
245 break;
247 *val = tmp;
248 ret = IIO_VAL_INT;
249 break;
251 mutex_unlock(&state->lock);
253 return ret;
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);
260 int ret = -EINVAL;
262 mutex_lock(&state->lock);
263 switch (mask) {
264 case IIO_CHAN_INFO_SAMP_FREQ:
265 if (val)
266 break;
268 val = 1000000000 / val2;
269 if (val < SCD30_MEAS_INTERVAL_MIN_S || val > SCD30_MEAS_INTERVAL_MAX_S)
270 break;
272 ret = scd30_command_write(state, CMD_MEAS_INTERVAL, val);
273 if (ret)
274 break;
276 state->meas_interval = val;
277 break;
278 case IIO_CHAN_INFO_RAW:
279 switch (chan->type) {
280 case IIO_PRESSURE:
281 if (val < SCD30_PRESSURE_COMP_MIN_MBAR ||
282 val > SCD30_PRESSURE_COMP_MAX_MBAR)
283 break;
285 ret = scd30_command_write(state, CMD_START_MEAS, val);
286 if (ret)
287 break;
289 state->pressure_comp = val;
290 break;
291 default:
292 break;
294 break;
295 case IIO_CHAN_INFO_CALIBBIAS:
296 if (val < 0 || val > SCD30_TEMP_OFFSET_MAX)
297 break;
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);
306 return ret;
309 static int scd30_write_raw_get_fmt(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
310 long mask)
312 switch (mask) {
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:
317 return IIO_VAL_INT;
320 return -EINVAL;
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)
334 switch (mask) {
335 case IIO_CHAN_INFO_RAW:
336 *vals = scd30_pressure_raw_available;
337 *type = IIO_VAL_INT;
339 return IIO_AVAIL_RANGE;
340 case IIO_CHAN_INFO_CALIBBIAS:
341 *vals = scd30_temp_calibbias_available;
342 *type = IIO_VAL_INT;
344 return IIO_AVAIL_RANGE;
347 return -EINVAL;
350 static ssize_t sampling_frequency_available_show(struct device *dev, struct device_attribute *attr,
351 char *buf)
353 int i = SCD30_MEAS_INTERVAL_MIN_S;
354 ssize_t len = 0;
356 do {
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.
363 i += 6;
364 } while (i <= SCD30_MEAS_INTERVAL_MAX_S);
366 buf[len - 1] = '\n';
368 return len;
371 static ssize_t calibration_auto_enable_show(struct device *dev, struct device_attribute *attr,
372 char *buf)
374 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
375 struct scd30_state *state = iio_priv(indio_dev);
376 int ret;
377 u16 val;
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);
391 bool val;
392 int ret;
394 ret = kstrtobool(buf, &val);
395 if (ret)
396 return ret;
398 mutex_lock(&state->lock);
399 ret = scd30_command_write(state, CMD_ASC, val);
400 mutex_unlock(&state->lock);
402 return ret ?: len;
405 static ssize_t calibration_forced_value_show(struct device *dev, struct device_attribute *attr,
406 char *buf)
408 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
409 struct scd30_state *state = iio_priv(indio_dev);
410 int ret;
411 u16 val;
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);
425 int ret;
426 u16 val;
428 ret = kstrtou16(buf, 0, &val);
429 if (ret)
430 return ret;
432 if (val < SCD30_FRC_MIN_PPM || val > SCD30_FRC_MAX_PPM)
433 return -EINVAL;
435 mutex_lock(&state->lock);
436 ret = scd30_command_write(state, CMD_FRC, val);
437 mutex_unlock(&state->lock);
439 return ret ?: len;
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,
450 NULL
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 = { \
466 .sign = _sign, \
467 .realbits = _realbits, \
468 .storagebits = 32, \
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),
482 .output = 1,
483 .scan_index = -1,
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),
493 .modified = 1,
495 SCD30_CHAN_SCAN_TYPE('u', 20),
498 .type = IIO_TEMP,
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,
510 .address = SCD30_HR,
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);
524 int ret;
526 ret = scd30_command_write(state, CMD_STOP_MEAS, 0);
527 if (ret)
528 return ret;
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);
537 int ret;
539 ret = regulator_enable(state->vdd);
540 if (ret)
541 return ret;
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);
569 return IRQ_HANDLED;
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);
579 int ret;
581 ret = scd30_read_meas(state);
582 if (ret)
583 goto out;
585 complete_all(&state->meas_ready);
586 out:
587 return IRQ_HANDLED;
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);
595 struct {
596 int data[SCD30_MEAS_COUNT];
597 s64 ts __aligned(8);
598 } scan;
599 int ret;
601 mutex_lock(&state->lock);
602 if (!iio_trigger_using_own(indio_dev))
603 ret = scd30_read_poll(state);
604 else
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);
609 if (ret)
610 goto out;
612 iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
613 out:
614 iio_trigger_notify_done(indio_dev->trig);
615 return IRQ_HANDLED;
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);
623 if (state)
624 enable_irq(st->irq);
625 else
626 disable_irq(st->irq);
628 return 0;
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;
641 int ret;
643 trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
644 iio_device_id(indio_dev));
645 if (!trig)
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);
652 if (ret)
653 return ret;
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 |
665 IRQF_NO_AUTOEN,
666 indio_dev->name, indio_dev);
667 if (ret)
668 return dev_err_probe(dev, ret, "failed to request irq\n");
670 return 0;
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;
679 int ret;
680 u16 val;
682 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
683 if (!indio_dev)
684 return -ENOMEM;
686 state = iio_priv(indio_dev);
687 state->dev = dev;
688 state->priv = priv;
689 state->irq = irq;
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);
710 if (ret)
711 return ret;
713 ret = devm_add_action_or_reset(dev, scd30_disable_regulator, state);
714 if (ret)
715 return ret;
717 ret = scd30_reset(state);
718 if (ret)
719 return dev_err_probe(dev, ret, "failed to reset device\n");
721 if (state->irq > 0) {
722 ret = scd30_setup_trigger(indio_dev);
723 if (ret)
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);
728 if (ret)
729 return ret;
731 ret = scd30_command_read(state, CMD_FW_VERSION, &val);
732 if (ret)
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);
737 if (ret)
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);
741 if (ret)
742 return dev_err_probe(dev, ret, "failed to start measurement\n");
744 ret = devm_add_action_or_reset(dev, scd30_stop_meas, state);
745 if (ret)
746 return ret;
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");