1 // SPDX-License-Identifier: GPL-2.0
3 * Sensirion SCD30 carbon dioxide sensor i2c driver
5 * Copyright (c) 2020 Tomasz Duszynski <tomasz.duszynski@octakon.com>
7 * I2C slave address: 0x61
9 #include <linux/crc8.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/i2c.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <asm/unaligned.h>
20 #define SCD30_I2C_MAX_BUF_SIZE 18
21 #define SCD30_I2C_CRC8_POLYNOMIAL 0x31
23 static u16 scd30_i2c_cmd_lookup_tbl
[] = {
24 [CMD_START_MEAS
] = 0x0010,
25 [CMD_STOP_MEAS
] = 0x0104,
26 [CMD_MEAS_INTERVAL
] = 0x4600,
27 [CMD_MEAS_READY
] = 0x0202,
28 [CMD_READ_MEAS
] = 0x0300,
31 [CMD_TEMP_OFFSET
] = 0x5403,
32 [CMD_FW_VERSION
] = 0xd100,
36 DECLARE_CRC8_TABLE(scd30_i2c_crc8_tbl
);
38 static int scd30_i2c_xfer(struct scd30_state
*state
, char *txbuf
, int txsize
,
39 char *rxbuf
, int rxsize
)
41 struct i2c_client
*client
= to_i2c_client(state
->dev
);
45 * repeated start is not supported hence instead of sending two i2c
46 * messages in a row we send one by one
48 ret
= i2c_master_send(client
, txbuf
, txsize
);
57 ret
= i2c_master_recv(client
, rxbuf
, rxsize
);
66 static int scd30_i2c_command(struct scd30_state
*state
, enum scd30_cmd cmd
, u16 arg
,
67 void *response
, int size
)
69 char buf
[SCD30_I2C_MAX_BUF_SIZE
];
74 put_unaligned_be16(scd30_i2c_cmd_lookup_tbl
[cmd
], buf
);
78 /* each two bytes are followed by a crc8 */
81 put_unaligned_be16(arg
, buf
+ i
);
82 crc
= crc8(scd30_i2c_crc8_tbl
, buf
+ i
, 2, CRC8_INIT_VALUE
);
87 /* commands below don't take an argument */
88 if ((cmd
== CMD_STOP_MEAS
) || (cmd
== CMD_RESET
))
92 ret
= scd30_i2c_xfer(state
, buf
, i
, buf
, size
);
96 /* validate received data and strip off crc bytes */
97 for (i
= 0; i
< size
; i
+= 3) {
98 crc
= crc8(scd30_i2c_crc8_tbl
, buf
+ i
, 2, CRC8_INIT_VALUE
);
99 if (crc
!= buf
[i
+ 2]) {
100 dev_err(state
->dev
, "data integrity check failed\n");
111 static int scd30_i2c_probe(struct i2c_client
*client
)
113 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
116 crc8_populate_msb(scd30_i2c_crc8_tbl
, SCD30_I2C_CRC8_POLYNOMIAL
);
118 return scd30_probe(&client
->dev
, client
->irq
, client
->name
, NULL
, scd30_i2c_command
);
121 static const struct of_device_id scd30_i2c_of_match
[] = {
122 { .compatible
= "sensirion,scd30" },
125 MODULE_DEVICE_TABLE(of
, scd30_i2c_of_match
);
127 static struct i2c_driver scd30_i2c_driver
= {
129 .name
= KBUILD_MODNAME
,
130 .of_match_table
= scd30_i2c_of_match
,
133 .probe_new
= scd30_i2c_probe
,
135 module_i2c_driver(scd30_i2c_driver
);
137 MODULE_AUTHOR("Tomasz Duszynski <tomasz.duszynski@octakon.com>");
138 MODULE_DESCRIPTION("Sensirion SCD30 carbon dioxide sensor i2c driver");
139 MODULE_LICENSE("GPL v2");