1 // SPDX-License-Identifier: GPL-2.0-only
3 * si7005.c - Support for Silabs Si7005 humidity and temperature sensor
5 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
7 * (7-bit I2C slave address 0x40)
9 * TODO: heater, fast mode, processed mode (temp. / linearity compensation)
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define SI7005_STATUS 0x00
22 #define SI7005_DATA 0x01 /* 16-bit, MSB */
23 #define SI7005_CONFIG 0x03
24 #define SI7005_ID 0x11
26 #define SI7005_STATUS_NRDY BIT(0)
27 #define SI7005_CONFIG_TEMP BIT(4)
28 #define SI7005_CONFIG_START BIT(0)
30 #define SI7005_ID_7005 0x50
31 #define SI7005_ID_7015 0xf0
34 struct i2c_client
*client
;
39 static int si7005_read_measurement(struct si7005_data
*data
, bool temp
)
44 mutex_lock(&data
->lock
);
46 ret
= i2c_smbus_write_byte_data(data
->client
, SI7005_CONFIG
,
47 data
->config
| SI7005_CONFIG_START
|
48 (temp
? SI7005_CONFIG_TEMP
: 0));
54 ret
= i2c_smbus_read_byte_data(data
->client
, SI7005_STATUS
);
57 if (!(ret
& SI7005_STATUS_NRDY
))
65 ret
= i2c_smbus_read_word_swapped(data
->client
, SI7005_DATA
);
68 mutex_unlock(&data
->lock
);
73 static int si7005_read_raw(struct iio_dev
*indio_dev
,
74 struct iio_chan_spec
const *chan
, int *val
,
77 struct si7005_data
*data
= iio_priv(indio_dev
);
81 case IIO_CHAN_INFO_RAW
:
82 ret
= si7005_read_measurement(data
, chan
->type
== IIO_TEMP
);
87 case IIO_CHAN_INFO_SCALE
:
88 if (chan
->type
== IIO_TEMP
) {
95 return IIO_VAL_INT_PLUS_MICRO
;
96 case IIO_CHAN_INFO_OFFSET
:
97 if (chan
->type
== IIO_TEMP
)
100 *val
= -24 * 16 * 16;
109 static const struct iio_chan_spec si7005_channels
[] = {
111 .type
= IIO_HUMIDITYRELATIVE
,
112 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
113 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_OFFSET
),
117 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
118 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_OFFSET
),
122 static const struct iio_info si7005_info
= {
123 .read_raw
= si7005_read_raw
,
126 static int si7005_probe(struct i2c_client
*client
,
127 const struct i2c_device_id
*id
)
129 struct iio_dev
*indio_dev
;
130 struct si7005_data
*data
;
133 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
136 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
140 data
= iio_priv(indio_dev
);
141 i2c_set_clientdata(client
, indio_dev
);
142 data
->client
= client
;
143 mutex_init(&data
->lock
);
145 indio_dev
->name
= dev_name(&client
->dev
);
146 indio_dev
->modes
= INDIO_DIRECT_MODE
;
147 indio_dev
->info
= &si7005_info
;
149 indio_dev
->channels
= si7005_channels
;
150 indio_dev
->num_channels
= ARRAY_SIZE(si7005_channels
);
152 ret
= i2c_smbus_read_byte_data(client
, SI7005_ID
);
155 if (ret
!= SI7005_ID_7005
&& ret
!= SI7005_ID_7015
)
158 ret
= i2c_smbus_read_byte_data(client
, SI7005_CONFIG
);
163 return devm_iio_device_register(&client
->dev
, indio_dev
);
166 static const struct i2c_device_id si7005_id
[] = {
171 MODULE_DEVICE_TABLE(i2c
, si7005_id
);
173 static struct i2c_driver si7005_driver
= {
177 .probe
= si7005_probe
,
178 .id_table
= si7005_id
,
180 module_i2c_driver(si7005_driver
);
182 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
183 MODULE_DESCRIPTION("Silabs Si7005 humidity and temperature sensor driver");
184 MODULE_LICENSE("GPL");