1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * The driver for Measurement Specialties HTU31 Temperature and Humidity sensor.
4  *
5  * Copyright (C) 2025
6  * Author: Andrei Lalaev <andrey.lalaev@gmail.com>
7  */
8 
9 #include <linux/array_size.h>
10 #include <linux/cleanup.h>
11 #include <linux/crc8.h>
12 #include <linux/debugfs.h>
13 #include <linux/delay.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 
20 #define HTU31_READ_TEMP_HUM_CMD	0x00
21 #define HTU31_READ_SERIAL_CMD		0x0a
22 #define HTU31_CONVERSION_CMD		0x5e
23 #define HTU31_HEATER_OFF_CMD		0x02
24 #define HTU31_HEATER_ON_CMD		0x04
25 
26 #define HTU31_TEMP_HUM_LEN		6
27 
28 /* Conversion time for the highest resolution */
29 #define HTU31_HUMIDITY_CONV_TIME	10000 /* us */
30 #define HTU31_TEMPERATURE_CONV_TIME	15000 /* us */
31 
32 #define HTU31_SERIAL_NUMBER_LEN	3
33 #define HTU31_SERIAL_NUMBER_CRC_LEN	1
34 #define HTU31_SERIAL_NUMBER_CRC_OFFSET	3
35 
36 #define HTU31_CRC8_INIT_VAL		0
37 #define HTU31_CRC8_POLYNOMIAL		0x31
38 DECLARE_CRC8_TABLE(htu31_crc8_table);
39 
40 /**
41  * struct htu31_data - all the data required to operate a HTU31 chip
42  * @client: the i2c client associated with the HTU31
43  * @lock: a mutex to prevent parallel access to the data
44  * @wait_time: the time needed by sensor to convert values
45  * @temperature: the latest temperature value in millidegrees
46  * @humidity: the latest relative humidity value in millipercent
47  * @serial_number: the serial number of the sensor
48  * @heater_enable: the internal state of the heater
49  */
50 struct htu31_data {
51 	struct i2c_client *client;
52 	struct mutex lock; /* Used to protect against parallel data updates */
53 	long wait_time;
54 	long temperature;
55 	long humidity;
56 	u8 serial_number[HTU31_SERIAL_NUMBER_LEN];
57 	bool heater_enable;
58 };
59 
htu31_temp_to_millicelsius(u16 val)60 static long htu31_temp_to_millicelsius(u16 val)
61 {
62 	return -40000 + DIV_ROUND_CLOSEST_ULL(165000ULL * val, 65535);
63 }
64 
htu31_relative_humidity(u16 val)65 static long htu31_relative_humidity(u16 val)
66 {
67 	return DIV_ROUND_CLOSEST_ULL(100000ULL * val, 65535);
68 }
69 
htu31_data_fetch_command(struct htu31_data * data)70 static int htu31_data_fetch_command(struct htu31_data *data)
71 {
72 	struct i2c_client *client = data->client;
73 	u8 conversion_on = HTU31_CONVERSION_CMD;
74 	u8 read_data_cmd = HTU31_READ_TEMP_HUM_CMD;
75 	u8 t_h_buf[HTU31_TEMP_HUM_LEN] = {};
76 	struct i2c_msg msgs[] = {
77 		{
78 			.addr = client->addr,
79 			.flags = 0,
80 			.len = 1,
81 			.buf = &read_data_cmd,
82 		},
83 		{
84 			.addr = client->addr,
85 			.flags = I2C_M_RD,
86 			.len = sizeof(t_h_buf),
87 			.buf = t_h_buf,
88 		},
89 	};
90 	int ret;
91 	u8 crc;
92 
93 	guard(mutex)(&data->lock);
94 
95 	ret = i2c_master_send(client, &conversion_on, 1);
96 	if (ret != 1) {
97 		ret = ret < 0 ? ret : -EIO;
98 		dev_err(&client->dev,
99 			"Conversion command is failed. Error code: %d\n", ret);
100 		return ret;
101 	}
102 
103 	fsleep(data->wait_time);
104 
105 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
106 	if (ret != ARRAY_SIZE(msgs)) {
107 		ret = ret < 0 ? ret : -EIO;
108 		dev_err(&client->dev,
109 			"T&H command is failed. Error code: %d\n", ret);
110 		return ret;
111 	}
112 
113 	crc = crc8(htu31_crc8_table, &t_h_buf[0], 2, HTU31_CRC8_INIT_VAL);
114 	if (crc != t_h_buf[2]) {
115 		dev_err(&client->dev, "Temperature CRC mismatch\n");
116 		return -EIO;
117 	}
118 
119 	crc = crc8(htu31_crc8_table, &t_h_buf[3], 2, HTU31_CRC8_INIT_VAL);
120 	if (crc != t_h_buf[5]) {
121 		dev_err(&client->dev, "Humidity CRC mismatch\n");
122 		return -EIO;
123 	}
124 
125 	data->temperature = htu31_temp_to_millicelsius(be16_to_cpup((__be16 *)&t_h_buf[0]));
126 	data->humidity = htu31_relative_humidity(be16_to_cpup((__be16 *)&t_h_buf[3]));
127 
128 	return 0;
129 }
130 
htu31_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)131 static umode_t htu31_is_visible(const void *data, enum hwmon_sensor_types type,
132 				u32 attr, int channel)
133 {
134 	switch (type) {
135 	case hwmon_temp:
136 	case hwmon_humidity:
137 		return 0444;
138 	default:
139 		return 0;
140 	}
141 }
142 
htu31_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)143 static int htu31_read(struct device *dev, enum hwmon_sensor_types type,
144 		      u32 attr, int channel, long *val)
145 {
146 	struct htu31_data *data = dev_get_drvdata(dev);
147 	int ret;
148 
149 	ret = htu31_data_fetch_command(data);
150 	if (ret < 0)
151 		return ret;
152 
153 	switch (type) {
154 	case hwmon_temp:
155 		if (attr != hwmon_temp_input)
156 			return -EINVAL;
157 
158 		*val = data->temperature;
159 		break;
160 	case hwmon_humidity:
161 		if (attr != hwmon_humidity_input)
162 			return -EINVAL;
163 
164 		*val = data->humidity;
165 		break;
166 	default:
167 		return -EOPNOTSUPP;
168 	}
169 
170 	return 0;
171 }
172 
htu31_read_serial_number(struct htu31_data * data)173 static int htu31_read_serial_number(struct htu31_data *data)
174 {
175 	struct i2c_client *client = data->client;
176 	u8 read_sn_cmd = HTU31_READ_SERIAL_CMD;
177 	u8 sn_buf[HTU31_SERIAL_NUMBER_LEN + HTU31_SERIAL_NUMBER_CRC_LEN];
178 	struct i2c_msg msgs[] = {
179 		{
180 			.addr = client->addr,
181 			.flags = 0,
182 			.len = 1,
183 			.buf = &read_sn_cmd,
184 		},
185 		{
186 			.addr = client->addr,
187 			.flags = I2C_M_RD,
188 			.len = sizeof(sn_buf),
189 			.buf = sn_buf,
190 		},
191 	};
192 	int ret;
193 	u8 crc;
194 
195 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
196 	if (ret < 0)
197 		return ret;
198 
199 	crc = crc8(htu31_crc8_table, sn_buf, HTU31_SERIAL_NUMBER_LEN, HTU31_CRC8_INIT_VAL);
200 	if (crc != sn_buf[HTU31_SERIAL_NUMBER_CRC_OFFSET]) {
201 		dev_err(&client->dev, "Serial number CRC mismatch\n");
202 		return -EIO;
203 	}
204 
205 	memcpy(data->serial_number, sn_buf, HTU31_SERIAL_NUMBER_LEN);
206 
207 	return 0;
208 }
209 
heater_enable_show(struct device * dev,struct device_attribute * attr,char * buf)210 static ssize_t heater_enable_show(struct device *dev,
211 				  struct device_attribute *attr,
212 				  char *buf)
213 {
214 	struct htu31_data *data = dev_get_drvdata(dev);
215 
216 	return sysfs_emit(buf, "%d\n", data->heater_enable);
217 }
218 
heater_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)219 static ssize_t heater_enable_store(struct device *dev,
220 				   struct device_attribute *attr,
221 				   const char *buf,
222 				   size_t count)
223 {
224 	struct htu31_data *data = dev_get_drvdata(dev);
225 	u8 heater_cmd;
226 	bool status;
227 	int ret;
228 
229 	ret = kstrtobool(buf, &status);
230 	if (ret)
231 		return ret;
232 
233 	heater_cmd = status ? HTU31_HEATER_ON_CMD : HTU31_HEATER_OFF_CMD;
234 
235 	guard(mutex)(&data->lock);
236 
237 	ret = i2c_master_send(data->client, &heater_cmd, 1);
238 	if (ret < 0)
239 		return ret;
240 
241 	data->heater_enable = status;
242 
243 	return count;
244 }
245 
246 static DEVICE_ATTR_RW(heater_enable);
247 
serial_number_show(struct seq_file * seq_file,void * unused)248 static int serial_number_show(struct seq_file *seq_file,
249 			      void *unused)
250 {
251 	struct htu31_data *data = seq_file->private;
252 
253 	seq_printf(seq_file, "%X%X%X\n", data->serial_number[0],
254 		   data->serial_number[1], data->serial_number[2]);
255 	return 0;
256 }
257 
258 DEFINE_SHOW_ATTRIBUTE(serial_number);
259 
260 static struct attribute *htu31_attrs[] = {
261 	&dev_attr_heater_enable.attr,
262 	NULL
263 };
264 
265 ATTRIBUTE_GROUPS(htu31);
266 
267 static const struct hwmon_channel_info * const htu31_info[] = {
268 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
269 	HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
270 	NULL
271 };
272 
273 static const struct hwmon_ops htu31_hwmon_ops = {
274 	.is_visible = htu31_is_visible,
275 	.read = htu31_read,
276 };
277 
278 static const struct hwmon_chip_info htu31_chip_info = {
279 	.info = htu31_info,
280 	.ops = &htu31_hwmon_ops,
281 };
282 
htu31_probe(struct i2c_client * client)283 static int htu31_probe(struct i2c_client *client)
284 {
285 	struct device *dev = &client->dev;
286 	struct device *hwmon_dev;
287 	struct htu31_data *data;
288 	int ret;
289 
290 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
291 	if (!data)
292 		return -ENOMEM;
293 
294 	data->client = client;
295 	data->wait_time = HTU31_TEMPERATURE_CONV_TIME + HTU31_HUMIDITY_CONV_TIME;
296 
297 	ret = devm_mutex_init(dev, &data->lock);
298 	if (ret)
299 		return ret;
300 
301 	crc8_populate_msb(htu31_crc8_table, HTU31_CRC8_POLYNOMIAL);
302 
303 	ret = htu31_read_serial_number(data);
304 	if (ret) {
305 		dev_err(dev, "Failed to read serial number\n");
306 		return ret;
307 	}
308 
309 	debugfs_create_file("serial_number",
310 			    0444,
311 			    client->debugfs,
312 			    data,
313 			    &serial_number_fops);
314 
315 	hwmon_dev = devm_hwmon_device_register_with_info(dev,
316 							 client->name,
317 							 data,
318 							 &htu31_chip_info,
319 							 htu31_groups);
320 
321 	return PTR_ERR_OR_ZERO(hwmon_dev);
322 }
323 
324 static const struct i2c_device_id htu31_id[] = {
325 	{ "htu31" },
326 	{ }
327 };
328 MODULE_DEVICE_TABLE(i2c, htu31_id);
329 
330 #if IS_ENABLED(CONFIG_OF)
331 static const struct of_device_id htu31_of_match[] = {
332 	{ .compatible = "meas,htu31" },
333 	{ }
334 };
335 MODULE_DEVICE_TABLE(of, htu31_of_match);
336 #endif
337 
338 static struct i2c_driver htu31_driver = {
339 	.driver = {
340 		.name = "htu31",
341 		.of_match_table = of_match_ptr(htu31_of_match),
342 	},
343 	.probe = htu31_probe,
344 	.id_table = htu31_id,
345 };
346 module_i2c_driver(htu31_driver);
347 
348 MODULE_AUTHOR("Andrei Lalaev <andrey.lalaev@gmail.com>");
349 MODULE_DESCRIPTION("HTU31 Temperature and Humidity sensor driver");
350 MODULE_LICENSE("GPL");
351