xref: /linux/drivers/hwmon/sht4x.c (revision 2cddfc2e8fc78c13b0f5286ea5dd48cdf527ad41)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
4  * Copyright (c) Linumiz 2021
5  *
6  * sht4x.c - Linux hwmon driver for SHT4x Temperature and Humidity sensor
7  *
8  * Author: Navin Sankar Velliangiri <navin@linumiz.com>
9  */
10 
11 #include <linux/crc8.h>
12 #include <linux/delay.h>
13 #include <linux/hwmon.h>
14 #include <linux/hwmon-sysfs.h>
15 #include <linux/i2c.h>
16 #include <linux/jiffies.h>
17 #include <linux/module.h>
18 
19 /*
20  * Poll intervals (in milliseconds)
21  */
22 #define SHT4X_MIN_POLL_INTERVAL	2000
23 
24 /*
25  * I2C command delays (in microseconds)
26  */
27 #define SHT4X_MEAS_DELAY_HPM	8200	/* see t_MEAS,h in datasheet */
28 #define SHT4X_DELAY_EXTRA	10000
29 
30 /*
31  * Command Bytes
32  */
33 #define SHT4X_CMD_MEASURE_HPM	0b11111101
34 #define SHT4X_CMD_RESET		0b10010100
35 #define SHT4X_CMD_HEATER_20_1	0b00011110
36 #define SHT4X_CMD_HEATER_20_01	0b00010101
37 #define SHT4X_CMD_HEATER_110_1	0b00101111
38 #define SHT4X_CMD_HEATER_110_01	0b00100100
39 #define SHT4X_CMD_HEATER_200_1	0b00111001
40 #define SHT4X_CMD_HEATER_200_01 0b00110010
41 
42 #define SHT4X_CMD_LEN		1
43 #define SHT4X_CRC8_LEN		1
44 #define SHT4X_WORD_LEN		2
45 #define SHT4X_RESPONSE_LENGTH	6
46 #define SHT4X_CRC8_POLYNOMIAL	0x31
47 #define SHT4X_CRC8_INIT		0xff
48 #define SHT4X_MIN_TEMPERATURE	-45000
49 #define SHT4X_MAX_TEMPERATURE	125000
50 #define SHT4X_MIN_HUMIDITY	0
51 #define SHT4X_MAX_HUMIDITY	100000
52 
53 DECLARE_CRC8_TABLE(sht4x_crc8_table);
54 
55 /**
56  * struct sht4x_data - All the data required to operate an SHT4X chip
57  * @client: the i2c client associated with the SHT4X
58  * @heating_complete: the time that the last heating finished
59  * @data_pending: true if and only if there are measurements to retrieve after heating
60  * @heater_power: the power at which the heater will be started
61  * @heater_time: the time for which the heater will remain turned on
62  * @valid: validity of fields below
63  * @update_interval: the minimum poll interval
64  * @last_updated: the previous time that the SHT4X was polled
65  * @temperature: the latest temperature value received from the SHT4X
66  * @humidity: the latest humidity value received from the SHT4X
67  */
68 struct sht4x_data {
69 	struct i2c_client	*client;
70 	unsigned long		heating_complete;	/* in jiffies */
71 	bool			data_pending;
72 	u32			heater_power;	/* in milli-watts */
73 	u32			heater_time;	/* in milli-seconds */
74 	bool			valid;	/* validity of fields below */
75 	long			update_interval;	/* in milli-seconds */
76 	long			last_updated;	/* in jiffies */
77 	s32			temperature;
78 	s32			humidity;
79 };
80 
81 /**
82  * sht4x_read_values() - read and parse the raw data from the SHT4X
83  * @data: the struct sht4x_data to use for the lock
84  * Return: 0 if successful, -ERRNO if not
85  */
86 static int sht4x_read_values(struct sht4x_data *data)
87 {
88 	int ret;
89 	u16 t_ticks, rh_ticks;
90 	unsigned long next_update;
91 	struct i2c_client *client = data->client;
92 	u8 crc;
93 	u8 cmd[SHT4X_CMD_LEN] = {SHT4X_CMD_MEASURE_HPM};
94 	u8 raw_data[SHT4X_RESPONSE_LENGTH];
95 	unsigned long curr_jiffies;
96 
97 	curr_jiffies = jiffies;
98 	if (time_before(curr_jiffies, data->heating_complete))
99 		msleep(jiffies_to_msecs(data->heating_complete - curr_jiffies));
100 
101 	if (data->data_pending &&
102 	    time_before(jiffies, data->heating_complete + data->update_interval)) {
103 		data->data_pending = false;
104 	} else {
105 		next_update = data->last_updated +
106 			msecs_to_jiffies(data->update_interval);
107 
108 		if (data->valid && time_before_eq(jiffies, next_update))
109 			return 0;
110 
111 		ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
112 		if (ret < 0)
113 			return ret;
114 
115 		usleep_range(SHT4X_MEAS_DELAY_HPM, SHT4X_MEAS_DELAY_HPM + SHT4X_DELAY_EXTRA);
116 	}
117 
118 	ret = i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH);
119 	if (ret != SHT4X_RESPONSE_LENGTH) {
120 		if (ret >= 0)
121 			ret = -ENODATA;
122 		return ret;
123 	}
124 
125 	t_ticks = raw_data[0] << 8 | raw_data[1];
126 	rh_ticks = raw_data[3] << 8 | raw_data[4];
127 
128 	crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
129 	if (crc != raw_data[2]) {
130 		dev_err(&client->dev, "data integrity check failed\n");
131 		return -EIO;
132 	}
133 
134 	crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
135 	if (crc != raw_data[5]) {
136 		dev_err(&client->dev, "data integrity check failed\n");
137 		return -EIO;
138 	}
139 
140 	data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000;
141 	data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000;
142 	data->last_updated = jiffies;
143 	data->valid = true;
144 	return 0;
145 }
146 
147 static ssize_t sht4x_interval_write(struct sht4x_data *data, long val)
148 {
149 	data->update_interval = clamp_val(val, SHT4X_MIN_POLL_INTERVAL, INT_MAX);
150 
151 	return 0;
152 }
153 
154 /* sht4x_interval_read() - read the minimum poll interval in milliseconds */
155 static size_t sht4x_interval_read(struct sht4x_data *data, long *val)
156 {
157 	*val = data->update_interval;
158 	return 0;
159 }
160 
161 /* sht4x_temperature1_read() - read the temperature in millidegrees */
162 static int sht4x_temperature1_read(struct sht4x_data *data, long *val)
163 {
164 	int ret;
165 
166 	ret = sht4x_read_values(data);
167 	if (ret < 0)
168 		return ret;
169 
170 	*val = data->temperature;
171 
172 	return 0;
173 }
174 
175 /* sht4x_humidity1_read() - read a relative humidity in millipercent */
176 static int sht4x_humidity1_read(struct sht4x_data *data, long *val)
177 {
178 	int ret;
179 
180 	ret = sht4x_read_values(data);
181 	if (ret < 0)
182 		return ret;
183 
184 	*val = data->humidity;
185 
186 	return 0;
187 }
188 
189 static umode_t sht4x_hwmon_visible(const void *data,
190 				   enum hwmon_sensor_types type,
191 				   u32 attr, int channel)
192 {
193 	switch (type) {
194 	case hwmon_temp:
195 	case hwmon_humidity:
196 		return 0444;
197 	case hwmon_chip:
198 		return 0644;
199 	default:
200 		return 0;
201 	}
202 }
203 
204 static int sht4x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
205 			    u32 attr, int channel, long *val)
206 {
207 	struct sht4x_data *data = dev_get_drvdata(dev);
208 
209 	switch (type) {
210 	case hwmon_temp:
211 		return sht4x_temperature1_read(data, val);
212 	case hwmon_humidity:
213 		return sht4x_humidity1_read(data, val);
214 	case hwmon_chip:
215 		return sht4x_interval_read(data, val);
216 	default:
217 		return -EOPNOTSUPP;
218 	}
219 }
220 
221 static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
222 			     u32 attr, int channel, long val)
223 {
224 	struct sht4x_data *data = dev_get_drvdata(dev);
225 
226 	switch (type) {
227 	case hwmon_chip:
228 		return sht4x_interval_write(data, val);
229 	default:
230 		return -EOPNOTSUPP;
231 	}
232 }
233 
234 static ssize_t heater_enable_show(struct device *dev,
235 				  struct device_attribute *attr,
236 				  char *buf)
237 {
238 	struct sht4x_data *data = dev_get_drvdata(dev);
239 
240 	return sysfs_emit(buf, "%u\n", time_before(jiffies, data->heating_complete));
241 }
242 
243 static ssize_t heater_enable_store(struct device *dev,
244 				   struct device_attribute *attr,
245 				   const char *buf,
246 				   size_t count)
247 {
248 	struct sht4x_data *data = dev_get_drvdata(dev);
249 	bool status;
250 	ssize_t ret;
251 	u8 cmd;
252 	u32 heating_time_bound;
253 
254 	ret = kstrtobool(buf, &status);
255 	if (ret)
256 		return ret;
257 	if (!status)
258 		return -EINVAL;
259 
260 	if (data->heater_time == 100) {
261 		if (data->heater_power == 20)
262 			cmd = SHT4X_CMD_HEATER_20_01;
263 		else if (data->heater_power == 110)
264 			cmd = SHT4X_CMD_HEATER_110_01;
265 		else /* data->heater_power == 200 */
266 			cmd = SHT4X_CMD_HEATER_200_01;
267 
268 		heating_time_bound = 110;
269 	} else { /* data->heater_time == 1000 */
270 		if (data->heater_power == 20)
271 			cmd = SHT4X_CMD_HEATER_20_1;
272 		else if (data->heater_power == 110)
273 			cmd = SHT4X_CMD_HEATER_110_1;
274 		else /* data->heater_power == 200 */
275 			cmd = SHT4X_CMD_HEATER_200_1;
276 
277 		heating_time_bound = 1100;
278 	}
279 
280 	if (time_before(jiffies, data->heating_complete))
281 		return -EBUSY;
282 
283 	ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
284 	if (ret < 0)
285 		return ret;
286 
287 	data->heating_complete = jiffies + msecs_to_jiffies(heating_time_bound);
288 	data->data_pending = true;
289 	return 0;
290 }
291 
292 static ssize_t heater_power_show(struct device *dev,
293 				 struct device_attribute *attr,
294 				 char *buf)
295 {
296 	struct sht4x_data *data = dev_get_drvdata(dev);
297 
298 	return sysfs_emit(buf, "%u\n", data->heater_power);
299 }
300 
301 static ssize_t heater_power_store(struct device *dev,
302 				  struct device_attribute *attr,
303 				  const char *buf,
304 				  size_t count)
305 {
306 	struct sht4x_data *data = dev_get_drvdata(dev);
307 	u32 power;
308 	ssize_t ret;
309 
310 	ret = kstrtou32(buf, 10, &power);
311 	if (ret)
312 		return ret;
313 
314 	if (power != 20 && power != 110 && power != 200)
315 		return -EINVAL;
316 
317 	data->heater_power = power;
318 
319 	return count;
320 }
321 
322 static ssize_t heater_time_show(struct device *dev,
323 				struct device_attribute *attr,
324 				char *buf)
325 {
326 	struct sht4x_data *data = dev_get_drvdata(dev);
327 
328 	return sysfs_emit(buf, "%u\n", data->heater_time);
329 }
330 
331 static ssize_t heater_time_store(struct device *dev,
332 				 struct device_attribute *attr,
333 				 const char *buf,
334 				 size_t count)
335 {
336 	struct sht4x_data *data = dev_get_drvdata(dev);
337 	u32 time;
338 	ssize_t ret;
339 
340 	ret = kstrtou32(buf, 10, &time);
341 	if (ret)
342 		return ret;
343 
344 	if (time != 100 && time != 1000)
345 		return -EINVAL;
346 
347 	data->heater_time = time;
348 
349 	return count;
350 }
351 
352 static DEVICE_ATTR_RW(heater_enable);
353 static DEVICE_ATTR_RW(heater_power);
354 static DEVICE_ATTR_RW(heater_time);
355 
356 static struct attribute *sht4x_attrs[] = {
357 	&dev_attr_heater_enable.attr,
358 	&dev_attr_heater_power.attr,
359 	&dev_attr_heater_time.attr,
360 	NULL
361 };
362 
363 ATTRIBUTE_GROUPS(sht4x);
364 
365 static const struct hwmon_channel_info * const sht4x_info[] = {
366 	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
367 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
368 	HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
369 	NULL,
370 };
371 
372 static const struct hwmon_ops sht4x_hwmon_ops = {
373 	.is_visible = sht4x_hwmon_visible,
374 	.read = sht4x_hwmon_read,
375 	.write = sht4x_hwmon_write,
376 };
377 
378 static const struct hwmon_chip_info sht4x_chip_info = {
379 	.ops = &sht4x_hwmon_ops,
380 	.info = sht4x_info,
381 };
382 
383 static int sht4x_probe(struct i2c_client *client)
384 {
385 	struct device *device = &client->dev;
386 	struct device *hwmon_dev;
387 	struct sht4x_data *data;
388 	u8 cmd[] = {SHT4X_CMD_RESET};
389 	int ret;
390 
391 	/*
392 	 * we require full i2c support since the sht4x uses multi-byte read and
393 	 * writes as well as multi-byte commands which are not supported by
394 	 * the smbus protocol
395 	 */
396 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
397 		return -EOPNOTSUPP;
398 
399 	data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
400 	if (!data)
401 		return -ENOMEM;
402 
403 	data->update_interval = SHT4X_MIN_POLL_INTERVAL;
404 	data->client = client;
405 	data->heater_power = 200;
406 	data->heater_time = 1000;
407 	data->heating_complete = jiffies;
408 
409 	crc8_populate_msb(sht4x_crc8_table, SHT4X_CRC8_POLYNOMIAL);
410 
411 	ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
412 	if (ret < 0)
413 		return ret;
414 	if (ret != SHT4X_CMD_LEN)
415 		return -EIO;
416 
417 	hwmon_dev = devm_hwmon_device_register_with_info(device,
418 							 client->name,
419 							 data,
420 							 &sht4x_chip_info,
421 							 sht4x_groups);
422 
423 	return PTR_ERR_OR_ZERO(hwmon_dev);
424 }
425 
426 static const struct i2c_device_id sht4x_id[] = {
427 	{ "sht4x" },
428 	{ },
429 };
430 MODULE_DEVICE_TABLE(i2c, sht4x_id);
431 
432 static const struct of_device_id sht4x_of_match[] = {
433 	{ .compatible = "sensirion,sht4x" },
434 	{ }
435 };
436 MODULE_DEVICE_TABLE(of, sht4x_of_match);
437 
438 static struct i2c_driver sht4x_driver = {
439 	.driver = {
440 		.name = "sht4x",
441 		.of_match_table = sht4x_of_match,
442 	},
443 	.probe		= sht4x_probe,
444 	.id_table	= sht4x_id,
445 };
446 
447 module_i2c_driver(sht4x_driver);
448 
449 MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>");
450 MODULE_DESCRIPTION("Sensirion SHT4x humidity and temperature sensor driver");
451 MODULE_LICENSE("GPL v2");
452