xref: /linux/drivers/iio/humidity/si7005.c (revision be709d48329a500621d2a05835283150ae137b45)
1 /*
2  * si7005.c - Support for Silabs Si7005 humidity and temperature sensor
3  *
4  * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
5  *
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.
9  *
10  * (7-bit I2C slave address 0x40)
11  *
12  * TODO: heater, fast mode, processed mode (temp. / linearity compensation)
13  */
14 
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/pm.h>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 
24 #define SI7005_STATUS 0x00
25 #define SI7005_DATA 0x01 /* 16-bit, MSB */
26 #define SI7005_CONFIG 0x03
27 #define SI7005_ID 0x11
28 
29 #define SI7005_STATUS_NRDY BIT(0)
30 #define SI7005_CONFIG_TEMP BIT(4)
31 #define SI7005_CONFIG_START BIT(0)
32 
33 #define SI7005_ID_7005 0x50
34 #define SI7005_ID_7015 0xf0
35 
36 struct si7005_data {
37 	struct i2c_client *client;
38 	struct mutex lock;
39 	u8 config;
40 };
41 
42 static int si7005_read_measurement(struct si7005_data *data, bool temp)
43 {
44 	int tries = 50;
45 	int ret;
46 
47 	mutex_lock(&data->lock);
48 
49 	ret = i2c_smbus_write_byte_data(data->client, SI7005_CONFIG,
50 		data->config | SI7005_CONFIG_START |
51 		(temp ? SI7005_CONFIG_TEMP : 0));
52 	if (ret < 0)
53 		goto done;
54 
55 	while (tries-- > 0) {
56 		msleep(20);
57 		ret = i2c_smbus_read_byte_data(data->client, SI7005_STATUS);
58 		if (ret < 0)
59 			goto done;
60 		if (!(ret & SI7005_STATUS_NRDY))
61 			break;
62 	}
63 	if (tries < 0) {
64 		ret = -EIO;
65 		goto done;
66 	}
67 
68 	ret = i2c_smbus_read_word_swapped(data->client, SI7005_DATA);
69 
70 done:
71 	mutex_unlock(&data->lock);
72 
73 	return ret;
74 }
75 
76 static int si7005_read_raw(struct iio_dev *indio_dev,
77 			    struct iio_chan_spec const *chan, int *val,
78 			    int *val2, long mask)
79 {
80 	struct si7005_data *data = iio_priv(indio_dev);
81 	int ret;
82 
83 	switch (mask) {
84 	case IIO_CHAN_INFO_RAW:
85 		ret = si7005_read_measurement(data, chan->type == IIO_TEMP);
86 		if (ret < 0)
87 			return ret;
88 		*val = ret;
89 		return IIO_VAL_INT;
90 	case IIO_CHAN_INFO_SCALE:
91 		if (chan->type == IIO_TEMP) {
92 			*val = 7;
93 			*val2 = 812500;
94 		} else {
95 			*val = 3;
96 			*val2 = 906250;
97 		}
98 		return IIO_VAL_INT_PLUS_MICRO;
99 	case IIO_CHAN_INFO_OFFSET:
100 		if (chan->type == IIO_TEMP)
101 			*val = -50 * 32 * 4;
102 		else
103 			*val = -24 * 16 * 16;
104 		return IIO_VAL_INT;
105 	default:
106 		break;
107 	}
108 
109 	return -EINVAL;
110 }
111 
112 static const struct iio_chan_spec si7005_channels[] = {
113 	{
114 		.type = IIO_HUMIDITYRELATIVE,
115 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
116 			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
117 	},
118 	{
119 		.type = IIO_TEMP,
120 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
121 			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
122 	}
123 };
124 
125 static const struct iio_info si7005_info = {
126 	.read_raw = si7005_read_raw,
127 };
128 
129 static int si7005_probe(struct i2c_client *client,
130 			 const struct i2c_device_id *id)
131 {
132 	struct iio_dev *indio_dev;
133 	struct si7005_data *data;
134 	int ret;
135 
136 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
137 		return -EOPNOTSUPP;
138 
139 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
140 	if (!indio_dev)
141 		return -ENOMEM;
142 
143 	data = iio_priv(indio_dev);
144 	i2c_set_clientdata(client, indio_dev);
145 	data->client = client;
146 	mutex_init(&data->lock);
147 
148 	indio_dev->dev.parent = &client->dev;
149 	indio_dev->name = dev_name(&client->dev);
150 	indio_dev->modes = INDIO_DIRECT_MODE;
151 	indio_dev->info = &si7005_info;
152 
153 	indio_dev->channels = si7005_channels;
154 	indio_dev->num_channels = ARRAY_SIZE(si7005_channels);
155 
156 	ret = i2c_smbus_read_byte_data(client, SI7005_ID);
157 	if (ret < 0)
158 		return ret;
159 	if (ret != SI7005_ID_7005 && ret != SI7005_ID_7015)
160 		return -ENODEV;
161 
162 	ret = i2c_smbus_read_byte_data(client, SI7005_CONFIG);
163 	if (ret < 0)
164 		return ret;
165 	data->config = ret;
166 
167 	return devm_iio_device_register(&client->dev, indio_dev);
168 }
169 
170 static const struct i2c_device_id si7005_id[] = {
171 	{ "si7005", 0 },
172 	{ "th02", 0 },
173 	{ }
174 };
175 MODULE_DEVICE_TABLE(i2c, si7005_id);
176 
177 static struct i2c_driver si7005_driver = {
178 	.driver = {
179 		.name	= "si7005",
180 	},
181 	.probe = si7005_probe,
182 	.id_table = si7005_id,
183 };
184 module_i2c_driver(si7005_driver);
185 
186 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
187 MODULE_DESCRIPTION("Silabs Si7005 humidity and temperature sensor driver");
188 MODULE_LICENSE("GPL");
189