xref: /linux/drivers/hwmon/sht3x.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Sensirion SHT3x-DIS humidity and temperature sensor driver.
3  * The SHT3x comes in many different versions, this driver is for the
4  * I2C version only.
5  *
6  * Copyright (C) 2016 Sensirion AG, Switzerland
7  * Author: David Frey <david.frey@sensirion.com>
8  * Author: Pascal Sachs <pascal.sachs@sensirion.com>
9  */
10 
11 #include <asm/page.h>
12 #include <linux/crc8.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/i2c.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/unaligned.h>
25 
26 /* commands (high repeatability mode) */
27 static const unsigned char sht3x_cmd_measure_single_hpm[] = { 0x24, 0x00 };
28 
29 /* commands (medium repeatability mode) */
30 static const unsigned char sht3x_cmd_measure_single_mpm[] = { 0x24, 0x0b };
31 
32 /* commands (low repeatability mode) */
33 static const unsigned char sht3x_cmd_measure_single_lpm[] = { 0x24, 0x16 };
34 
35 /* commands for periodic mode */
36 static const unsigned char sht3x_cmd_measure_periodic_mode[]   = { 0xe0, 0x00 };
37 static const unsigned char sht3x_cmd_break[]                   = { 0x30, 0x93 };
38 
39 /* commands for heater control */
40 static const unsigned char sht3x_cmd_heater_on[]               = { 0x30, 0x6d };
41 static const unsigned char sht3x_cmd_heater_off[]              = { 0x30, 0x66 };
42 
43 /* other commands */
44 static const unsigned char sht3x_cmd_read_status_reg[]         = { 0xf3, 0x2d };
45 static const unsigned char sht3x_cmd_clear_status_reg[]        = { 0x30, 0x41 };
46 static const unsigned char sht3x_cmd_read_serial_number[]      = { 0x37, 0x80 };
47 
48 /* delays for single-shot mode i2c commands, both in us */
49 #define SHT3X_SINGLE_WAIT_TIME_HPM  15000
50 #define SHT3X_SINGLE_WAIT_TIME_MPM   6000
51 #define SHT3X_SINGLE_WAIT_TIME_LPM   4000
52 
53 #define SHT3X_WORD_LEN         2
54 #define SHT3X_CMD_LENGTH       2
55 #define SHT3X_CRC8_LEN         1
56 #define SHT3X_RESPONSE_LENGTH  6
57 #define SHT3X_CRC8_POLYNOMIAL  0x31
58 #define SHT3X_CRC8_INIT        0xFF
59 #define SHT3X_MIN_TEMPERATURE  -45000
60 #define SHT3X_MAX_TEMPERATURE  130000
61 #define SHT3X_MIN_HUMIDITY     0
62 #define SHT3X_MAX_HUMIDITY     100000
63 
64 enum sht3x_chips {
65 	sht3x = 1,
66 	sts3x,
67 };
68 
69 enum sht3x_limits {
70 	limit_max = 0,
71 	limit_max_hyst,
72 	limit_min,
73 	limit_min_hyst,
74 };
75 
76 enum sht3x_repeatability {
77 	low_repeatability,
78 	medium_repeatability,
79 	high_repeatability,
80 };
81 
82 DECLARE_CRC8_TABLE(sht3x_crc8_table);
83 
84 /* periodic measure commands (high repeatability mode) */
85 static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = {
86 	/* 0.5 measurements per second */
87 	{0x20, 0x32},
88 	/* 1 measurements per second */
89 	{0x21, 0x30},
90 	/* 2 measurements per second */
91 	{0x22, 0x36},
92 	/* 4 measurements per second */
93 	{0x23, 0x34},
94 	/* 10 measurements per second */
95 	{0x27, 0x37},
96 };
97 
98 /* periodic measure commands (medium repeatability) */
99 static const char periodic_measure_commands_mpm[][SHT3X_CMD_LENGTH] = {
100 	/* 0.5 measurements per second */
101 	{0x20, 0x24},
102 	/* 1 measurements per second */
103 	{0x21, 0x26},
104 	/* 2 measurements per second */
105 	{0x22, 0x20},
106 	/* 4 measurements per second */
107 	{0x23, 0x22},
108 	/* 10 measurements per second */
109 	{0x27, 0x21},
110 };
111 
112 /* periodic measure commands (low repeatability mode) */
113 static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = {
114 	/* 0.5 measurements per second */
115 	{0x20, 0x2f},
116 	/* 1 measurements per second */
117 	{0x21, 0x2d},
118 	/* 2 measurements per second */
119 	{0x22, 0x2b},
120 	/* 4 measurements per second */
121 	{0x23, 0x29},
122 	/* 10 measurements per second */
123 	{0x27, 0x2a},
124 };
125 
126 struct sht3x_limit_commands {
127 	const char read_command[SHT3X_CMD_LENGTH];
128 	const char write_command[SHT3X_CMD_LENGTH];
129 };
130 
131 static const struct sht3x_limit_commands limit_commands[] = {
132 	/* temp1_max, humidity1_max */
133 	[limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} },
134 	/* temp_1_max_hyst, humidity1_max_hyst */
135 	[limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} },
136 	/* temp1_min, humidity1_min */
137 	[limit_min] = { {0xe1, 0x02}, {0x61, 0x00} },
138 	/* temp_1_min_hyst, humidity1_min_hyst */
139 	[limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} },
140 };
141 
142 #define SHT3X_NUM_LIMIT_CMD  ARRAY_SIZE(limit_commands)
143 
144 static const u16 mode_to_update_interval[] = {
145 	   0,
146 	2000,
147 	1000,
148 	 500,
149 	 250,
150 	 100,
151 };
152 
153 static const struct hwmon_channel_info * const sht3x_channel_info[] = {
154 	HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
155 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MIN |
156 				HWMON_T_MIN_HYST | HWMON_T_MAX |
157 				HWMON_T_MAX_HYST | HWMON_T_ALARM),
158 	HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT | HWMON_H_MIN |
159 				HWMON_H_MIN_HYST | HWMON_H_MAX |
160 				HWMON_H_MAX_HYST | HWMON_H_ALARM),
161 	NULL,
162 };
163 
164 struct sht3x_data {
165 	struct i2c_client *client;
166 	enum sht3x_chips chip_id;
167 	struct mutex i2c_lock; /* lock for sending i2c commands */
168 	struct mutex data_lock; /* lock for updating driver data */
169 
170 	u8 mode;
171 	const unsigned char *command;
172 	u32 wait_time;			/* in us*/
173 	unsigned long last_update;	/* last update in periodic mode*/
174 	enum sht3x_repeatability repeatability;
175 	u32 serial_number;
176 
177 	/*
178 	 * cached values for temperature and humidity and limits
179 	 * the limits arrays have the following order:
180 	 * max, max_hyst, min, min_hyst
181 	 */
182 	int temperature;
183 	int temperature_limits[SHT3X_NUM_LIMIT_CMD];
184 	u32 humidity;
185 	u32 humidity_limits[SHT3X_NUM_LIMIT_CMD];
186 };
187 
get_mode_from_update_interval(u16 value)188 static u8 get_mode_from_update_interval(u16 value)
189 {
190 	size_t index;
191 	u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval);
192 
193 	if (value == 0)
194 		return 0;
195 
196 	/* find next faster update interval */
197 	for (index = 1; index < number_of_modes; index++) {
198 		if (mode_to_update_interval[index] <= value)
199 			return index;
200 	}
201 
202 	return number_of_modes - 1;
203 }
204 
sht3x_read_from_command(struct i2c_client * client,struct sht3x_data * data,const char * command,char * buf,int length,u32 wait_time)205 static int sht3x_read_from_command(struct i2c_client *client,
206 				   struct sht3x_data *data,
207 				   const char *command,
208 				   char *buf, int length, u32 wait_time)
209 {
210 	int ret;
211 
212 	mutex_lock(&data->i2c_lock);
213 	ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
214 
215 	if (ret != SHT3X_CMD_LENGTH) {
216 		ret = ret < 0 ? ret : -EIO;
217 		goto out;
218 	}
219 
220 	if (wait_time)
221 		usleep_range(wait_time, wait_time + 1000);
222 
223 	ret = i2c_master_recv(client, buf, length);
224 	if (ret != length) {
225 		ret = ret < 0 ? ret : -EIO;
226 		goto out;
227 	}
228 
229 	ret = 0;
230 out:
231 	mutex_unlock(&data->i2c_lock);
232 	return ret;
233 }
234 
sht3x_extract_temperature(u16 raw)235 static int sht3x_extract_temperature(u16 raw)
236 {
237 	/*
238 	 * From datasheet:
239 	 * T = -45 + 175 * ST / 2^16
240 	 * Adapted for integer fixed point (3 digit) arithmetic.
241 	 */
242 	return ((21875 * (int)raw) >> 13) - 45000;
243 }
244 
sht3x_extract_humidity(u16 raw)245 static u32 sht3x_extract_humidity(u16 raw)
246 {
247 	/*
248 	 * From datasheet:
249 	 * RH = 100 * SRH / 2^16
250 	 * Adapted for integer fixed point (3 digit) arithmetic.
251 	 */
252 	return (12500 * (u32)raw) >> 13;
253 }
254 
sht3x_update_client(struct device * dev)255 static struct sht3x_data *sht3x_update_client(struct device *dev)
256 {
257 	struct sht3x_data *data = dev_get_drvdata(dev);
258 	struct i2c_client *client = data->client;
259 	u16 interval_ms = mode_to_update_interval[data->mode];
260 	unsigned long interval_jiffies = msecs_to_jiffies(interval_ms);
261 	unsigned char buf[SHT3X_RESPONSE_LENGTH];
262 	u16 val;
263 	int ret = 0;
264 
265 	mutex_lock(&data->data_lock);
266 	/*
267 	 * Only update cached readings once per update interval in periodic
268 	 * mode. In single shot mode the sensor measures values on demand, so
269 	 * every time the sysfs interface is called, a measurement is triggered.
270 	 * In periodic mode however, the measurement process is handled
271 	 * internally by the sensor and reading out sensor values only makes
272 	 * sense if a new reading is available.
273 	 */
274 	if (time_after(jiffies, data->last_update + interval_jiffies)) {
275 		ret = sht3x_read_from_command(client, data, data->command, buf,
276 					      sizeof(buf), data->wait_time);
277 		if (ret)
278 			goto out;
279 
280 		val = get_unaligned_be16(buf);
281 		data->temperature = sht3x_extract_temperature(val);
282 		val = get_unaligned_be16(buf + 3);
283 		data->humidity = sht3x_extract_humidity(val);
284 		data->last_update = jiffies;
285 	}
286 
287 out:
288 	mutex_unlock(&data->data_lock);
289 	if (ret)
290 		return ERR_PTR(ret);
291 
292 	return data;
293 }
294 
temp1_input_read(struct device * dev,long * temp)295 static int temp1_input_read(struct device *dev, long *temp)
296 {
297 	struct sht3x_data *data = sht3x_update_client(dev);
298 
299 	if (IS_ERR(data))
300 		return PTR_ERR(data);
301 
302 	*temp = data->temperature;
303 	return 0;
304 }
305 
humidity1_input_read(struct device * dev,long * humidity)306 static int humidity1_input_read(struct device *dev, long *humidity)
307 {
308 	struct sht3x_data *data = sht3x_update_client(dev);
309 
310 	if (IS_ERR(data))
311 		return PTR_ERR(data);
312 
313 	*humidity = data->humidity;
314 	return 0;
315 }
316 
317 /*
318  * limits_update must only be called from probe or with data_lock held
319  */
limits_update(struct sht3x_data * data)320 static int limits_update(struct sht3x_data *data)
321 {
322 	int ret;
323 	u8 index;
324 	int temperature;
325 	u32 humidity;
326 	u16 raw;
327 	char buffer[SHT3X_RESPONSE_LENGTH];
328 	const struct sht3x_limit_commands *commands;
329 	struct i2c_client *client = data->client;
330 
331 	for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) {
332 		commands = &limit_commands[index];
333 		ret = sht3x_read_from_command(client, data,
334 					      commands->read_command, buffer,
335 					      SHT3X_RESPONSE_LENGTH, 0);
336 
337 		if (ret)
338 			return ret;
339 
340 		raw = get_unaligned_be16(buffer);
341 		temperature = sht3x_extract_temperature((raw & 0x01ff) << 7);
342 		humidity = sht3x_extract_humidity(raw & 0xfe00);
343 		data->temperature_limits[index] = temperature;
344 		data->humidity_limits[index] = humidity;
345 	}
346 
347 	return ret;
348 }
349 
temp1_limit_read(struct device * dev,int index)350 static int temp1_limit_read(struct device *dev, int index)
351 {
352 	struct sht3x_data *data = dev_get_drvdata(dev);
353 
354 	return data->temperature_limits[index];
355 }
356 
humidity1_limit_read(struct device * dev,int index)357 static int humidity1_limit_read(struct device *dev, int index)
358 {
359 	struct sht3x_data *data = dev_get_drvdata(dev);
360 
361 	return data->humidity_limits[index];
362 }
363 
364 /*
365  * limit_write must only be called with data_lock held
366  */
limit_write(struct device * dev,u8 index,int temperature,u32 humidity)367 static size_t limit_write(struct device *dev,
368 			  u8 index,
369 			  int temperature,
370 			  u32 humidity)
371 {
372 	char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
373 	char *position = buffer;
374 	int ret;
375 	u16 raw;
376 	struct sht3x_data *data = dev_get_drvdata(dev);
377 	struct i2c_client *client = data->client;
378 	const struct sht3x_limit_commands *commands;
379 
380 	commands = &limit_commands[index];
381 
382 	memcpy(position, commands->write_command, SHT3X_CMD_LENGTH);
383 	position += SHT3X_CMD_LENGTH;
384 	/*
385 	 * ST = (T + 45) / 175 * 2^16
386 	 * SRH = RH / 100 * 2^16
387 	 * adapted for fixed point arithmetic and packed the same as
388 	 * in limit_read()
389 	 */
390 	raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7);
391 	raw |= ((humidity * 42950) >> 16) & 0xfe00;
392 
393 	put_unaligned_be16(raw, position);
394 	position += SHT3X_WORD_LEN;
395 	*position = crc8(sht3x_crc8_table,
396 			 position - SHT3X_WORD_LEN,
397 			 SHT3X_WORD_LEN,
398 			 SHT3X_CRC8_INIT);
399 
400 	mutex_lock(&data->i2c_lock);
401 	ret = i2c_master_send(client, buffer, sizeof(buffer));
402 	mutex_unlock(&data->i2c_lock);
403 
404 	if (ret != sizeof(buffer))
405 		return ret < 0 ? ret : -EIO;
406 
407 	data->temperature_limits[index] = temperature;
408 	data->humidity_limits[index] = humidity;
409 
410 	return 0;
411 }
412 
temp1_limit_write(struct device * dev,int index,int val)413 static int temp1_limit_write(struct device *dev, int index, int val)
414 {
415 	int temperature;
416 	int ret;
417 	struct sht3x_data *data = dev_get_drvdata(dev);
418 
419 	temperature = clamp_val(val, SHT3X_MIN_TEMPERATURE,
420 				SHT3X_MAX_TEMPERATURE);
421 	mutex_lock(&data->data_lock);
422 	ret = limit_write(dev, index, temperature,
423 			  data->humidity_limits[index]);
424 	mutex_unlock(&data->data_lock);
425 
426 	return ret;
427 }
428 
humidity1_limit_write(struct device * dev,int index,int val)429 static int humidity1_limit_write(struct device *dev, int index, int val)
430 {
431 	u32 humidity;
432 	int ret;
433 	struct sht3x_data *data = dev_get_drvdata(dev);
434 
435 	humidity = clamp_val(val, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY);
436 	mutex_lock(&data->data_lock);
437 	ret = limit_write(dev, index, data->temperature_limits[index],
438 			  humidity);
439 	mutex_unlock(&data->data_lock);
440 
441 	return ret;
442 }
443 
sht3x_select_command(struct sht3x_data * data)444 static void sht3x_select_command(struct sht3x_data *data)
445 {
446 	/*
447 	 * For single-shot mode, only non blocking mode is support,
448 	 * we have to wait ourselves for result.
449 	 */
450 	if (data->mode > 0) {
451 		data->command = sht3x_cmd_measure_periodic_mode;
452 		data->wait_time = 0;
453 	} else {
454 		if (data->repeatability == high_repeatability) {
455 			data->command = sht3x_cmd_measure_single_hpm;
456 			data->wait_time = SHT3X_SINGLE_WAIT_TIME_HPM;
457 		} else if (data->repeatability ==  medium_repeatability) {
458 			data->command = sht3x_cmd_measure_single_mpm;
459 			data->wait_time = SHT3X_SINGLE_WAIT_TIME_MPM;
460 		} else {
461 			data->command = sht3x_cmd_measure_single_lpm;
462 			data->wait_time = SHT3X_SINGLE_WAIT_TIME_LPM;
463 		}
464 	}
465 }
466 
status_register_read(struct device * dev,char * buffer,int length)467 static int status_register_read(struct device *dev,
468 				char *buffer, int length)
469 {
470 	int ret;
471 	struct sht3x_data *data = dev_get_drvdata(dev);
472 	struct i2c_client *client = data->client;
473 
474 	ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg,
475 				      buffer, length, 0);
476 
477 	return ret;
478 }
479 
temp1_alarm_read(struct device * dev)480 static int temp1_alarm_read(struct device *dev)
481 {
482 	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
483 	int ret;
484 
485 	ret = status_register_read(dev, buffer,
486 				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
487 	if (ret)
488 		return ret;
489 
490 	return !!(buffer[0] & 0x04);
491 }
492 
humidity1_alarm_read(struct device * dev)493 static int humidity1_alarm_read(struct device *dev)
494 {
495 	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
496 	int ret;
497 
498 	ret = status_register_read(dev, buffer,
499 				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
500 	if (ret)
501 		return ret;
502 
503 	return !!(buffer[0] & 0x08);
504 }
505 
heater_enable_show(struct device * dev,struct device_attribute * attr,char * buf)506 static ssize_t heater_enable_show(struct device *dev,
507 				  struct device_attribute *attr,
508 				  char *buf)
509 {
510 	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
511 	int ret;
512 
513 	ret = status_register_read(dev, buffer,
514 				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
515 	if (ret)
516 		return ret;
517 
518 	return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x20));
519 }
520 
heater_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)521 static ssize_t heater_enable_store(struct device *dev,
522 				   struct device_attribute *attr,
523 				   const char *buf,
524 				   size_t count)
525 {
526 	struct sht3x_data *data = dev_get_drvdata(dev);
527 	struct i2c_client *client = data->client;
528 	int ret;
529 	bool status;
530 
531 	ret = kstrtobool(buf, &status);
532 	if (ret)
533 		return ret;
534 
535 	mutex_lock(&data->i2c_lock);
536 
537 	if (status)
538 		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on,
539 				      SHT3X_CMD_LENGTH);
540 	else
541 		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off,
542 				      SHT3X_CMD_LENGTH);
543 
544 	mutex_unlock(&data->i2c_lock);
545 
546 	return ret;
547 }
548 
update_interval_read(struct device * dev)549 static int update_interval_read(struct device *dev)
550 {
551 	struct sht3x_data *data = dev_get_drvdata(dev);
552 
553 	return mode_to_update_interval[data->mode];
554 }
555 
update_interval_write(struct device * dev,int val)556 static int update_interval_write(struct device *dev, int val)
557 {
558 	u8 mode;
559 	int ret;
560 	const char *command;
561 	struct sht3x_data *data = dev_get_drvdata(dev);
562 	struct i2c_client *client = data->client;
563 
564 	mode = get_mode_from_update_interval(val);
565 
566 	mutex_lock(&data->data_lock);
567 	/* mode did not change */
568 	if (mode == data->mode) {
569 		mutex_unlock(&data->data_lock);
570 		return 0;
571 	}
572 
573 	mutex_lock(&data->i2c_lock);
574 	/*
575 	 * Abort periodic measure mode.
576 	 * To do any changes to the configuration while in periodic mode, we
577 	 * have to send a break command to the sensor, which then falls back
578 	 * to single shot (mode = 0).
579 	 */
580 	if (data->mode > 0) {
581 		ret = i2c_master_send(client, sht3x_cmd_break,
582 				      SHT3X_CMD_LENGTH);
583 		if (ret != SHT3X_CMD_LENGTH)
584 			goto out;
585 		data->mode = 0;
586 	}
587 
588 	if (mode > 0) {
589 		if (data->repeatability == high_repeatability)
590 			command = periodic_measure_commands_hpm[mode - 1];
591 		else if (data->repeatability == medium_repeatability)
592 			command = periodic_measure_commands_mpm[mode - 1];
593 		else
594 			command = periodic_measure_commands_lpm[mode - 1];
595 
596 		/* select mode */
597 		ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
598 		if (ret != SHT3X_CMD_LENGTH)
599 			goto out;
600 	}
601 
602 	/* select mode and command */
603 	data->mode = mode;
604 	sht3x_select_command(data);
605 
606 out:
607 	mutex_unlock(&data->i2c_lock);
608 	mutex_unlock(&data->data_lock);
609 	if (ret != SHT3X_CMD_LENGTH)
610 		return ret < 0 ? ret : -EIO;
611 
612 	return 0;
613 }
614 
repeatability_show(struct device * dev,struct device_attribute * attr,char * buf)615 static ssize_t repeatability_show(struct device *dev,
616 				  struct device_attribute *attr,
617 				  char *buf)
618 {
619 	struct sht3x_data *data = dev_get_drvdata(dev);
620 
621 	return sysfs_emit(buf, "%d\n", data->repeatability);
622 }
623 
repeatability_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)624 static ssize_t repeatability_store(struct device *dev,
625 				   struct device_attribute *attr,
626 				   const char *buf,
627 				   size_t count)
628 {
629 	int ret;
630 	u8 val;
631 
632 	struct sht3x_data *data = dev_get_drvdata(dev);
633 
634 	ret = kstrtou8(buf, 0, &val);
635 	if (ret)
636 		return ret;
637 
638 	if (val > 2)
639 		return -EINVAL;
640 
641 	data->repeatability = val;
642 
643 	return count;
644 }
645 
646 static SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0);
647 static SENSOR_DEVICE_ATTR_RW(repeatability, repeatability, 0);
648 
649 static struct attribute *sht3x_attrs[] = {
650 	&sensor_dev_attr_heater_enable.dev_attr.attr,
651 	&sensor_dev_attr_repeatability.dev_attr.attr,
652 	NULL
653 };
654 
655 ATTRIBUTE_GROUPS(sht3x);
656 
sht3x_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)657 static umode_t sht3x_is_visible(const void *data, enum hwmon_sensor_types type,
658 				u32 attr, int channel)
659 {
660 	const struct sht3x_data *chip_data = data;
661 
662 	switch (type) {
663 	case hwmon_chip:
664 		switch (attr) {
665 		case hwmon_chip_update_interval:
666 			return 0644;
667 		default:
668 			break;
669 		}
670 		break;
671 	case hwmon_temp:
672 		switch (attr) {
673 		case hwmon_temp_input:
674 		case hwmon_temp_alarm:
675 			return 0444;
676 		case hwmon_temp_max:
677 		case hwmon_temp_max_hyst:
678 		case hwmon_temp_min:
679 		case hwmon_temp_min_hyst:
680 			return 0644;
681 		default:
682 			break;
683 		}
684 		break;
685 	case hwmon_humidity:
686 		if (chip_data->chip_id == sts3x)
687 			break;
688 		switch (attr) {
689 		case hwmon_humidity_input:
690 		case hwmon_humidity_alarm:
691 			return 0444;
692 		case hwmon_humidity_max:
693 		case hwmon_humidity_max_hyst:
694 		case hwmon_humidity_min:
695 		case hwmon_humidity_min_hyst:
696 			return 0644;
697 		default:
698 			break;
699 		}
700 		break;
701 	default:
702 		break;
703 	}
704 
705 	return 0;
706 }
707 
sht3x_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)708 static int sht3x_read(struct device *dev, enum hwmon_sensor_types type,
709 		      u32 attr, int channel, long *val)
710 {
711 	enum sht3x_limits index;
712 	int ret;
713 
714 	switch (type) {
715 	case hwmon_chip:
716 		switch (attr) {
717 		case hwmon_chip_update_interval:
718 			*val = update_interval_read(dev);
719 			break;
720 		default:
721 			return -EOPNOTSUPP;
722 		}
723 		break;
724 	case hwmon_temp:
725 		switch (attr) {
726 		case hwmon_temp_input:
727 			return temp1_input_read(dev, val);
728 		case hwmon_temp_alarm:
729 			ret = temp1_alarm_read(dev);
730 			if (ret < 0)
731 				return ret;
732 			*val = ret;
733 			break;
734 		case hwmon_temp_max:
735 			index = limit_max;
736 			*val = temp1_limit_read(dev, index);
737 			break;
738 		case hwmon_temp_max_hyst:
739 			index = limit_max_hyst;
740 			*val = temp1_limit_read(dev, index);
741 			break;
742 		case hwmon_temp_min:
743 			index = limit_min;
744 			*val = temp1_limit_read(dev, index);
745 			break;
746 		case hwmon_temp_min_hyst:
747 			index = limit_min_hyst;
748 			*val = temp1_limit_read(dev, index);
749 			break;
750 		default:
751 			return -EOPNOTSUPP;
752 		}
753 		break;
754 	case hwmon_humidity:
755 		switch (attr) {
756 		case hwmon_humidity_input:
757 			return humidity1_input_read(dev, val);
758 		case hwmon_humidity_alarm:
759 			ret = humidity1_alarm_read(dev);
760 			if (ret < 0)
761 				return ret;
762 			*val = ret;
763 			break;
764 		case hwmon_humidity_max:
765 			index = limit_max;
766 			*val = humidity1_limit_read(dev, index);
767 			break;
768 		case hwmon_humidity_max_hyst:
769 			index = limit_max_hyst;
770 			*val = humidity1_limit_read(dev, index);
771 			break;
772 		case hwmon_humidity_min:
773 			index = limit_min;
774 			*val = humidity1_limit_read(dev, index);
775 			break;
776 		case hwmon_humidity_min_hyst:
777 			index = limit_min_hyst;
778 			*val = humidity1_limit_read(dev, index);
779 			break;
780 		default:
781 			return -EOPNOTSUPP;
782 		}
783 		break;
784 	default:
785 		return -EOPNOTSUPP;
786 	}
787 
788 	return 0;
789 }
790 
sht3x_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)791 static int sht3x_write(struct device *dev, enum hwmon_sensor_types type,
792 		       u32 attr, int channel, long val)
793 {
794 	enum sht3x_limits index;
795 
796 	switch (type) {
797 	case hwmon_chip:
798 		switch (attr) {
799 		case hwmon_chip_update_interval:
800 			return update_interval_write(dev, val);
801 		default:
802 			return -EOPNOTSUPP;
803 		}
804 	case hwmon_temp:
805 		switch (attr) {
806 		case hwmon_temp_max:
807 			index = limit_max;
808 			break;
809 		case hwmon_temp_max_hyst:
810 			index = limit_max_hyst;
811 			break;
812 		case hwmon_temp_min:
813 			index = limit_min;
814 			break;
815 		case hwmon_temp_min_hyst:
816 			index = limit_min_hyst;
817 			break;
818 		default:
819 			return -EOPNOTSUPP;
820 		}
821 		return temp1_limit_write(dev, index, val);
822 	case hwmon_humidity:
823 		switch (attr) {
824 		case hwmon_humidity_max:
825 			index = limit_max;
826 			break;
827 		case hwmon_humidity_max_hyst:
828 			index = limit_max_hyst;
829 			break;
830 		case hwmon_humidity_min:
831 			index = limit_min;
832 			break;
833 		case hwmon_humidity_min_hyst:
834 			index = limit_min_hyst;
835 			break;
836 		default:
837 			return -EOPNOTSUPP;
838 		}
839 		return humidity1_limit_write(dev, index, val);
840 	default:
841 		return -EOPNOTSUPP;
842 	}
843 }
844 
sht3x_serial_number_read(struct sht3x_data * data)845 static void sht3x_serial_number_read(struct sht3x_data *data)
846 {
847 	int ret;
848 	char buffer[SHT3X_RESPONSE_LENGTH];
849 	struct i2c_client *client = data->client;
850 
851 	ret = sht3x_read_from_command(client, data,
852 				      sht3x_cmd_read_serial_number,
853 				      buffer,
854 				      SHT3X_RESPONSE_LENGTH, 0);
855 	if (ret)
856 		return;
857 
858 	data->serial_number = (buffer[0] << 24) | (buffer[1] << 16) |
859 			      (buffer[3] << 8) | buffer[4];
860 
861 	debugfs_create_u32("serial_number", 0444, client->debugfs, &data->serial_number);
862 }
863 
864 static const struct hwmon_ops sht3x_ops = {
865 	.is_visible = sht3x_is_visible,
866 	.read = sht3x_read,
867 	.write = sht3x_write,
868 };
869 
870 static const struct hwmon_chip_info sht3x_chip_info = {
871 	.ops = &sht3x_ops,
872 	.info = sht3x_channel_info,
873 };
874 
sht3x_probe(struct i2c_client * client)875 static int sht3x_probe(struct i2c_client *client)
876 {
877 	int ret;
878 	struct sht3x_data *data;
879 	struct device *hwmon_dev;
880 	struct i2c_adapter *adap = client->adapter;
881 	struct device *dev = &client->dev;
882 
883 	/*
884 	 * we require full i2c support since the sht3x uses multi-byte read and
885 	 * writes as well as multi-byte commands which are not supported by
886 	 * the smbus protocol
887 	 */
888 	if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
889 		return -ENODEV;
890 
891 	ret = i2c_master_send(client, sht3x_cmd_clear_status_reg,
892 			      SHT3X_CMD_LENGTH);
893 	if (ret != SHT3X_CMD_LENGTH)
894 		return ret < 0 ? ret : -ENODEV;
895 
896 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
897 	if (!data)
898 		return -ENOMEM;
899 
900 	data->repeatability = high_repeatability;
901 	data->mode = 0;
902 	data->last_update = jiffies - msecs_to_jiffies(3000);
903 	data->client = client;
904 	data->chip_id = (uintptr_t)i2c_get_match_data(client);
905 	crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
906 
907 	sht3x_select_command(data);
908 
909 	mutex_init(&data->i2c_lock);
910 	mutex_init(&data->data_lock);
911 
912 	/*
913 	 * An attempt to read limits register too early
914 	 * causes a NACK response from the chip.
915 	 * Waiting for an empirical delay of 500 us solves the issue.
916 	 */
917 	usleep_range(500, 600);
918 
919 	ret = limits_update(data);
920 	if (ret)
921 		return ret;
922 
923 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
924 							 &sht3x_chip_info, sht3x_groups);
925 	if (IS_ERR(hwmon_dev))
926 		return PTR_ERR(hwmon_dev);
927 
928 	sht3x_serial_number_read(data);
929 
930 	return 0;
931 }
932 
933 /* device ID table */
934 static const struct i2c_device_id sht3x_ids[] = {
935 	{ .name = "sht3x", .driver_data = sht3x },
936 	{ .name = "sts3x", .driver_data = sts3x },
937 	{ .name = "sht85", .driver_data = sht3x },
938 	{ }
939 };
940 
941 MODULE_DEVICE_TABLE(i2c, sht3x_ids);
942 
943 static const struct of_device_id sht3x_of_match[] = {
944 	{ .compatible = "sensirion,sht30", .data = (void *)(uintptr_t)sht3x },
945 	{ .compatible = "sensirion,sts30", .data = (void *)(uintptr_t)sts3x },
946 	{ }
947 };
948 
949 MODULE_DEVICE_TABLE(of, sht3x_of_match);
950 
951 static struct i2c_driver sht3x_i2c_driver = {
952 	.driver = {
953 		.name = "sht3x",
954 		.of_match_table = sht3x_of_match,
955 	},
956 	.probe       = sht3x_probe,
957 	.id_table    = sht3x_ids,
958 };
959 module_i2c_driver(sht3x_i2c_driver);
960 
961 MODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
962 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
963 MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
964 MODULE_LICENSE("GPL");
965