1 // SPDX-License-Identifier: GPL-2.0
3 * ScioSense ENS160 multi-gas sensor driver
5 * Copyright (c) 2024 Gustavo Silva <gustavograzs@gmail.com>
8 * https://www.sciosense.com/wp-content/uploads/2023/12/ENS160-Datasheet.pdf
11 #include <linux/bitfield.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/trigger.h>
14 #include <linux/iio/trigger_consumer.h>
15 #include <linux/iio/triggered_buffer.h>
16 #include <linux/module.h>
17 #include <linux/regmap.h>
21 #define ENS160_PART_ID 0x160
23 #define ENS160_BOOTING_TIME_MS 10U
25 #define ENS160_REG_PART_ID 0x00
27 #define ENS160_REG_OPMODE 0x10
29 #define ENS160_REG_CONFIG 0x11
30 #define ENS160_REG_CONFIG_INTEN BIT(0)
31 #define ENS160_REG_CONFIG_INTDAT BIT(1)
32 #define ENS160_REG_CONFIG_INT_CFG BIT(5)
34 #define ENS160_REG_MODE_DEEP_SLEEP 0x00
35 #define ENS160_REG_MODE_IDLE 0x01
36 #define ENS160_REG_MODE_STANDARD 0x02
37 #define ENS160_REG_MODE_RESET 0xF0
39 #define ENS160_REG_COMMAND 0x12
40 #define ENS160_REG_COMMAND_GET_APPVER 0x0E
41 #define ENS160_REG_COMMAND_CLRGPR 0xCC
43 #define ENS160_REG_TEMP_IN 0x13
44 #define ENS160_REG_RH_IN 0x15
45 #define ENS160_REG_DEVICE_STATUS 0x20
46 #define ENS160_REG_DATA_AQI 0x21
47 #define ENS160_REG_DATA_TVOC 0x22
48 #define ENS160_REG_DATA_ECO2 0x24
49 #define ENS160_REG_DATA_T 0x30
50 #define ENS160_REG_DATA_RH 0x32
51 #define ENS160_REG_GPR_READ4 0x4C
53 #define ENS160_STATUS_VALIDITY_FLAG GENMASK(3, 2)
55 #define ENS160_STATUS_NORMAL 0x00
58 struct regmap
*regmap
;
59 /* Protect reads from the sensor */
63 s64 timestamp
__aligned(8);
64 } scan
__aligned(IIO_DMA_MINALIGN
);
69 static const struct iio_chan_spec ens160_channels
[] = {
71 .type
= IIO_CONCENTRATION
,
72 .channel2
= IIO_MOD_VOC
,
74 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
75 BIT(IIO_CHAN_INFO_SCALE
),
76 .address
= ENS160_REG_DATA_TVOC
,
86 .type
= IIO_CONCENTRATION
,
87 .channel2
= IIO_MOD_CO2
,
89 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
90 BIT(IIO_CHAN_INFO_SCALE
),
91 .address
= ENS160_REG_DATA_ECO2
,
100 IIO_CHAN_SOFT_TIMESTAMP(2),
103 static int ens160_read_raw(struct iio_dev
*indio_dev
,
104 struct iio_chan_spec
const *chan
,
105 int *val
, int *val2
, long mask
)
107 struct ens160_data
*data
= iio_priv(indio_dev
);
111 case IIO_CHAN_INFO_RAW
:
112 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
) {
113 guard(mutex
)(&data
->mutex
);
114 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
115 &data
->buf
, sizeof(data
->buf
));
118 *val
= le16_to_cpu(data
->buf
);
122 case IIO_CHAN_INFO_SCALE
:
123 switch (chan
->channel2
) {
125 /* The sensor reads CO2 data as ppm */
128 return IIO_VAL_INT_PLUS_MICRO
;
130 /* The sensor reads VOC data as ppb */
133 return IIO_VAL_INT_PLUS_NANO
;
142 static int ens160_set_mode(struct ens160_data
*data
, u8 mode
)
146 ret
= regmap_write(data
->regmap
, ENS160_REG_OPMODE
, mode
);
150 msleep(ENS160_BOOTING_TIME_MS
);
155 static void ens160_set_idle(void *data
)
157 ens160_set_mode(data
, ENS160_REG_MODE_IDLE
);
160 static int ens160_chip_init(struct ens160_data
*data
)
162 struct device
*dev
= regmap_get_device(data
->regmap
);
166 ret
= ens160_set_mode(data
, ENS160_REG_MODE_RESET
);
170 ret
= regmap_bulk_read(data
->regmap
, ENS160_REG_PART_ID
, &data
->buf
,
175 if (le16_to_cpu(data
->buf
) != ENS160_PART_ID
)
178 ret
= ens160_set_mode(data
, ENS160_REG_MODE_IDLE
);
182 ret
= regmap_write(data
->regmap
, ENS160_REG_COMMAND
,
183 ENS160_REG_COMMAND_CLRGPR
);
187 ret
= regmap_write(data
->regmap
, ENS160_REG_COMMAND
,
188 ENS160_REG_COMMAND_GET_APPVER
);
192 ret
= regmap_bulk_read(data
->regmap
, ENS160_REG_GPR_READ4
,
193 data
->fw_version
, sizeof(data
->fw_version
));
197 dev_info(dev
, "firmware version: %u.%u.%u\n", data
->fw_version
[2],
198 data
->fw_version
[1], data
->fw_version
[0]);
200 ret
= ens160_set_mode(data
, ENS160_REG_MODE_STANDARD
);
204 ret
= devm_add_action_or_reset(dev
, ens160_set_idle
, data
);
208 ret
= regmap_read(data
->regmap
, ENS160_REG_DEVICE_STATUS
, &status
);
212 if (FIELD_GET(ENS160_STATUS_VALIDITY_FLAG
, status
)
213 != ENS160_STATUS_NORMAL
)
219 static const struct iio_info ens160_info
= {
220 .read_raw
= ens160_read_raw
,
223 static int ens160_suspend(struct device
*dev
)
225 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
226 struct ens160_data
*data
= iio_priv(indio_dev
);
228 return ens160_set_mode(data
, ENS160_REG_MODE_DEEP_SLEEP
);
231 static int ens160_resume(struct device
*dev
)
233 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
234 struct ens160_data
*data
= iio_priv(indio_dev
);
237 ret
= ens160_set_mode(data
, ENS160_REG_MODE_IDLE
);
241 return ens160_set_mode(data
, ENS160_REG_MODE_STANDARD
);
243 EXPORT_NS_SIMPLE_DEV_PM_OPS(ens160_pm_ops
, ens160_suspend
, ens160_resume
,
246 static irqreturn_t
ens160_trigger_handler(int irq
, void *p
)
248 struct iio_poll_func
*pf
= p
;
249 struct iio_dev
*indio_dev
= pf
->indio_dev
;
250 struct ens160_data
*data
= iio_priv(indio_dev
);
253 guard(mutex
)(&data
->mutex
);
255 ret
= regmap_bulk_read(data
->regmap
, ENS160_REG_DATA_TVOC
,
256 data
->scan
.chans
, sizeof(data
->scan
.chans
));
260 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
263 iio_trigger_notify_done(indio_dev
->trig
);
268 static int ens160_set_trigger_state(struct iio_trigger
*trig
, bool state
)
270 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
271 struct ens160_data
*data
= iio_priv(indio_dev
);
272 unsigned int int_bits
= ENS160_REG_CONFIG_INTEN
|
273 ENS160_REG_CONFIG_INTDAT
|
274 ENS160_REG_CONFIG_INT_CFG
;
277 return regmap_set_bits(data
->regmap
, ENS160_REG_CONFIG
,
280 return regmap_clear_bits(data
->regmap
, ENS160_REG_CONFIG
,
284 static const struct iio_trigger_ops ens160_trigger_ops
= {
285 .set_trigger_state
= ens160_set_trigger_state
,
286 .validate_device
= iio_trigger_validate_own_device
,
289 static int ens160_setup_trigger(struct iio_dev
*indio_dev
, int irq
)
291 struct device
*dev
= indio_dev
->dev
.parent
;
292 struct iio_trigger
*trig
;
295 trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d", indio_dev
->name
,
296 iio_device_id(indio_dev
));
298 return dev_err_probe(dev
, -ENOMEM
,
299 "failed to allocate trigger\n");
301 trig
->ops
= &ens160_trigger_ops
;
302 iio_trigger_set_drvdata(trig
, indio_dev
);
304 ret
= devm_iio_trigger_register(dev
, trig
);
308 indio_dev
->trig
= iio_trigger_get(trig
);
310 ret
= devm_request_threaded_irq(dev
, irq
,
311 iio_trigger_generic_data_rdy_poll
,
317 return dev_err_probe(dev
, ret
, "failed to request irq\n");
322 int devm_ens160_core_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
325 struct ens160_data
*data
;
326 struct iio_dev
*indio_dev
;
329 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
333 data
= iio_priv(indio_dev
);
334 data
->regmap
= regmap
;
336 indio_dev
->name
= name
;
337 indio_dev
->info
= &ens160_info
;
338 indio_dev
->modes
= INDIO_DIRECT_MODE
;
339 indio_dev
->channels
= ens160_channels
;
340 indio_dev
->num_channels
= ARRAY_SIZE(ens160_channels
);
343 ret
= ens160_setup_trigger(indio_dev
, irq
);
345 return dev_err_probe(dev
, ret
,
346 "failed to setup trigger\n");
349 ret
= ens160_chip_init(data
);
351 return dev_err_probe(dev
, ret
, "chip initialization failed\n");
353 mutex_init(&data
->mutex
);
355 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
356 iio_pollfunc_store_time
,
357 ens160_trigger_handler
, NULL
);
361 return devm_iio_device_register(dev
, indio_dev
);
363 EXPORT_SYMBOL_NS(devm_ens160_core_probe
, IIO_ENS160
);
365 MODULE_AUTHOR("Gustavo Silva <gustavograzs@gmail.com>");
366 MODULE_DESCRIPTION("ScioSense ENS160 driver");
367 MODULE_LICENSE("GPL v2");