| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* mh-z19b CO₂ sensor driver |
| 4 |
* |
| 5 |
* Copyright (c) 2025 Gyeyoung Baek <[email protected]> |
| 6 |
* |
| 7 |
* Datasheet: |
| 8 |
* https://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <linux/array_size.h> |
| 12 |
#include <linux/completion.h> |
| 13 |
#include <linux/device.h> |
| 14 |
#include <linux/errno.h> |
| 15 |
#include <linux/iio/iio.h> |
| 16 |
#include <linux/iio/sysfs.h> |
| 17 |
#include <linux/jiffies.h> |
| 18 |
#include <linux/kstrtox.h> |
| 19 |
#include <linux/minmax.h> |
| 20 |
#include <linux/mod_devicetable.h> |
| 21 |
#include <linux/module.h> |
| 22 |
#include <linux/regulator/consumer.h> |
| 23 |
#include <linux/serdev.h> |
| 24 |
#include <linux/string.h> |
| 25 |
#include <linux/types.h> |
| 26 |
#include <linux/unaligned.h> |
| 27 |
|
| 28 |
/* |
| 29 |
* Commands have following format: |
| 30 |
* |
| 31 |
* +------+------+-----+------+------+------+------+------+-------+ |
| 32 |
* | 0xFF | 0x01 | cmd | arg0 | arg1 | 0x00 | 0x00 | 0x00 | cksum | |
| 33 |
* +------+------+-----+------+------+------+------+------+-------+ |
| 34 |
*/ |
| 35 |
#define MHZ19B_CMD_SIZE 9 |
| 36 |
|
| 37 |
/* ABC logic in MHZ19B means auto calibration. */ |
| 38 |
#define MHZ19B_ABC_LOGIC_CMD 0x79 |
| 39 |
#define MHZ19B_READ_CO2_CMD 0x86 |
| 40 |
#define MHZ19B_SPAN_POINT_CMD 0x88 |
| 41 |
#define MHZ19B_ZERO_POINT_CMD 0x87 |
| 42 |
|
| 43 |
#define MHZ19B_SPAN_POINT_PPM_MIN 1000 |
| 44 |
#define MHZ19B_SPAN_POINT_PPM_MAX 5000 |
| 45 |
|
| 46 |
#define MHZ19B_SERDEV_TIMEOUT msecs_to_jiffies(100) |
| 47 |
|
| 48 |
struct mhz19b_state { |
| 49 |
struct serdev_device *serdev; |
| 50 |
|
| 51 |
/* Must wait until the 'buf' is filled with 9 bytes.*/ |
| 52 |
struct completion buf_ready; |
| 53 |
|
| 54 |
u8 buf_idx; |
| 55 |
bool buf_overflow; |
| 56 |
|
| 57 |
/* |
| 58 |
* Serdev receive buffer. |
| 59 |
* When data is received from the MH-Z19B, |
| 60 |
* the 'mhz19b_receive_buf' callback function is called and fills this buffer. |
| 61 |
*/ |
| 62 |
u8 buf[MHZ19B_CMD_SIZE] __aligned(IIO_DMA_MINALIGN); |
| 63 |
}; |
| 64 |
|
| 65 |
static u8 mhz19b_get_checksum(u8 *cmd_buf) |
| 66 |
{ |
| 67 |
u8 i, checksum = 0; |
| 68 |
|
| 69 |
/* |
| 70 |
* +------+------+-----+------+------+------+------+------+-------+ |
| 71 |
* | 0xFF | 0x01 | cmd | arg0 | arg1 | 0x00 | 0x00 | 0x00 | cksum | |
| 72 |
* +------+------+-----+------+------+------+------+------+-------+ |
| 73 |
* i:1 2 3 4 5 6 7 |
| 74 |
* |
| 75 |
* Sum all cmd_buf elements from index 1 to 7. |
| 76 |
*/ |
| 77 |
for (i = 1; i < 8; i++) |
| 78 |
checksum += cmd_buf[i]; |
| 79 |
|
| 80 |
return -checksum; |
| 81 |
} |
| 82 |
|
| 83 |
static int mhz19b_serdev_cmd(struct iio_dev *indio_dev, int cmd, u16 arg) |
| 84 |
{ |
| 85 |
struct mhz19b_state *st = iio_priv(indio_dev); |
| 86 |
struct serdev_device *serdev = st->serdev; |
| 87 |
struct device *dev = &indio_dev->dev; |
| 88 |
int ret; |
| 89 |
|
| 90 |
/* |
| 91 |
* cmd_buf[3,4] : arg0,1 |
| 92 |
* cmd_buf[8] : checksum |
| 93 |
*/ |
| 94 |
u8 cmd_buf[MHZ19B_CMD_SIZE] = { |
| 95 |
0xFF, 0x01, cmd, |
| 96 |
}; |
| 97 |
|
| 98 |
switch (cmd) { |
| 99 |
case MHZ19B_ABC_LOGIC_CMD: |
| 100 |
cmd_buf[3] = arg ? 0xA0 : 0; |
| 101 |
break; |
| 102 |
case MHZ19B_SPAN_POINT_CMD: |
| 103 |
put_unaligned_be16(arg, &cmd_buf[3]); |
| 104 |
break; |
| 105 |
default: |
| 106 |
break; |
| 107 |
} |
| 108 |
cmd_buf[8] = mhz19b_get_checksum(cmd_buf); |
| 109 |
|
| 110 |
/* Write buf to uart ctrl synchronously */ |
| 111 |
st->buf_idx = 0; |
| 112 |
st->buf_overflow = false; |
| 113 |
reinit_completion(&st->buf_ready); |
| 114 |
|
| 115 |
ret = serdev_device_write(serdev, cmd_buf, MHZ19B_CMD_SIZE, 0); |
| 116 |
if (ret < 0) |
| 117 |
return ret; |
| 118 |
if (ret != MHZ19B_CMD_SIZE) |
| 119 |
return -EIO; |
| 120 |
|
| 121 |
switch (cmd) { |
| 122 |
case MHZ19B_READ_CO2_CMD: |
| 123 |
ret = wait_for_completion_interruptible_timeout(&st->buf_ready, |
| 124 |
MHZ19B_SERDEV_TIMEOUT); |
| 125 |
if (ret < 0) |
| 126 |
return ret; |
| 127 |
if (!ret) |
| 128 |
return -ETIMEDOUT; |
| 129 |
|
| 130 |
if (st->buf_overflow) |
| 131 |
return -EMSGSIZE; |
| 132 |
|
| 133 |
if (st->buf[8] != mhz19b_get_checksum(st->buf)) { |
| 134 |
dev_err(dev, "checksum err"); |
| 135 |
return -EINVAL; |
| 136 |
} |
| 137 |
|
| 138 |
return get_unaligned_be16(&st->buf[2]); |
| 139 |
default: |
| 140 |
/* No response commands. */ |
| 141 |
return 0; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
static int mhz19b_read_raw(struct iio_dev *indio_dev, |
| 146 |
struct iio_chan_spec const *chan, |
| 147 |
int *val, int *val2, long mask) |
| 148 |
{ |
| 149 |
int ret; |
| 150 |
|
| 151 |
ret = mhz19b_serdev_cmd(indio_dev, MHZ19B_READ_CO2_CMD, 0); |
| 152 |
if (ret < 0) |
| 153 |
return ret; |
| 154 |
|
| 155 |
*val = ret; |
| 156 |
return IIO_VAL_INT; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* echo 0 > calibration_auto_enable : ABC logic off |
| 161 |
* echo 1 > calibration_auto_enable : ABC logic on |
| 162 |
*/ |
| 163 |
static ssize_t calibration_auto_enable_store(struct device *dev, |
| 164 |
struct device_attribute *attr, |
| 165 |
const char *buf, size_t len) |
| 166 |
{ |
| 167 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 168 |
bool enable; |
| 169 |
int ret; |
| 170 |
|
| 171 |
ret = kstrtobool(buf, &enable); |
| 172 |
if (ret) |
| 173 |
return ret; |
| 174 |
|
| 175 |
ret = mhz19b_serdev_cmd(indio_dev, MHZ19B_ABC_LOGIC_CMD, enable); |
| 176 |
if (ret < 0) |
| 177 |
return ret; |
| 178 |
|
| 179 |
return len; |
| 180 |
} |
| 181 |
static IIO_DEVICE_ATTR_WO(calibration_auto_enable, 0); |
| 182 |
|
| 183 |
/* |
| 184 |
* echo 0 > calibration_forced_value : zero point calibration |
| 185 |
* (make sure the sensor has been working under 400ppm for over 20 minutes.) |
| 186 |
* echo [1000 1 5000] > calibration_forced_value : span point calibration |
| 187 |
* (make sure the sensor has been working under a certain level CO₂ for over 20 minutes.) |
| 188 |
*/ |
| 189 |
static ssize_t calibration_forced_value_store(struct device *dev, |
| 190 |
struct device_attribute *attr, |
| 191 |
const char *buf, size_t len) |
| 192 |
{ |
| 193 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 194 |
u16 ppm; |
| 195 |
int cmd, ret; |
| 196 |
|
| 197 |
ret = kstrtou16(buf, 0, &ppm); |
| 198 |
if (ret) |
| 199 |
return ret; |
| 200 |
|
| 201 |
if (ppm) { |
| 202 |
if (!in_range(ppm, MHZ19B_SPAN_POINT_PPM_MIN, |
| 203 |
MHZ19B_SPAN_POINT_PPM_MAX - MHZ19B_SPAN_POINT_PPM_MIN + 1)) { |
| 204 |
dev_dbg(&indio_dev->dev, |
| 205 |
"span point ppm should be in a range [%d-%d]\n", |
| 206 |
MHZ19B_SPAN_POINT_PPM_MIN, MHZ19B_SPAN_POINT_PPM_MAX); |
| 207 |
return -EINVAL; |
| 208 |
} |
| 209 |
|
| 210 |
cmd = MHZ19B_SPAN_POINT_CMD; |
| 211 |
} else { |
| 212 |
cmd = MHZ19B_ZERO_POINT_CMD; |
| 213 |
} |
| 214 |
|
| 215 |
ret = mhz19b_serdev_cmd(indio_dev, cmd, ppm); |
| 216 |
if (ret < 0) |
| 217 |
return ret; |
| 218 |
|
| 219 |
return len; |
| 220 |
} |
| 221 |
static IIO_DEVICE_ATTR_WO(calibration_forced_value, 0); |
| 222 |
|
| 223 |
static struct attribute *mhz19b_attrs[] = { |
| 224 |
&iio_dev_attr_calibration_auto_enable.dev_attr.attr, |
| 225 |
&iio_dev_attr_calibration_forced_value.dev_attr.attr, |
| 226 |
NULL |
| 227 |
}; |
| 228 |
|
| 229 |
static const struct attribute_group mhz19b_attr_group = { |
| 230 |
.attrs = mhz19b_attrs, |
| 231 |
}; |
| 232 |
|
| 233 |
static const struct iio_info mhz19b_info = { |
| 234 |
.attrs = &mhz19b_attr_group, |
| 235 |
.read_raw = mhz19b_read_raw, |
| 236 |
}; |
| 237 |
|
| 238 |
static const struct iio_chan_spec mhz19b_channels[] = { |
| 239 |
{ |
| 240 |
.type = IIO_CONCENTRATION, |
| 241 |
.channel2 = IIO_MOD_CO2, |
| 242 |
.modified = 1, |
| 243 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 244 |
}, |
| 245 |
}; |
| 246 |
|
| 247 |
static size_t mhz19b_receive_buf(struct serdev_device *serdev, |
| 248 |
const u8 *data, size_t len) |
| 249 |
{ |
| 250 |
struct iio_dev *indio_dev = dev_get_drvdata(&serdev->dev); |
| 251 |
struct mhz19b_state *st = iio_priv(indio_dev); |
| 252 |
size_t remaining = MHZ19B_CMD_SIZE - st->buf_idx; |
| 253 |
|
| 254 |
if (len > remaining) { |
| 255 |
st->buf_idx = 0; |
| 256 |
st->buf_overflow = true; |
| 257 |
complete(&st->buf_ready); |
| 258 |
return len; |
| 259 |
} |
| 260 |
|
| 261 |
memcpy(st->buf + st->buf_idx, data, len); |
| 262 |
st->buf_idx += len; |
| 263 |
|
| 264 |
if (st->buf_idx == MHZ19B_CMD_SIZE) { |
| 265 |
st->buf_idx = 0; |
| 266 |
complete(&st->buf_ready); |
| 267 |
} |
| 268 |
|
| 269 |
return len; |
| 270 |
} |
| 271 |
|
| 272 |
static const struct serdev_device_ops mhz19b_ops = { |
| 273 |
.receive_buf = mhz19b_receive_buf, |
| 274 |
.write_wakeup = serdev_device_write_wakeup, |
| 275 |
}; |
| 276 |
|
| 277 |
static int mhz19b_probe(struct serdev_device *serdev) |
| 278 |
{ |
| 279 |
int ret; |
| 280 |
struct device *dev = &serdev->dev; |
| 281 |
struct iio_dev *indio_dev; |
| 282 |
struct mhz19b_state *st; |
| 283 |
|
| 284 |
serdev_device_set_client_ops(serdev, &mhz19b_ops); |
| 285 |
ret = devm_serdev_device_open(dev, serdev); |
| 286 |
if (ret) |
| 287 |
return ret; |
| 288 |
serdev_device_set_baudrate(serdev, 9600); |
| 289 |
serdev_device_set_flow_control(serdev, false); |
| 290 |
ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE); |
| 291 |
if (ret) |
| 292 |
return ret; |
| 293 |
|
| 294 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); |
| 295 |
if (!indio_dev) |
| 296 |
return -ENOMEM; |
| 297 |
serdev_device_set_drvdata(serdev, indio_dev); |
| 298 |
|
| 299 |
st = iio_priv(indio_dev); |
| 300 |
st->serdev = serdev; |
| 301 |
|
| 302 |
init_completion(&st->buf_ready); |
| 303 |
|
| 304 |
ret = devm_regulator_get_enable(dev, "vin"); |
| 305 |
if (ret) |
| 306 |
return ret; |
| 307 |
|
| 308 |
indio_dev->name = "mh-z19b"; |
| 309 |
indio_dev->channels = mhz19b_channels; |
| 310 |
indio_dev->num_channels = ARRAY_SIZE(mhz19b_channels); |
| 311 |
indio_dev->info = &mhz19b_info; |
| 312 |
|
| 313 |
return devm_iio_device_register(dev, indio_dev); |
| 314 |
} |
| 315 |
|
| 316 |
static const struct of_device_id mhz19b_of_match[] = { |
| 317 |
{ .compatible = "winsen,mhz19b", }, |
| 318 |
{ } |
| 319 |
}; |
| 320 |
MODULE_DEVICE_TABLE(of, mhz19b_of_match); |
| 321 |
|
| 322 |
static struct serdev_device_driver mhz19b_driver = { |
| 323 |
.driver = { |
| 324 |
.name = "mhz19b", |
| 325 |
.of_match_table = mhz19b_of_match, |
| 326 |
}, |
| 327 |
.probe = mhz19b_probe, |
| 328 |
}; |
| 329 |
module_serdev_device_driver(mhz19b_driver); |
| 330 |
|
| 331 |
MODULE_AUTHOR("Gyeyoung Baek"); |
| 332 |
MODULE_DESCRIPTION("MH-Z19B CO2 sensor driver using serdev interface"); |
| 333 |
MODULE_LICENSE("GPL"); |