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 */
sht4x_read_values(struct sht4x_data * data)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
sht4x_interval_write(struct sht4x_data * data,long val)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 */
sht4x_interval_read(struct sht4x_data * data,long * val)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 */
sht4x_temperature1_read(struct sht4x_data * data,long * val)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 */
sht4x_humidity1_read(struct sht4x_data * data,long * val)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
sht4x_hwmon_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)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
sht4x_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)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
sht4x_hwmon_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)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
heater_enable_show(struct device * dev,struct device_attribute * attr,char * buf)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
heater_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 guard(hwmon_lock)(dev);
281
282 if (time_before(jiffies, data->heating_complete))
283 return -EBUSY;
284
285 ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
286 if (ret < 0)
287 return ret;
288
289 data->heating_complete = jiffies + msecs_to_jiffies(heating_time_bound);
290 data->data_pending = true;
291 return count;
292 }
293
heater_power_show(struct device * dev,struct device_attribute * attr,char * buf)294 static ssize_t heater_power_show(struct device *dev,
295 struct device_attribute *attr,
296 char *buf)
297 {
298 struct sht4x_data *data = dev_get_drvdata(dev);
299
300 return sysfs_emit(buf, "%u\n", data->heater_power);
301 }
302
heater_power_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)303 static ssize_t heater_power_store(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf,
306 size_t count)
307 {
308 struct sht4x_data *data = dev_get_drvdata(dev);
309 u32 power;
310 ssize_t ret;
311
312 ret = kstrtou32(buf, 10, &power);
313 if (ret)
314 return ret;
315
316 if (power != 20 && power != 110 && power != 200)
317 return -EINVAL;
318
319 guard(hwmon_lock)(dev);
320
321 data->heater_power = power;
322
323 return count;
324 }
325
heater_time_show(struct device * dev,struct device_attribute * attr,char * buf)326 static ssize_t heater_time_show(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329 {
330 struct sht4x_data *data = dev_get_drvdata(dev);
331
332 return sysfs_emit(buf, "%u\n", data->heater_time);
333 }
334
heater_time_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)335 static ssize_t heater_time_store(struct device *dev,
336 struct device_attribute *attr,
337 const char *buf,
338 size_t count)
339 {
340 struct sht4x_data *data = dev_get_drvdata(dev);
341 u32 time;
342 ssize_t ret;
343
344 ret = kstrtou32(buf, 10, &time);
345 if (ret)
346 return ret;
347
348 if (time != 100 && time != 1000)
349 return -EINVAL;
350
351 guard(hwmon_lock)(dev);
352
353 data->heater_time = time;
354
355 return count;
356 }
357
358 static DEVICE_ATTR_RW(heater_enable);
359 static DEVICE_ATTR_RW(heater_power);
360 static DEVICE_ATTR_RW(heater_time);
361
362 static struct attribute *sht4x_attrs[] = {
363 &dev_attr_heater_enable.attr,
364 &dev_attr_heater_power.attr,
365 &dev_attr_heater_time.attr,
366 NULL
367 };
368
369 ATTRIBUTE_GROUPS(sht4x);
370
371 static const struct hwmon_channel_info * const sht4x_info[] = {
372 HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
373 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
374 HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
375 NULL,
376 };
377
378 static const struct hwmon_ops sht4x_hwmon_ops = {
379 .is_visible = sht4x_hwmon_visible,
380 .read = sht4x_hwmon_read,
381 .write = sht4x_hwmon_write,
382 };
383
384 static const struct hwmon_chip_info sht4x_chip_info = {
385 .ops = &sht4x_hwmon_ops,
386 .info = sht4x_info,
387 };
388
sht4x_probe(struct i2c_client * client)389 static int sht4x_probe(struct i2c_client *client)
390 {
391 struct device *device = &client->dev;
392 struct device *hwmon_dev;
393 struct sht4x_data *data;
394 u8 cmd[] = {SHT4X_CMD_RESET};
395 int ret;
396
397 /*
398 * we require full i2c support since the sht4x uses multi-byte read and
399 * writes as well as multi-byte commands which are not supported by
400 * the smbus protocol
401 */
402 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
403 return -EOPNOTSUPP;
404
405 data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
406 if (!data)
407 return -ENOMEM;
408
409 data->update_interval = SHT4X_MIN_POLL_INTERVAL;
410 data->client = client;
411 data->heater_power = 200;
412 data->heater_time = 1000;
413 data->heating_complete = jiffies;
414
415 crc8_populate_msb(sht4x_crc8_table, SHT4X_CRC8_POLYNOMIAL);
416
417 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
418 if (ret < 0)
419 return ret;
420 if (ret != SHT4X_CMD_LEN)
421 return -EIO;
422
423 hwmon_dev = devm_hwmon_device_register_with_info(device,
424 client->name,
425 data,
426 &sht4x_chip_info,
427 sht4x_groups);
428
429 return PTR_ERR_OR_ZERO(hwmon_dev);
430 }
431
432 static const struct i2c_device_id sht4x_id[] = {
433 { .name = "sht4x" },
434 { }
435 };
436 MODULE_DEVICE_TABLE(i2c, sht4x_id);
437
438 static const struct of_device_id sht4x_of_match[] = {
439 { .compatible = "sensirion,sht4x" },
440 { }
441 };
442 MODULE_DEVICE_TABLE(of, sht4x_of_match);
443
444 static struct i2c_driver sht4x_driver = {
445 .driver = {
446 .name = "sht4x",
447 .of_match_table = sht4x_of_match,
448 },
449 .probe = sht4x_probe,
450 .id_table = sht4x_id,
451 };
452
453 module_i2c_driver(sht4x_driver);
454
455 MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>");
456 MODULE_DESCRIPTION("Sensirion SHT4x humidity and temperature sensor driver");
457 MODULE_LICENSE("GPL v2");
458