1 // SPDX-License-Identifier: GPL-2.0+
3 * ens210.c - Support for ScioSense ens210 temperature & humidity sensor family
5 * (7-bit I2C slave address 0x43 ENS210)
6 * (7-bit I2C slave address 0x43 ENS210A)
7 * (7-bit I2C slave address 0x44 ENS211)
8 * (7-bit I2C slave address 0x45 ENS212)
9 * (7-bit I2C slave address 0x46 ENS213A)
10 * (7-bit I2C slave address 0x47 ENS215)
13 * https://www.sciosense.com/wp-content/uploads/2024/04/ENS21x-Datasheet.pdf
14 * https://www.sciosense.com/wp-content/uploads/2023/12/ENS210-Datasheet.pdf
17 #include <linux/crc7.h>
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/iio/iio.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
25 #include <linux/unaligned.h>
27 /* register definitions */
28 #define ENS210_REG_PART_ID 0x00
29 #define ENS210_REG_DIE_REV 0x02
30 #define ENS210_REG_UID 0x04
31 #define ENS210_REG_SYS_CTRL 0x10
32 #define ENS210_REG_SYS_STAT 0x11
33 #define ENS210_REG_SENS_RUN 0x21
34 #define ENS210_REG_SENS_START 0x22
35 #define ENS210_REG_SENS_STOP 0x23
36 #define ENS210_REG_SENS_STAT 0x24
37 #define ENS210_REG_T_VAL 0x30
38 #define ENS210_REG_H_VAL 0x33
40 /* value definitions */
41 #define ENS210_SENS_START_T_START BIT(0)
42 #define ENS210_SENS_START_H_START BIT(1)
44 #define ENS210_SENS_STAT_T_ACTIVE BIT(0)
45 #define ENS210_SENS_STAT_H_ACTIVE BIT(1)
47 #define ENS210_SYS_CTRL_LOW_POWER_ENABLE BIT(0)
48 #define ENS210_SYS_CTRL_SYS_RESET BIT(7)
50 #define ENS210_SYS_STAT_SYS_ACTIVE BIT(0)
52 enum ens210_partnumber
{
62 * struct ens210_chip_info - Humidity/Temperature chip specific information
63 * @name: name of device
64 * @part_id: chip identifier
65 * @conv_time_msec: time for conversion calculation in m/s
67 struct ens210_chip_info
{
69 enum ens210_partnumber part_id
;
70 unsigned int conv_time_msec
;
74 * struct ens210_data - Humidity/Temperature sensor device structure
76 * @chip_info: chip specific information
77 * @lock: lock protecting against simultaneous callers of get_measurement
78 * since multiple uninterrupted transactions are required
81 struct i2c_client
*client
;
82 const struct ens210_chip_info
*chip_info
;
86 /* calculate 17-bit crc7 */
87 static u8
ens210_crc7(u32 val
)
89 unsigned int val_be
= (val
& 0x1ffff) >> 0x8;
91 return crc7_be(0xde, (u8
*)&val_be
, 3) >> 1;
94 static int ens210_get_measurement(struct iio_dev
*indio_dev
, bool temp
, int *val
)
96 struct ens210_data
*data
= iio_priv(indio_dev
);
97 struct device
*dev
= &data
->client
->dev
;
103 ret
= i2c_smbus_write_byte_data(data
->client
, ENS210_REG_SENS_START
,
104 temp
? ENS210_SENS_START_T_START
:
105 ENS210_SENS_START_H_START
);
109 /* wait for conversion to be ready */
110 msleep(data
->chip_info
->conv_time_msec
);
112 ret
= i2c_smbus_read_byte_data(data
->client
, ENS210_REG_SENS_STAT
);
117 ret
= i2c_smbus_read_i2c_block_data(
118 data
->client
, temp
? ENS210_REG_T_VAL
: ENS210_REG_H_VAL
, 3,
121 dev_err(dev
, "failed to read register");
125 dev_err(dev
, "expected 3 bytes, received %d\n", ret
);
129 regval
= get_unaligned_le24(regval_le
);
130 if (ens210_crc7(regval
) != ((regval
>> 17) & 0x7f)) {
131 dev_err(dev
, "invalid crc\n");
135 if (!((regval
>> 16) & 0x1)) {
136 dev_err(dev
, "data is not valid");
140 *val
= regval
& GENMASK(15, 0);
144 static int ens210_read_raw(struct iio_dev
*indio_dev
,
145 struct iio_chan_spec
const *channel
, int *val
,
146 int *val2
, long mask
)
148 struct ens210_data
*data
= iio_priv(indio_dev
);
152 case IIO_CHAN_INFO_RAW
:
153 scoped_guard(mutex
, &data
->lock
) {
154 ret
= ens210_get_measurement(
155 indio_dev
, channel
->type
== IIO_TEMP
, val
);
160 return -EINVAL
; /* compiler warning workaround */
161 case IIO_CHAN_INFO_SCALE
:
162 if (channel
->type
== IIO_TEMP
) {
169 return IIO_VAL_INT_PLUS_MICRO
;
170 case IIO_CHAN_INFO_OFFSET
:
173 return IIO_VAL_INT_PLUS_MICRO
;
179 static const struct iio_chan_spec ens210_channels
[] = {
182 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
183 BIT(IIO_CHAN_INFO_SCALE
) |
184 BIT(IIO_CHAN_INFO_OFFSET
),
187 .type
= IIO_HUMIDITYRELATIVE
,
188 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
189 BIT(IIO_CHAN_INFO_SCALE
),
193 static const struct iio_info ens210_info
= {
194 .read_raw
= ens210_read_raw
,
197 static int ens210_probe(struct i2c_client
*client
)
199 struct ens210_data
*data
;
200 struct iio_dev
*indio_dev
;
204 if (!i2c_check_functionality(client
->adapter
,
205 I2C_FUNC_SMBUS_WRITE_BYTE_DATA
|
206 I2C_FUNC_SMBUS_WRITE_BYTE
|
207 I2C_FUNC_SMBUS_READ_I2C_BLOCK
)) {
208 return dev_err_probe(&client
->dev
, -EOPNOTSUPP
,
209 "adapter does not support some i2c transactions\n");
212 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
216 data
= iio_priv(indio_dev
);
217 data
->client
= client
;
218 mutex_init(&data
->lock
);
219 data
->chip_info
= i2c_get_match_data(client
);
221 ret
= devm_regulator_get_enable(&client
->dev
, "vdd");
226 ret
= i2c_smbus_write_byte_data(client
, ENS210_REG_SYS_CTRL
,
227 ENS210_SYS_CTRL_SYS_RESET
);
231 /* wait for device to become active */
232 usleep_range(4000, 5000);
234 /* disable low power mode */
235 ret
= i2c_smbus_write_byte_data(client
, ENS210_REG_SYS_CTRL
, 0x00);
239 /* wait for device to finish */
240 usleep_range(4000, 5000);
243 ret
= i2c_smbus_read_word_data(client
, ENS210_REG_PART_ID
);
248 if (part_id
!= data
->chip_info
->part_id
) {
249 dev_info(&client
->dev
,
250 "Part ID does not match (0x%04x != 0x%04x)\n", part_id
,
251 data
->chip_info
->part_id
);
254 /* reenable low power */
255 ret
= i2c_smbus_write_byte_data(client
, ENS210_REG_SYS_CTRL
,
256 ENS210_SYS_CTRL_LOW_POWER_ENABLE
);
260 indio_dev
->name
= data
->chip_info
->name
;
261 indio_dev
->modes
= INDIO_DIRECT_MODE
;
262 indio_dev
->channels
= ens210_channels
;
263 indio_dev
->num_channels
= ARRAY_SIZE(ens210_channels
);
264 indio_dev
->info
= &ens210_info
;
266 return devm_iio_device_register(&client
->dev
, indio_dev
);
269 static const struct ens210_chip_info ens210_chip_info_data
= {
272 .conv_time_msec
= 130,
275 static const struct ens210_chip_info ens210a_chip_info_data
= {
278 .conv_time_msec
= 130,
281 static const struct ens210_chip_info ens211_chip_info_data
= {
284 .conv_time_msec
= 32,
287 static const struct ens210_chip_info ens212_chip_info_data
= {
290 .conv_time_msec
= 32,
293 static const struct ens210_chip_info ens213a_chip_info_data
= {
296 .conv_time_msec
= 130,
299 static const struct ens210_chip_info ens215_chip_info_data
= {
302 .conv_time_msec
= 130,
305 static const struct of_device_id ens210_of_match
[] = {
306 { .compatible
= "sciosense,ens210", .data
= &ens210_chip_info_data
},
307 { .compatible
= "sciosense,ens210a", .data
= &ens210a_chip_info_data
},
308 { .compatible
= "sciosense,ens211", .data
= &ens211_chip_info_data
},
309 { .compatible
= "sciosense,ens212", .data
= &ens212_chip_info_data
},
310 { .compatible
= "sciosense,ens213a", .data
= &ens213a_chip_info_data
},
311 { .compatible
= "sciosense,ens215", .data
= &ens215_chip_info_data
},
314 MODULE_DEVICE_TABLE(of
, ens210_of_match
);
316 static const struct i2c_device_id ens210_id_table
[] = {
317 { "ens210", (kernel_ulong_t
)&ens210_chip_info_data
},
318 { "ens210a", (kernel_ulong_t
)&ens210a_chip_info_data
},
319 { "ens211", (kernel_ulong_t
)&ens211_chip_info_data
},
320 { "ens212", (kernel_ulong_t
)&ens212_chip_info_data
},
321 { "ens213a", (kernel_ulong_t
)&ens213a_chip_info_data
},
322 { "ens215", (kernel_ulong_t
)&ens215_chip_info_data
},
325 MODULE_DEVICE_TABLE(i2c
, ens210_id_table
);
327 static struct i2c_driver ens210_driver
= {
328 .probe
= ens210_probe
,
329 .id_table
= ens210_id_table
,
332 .of_match_table
= ens210_of_match
,
335 module_i2c_driver(ens210_driver
);
337 MODULE_DESCRIPTION("ScioSense ENS210 temperature and humidity sensor driver");
338 MODULE_AUTHOR("Joshua Felmeden <jfelmeden@thegoodpenguin.co.uk>");
339 MODULE_LICENSE("GPL");