2 * si7005.c - Support for Silabs Si7005 humidity and temperature sensor
4 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * (7-bit I2C slave address 0x40)
12 * TODO: heater, fast mode, processed mode (temp. / linearity compensation)
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define SI7005_STATUS 0x00
25 #define SI7005_DATA 0x01 /* 16-bit, MSB */
26 #define SI7005_CONFIG 0x03
27 #define SI7005_ID 0x11
29 #define SI7005_STATUS_NRDY BIT(0)
30 #define SI7005_CONFIG_TEMP BIT(4)
31 #define SI7005_CONFIG_START BIT(0)
33 #define SI7005_ID_7005 0x50
34 #define SI7005_ID_7015 0xf0
37 struct i2c_client
*client
;
42 static int si7005_read_measurement(struct si7005_data
*data
, bool temp
)
47 mutex_lock(&data
->lock
);
49 ret
= i2c_smbus_write_byte_data(data
->client
, SI7005_CONFIG
,
50 data
->config
| SI7005_CONFIG_START
|
51 (temp
? SI7005_CONFIG_TEMP
: 0));
57 ret
= i2c_smbus_read_byte_data(data
->client
, SI7005_STATUS
);
60 if (!(ret
& SI7005_STATUS_NRDY
))
68 ret
= i2c_smbus_read_word_swapped(data
->client
, SI7005_DATA
);
71 mutex_unlock(&data
->lock
);
76 static int si7005_read_raw(struct iio_dev
*indio_dev
,
77 struct iio_chan_spec
const *chan
, int *val
,
80 struct si7005_data
*data
= iio_priv(indio_dev
);
84 case IIO_CHAN_INFO_RAW
:
85 ret
= si7005_read_measurement(data
, chan
->type
== IIO_TEMP
);
90 case IIO_CHAN_INFO_SCALE
:
91 if (chan
->type
== IIO_TEMP
) {
98 return IIO_VAL_INT_PLUS_MICRO
;
99 case IIO_CHAN_INFO_OFFSET
:
100 if (chan
->type
== IIO_TEMP
)
103 *val
= -24 * 16 * 16;
112 static const struct iio_chan_spec si7005_channels
[] = {
114 .type
= IIO_HUMIDITYRELATIVE
,
115 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
116 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_OFFSET
),
120 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
121 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_OFFSET
),
125 static const struct iio_info si7005_info
= {
126 .read_raw
= si7005_read_raw
,
127 .driver_module
= THIS_MODULE
,
130 static int si7005_probe(struct i2c_client
*client
,
131 const struct i2c_device_id
*id
)
133 struct iio_dev
*indio_dev
;
134 struct si7005_data
*data
;
137 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
140 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
144 data
= iio_priv(indio_dev
);
145 i2c_set_clientdata(client
, indio_dev
);
146 data
->client
= client
;
147 mutex_init(&data
->lock
);
149 indio_dev
->dev
.parent
= &client
->dev
;
150 indio_dev
->name
= dev_name(&client
->dev
);
151 indio_dev
->modes
= INDIO_DIRECT_MODE
;
152 indio_dev
->info
= &si7005_info
;
154 indio_dev
->channels
= si7005_channels
;
155 indio_dev
->num_channels
= ARRAY_SIZE(si7005_channels
);
157 ret
= i2c_smbus_read_byte_data(client
, SI7005_ID
);
160 if (ret
!= SI7005_ID_7005
&& ret
!= SI7005_ID_7015
)
163 ret
= i2c_smbus_read_byte_data(client
, SI7005_CONFIG
);
168 return devm_iio_device_register(&client
->dev
, indio_dev
);
171 static const struct i2c_device_id si7005_id
[] = {
176 MODULE_DEVICE_TABLE(i2c
, si7005_id
);
178 static struct i2c_driver si7005_driver
= {
182 .probe
= si7005_probe
,
183 .id_table
= si7005_id
,
185 module_i2c_driver(si7005_driver
);
187 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
188 MODULE_DESCRIPTION("Silabs Si7005 humidity and temperature sensor driver");
189 MODULE_LICENSE("GPL");