1 // SPDX-License-Identifier: GPL-2.0
3 * Senseair Sunrise 006-0-0007 CO2 sensor driver.
5 * Copyright (C) 2021 Jacopo Mondi
7 * List of features not yet supported by the driver:
8 * - controllable EN pin
9 * - single-shot operations using the nDRY pin.
10 * - ABC/target calibration
13 #include <linux/bitops.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/regmap.h>
20 #include <linux/time64.h>
22 #include <linux/iio/iio.h>
24 #define DRIVER_NAME "sunrise_co2"
26 #define SUNRISE_ERROR_STATUS_REG 0x00
27 #define SUNRISE_CO2_FILTERED_COMP_REG 0x06
28 #define SUNRISE_CHIP_TEMPERATURE_REG 0x08
29 #define SUNRISE_CALIBRATION_STATUS_REG 0x81
30 #define SUNRISE_CALIBRATION_COMMAND_REG 0x82
31 #define SUNRISE_CALIBRATION_FACTORY_CMD 0x7c02
32 #define SUNRISE_CALIBRATION_BACKGROUND_CMD 0x7c06
34 * The calibration timeout is not characterized in the datasheet.
35 * Use 30 seconds as a reasonable upper limit.
37 #define SUNRISE_CALIBRATION_TIMEOUT_US (30 * USEC_PER_SEC)
40 struct i2c_client *client;
41 struct regmap *regmap;
42 /* Protects access to IIO attributes. */
47 /* Custom regmap read/write operations: perform unlocked access to the i2c bus. */
49 static int sunrise_regmap_read(void *context, const void *reg_buf,
50 size_t reg_size, void *val_buf, size_t val_size)
52 struct i2c_client *client = context;
53 struct sunrise_dev *sunrise = i2c_get_clientdata(client);
54 union i2c_smbus_data data;
57 if (reg_size != 1 || !val_size)
60 memset(&data, 0, sizeof(data));
61 data.block[0] = val_size;
64 * Wake up sensor by sending sensor address: START, sensor address,
65 * STOP. Sensor will not ACK this byte.
67 * The chip enters a low power state after 15ms without
68 * communications or after a complete read/write sequence.
70 __i2c_smbus_xfer(client->adapter, client->addr,
71 sunrise->ignore_nak ? I2C_M_IGNORE_NAK : 0,
72 I2C_SMBUS_WRITE, 0, I2C_SMBUS_BYTE_DATA, &data);
74 usleep_range(500, 1500);
76 ret = __i2c_smbus_xfer(client->adapter, client->addr, client->flags,
77 I2C_SMBUS_READ, ((u8 *)reg_buf)[0],
78 I2C_SMBUS_I2C_BLOCK_DATA, &data);
82 memcpy(val_buf, &data.block[1], data.block[0]);
87 static int sunrise_regmap_write(void *context, const void *val_buf, size_t count)
89 struct i2c_client *client = context;
90 struct sunrise_dev *sunrise = i2c_get_clientdata(client);
91 union i2c_smbus_data data;
93 /* Discard reg address from values count. */
98 memset(&data, 0, sizeof(data));
99 data.block[0] = count;
100 memcpy(&data.block[1], (u8 *)val_buf + 1, count);
102 __i2c_smbus_xfer(client->adapter, client->addr,
103 sunrise->ignore_nak ? I2C_M_IGNORE_NAK : 0,
104 I2C_SMBUS_WRITE, 0, I2C_SMBUS_BYTE_DATA, &data);
106 usleep_range(500, 1500);
108 return __i2c_smbus_xfer(client->adapter, client->addr, client->flags,
109 I2C_SMBUS_WRITE, ((u8 *)val_buf)[0],
110 I2C_SMBUS_I2C_BLOCK_DATA, &data);
114 * Sunrise i2c read/write operations: lock the i2c segment to avoid losing the
115 * wake up session. Use custom regmap operations that perform unlocked access to
118 static int sunrise_read_byte(struct sunrise_dev *sunrise, u8 reg)
120 const struct i2c_client *client = sunrise->client;
121 const struct device *dev = &client->dev;
125 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
126 ret = regmap_read(sunrise->regmap, reg, &val);
127 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
129 dev_err(dev, "Read byte failed: reg 0x%02x (%d)\n", reg, ret);
136 static int sunrise_read_word(struct sunrise_dev *sunrise, u8 reg, u16 *val)
138 const struct i2c_client *client = sunrise->client;
139 const struct device *dev = &client->dev;
143 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
144 ret = regmap_bulk_read(sunrise->regmap, reg, &be_val, sizeof(be_val));
145 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
147 dev_err(dev, "Read word failed: reg 0x%02x (%d)\n", reg, ret);
151 *val = be16_to_cpu(be_val);
156 static int sunrise_write_byte(struct sunrise_dev *sunrise, u8 reg, u8 val)
158 const struct i2c_client *client = sunrise->client;
159 const struct device *dev = &client->dev;
162 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
163 ret = regmap_write(sunrise->regmap, reg, val);
164 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
166 dev_err(dev, "Write byte failed: reg 0x%02x (%d)\n", reg, ret);
171 static int sunrise_write_word(struct sunrise_dev *sunrise, u8 reg, u16 data)
173 const struct i2c_client *client = sunrise->client;
174 const struct device *dev = &client->dev;
175 __be16 be_data = cpu_to_be16(data);
178 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
179 ret = regmap_bulk_write(sunrise->regmap, reg, &be_data, sizeof(be_data));
180 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
182 dev_err(dev, "Write word failed: reg 0x%02x (%d)\n", reg, ret);
187 /* Trigger a calibration cycle. */
190 SUNRISE_CALIBRATION_FACTORY,
191 SUNRISE_CALIBRATION_BACKGROUND,
194 static const struct sunrise_calib_data {
197 const char * const name;
199 [SUNRISE_CALIBRATION_FACTORY] = {
200 SUNRISE_CALIBRATION_FACTORY_CMD,
202 "factory_calibration",
204 [SUNRISE_CALIBRATION_BACKGROUND] = {
205 SUNRISE_CALIBRATION_BACKGROUND_CMD,
207 "background_calibration",
211 static int sunrise_calibrate(struct sunrise_dev *sunrise,
212 const struct sunrise_calib_data *data)
217 /* Reset the calibration status reg. */
218 ret = sunrise_write_byte(sunrise, SUNRISE_CALIBRATION_STATUS_REG, 0x00);
222 /* Write a calibration command and poll the calibration status bit. */
223 ret = sunrise_write_word(sunrise, SUNRISE_CALIBRATION_COMMAND_REG, data->cmd);
227 dev_dbg(&sunrise->client->dev, "%s in progress\n", data->name);
230 * Calibration takes several seconds, so the sleep time between reads
231 * can be pretty relaxed.
233 return read_poll_timeout(sunrise_read_byte, status, status & data->bit,
234 200000, SUNRISE_CALIBRATION_TIMEOUT_US, false,
235 sunrise, SUNRISE_CALIBRATION_STATUS_REG);
238 static ssize_t sunrise_cal_factory_write(struct iio_dev *iiodev,
240 const struct iio_chan_spec *chan,
241 const char *buf, size_t len)
243 struct sunrise_dev *sunrise = iio_priv(iiodev);
247 ret = kstrtobool(buf, &enable);
254 mutex_lock(&sunrise->lock);
255 ret = sunrise_calibrate(sunrise, &calib_data[SUNRISE_CALIBRATION_FACTORY]);
256 mutex_unlock(&sunrise->lock);
263 static ssize_t sunrise_cal_background_write(struct iio_dev *iiodev,
265 const struct iio_chan_spec *chan,
266 const char *buf, size_t len)
268 struct sunrise_dev *sunrise = iio_priv(iiodev);
272 ret = kstrtobool(buf, &enable);
279 mutex_lock(&sunrise->lock);
280 ret = sunrise_calibrate(sunrise, &calib_data[SUNRISE_CALIBRATION_BACKGROUND]);
281 mutex_unlock(&sunrise->lock);
288 /* Enumerate and retrieve the chip error status. */
292 SUNRISE_ERROR_ALGORITHM,
293 SUNRISE_ERROR_CALIBRATION,
294 SUNRISE_ERROR_SELF_DIAGNOSTIC,
295 SUNRISE_ERROR_OUT_OF_RANGE,
296 SUNRISE_ERROR_MEMORY,
297 SUNRISE_ERROR_NO_MEASUREMENT,
298 SUNRISE_ERROR_LOW_VOLTAGE,
299 SUNRISE_ERROR_MEASUREMENT_TIMEOUT,
302 static const char * const sunrise_error_statuses[] = {
303 [SUNRISE_ERROR_FATAL] = "error_fatal",
304 [SUNRISE_ERROR_I2C] = "error_i2c",
305 [SUNRISE_ERROR_ALGORITHM] = "error_algorithm",
306 [SUNRISE_ERROR_CALIBRATION] = "error_calibration",
307 [SUNRISE_ERROR_SELF_DIAGNOSTIC] = "error_self_diagnostic",
308 [SUNRISE_ERROR_OUT_OF_RANGE] = "error_out_of_range",
309 [SUNRISE_ERROR_MEMORY] = "error_memory",
310 [SUNRISE_ERROR_NO_MEASUREMENT] = "error_no_measurement",
311 [SUNRISE_ERROR_LOW_VOLTAGE] = "error_low_voltage",
312 [SUNRISE_ERROR_MEASUREMENT_TIMEOUT] = "error_measurement_timeout",
315 static const struct iio_enum sunrise_error_statuses_enum = {
316 .items = sunrise_error_statuses,
317 .num_items = ARRAY_SIZE(sunrise_error_statuses),
320 static ssize_t sunrise_error_status_read(struct iio_dev *iiodev,
322 const struct iio_chan_spec *chan,
325 struct sunrise_dev *sunrise = iio_priv(iiodev);
326 unsigned long errors;
332 mutex_lock(&sunrise->lock);
333 ret = sunrise_read_word(sunrise, SUNRISE_ERROR_STATUS_REG, &value);
335 mutex_unlock(&sunrise->lock);
340 for_each_set_bit(i, &errors, ARRAY_SIZE(sunrise_error_statuses))
341 len += sysfs_emit_at(buf, len, "%s ", sunrise_error_statuses[i]);
346 mutex_unlock(&sunrise->lock);
351 static const struct iio_chan_spec_ext_info sunrise_concentration_ext_info[] = {
352 /* Calibration triggers. */
354 .name = "calibration_factory",
355 .write = sunrise_cal_factory_write,
356 .shared = IIO_SEPARATE,
359 .name = "calibration_background",
360 .write = sunrise_cal_background_write,
361 .shared = IIO_SEPARATE,
364 /* Error statuses. */
366 .name = "error_status",
367 .read = sunrise_error_status_read,
368 .shared = IIO_SHARED_BY_ALL,
371 .name = "error_status_available",
372 .shared = IIO_SHARED_BY_ALL,
373 .read = iio_enum_available_read,
374 .private = (uintptr_t)&sunrise_error_statuses_enum,
379 static const struct iio_chan_spec sunrise_channels[] = {
381 .type = IIO_CONCENTRATION,
383 .channel2 = IIO_MOD_CO2,
384 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
385 BIT(IIO_CHAN_INFO_SCALE),
386 .ext_info = sunrise_concentration_ext_info,
390 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
391 BIT(IIO_CHAN_INFO_SCALE),
395 static int sunrise_read_raw(struct iio_dev *iio_dev,
396 const struct iio_chan_spec *chan,
397 int *val, int *val2, long mask)
399 struct sunrise_dev *sunrise = iio_priv(iio_dev);
404 case IIO_CHAN_INFO_RAW:
405 switch (chan->type) {
406 case IIO_CONCENTRATION:
407 mutex_lock(&sunrise->lock);
408 ret = sunrise_read_word(sunrise, SUNRISE_CO2_FILTERED_COMP_REG,
410 mutex_unlock(&sunrise->lock);
419 mutex_lock(&sunrise->lock);
420 ret = sunrise_read_word(sunrise, SUNRISE_CHIP_TEMPERATURE_REG,
422 mutex_unlock(&sunrise->lock);
434 case IIO_CHAN_INFO_SCALE:
435 switch (chan->type) {
436 case IIO_CONCENTRATION:
438 * 1 / 10^4 to comply with IIO scale for CO2
439 * (percentage). The chip CO2 reading range is [400 -
440 * 5000] ppm which corresponds to [0,004 - 0,5] %.
444 return IIO_VAL_FRACTIONAL;
447 /* x10 to comply with IIO scale (millidegrees celsius). */
460 static const struct iio_info sunrise_info = {
461 .read_raw = sunrise_read_raw,
464 static const struct regmap_bus sunrise_regmap_bus = {
465 .read = sunrise_regmap_read,
466 .write = sunrise_regmap_write,
469 static const struct regmap_config sunrise_regmap_config = {
474 static int sunrise_probe(struct i2c_client *client)
476 struct sunrise_dev *sunrise;
477 struct iio_dev *iio_dev;
479 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
480 I2C_FUNC_SMBUS_BLOCK_DATA)) {
481 dev_err(&client->dev,
482 "Adapter does not support required functionalities\n");
486 iio_dev = devm_iio_device_alloc(&client->dev, sizeof(*sunrise));
490 sunrise = iio_priv(iio_dev);
491 sunrise->client = client;
492 mutex_init(&sunrise->lock);
494 i2c_set_clientdata(client, sunrise);
496 sunrise->regmap = devm_regmap_init(&client->dev, &sunrise_regmap_bus,
497 client, &sunrise_regmap_config);
498 if (IS_ERR(sunrise->regmap)) {
499 dev_err(&client->dev, "Failed to initialize regmap\n");
500 return PTR_ERR(sunrise->regmap);
504 * The chip nacks the wake up message. If the adapter does not support
505 * protocol mangling do not set the I2C_M_IGNORE_NAK flag at the expense
506 * of possible cruft in the logs.
508 if (i2c_check_functionality(client->adapter, I2C_FUNC_PROTOCOL_MANGLING))
509 sunrise->ignore_nak = true;
511 iio_dev->info = &sunrise_info;
512 iio_dev->name = DRIVER_NAME;
513 iio_dev->channels = sunrise_channels;
514 iio_dev->num_channels = ARRAY_SIZE(sunrise_channels);
515 iio_dev->modes = INDIO_DIRECT_MODE;
517 return devm_iio_device_register(&client->dev, iio_dev);
520 static const struct of_device_id sunrise_of_match[] = {
521 { .compatible = "senseair,sunrise-006-0-0007" },
524 MODULE_DEVICE_TABLE(of, sunrise_of_match);
526 static struct i2c_driver sunrise_driver = {
529 .of_match_table = sunrise_of_match,
531 .probe_new = sunrise_probe,
533 module_i2c_driver(sunrise_driver);
535 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
536 MODULE_DESCRIPTION("Senseair Sunrise 006-0-0007 CO2 sensor IIO driver");
537 MODULE_LICENSE("GPL v2");