xref: /linux/drivers/iio/humidity/si7020.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * si7020.c - Silicon Labs Si7013/20/21 Relative Humidity and Temp Sensors
4  * Copyright (c) 2013,2014  Uplogix, Inc.
5  * David Barksdale <dbarksdale@uplogix.com>
6  */
7 
8 /*
9  * The Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors
10  * are i2c devices which have an identical programming interface for
11  * measuring relative humidity and temperature. The Si7013 has an additional
12  * temperature input which this driver does not support.
13  *
14  * Data Sheets:
15  *   Si7013: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7013.pdf
16  *   Si7020: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7020.pdf
17  *   Si7021: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7021.pdf
18  */
19 
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/sysfs.h>
25 #include <linux/stat.h>
26 
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 
30 /* Measure Relative Humidity, Hold Master Mode */
31 #define SI7020CMD_RH_HOLD	0xE5
32 /* Measure Temperature, Hold Master Mode */
33 #define SI7020CMD_TEMP_HOLD	0xE3
34 /* Software Reset */
35 #define SI7020CMD_RESET		0xFE
36 #define SI7020CMD_USR_WRITE	0xE6
37 /* "Heater Enabled" bit in the User Register */
38 #define SI7020_USR_HEATER_EN	BIT(2)
39 #define SI7020CMD_HEATER_WRITE	0x51
40 /* Heater current configuration bits */
41 #define SI7020_HEATER_VAL	GENMASK(3, 0)
42 
43 struct si7020_data {
44 	struct i2c_client *client;
45 	/* Lock for cached register values */
46 	struct mutex lock;
47 	u8 user_reg;
48 	u8 heater_reg;
49 };
50 
51 static const int si7020_heater_vals[] = { 0, 1, 0xF };
52 
si7020_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)53 static int si7020_read_raw(struct iio_dev *indio_dev,
54 			   struct iio_chan_spec const *chan, int *val,
55 			   int *val2, long mask)
56 {
57 	struct si7020_data *data = iio_priv(indio_dev);
58 	int ret;
59 
60 	switch (mask) {
61 	case IIO_CHAN_INFO_RAW:
62 		if (chan->type == IIO_CURRENT) {
63 			*val = data->heater_reg;
64 			return IIO_VAL_INT;
65 		}
66 
67 		ret = i2c_smbus_read_word_swapped(data->client,
68 						  chan->type == IIO_TEMP ?
69 						  SI7020CMD_TEMP_HOLD :
70 						  SI7020CMD_RH_HOLD);
71 		if (ret < 0)
72 			return ret;
73 		*val = ret >> 2;
74 		/*
75 		 * Humidity values can slightly exceed the 0-100%RH
76 		 * range and should be corrected by software
77 		 */
78 		if (chan->type == IIO_HUMIDITYRELATIVE)
79 			*val = clamp_val(*val, 786, 13893);
80 		return IIO_VAL_INT;
81 	case IIO_CHAN_INFO_SCALE:
82 		if (chan->type == IIO_TEMP)
83 			*val = 175720; /* = 175.72 * 1000 */
84 		else
85 			*val = 125 * 1000;
86 		*val2 = 65536 >> 2;
87 		return IIO_VAL_FRACTIONAL;
88 	case IIO_CHAN_INFO_OFFSET:
89 		/*
90 		 * Since iio_convert_raw_to_processed_unlocked assumes offset
91 		 * is an integer we have to round these values and lose
92 		 * accuracy.
93 		 * Relative humidity will be 0.0032959% too high and
94 		 * temperature will be 0.00277344 degrees too high.
95 		 * This is no big deal because it's within the accuracy of the
96 		 * sensor.
97 		 */
98 		if (chan->type == IIO_TEMP)
99 			*val = -4368; /* = -46.85 * (65536 >> 2) / 175.72 */
100 		else
101 			*val = -786; /* = -6 * (65536 >> 2) / 125 */
102 		return IIO_VAL_INT;
103 	default:
104 		break;
105 	}
106 
107 	return -EINVAL;
108 }
109 
110 static const struct iio_chan_spec si7020_channels[] = {
111 	{
112 		.type = IIO_HUMIDITYRELATIVE,
113 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
114 			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
115 	},
116 	{
117 		.type = IIO_TEMP,
118 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
119 			BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),
120 	},
121 	{
122 		.type = IIO_CURRENT,
123 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
124 		.info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
125 		.extend_name = "heater",
126 	}
127 };
128 
si7020_update_reg(struct si7020_data * data,u8 * reg,u8 cmd,u8 mask,u8 val)129 static int si7020_update_reg(struct si7020_data *data,
130 				u8 *reg, u8 cmd, u8 mask, u8 val)
131 {
132 	u8 new = (*reg & ~mask) | val;
133 	int ret;
134 
135 	ret = i2c_smbus_write_byte_data(data->client, cmd, new);
136 	if (ret)
137 		return ret;
138 
139 	*reg = new;
140 
141 	return 0;
142 }
143 
si7020_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)144 static int si7020_write_raw(struct iio_dev *indio_dev,
145 			     struct iio_chan_spec const *chan,
146 			     int val, int val2, long mask)
147 {
148 	struct si7020_data *data = iio_priv(indio_dev);
149 	int ret;
150 
151 	switch (mask) {
152 	case IIO_CHAN_INFO_RAW:
153 		if (chan->type != IIO_CURRENT || val2 != 0 ||
154 			val < si7020_heater_vals[0] || val > si7020_heater_vals[2])
155 			return -EINVAL;
156 
157 		scoped_guard(mutex, &data->lock)
158 			ret = si7020_update_reg(data, &data->heater_reg,
159 					SI7020CMD_HEATER_WRITE, SI7020_HEATER_VAL, val);
160 		return ret;
161 	default:
162 		return -EINVAL;
163 	}
164 }
165 
si7020_read_available(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)166 static int si7020_read_available(struct iio_dev *indio_dev,
167 				  struct iio_chan_spec const *chan,
168 				  const int **vals,
169 				  int *type, int *length, long mask)
170 {
171 	if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_CURRENT)
172 		return -EINVAL;
173 
174 	*vals = si7020_heater_vals;
175 	*type = IIO_VAL_INT;
176 
177 	return IIO_AVAIL_RANGE;
178 }
179 
si7020_show_heater_en(struct device * dev,struct device_attribute * attr,char * buf)180 static ssize_t si7020_show_heater_en(struct device *dev,
181 				 struct device_attribute *attr, char *buf)
182 {
183 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
184 	struct si7020_data *data = iio_priv(indio_dev);
185 
186 	return sysfs_emit(buf, "%d\n", !!(data->user_reg & SI7020_USR_HEATER_EN));
187 }
188 
si7020_store_heater_en(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)189 static ssize_t si7020_store_heater_en(struct device *dev,
190 				  struct device_attribute *attr,
191 				  const char *buf, size_t len)
192 {
193 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
194 	struct si7020_data *data = iio_priv(indio_dev);
195 	int ret;
196 	bool val;
197 
198 	ret = kstrtobool(buf, &val);
199 	if (ret)
200 		return ret;
201 
202 	scoped_guard(mutex, &data->lock)
203 		ret = si7020_update_reg(data, &data->user_reg, SI7020CMD_USR_WRITE,
204 				SI7020_USR_HEATER_EN, val ? SI7020_USR_HEATER_EN : 0);
205 
206 	return ret < 0 ? ret : len;
207 }
208 
209 static IIO_DEVICE_ATTR(heater_enable, 0644,
210 		       si7020_show_heater_en, si7020_store_heater_en, 0);
211 
212 static struct attribute *si7020_attributes[] = {
213 	&iio_dev_attr_heater_enable.dev_attr.attr,
214 	NULL
215 };
216 
217 static const struct attribute_group si7020_attribute_group = {
218 	.attrs = si7020_attributes,
219 };
220 
221 static const struct iio_info si7020_info = {
222 	.read_raw = si7020_read_raw,
223 	.write_raw = si7020_write_raw,
224 	.read_avail = si7020_read_available,
225 	.attrs = &si7020_attribute_group,
226 };
227 
si7020_probe(struct i2c_client * client)228 static int si7020_probe(struct i2c_client *client)
229 {
230 	struct iio_dev *indio_dev;
231 	struct si7020_data *data;
232 	int ret;
233 
234 	if (!i2c_check_functionality(client->adapter,
235 				     I2C_FUNC_SMBUS_WRITE_BYTE |
236 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
237 		return -EOPNOTSUPP;
238 
239 	/* Reset device, loads default settings. */
240 	ret = i2c_smbus_write_byte(client, SI7020CMD_RESET);
241 	if (ret < 0)
242 		return ret;
243 	/* Wait the maximum power-up time after software reset. */
244 	msleep(15);
245 
246 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
247 	if (!indio_dev)
248 		return -ENOMEM;
249 
250 	data = iio_priv(indio_dev);
251 	i2c_set_clientdata(client, indio_dev);
252 	data->client = client;
253 	mutex_init(&data->lock);
254 
255 	indio_dev->name = dev_name(&client->dev);
256 	indio_dev->modes = INDIO_DIRECT_MODE;
257 	indio_dev->info = &si7020_info;
258 	indio_dev->channels = si7020_channels;
259 	indio_dev->num_channels = ARRAY_SIZE(si7020_channels);
260 
261 	/* All the "reserved" bits in the User Register are 1s by default */
262 	data->user_reg = 0x3A;
263 	data->heater_reg = 0x0;
264 
265 	return devm_iio_device_register(&client->dev, indio_dev);
266 }
267 
268 static const struct i2c_device_id si7020_id[] = {
269 	{ .name = "si7020" },
270 	{ .name = "th06" },
271 	{ }
272 };
273 MODULE_DEVICE_TABLE(i2c, si7020_id);
274 
275 static const struct of_device_id si7020_dt_ids[] = {
276 	{ .compatible = "silabs,si7020" },
277 	{ }
278 };
279 MODULE_DEVICE_TABLE(of, si7020_dt_ids);
280 
281 static struct i2c_driver si7020_driver = {
282 	.driver = {
283 		.name = "si7020",
284 		.of_match_table = si7020_dt_ids,
285 	},
286 	.probe		= si7020_probe,
287 	.id_table	= si7020_id,
288 };
289 
290 module_i2c_driver(si7020_driver);
291 MODULE_DESCRIPTION("Silicon Labs Si7013/20/21 Relative Humidity and Temperature Sensors");
292 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
293 MODULE_LICENSE("GPL");
294