Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/* Sensirion SHT3x-DIS humidity and temperature sensor driver.
  2 * The SHT3x comes in many different versions, this driver is for the
  3 * I2C version only.
  4 *
  5 * Copyright (C) 2016 Sensirion AG, Switzerland
  6 * Author: David Frey <david.frey@sensirion.com>
  7 * Author: Pascal Sachs <pascal.sachs@sensirion.com>
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published by
 11 * the Free Software Foundation; either version 2 of the License, or
 12 * (at your option) any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 */
 20
 21#include <asm/page.h>
 22#include <linux/crc8.h>
 23#include <linux/delay.h>
 24#include <linux/err.h>
 25#include <linux/hwmon.h>
 26#include <linux/hwmon-sysfs.h>
 27#include <linux/i2c.h>
 28#include <linux/init.h>
 29#include <linux/kernel.h>
 30#include <linux/module.h>
 31#include <linux/slab.h>
 32#include <linux/jiffies.h>
 33#include <linux/platform_data/sht3x.h>
 34
 35/* commands (high precision mode) */
 36static const unsigned char sht3x_cmd_measure_blocking_hpm[]    = { 0x2c, 0x06 };
 37static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = { 0x24, 0x00 };
 38
 39/* commands (low power mode) */
 40static const unsigned char sht3x_cmd_measure_blocking_lpm[]    = { 0x2c, 0x10 };
 41static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = { 0x24, 0x16 };
 42
 43/* commands for periodic mode */
 44static const unsigned char sht3x_cmd_measure_periodic_mode[]   = { 0xe0, 0x00 };
 45static const unsigned char sht3x_cmd_break[]                   = { 0x30, 0x93 };
 46
 47/* commands for heater control */
 48static const unsigned char sht3x_cmd_heater_on[]               = { 0x30, 0x6d };
 49static const unsigned char sht3x_cmd_heater_off[]              = { 0x30, 0x66 };
 50
 51/* other commands */
 52static const unsigned char sht3x_cmd_read_status_reg[]         = { 0xf3, 0x2d };
 53static const unsigned char sht3x_cmd_clear_status_reg[]        = { 0x30, 0x41 };
 54
 55/* delays for non-blocking i2c commands, both in us */
 56#define SHT3X_NONBLOCKING_WAIT_TIME_HPM  15000
 57#define SHT3X_NONBLOCKING_WAIT_TIME_LPM   4000
 58
 59#define SHT3X_WORD_LEN         2
 60#define SHT3X_CMD_LENGTH       2
 61#define SHT3X_CRC8_LEN         1
 62#define SHT3X_RESPONSE_LENGTH  6
 63#define SHT3X_CRC8_POLYNOMIAL  0x31
 64#define SHT3X_CRC8_INIT        0xFF
 65#define SHT3X_MIN_TEMPERATURE  -45000
 66#define SHT3X_MAX_TEMPERATURE  130000
 67#define SHT3X_MIN_HUMIDITY     0
 68#define SHT3X_MAX_HUMIDITY     100000
 69
 70enum sht3x_chips {
 71	sht3x,
 72	sts3x,
 73};
 74
 75enum sht3x_limits {
 76	limit_max = 0,
 77	limit_max_hyst,
 78	limit_min,
 79	limit_min_hyst,
 80};
 81
 82DECLARE_CRC8_TABLE(sht3x_crc8_table);
 83
 84/* periodic measure commands (high precision mode) */
 85static 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 (low power mode) */
 99static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = {
100	/* 0.5 measurements per second */
101	{0x20, 0x2f},
102	/* 1 measurements per second */
103	{0x21, 0x2d},
104	/* 2 measurements per second */
105	{0x22, 0x2b},
106	/* 4 measurements per second */
107	{0x23, 0x29},
108	/* 10 measurements per second */
109	{0x27, 0x2a},
110};
111
112struct sht3x_limit_commands {
113	const char read_command[SHT3X_CMD_LENGTH];
114	const char write_command[SHT3X_CMD_LENGTH];
115};
116
117static const struct sht3x_limit_commands limit_commands[] = {
118	/* temp1_max, humidity1_max */
119	[limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} },
120	/* temp_1_max_hyst, humidity1_max_hyst */
121	[limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} },
122	/* temp1_min, humidity1_min */
123	[limit_min] = { {0xe1, 0x02}, {0x61, 0x00} },
124	/* temp_1_min_hyst, humidity1_min_hyst */
125	[limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} },
126};
127
128#define SHT3X_NUM_LIMIT_CMD  ARRAY_SIZE(limit_commands)
129
130static const u16 mode_to_update_interval[] = {
131	   0,
132	2000,
133	1000,
134	 500,
135	 250,
136	 100,
137};
138
139struct sht3x_data {
140	struct i2c_client *client;
141	struct mutex i2c_lock; /* lock for sending i2c commands */
142	struct mutex data_lock; /* lock for updating driver data */
143
144	u8 mode;
145	const unsigned char *command;
146	u32 wait_time;			/* in us*/
147	unsigned long last_update;	/* last update in periodic mode*/
148
149	struct sht3x_platform_data setup;
150
151	/*
152	 * cached values for temperature and humidity and limits
153	 * the limits arrays have the following order:
154	 * max, max_hyst, min, min_hyst
155	 */
156	int temperature;
157	int temperature_limits[SHT3X_NUM_LIMIT_CMD];
158	u32 humidity;
159	u32 humidity_limits[SHT3X_NUM_LIMIT_CMD];
160};
161
162static u8 get_mode_from_update_interval(u16 value)
163{
164	size_t index;
165	u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval);
166
167	if (value == 0)
168		return 0;
169
170	/* find next faster update interval */
171	for (index = 1; index < number_of_modes; index++) {
172		if (mode_to_update_interval[index] <= value)
173			return index;
174	}
175
176	return number_of_modes - 1;
177}
178
179static int sht3x_read_from_command(struct i2c_client *client,
180				   struct sht3x_data *data,
181				   const char *command,
182				   char *buf, int length, u32 wait_time)
183{
184	int ret;
185
186	mutex_lock(&data->i2c_lock);
187	ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
188
189	if (ret != SHT3X_CMD_LENGTH) {
190		ret = ret < 0 ? ret : -EIO;
191		goto out;
192	}
193
194	if (wait_time)
195		usleep_range(wait_time, wait_time + 1000);
196
197	ret = i2c_master_recv(client, buf, length);
198	if (ret != length) {
199		ret = ret < 0 ? ret : -EIO;
200		goto out;
201	}
202
203	ret = 0;
204out:
205	mutex_unlock(&data->i2c_lock);
206	return ret;
207}
208
209static int sht3x_extract_temperature(u16 raw)
210{
211	/*
212	 * From datasheet:
213	 * T = -45 + 175 * ST / 2^16
214	 * Adapted for integer fixed point (3 digit) arithmetic.
215	 */
216	return ((21875 * (int)raw) >> 13) - 45000;
217}
218
219static u32 sht3x_extract_humidity(u16 raw)
220{
221	/*
222	 * From datasheet:
223	 * RH = 100 * SRH / 2^16
224	 * Adapted for integer fixed point (3 digit) arithmetic.
225	 */
226	return (12500 * (u32)raw) >> 13;
227}
228
229static struct sht3x_data *sht3x_update_client(struct device *dev)
230{
231	struct sht3x_data *data = dev_get_drvdata(dev);
232	struct i2c_client *client = data->client;
233	u16 interval_ms = mode_to_update_interval[data->mode];
234	unsigned long interval_jiffies = msecs_to_jiffies(interval_ms);
235	unsigned char buf[SHT3X_RESPONSE_LENGTH];
236	u16 val;
237	int ret = 0;
238
239	mutex_lock(&data->data_lock);
240	/*
241	 * Only update cached readings once per update interval in periodic
242	 * mode. In single shot mode the sensor measures values on demand, so
243	 * every time the sysfs interface is called, a measurement is triggered.
244	 * In periodic mode however, the measurement process is handled
245	 * internally by the sensor and reading out sensor values only makes
246	 * sense if a new reading is available.
247	 */
248	if (time_after(jiffies, data->last_update + interval_jiffies)) {
249		ret = sht3x_read_from_command(client, data, data->command, buf,
250					      sizeof(buf), data->wait_time);
251		if (ret)
252			goto out;
253
254		val = be16_to_cpup((__be16 *)buf);
255		data->temperature = sht3x_extract_temperature(val);
256		val = be16_to_cpup((__be16 *)(buf + 3));
257		data->humidity = sht3x_extract_humidity(val);
258		data->last_update = jiffies;
259	}
260
261out:
262	mutex_unlock(&data->data_lock);
263	if (ret)
264		return ERR_PTR(ret);
265
266	return data;
267}
268
269/* sysfs attributes */
270static ssize_t temp1_input_show(struct device *dev,
271				struct device_attribute *attr, char *buf)
272{
273	struct sht3x_data *data = sht3x_update_client(dev);
274
275	if (IS_ERR(data))
276		return PTR_ERR(data);
277
278	return sprintf(buf, "%d\n", data->temperature);
279}
280
281static ssize_t humidity1_input_show(struct device *dev,
282				    struct device_attribute *attr, char *buf)
283{
284	struct sht3x_data *data = sht3x_update_client(dev);
285
286	if (IS_ERR(data))
287		return PTR_ERR(data);
288
289	return sprintf(buf, "%u\n", data->humidity);
290}
291
292/*
293 * limits_update must only be called from probe or with data_lock held
294 */
295static int limits_update(struct sht3x_data *data)
296{
297	int ret;
298	u8 index;
299	int temperature;
300	u32 humidity;
301	u16 raw;
302	char buffer[SHT3X_RESPONSE_LENGTH];
303	const struct sht3x_limit_commands *commands;
304	struct i2c_client *client = data->client;
305
306	for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) {
307		commands = &limit_commands[index];
308		ret = sht3x_read_from_command(client, data,
309					      commands->read_command, buffer,
310					      SHT3X_RESPONSE_LENGTH, 0);
311
312		if (ret)
313			return ret;
314
315		raw = be16_to_cpup((__be16 *)buffer);
316		temperature = sht3x_extract_temperature((raw & 0x01ff) << 7);
317		humidity = sht3x_extract_humidity(raw & 0xfe00);
318		data->temperature_limits[index] = temperature;
319		data->humidity_limits[index] = humidity;
320	}
321
322	return ret;
323}
324
325static ssize_t temp1_limit_show(struct device *dev,
326				struct device_attribute *attr,
327				char *buf)
328{
329	struct sht3x_data *data = dev_get_drvdata(dev);
330	u8 index = to_sensor_dev_attr(attr)->index;
331	int temperature_limit = data->temperature_limits[index];
332
333	return scnprintf(buf, PAGE_SIZE, "%d\n", temperature_limit);
334}
335
336static ssize_t humidity1_limit_show(struct device *dev,
337				    struct device_attribute *attr,
338				    char *buf)
339{
340	struct sht3x_data *data = dev_get_drvdata(dev);
341	u8 index = to_sensor_dev_attr(attr)->index;
342	u32 humidity_limit = data->humidity_limits[index];
343
344	return scnprintf(buf, PAGE_SIZE, "%u\n", humidity_limit);
345}
346
347/*
348 * limit_store must only be called with data_lock held
349 */
350static size_t limit_store(struct device *dev,
351			  size_t count,
352			  u8 index,
353			  int temperature,
354			  u32 humidity)
355{
356	char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
357	char *position = buffer;
358	int ret;
359	u16 raw;
360	struct sht3x_data *data = dev_get_drvdata(dev);
361	struct i2c_client *client = data->client;
362	const struct sht3x_limit_commands *commands;
363
364	commands = &limit_commands[index];
365
366	memcpy(position, commands->write_command, SHT3X_CMD_LENGTH);
367	position += SHT3X_CMD_LENGTH;
368	/*
369	 * ST = (T + 45) / 175 * 2^16
370	 * SRH = RH / 100 * 2^16
371	 * adapted for fixed point arithmetic and packed the same as
372	 * in limit_show()
373	 */
374	raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7);
375	raw |= ((humidity * 42950) >> 16) & 0xfe00;
376
377	*((__be16 *)position) = cpu_to_be16(raw);
378	position += SHT3X_WORD_LEN;
379	*position = crc8(sht3x_crc8_table,
380			 position - SHT3X_WORD_LEN,
381			 SHT3X_WORD_LEN,
382			 SHT3X_CRC8_INIT);
383
384	mutex_lock(&data->i2c_lock);
385	ret = i2c_master_send(client, buffer, sizeof(buffer));
386	mutex_unlock(&data->i2c_lock);
387
388	if (ret != sizeof(buffer))
389		return ret < 0 ? ret : -EIO;
390
391	data->temperature_limits[index] = temperature;
392	data->humidity_limits[index] = humidity;
393	return count;
394}
395
396static ssize_t temp1_limit_store(struct device *dev,
397				 struct device_attribute *attr,
398				 const char *buf,
399				 size_t count)
400{
401	int temperature;
402	int ret;
403	struct sht3x_data *data = dev_get_drvdata(dev);
404	u8 index = to_sensor_dev_attr(attr)->index;
405
406	ret = kstrtoint(buf, 0, &temperature);
407	if (ret)
408		return ret;
409
410	temperature = clamp_val(temperature, SHT3X_MIN_TEMPERATURE,
411				SHT3X_MAX_TEMPERATURE);
412	mutex_lock(&data->data_lock);
413	ret = limit_store(dev, count, index, temperature,
414			  data->humidity_limits[index]);
415	mutex_unlock(&data->data_lock);
416
417	return ret;
418}
419
420static ssize_t humidity1_limit_store(struct device *dev,
421				     struct device_attribute *attr,
422				     const char *buf,
423				     size_t count)
424{
425	u32 humidity;
426	int ret;
427	struct sht3x_data *data = dev_get_drvdata(dev);
428	u8 index = to_sensor_dev_attr(attr)->index;
429
430	ret = kstrtou32(buf, 0, &humidity);
431	if (ret)
432		return ret;
433
434	humidity = clamp_val(humidity, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY);
435	mutex_lock(&data->data_lock);
436	ret = limit_store(dev, count, index, data->temperature_limits[index],
437			  humidity);
438	mutex_unlock(&data->data_lock);
439
440	return ret;
441}
442
443static void sht3x_select_command(struct sht3x_data *data)
444{
445	/*
446	 * In blocking mode (clock stretching mode) the I2C bus
447	 * is blocked for other traffic, thus the call to i2c_master_recv()
448	 * will wait until the data is ready. For non blocking mode, we
449	 * have to wait ourselves.
450	 */
451	if (data->mode > 0) {
452		data->command = sht3x_cmd_measure_periodic_mode;
453		data->wait_time = 0;
454	} else if (data->setup.blocking_io) {
455		data->command = data->setup.high_precision ?
456				sht3x_cmd_measure_blocking_hpm :
457				sht3x_cmd_measure_blocking_lpm;
458		data->wait_time = 0;
459	} else {
460		if (data->setup.high_precision) {
461			data->command = sht3x_cmd_measure_nonblocking_hpm;
462			data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_HPM;
463		} else {
464			data->command = sht3x_cmd_measure_nonblocking_lpm;
465			data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_LPM;
466		}
467	}
468}
469
470static int status_register_read(struct device *dev,
471				struct device_attribute *attr,
472				char *buffer, int length)
473{
474	int ret;
475	struct sht3x_data *data = dev_get_drvdata(dev);
476	struct i2c_client *client = data->client;
477
478	ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg,
479				      buffer, length, 0);
480
481	return ret;
482}
483
484static ssize_t temp1_alarm_show(struct device *dev,
485				struct device_attribute *attr,
486				char *buf)
487{
488	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
489	int ret;
490
491	ret = status_register_read(dev, attr, buffer,
492				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
493	if (ret)
494		return ret;
495
496	return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x04));
497}
498
499static ssize_t humidity1_alarm_show(struct device *dev,
500				    struct device_attribute *attr,
501				    char *buf)
502{
503	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
504	int ret;
505
506	ret = status_register_read(dev, attr, buffer,
507				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
508	if (ret)
509		return ret;
510
511	return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x08));
512}
513
514static ssize_t heater_enable_show(struct device *dev,
515				  struct device_attribute *attr,
516				  char *buf)
517{
518	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
519	int ret;
520
521	ret = status_register_read(dev, attr, buffer,
522				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
523	if (ret)
524		return ret;
525
526	return scnprintf(buf, PAGE_SIZE, "%d\n", !!(buffer[0] & 0x20));
527}
528
529static ssize_t heater_enable_store(struct device *dev,
530				   struct device_attribute *attr,
531				   const char *buf,
532				   size_t count)
533{
534	struct sht3x_data *data = dev_get_drvdata(dev);
535	struct i2c_client *client = data->client;
536	int ret;
537	bool status;
538
539	ret = kstrtobool(buf, &status);
540	if (ret)
541		return ret;
542
543	mutex_lock(&data->i2c_lock);
544
545	if (status)
546		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on,
547				      SHT3X_CMD_LENGTH);
548	else
549		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off,
550				      SHT3X_CMD_LENGTH);
551
552	mutex_unlock(&data->i2c_lock);
553
554	return ret;
555}
556
557static ssize_t update_interval_show(struct device *dev,
558				    struct device_attribute *attr,
559				    char *buf)
560{
561	struct sht3x_data *data = dev_get_drvdata(dev);
562
563	return scnprintf(buf, PAGE_SIZE, "%u\n",
564			 mode_to_update_interval[data->mode]);
565}
566
567static ssize_t update_interval_store(struct device *dev,
568				     struct device_attribute *attr,
569				     const char *buf,
570				     size_t count)
571{
572	u16 update_interval;
573	u8 mode;
574	int ret;
575	const char *command;
576	struct sht3x_data *data = dev_get_drvdata(dev);
577	struct i2c_client *client = data->client;
578
579	ret = kstrtou16(buf, 0, &update_interval);
580	if (ret)
581		return ret;
582
583	mode = get_mode_from_update_interval(update_interval);
584
585	mutex_lock(&data->data_lock);
586	/* mode did not change */
587	if (mode == data->mode) {
588		mutex_unlock(&data->data_lock);
589		return count;
590	}
591
592	mutex_lock(&data->i2c_lock);
593	/*
594	 * Abort periodic measure mode.
595	 * To do any changes to the configuration while in periodic mode, we
596	 * have to send a break command to the sensor, which then falls back
597	 * to single shot (mode = 0).
598	 */
599	if (data->mode > 0) {
600		ret = i2c_master_send(client, sht3x_cmd_break,
601				      SHT3X_CMD_LENGTH);
602		if (ret != SHT3X_CMD_LENGTH)
603			goto out;
604		data->mode = 0;
605	}
606
607	if (mode > 0) {
608		if (data->setup.high_precision)
609			command = periodic_measure_commands_hpm[mode - 1];
610		else
611			command = periodic_measure_commands_lpm[mode - 1];
612
613		/* select mode */
614		ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
615		if (ret != SHT3X_CMD_LENGTH)
616			goto out;
617	}
618
619	/* select mode and command */
620	data->mode = mode;
621	sht3x_select_command(data);
622
623out:
624	mutex_unlock(&data->i2c_lock);
625	mutex_unlock(&data->data_lock);
626	if (ret != SHT3X_CMD_LENGTH)
627		return ret < 0 ? ret : -EIO;
628
629	return count;
630}
631
632static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, temp1_input_show, NULL, 0);
633static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, humidity1_input_show,
634			  NULL, 0);
635static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
636			  temp1_limit_show, temp1_limit_store,
637			  limit_max);
638static SENSOR_DEVICE_ATTR(humidity1_max, S_IRUGO | S_IWUSR,
639			  humidity1_limit_show, humidity1_limit_store,
640			  limit_max);
641static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
642			  temp1_limit_show, temp1_limit_store,
643			  limit_max_hyst);
644static SENSOR_DEVICE_ATTR(humidity1_max_hyst, S_IRUGO | S_IWUSR,
645			  humidity1_limit_show, humidity1_limit_store,
646			  limit_max_hyst);
647static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR,
648			  temp1_limit_show, temp1_limit_store,
649			  limit_min);
650static SENSOR_DEVICE_ATTR(humidity1_min, S_IRUGO | S_IWUSR,
651			  humidity1_limit_show, humidity1_limit_store,
652			  limit_min);
653static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO | S_IWUSR,
654			  temp1_limit_show, temp1_limit_store,
655			  limit_min_hyst);
656static SENSOR_DEVICE_ATTR(humidity1_min_hyst, S_IRUGO | S_IWUSR,
657			  humidity1_limit_show, humidity1_limit_store,
658			  limit_min_hyst);
659static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, temp1_alarm_show, NULL, 0);
660static SENSOR_DEVICE_ATTR(humidity1_alarm, S_IRUGO, humidity1_alarm_show,
661			  NULL, 0);
662static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR,
663			  heater_enable_show, heater_enable_store, 0);
664static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
665			  update_interval_show, update_interval_store, 0);
666
667static struct attribute *sht3x_attrs[] = {
668	&sensor_dev_attr_temp1_input.dev_attr.attr,
669	&sensor_dev_attr_humidity1_input.dev_attr.attr,
670	&sensor_dev_attr_temp1_max.dev_attr.attr,
671	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
672	&sensor_dev_attr_humidity1_max.dev_attr.attr,
673	&sensor_dev_attr_humidity1_max_hyst.dev_attr.attr,
674	&sensor_dev_attr_temp1_min.dev_attr.attr,
675	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
676	&sensor_dev_attr_humidity1_min.dev_attr.attr,
677	&sensor_dev_attr_humidity1_min_hyst.dev_attr.attr,
678	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
679	&sensor_dev_attr_humidity1_alarm.dev_attr.attr,
680	&sensor_dev_attr_heater_enable.dev_attr.attr,
681	&sensor_dev_attr_update_interval.dev_attr.attr,
682	NULL
683};
684
685static struct attribute *sts3x_attrs[] = {
686	&sensor_dev_attr_temp1_input.dev_attr.attr,
687	NULL
688};
689
690ATTRIBUTE_GROUPS(sht3x);
691ATTRIBUTE_GROUPS(sts3x);
692
693static int sht3x_probe(struct i2c_client *client,
694		       const struct i2c_device_id *id)
 
695{
696	int ret;
697	struct sht3x_data *data;
698	struct device *hwmon_dev;
699	struct i2c_adapter *adap = client->adapter;
700	struct device *dev = &client->dev;
701	const struct attribute_group **attribute_groups;
702
703	/*
704	 * we require full i2c support since the sht3x uses multi-byte read and
705	 * writes as well as multi-byte commands which are not supported by
706	 * the smbus protocol
707	 */
708	if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
709		return -ENODEV;
710
711	ret = i2c_master_send(client, sht3x_cmd_clear_status_reg,
712			      SHT3X_CMD_LENGTH);
713	if (ret != SHT3X_CMD_LENGTH)
714		return ret < 0 ? ret : -ENODEV;
715
716	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
717	if (!data)
718		return -ENOMEM;
719
720	data->setup.blocking_io = false;
721	data->setup.high_precision = true;
722	data->mode = 0;
723	data->last_update = jiffies - msecs_to_jiffies(3000);
724	data->client = client;
725	crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
726
727	if (client->dev.platform_data)
728		data->setup = *(struct sht3x_platform_data *)dev->platform_data;
729
730	sht3x_select_command(data);
731
732	mutex_init(&data->i2c_lock);
733	mutex_init(&data->data_lock);
734
 
 
 
 
 
 
 
735	ret = limits_update(data);
736	if (ret)
737		return ret;
738
739	if (id->driver_data == sts3x)
740		attribute_groups = sts3x_groups;
741	else
742		attribute_groups = sht3x_groups;
743
744	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
745							   client->name,
746							   data,
747							   attribute_groups);
748
749	if (IS_ERR(hwmon_dev))
750		dev_dbg(dev, "unable to register hwmon device\n");
751
752	return PTR_ERR_OR_ZERO(hwmon_dev);
753}
754
755/* device ID table */
756static const struct i2c_device_id sht3x_ids[] = {
757	{"sht3x", sht3x},
758	{"sts3x", sts3x},
759	{}
760};
761
762MODULE_DEVICE_TABLE(i2c, sht3x_ids);
763
764static struct i2c_driver sht3x_i2c_driver = {
765	.driver.name = "sht3x",
766	.probe       = sht3x_probe,
767	.id_table    = sht3x_ids,
768};
769
770module_i2c_driver(sht3x_i2c_driver);
771
772MODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
773MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
774MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
775MODULE_LICENSE("GPL");
v6.2
  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/delay.h>
 14#include <linux/err.h>
 15#include <linux/hwmon.h>
 16#include <linux/hwmon-sysfs.h>
 17#include <linux/i2c.h>
 18#include <linux/init.h>
 19#include <linux/kernel.h>
 20#include <linux/module.h>
 21#include <linux/slab.h>
 22#include <linux/jiffies.h>
 23#include <linux/platform_data/sht3x.h>
 24
 25/* commands (high precision mode) */
 26static const unsigned char sht3x_cmd_measure_blocking_hpm[]    = { 0x2c, 0x06 };
 27static const unsigned char sht3x_cmd_measure_nonblocking_hpm[] = { 0x24, 0x00 };
 28
 29/* commands (low power mode) */
 30static const unsigned char sht3x_cmd_measure_blocking_lpm[]    = { 0x2c, 0x10 };
 31static const unsigned char sht3x_cmd_measure_nonblocking_lpm[] = { 0x24, 0x16 };
 32
 33/* commands for periodic mode */
 34static const unsigned char sht3x_cmd_measure_periodic_mode[]   = { 0xe0, 0x00 };
 35static const unsigned char sht3x_cmd_break[]                   = { 0x30, 0x93 };
 36
 37/* commands for heater control */
 38static const unsigned char sht3x_cmd_heater_on[]               = { 0x30, 0x6d };
 39static const unsigned char sht3x_cmd_heater_off[]              = { 0x30, 0x66 };
 40
 41/* other commands */
 42static const unsigned char sht3x_cmd_read_status_reg[]         = { 0xf3, 0x2d };
 43static const unsigned char sht3x_cmd_clear_status_reg[]        = { 0x30, 0x41 };
 44
 45/* delays for non-blocking i2c commands, both in us */
 46#define SHT3X_NONBLOCKING_WAIT_TIME_HPM  15000
 47#define SHT3X_NONBLOCKING_WAIT_TIME_LPM   4000
 48
 49#define SHT3X_WORD_LEN         2
 50#define SHT3X_CMD_LENGTH       2
 51#define SHT3X_CRC8_LEN         1
 52#define SHT3X_RESPONSE_LENGTH  6
 53#define SHT3X_CRC8_POLYNOMIAL  0x31
 54#define SHT3X_CRC8_INIT        0xFF
 55#define SHT3X_MIN_TEMPERATURE  -45000
 56#define SHT3X_MAX_TEMPERATURE  130000
 57#define SHT3X_MIN_HUMIDITY     0
 58#define SHT3X_MAX_HUMIDITY     100000
 59
 60enum sht3x_chips {
 61	sht3x,
 62	sts3x,
 63};
 64
 65enum sht3x_limits {
 66	limit_max = 0,
 67	limit_max_hyst,
 68	limit_min,
 69	limit_min_hyst,
 70};
 71
 72DECLARE_CRC8_TABLE(sht3x_crc8_table);
 73
 74/* periodic measure commands (high precision mode) */
 75static const char periodic_measure_commands_hpm[][SHT3X_CMD_LENGTH] = {
 76	/* 0.5 measurements per second */
 77	{0x20, 0x32},
 78	/* 1 measurements per second */
 79	{0x21, 0x30},
 80	/* 2 measurements per second */
 81	{0x22, 0x36},
 82	/* 4 measurements per second */
 83	{0x23, 0x34},
 84	/* 10 measurements per second */
 85	{0x27, 0x37},
 86};
 87
 88/* periodic measure commands (low power mode) */
 89static const char periodic_measure_commands_lpm[][SHT3X_CMD_LENGTH] = {
 90	/* 0.5 measurements per second */
 91	{0x20, 0x2f},
 92	/* 1 measurements per second */
 93	{0x21, 0x2d},
 94	/* 2 measurements per second */
 95	{0x22, 0x2b},
 96	/* 4 measurements per second */
 97	{0x23, 0x29},
 98	/* 10 measurements per second */
 99	{0x27, 0x2a},
100};
101
102struct sht3x_limit_commands {
103	const char read_command[SHT3X_CMD_LENGTH];
104	const char write_command[SHT3X_CMD_LENGTH];
105};
106
107static const struct sht3x_limit_commands limit_commands[] = {
108	/* temp1_max, humidity1_max */
109	[limit_max] = { {0xe1, 0x1f}, {0x61, 0x1d} },
110	/* temp_1_max_hyst, humidity1_max_hyst */
111	[limit_max_hyst] = { {0xe1, 0x14}, {0x61, 0x16} },
112	/* temp1_min, humidity1_min */
113	[limit_min] = { {0xe1, 0x02}, {0x61, 0x00} },
114	/* temp_1_min_hyst, humidity1_min_hyst */
115	[limit_min_hyst] = { {0xe1, 0x09}, {0x61, 0x0B} },
116};
117
118#define SHT3X_NUM_LIMIT_CMD  ARRAY_SIZE(limit_commands)
119
120static const u16 mode_to_update_interval[] = {
121	   0,
122	2000,
123	1000,
124	 500,
125	 250,
126	 100,
127};
128
129struct sht3x_data {
130	struct i2c_client *client;
131	struct mutex i2c_lock; /* lock for sending i2c commands */
132	struct mutex data_lock; /* lock for updating driver data */
133
134	u8 mode;
135	const unsigned char *command;
136	u32 wait_time;			/* in us*/
137	unsigned long last_update;	/* last update in periodic mode*/
138
139	struct sht3x_platform_data setup;
140
141	/*
142	 * cached values for temperature and humidity and limits
143	 * the limits arrays have the following order:
144	 * max, max_hyst, min, min_hyst
145	 */
146	int temperature;
147	int temperature_limits[SHT3X_NUM_LIMIT_CMD];
148	u32 humidity;
149	u32 humidity_limits[SHT3X_NUM_LIMIT_CMD];
150};
151
152static u8 get_mode_from_update_interval(u16 value)
153{
154	size_t index;
155	u8 number_of_modes = ARRAY_SIZE(mode_to_update_interval);
156
157	if (value == 0)
158		return 0;
159
160	/* find next faster update interval */
161	for (index = 1; index < number_of_modes; index++) {
162		if (mode_to_update_interval[index] <= value)
163			return index;
164	}
165
166	return number_of_modes - 1;
167}
168
169static int sht3x_read_from_command(struct i2c_client *client,
170				   struct sht3x_data *data,
171				   const char *command,
172				   char *buf, int length, u32 wait_time)
173{
174	int ret;
175
176	mutex_lock(&data->i2c_lock);
177	ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
178
179	if (ret != SHT3X_CMD_LENGTH) {
180		ret = ret < 0 ? ret : -EIO;
181		goto out;
182	}
183
184	if (wait_time)
185		usleep_range(wait_time, wait_time + 1000);
186
187	ret = i2c_master_recv(client, buf, length);
188	if (ret != length) {
189		ret = ret < 0 ? ret : -EIO;
190		goto out;
191	}
192
193	ret = 0;
194out:
195	mutex_unlock(&data->i2c_lock);
196	return ret;
197}
198
199static int sht3x_extract_temperature(u16 raw)
200{
201	/*
202	 * From datasheet:
203	 * T = -45 + 175 * ST / 2^16
204	 * Adapted for integer fixed point (3 digit) arithmetic.
205	 */
206	return ((21875 * (int)raw) >> 13) - 45000;
207}
208
209static u32 sht3x_extract_humidity(u16 raw)
210{
211	/*
212	 * From datasheet:
213	 * RH = 100 * SRH / 2^16
214	 * Adapted for integer fixed point (3 digit) arithmetic.
215	 */
216	return (12500 * (u32)raw) >> 13;
217}
218
219static struct sht3x_data *sht3x_update_client(struct device *dev)
220{
221	struct sht3x_data *data = dev_get_drvdata(dev);
222	struct i2c_client *client = data->client;
223	u16 interval_ms = mode_to_update_interval[data->mode];
224	unsigned long interval_jiffies = msecs_to_jiffies(interval_ms);
225	unsigned char buf[SHT3X_RESPONSE_LENGTH];
226	u16 val;
227	int ret = 0;
228
229	mutex_lock(&data->data_lock);
230	/*
231	 * Only update cached readings once per update interval in periodic
232	 * mode. In single shot mode the sensor measures values on demand, so
233	 * every time the sysfs interface is called, a measurement is triggered.
234	 * In periodic mode however, the measurement process is handled
235	 * internally by the sensor and reading out sensor values only makes
236	 * sense if a new reading is available.
237	 */
238	if (time_after(jiffies, data->last_update + interval_jiffies)) {
239		ret = sht3x_read_from_command(client, data, data->command, buf,
240					      sizeof(buf), data->wait_time);
241		if (ret)
242			goto out;
243
244		val = be16_to_cpup((__be16 *)buf);
245		data->temperature = sht3x_extract_temperature(val);
246		val = be16_to_cpup((__be16 *)(buf + 3));
247		data->humidity = sht3x_extract_humidity(val);
248		data->last_update = jiffies;
249	}
250
251out:
252	mutex_unlock(&data->data_lock);
253	if (ret)
254		return ERR_PTR(ret);
255
256	return data;
257}
258
259/* sysfs attributes */
260static ssize_t temp1_input_show(struct device *dev,
261				struct device_attribute *attr, char *buf)
262{
263	struct sht3x_data *data = sht3x_update_client(dev);
264
265	if (IS_ERR(data))
266		return PTR_ERR(data);
267
268	return sprintf(buf, "%d\n", data->temperature);
269}
270
271static ssize_t humidity1_input_show(struct device *dev,
272				    struct device_attribute *attr, char *buf)
273{
274	struct sht3x_data *data = sht3x_update_client(dev);
275
276	if (IS_ERR(data))
277		return PTR_ERR(data);
278
279	return sprintf(buf, "%u\n", data->humidity);
280}
281
282/*
283 * limits_update must only be called from probe or with data_lock held
284 */
285static int limits_update(struct sht3x_data *data)
286{
287	int ret;
288	u8 index;
289	int temperature;
290	u32 humidity;
291	u16 raw;
292	char buffer[SHT3X_RESPONSE_LENGTH];
293	const struct sht3x_limit_commands *commands;
294	struct i2c_client *client = data->client;
295
296	for (index = 0; index < SHT3X_NUM_LIMIT_CMD; index++) {
297		commands = &limit_commands[index];
298		ret = sht3x_read_from_command(client, data,
299					      commands->read_command, buffer,
300					      SHT3X_RESPONSE_LENGTH, 0);
301
302		if (ret)
303			return ret;
304
305		raw = be16_to_cpup((__be16 *)buffer);
306		temperature = sht3x_extract_temperature((raw & 0x01ff) << 7);
307		humidity = sht3x_extract_humidity(raw & 0xfe00);
308		data->temperature_limits[index] = temperature;
309		data->humidity_limits[index] = humidity;
310	}
311
312	return ret;
313}
314
315static ssize_t temp1_limit_show(struct device *dev,
316				struct device_attribute *attr,
317				char *buf)
318{
319	struct sht3x_data *data = dev_get_drvdata(dev);
320	u8 index = to_sensor_dev_attr(attr)->index;
321	int temperature_limit = data->temperature_limits[index];
322
323	return sysfs_emit(buf, "%d\n", temperature_limit);
324}
325
326static ssize_t humidity1_limit_show(struct device *dev,
327				    struct device_attribute *attr,
328				    char *buf)
329{
330	struct sht3x_data *data = dev_get_drvdata(dev);
331	u8 index = to_sensor_dev_attr(attr)->index;
332	u32 humidity_limit = data->humidity_limits[index];
333
334	return sysfs_emit(buf, "%u\n", humidity_limit);
335}
336
337/*
338 * limit_store must only be called with data_lock held
339 */
340static size_t limit_store(struct device *dev,
341			  size_t count,
342			  u8 index,
343			  int temperature,
344			  u32 humidity)
345{
346	char buffer[SHT3X_CMD_LENGTH + SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
347	char *position = buffer;
348	int ret;
349	u16 raw;
350	struct sht3x_data *data = dev_get_drvdata(dev);
351	struct i2c_client *client = data->client;
352	const struct sht3x_limit_commands *commands;
353
354	commands = &limit_commands[index];
355
356	memcpy(position, commands->write_command, SHT3X_CMD_LENGTH);
357	position += SHT3X_CMD_LENGTH;
358	/*
359	 * ST = (T + 45) / 175 * 2^16
360	 * SRH = RH / 100 * 2^16
361	 * adapted for fixed point arithmetic and packed the same as
362	 * in limit_show()
363	 */
364	raw = ((u32)(temperature + 45000) * 24543) >> (16 + 7);
365	raw |= ((humidity * 42950) >> 16) & 0xfe00;
366
367	*((__be16 *)position) = cpu_to_be16(raw);
368	position += SHT3X_WORD_LEN;
369	*position = crc8(sht3x_crc8_table,
370			 position - SHT3X_WORD_LEN,
371			 SHT3X_WORD_LEN,
372			 SHT3X_CRC8_INIT);
373
374	mutex_lock(&data->i2c_lock);
375	ret = i2c_master_send(client, buffer, sizeof(buffer));
376	mutex_unlock(&data->i2c_lock);
377
378	if (ret != sizeof(buffer))
379		return ret < 0 ? ret : -EIO;
380
381	data->temperature_limits[index] = temperature;
382	data->humidity_limits[index] = humidity;
383	return count;
384}
385
386static ssize_t temp1_limit_store(struct device *dev,
387				 struct device_attribute *attr,
388				 const char *buf,
389				 size_t count)
390{
391	int temperature;
392	int ret;
393	struct sht3x_data *data = dev_get_drvdata(dev);
394	u8 index = to_sensor_dev_attr(attr)->index;
395
396	ret = kstrtoint(buf, 0, &temperature);
397	if (ret)
398		return ret;
399
400	temperature = clamp_val(temperature, SHT3X_MIN_TEMPERATURE,
401				SHT3X_MAX_TEMPERATURE);
402	mutex_lock(&data->data_lock);
403	ret = limit_store(dev, count, index, temperature,
404			  data->humidity_limits[index]);
405	mutex_unlock(&data->data_lock);
406
407	return ret;
408}
409
410static ssize_t humidity1_limit_store(struct device *dev,
411				     struct device_attribute *attr,
412				     const char *buf,
413				     size_t count)
414{
415	u32 humidity;
416	int ret;
417	struct sht3x_data *data = dev_get_drvdata(dev);
418	u8 index = to_sensor_dev_attr(attr)->index;
419
420	ret = kstrtou32(buf, 0, &humidity);
421	if (ret)
422		return ret;
423
424	humidity = clamp_val(humidity, SHT3X_MIN_HUMIDITY, SHT3X_MAX_HUMIDITY);
425	mutex_lock(&data->data_lock);
426	ret = limit_store(dev, count, index, data->temperature_limits[index],
427			  humidity);
428	mutex_unlock(&data->data_lock);
429
430	return ret;
431}
432
433static void sht3x_select_command(struct sht3x_data *data)
434{
435	/*
436	 * In blocking mode (clock stretching mode) the I2C bus
437	 * is blocked for other traffic, thus the call to i2c_master_recv()
438	 * will wait until the data is ready. For non blocking mode, we
439	 * have to wait ourselves.
440	 */
441	if (data->mode > 0) {
442		data->command = sht3x_cmd_measure_periodic_mode;
443		data->wait_time = 0;
444	} else if (data->setup.blocking_io) {
445		data->command = data->setup.high_precision ?
446				sht3x_cmd_measure_blocking_hpm :
447				sht3x_cmd_measure_blocking_lpm;
448		data->wait_time = 0;
449	} else {
450		if (data->setup.high_precision) {
451			data->command = sht3x_cmd_measure_nonblocking_hpm;
452			data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_HPM;
453		} else {
454			data->command = sht3x_cmd_measure_nonblocking_lpm;
455			data->wait_time = SHT3X_NONBLOCKING_WAIT_TIME_LPM;
456		}
457	}
458}
459
460static int status_register_read(struct device *dev,
461				struct device_attribute *attr,
462				char *buffer, int length)
463{
464	int ret;
465	struct sht3x_data *data = dev_get_drvdata(dev);
466	struct i2c_client *client = data->client;
467
468	ret = sht3x_read_from_command(client, data, sht3x_cmd_read_status_reg,
469				      buffer, length, 0);
470
471	return ret;
472}
473
474static ssize_t temp1_alarm_show(struct device *dev,
475				struct device_attribute *attr,
476				char *buf)
477{
478	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
479	int ret;
480
481	ret = status_register_read(dev, attr, buffer,
482				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
483	if (ret)
484		return ret;
485
486	return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x04));
487}
488
489static ssize_t humidity1_alarm_show(struct device *dev,
490				    struct device_attribute *attr,
491				    char *buf)
492{
493	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
494	int ret;
495
496	ret = status_register_read(dev, attr, buffer,
497				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
498	if (ret)
499		return ret;
500
501	return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x08));
502}
503
504static ssize_t heater_enable_show(struct device *dev,
505				  struct device_attribute *attr,
506				  char *buf)
507{
508	char buffer[SHT3X_WORD_LEN + SHT3X_CRC8_LEN];
509	int ret;
510
511	ret = status_register_read(dev, attr, buffer,
512				   SHT3X_WORD_LEN + SHT3X_CRC8_LEN);
513	if (ret)
514		return ret;
515
516	return sysfs_emit(buf, "%d\n", !!(buffer[0] & 0x20));
517}
518
519static ssize_t heater_enable_store(struct device *dev,
520				   struct device_attribute *attr,
521				   const char *buf,
522				   size_t count)
523{
524	struct sht3x_data *data = dev_get_drvdata(dev);
525	struct i2c_client *client = data->client;
526	int ret;
527	bool status;
528
529	ret = kstrtobool(buf, &status);
530	if (ret)
531		return ret;
532
533	mutex_lock(&data->i2c_lock);
534
535	if (status)
536		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_on,
537				      SHT3X_CMD_LENGTH);
538	else
539		ret = i2c_master_send(client, (char *)&sht3x_cmd_heater_off,
540				      SHT3X_CMD_LENGTH);
541
542	mutex_unlock(&data->i2c_lock);
543
544	return ret;
545}
546
547static ssize_t update_interval_show(struct device *dev,
548				    struct device_attribute *attr,
549				    char *buf)
550{
551	struct sht3x_data *data = dev_get_drvdata(dev);
552
553	return sysfs_emit(buf, "%u\n",
554			 mode_to_update_interval[data->mode]);
555}
556
557static ssize_t update_interval_store(struct device *dev,
558				     struct device_attribute *attr,
559				     const char *buf,
560				     size_t count)
561{
562	u16 update_interval;
563	u8 mode;
564	int ret;
565	const char *command;
566	struct sht3x_data *data = dev_get_drvdata(dev);
567	struct i2c_client *client = data->client;
568
569	ret = kstrtou16(buf, 0, &update_interval);
570	if (ret)
571		return ret;
572
573	mode = get_mode_from_update_interval(update_interval);
574
575	mutex_lock(&data->data_lock);
576	/* mode did not change */
577	if (mode == data->mode) {
578		mutex_unlock(&data->data_lock);
579		return count;
580	}
581
582	mutex_lock(&data->i2c_lock);
583	/*
584	 * Abort periodic measure mode.
585	 * To do any changes to the configuration while in periodic mode, we
586	 * have to send a break command to the sensor, which then falls back
587	 * to single shot (mode = 0).
588	 */
589	if (data->mode > 0) {
590		ret = i2c_master_send(client, sht3x_cmd_break,
591				      SHT3X_CMD_LENGTH);
592		if (ret != SHT3X_CMD_LENGTH)
593			goto out;
594		data->mode = 0;
595	}
596
597	if (mode > 0) {
598		if (data->setup.high_precision)
599			command = periodic_measure_commands_hpm[mode - 1];
600		else
601			command = periodic_measure_commands_lpm[mode - 1];
602
603		/* select mode */
604		ret = i2c_master_send(client, command, SHT3X_CMD_LENGTH);
605		if (ret != SHT3X_CMD_LENGTH)
606			goto out;
607	}
608
609	/* select mode and command */
610	data->mode = mode;
611	sht3x_select_command(data);
612
613out:
614	mutex_unlock(&data->i2c_lock);
615	mutex_unlock(&data->data_lock);
616	if (ret != SHT3X_CMD_LENGTH)
617		return ret < 0 ? ret : -EIO;
618
619	return count;
620}
621
622static SENSOR_DEVICE_ATTR_RO(temp1_input, temp1_input, 0);
623static SENSOR_DEVICE_ATTR_RO(humidity1_input, humidity1_input, 0);
624static SENSOR_DEVICE_ATTR_RW(temp1_max, temp1_limit, limit_max);
625static SENSOR_DEVICE_ATTR_RW(humidity1_max, humidity1_limit, limit_max);
626static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp1_limit, limit_max_hyst);
627static SENSOR_DEVICE_ATTR_RW(humidity1_max_hyst, humidity1_limit,
628			     limit_max_hyst);
629static SENSOR_DEVICE_ATTR_RW(temp1_min, temp1_limit, limit_min);
630static SENSOR_DEVICE_ATTR_RW(humidity1_min, humidity1_limit, limit_min);
631static SENSOR_DEVICE_ATTR_RW(temp1_min_hyst, temp1_limit, limit_min_hyst);
632static SENSOR_DEVICE_ATTR_RW(humidity1_min_hyst, humidity1_limit,
633			     limit_min_hyst);
634static SENSOR_DEVICE_ATTR_RO(temp1_alarm, temp1_alarm, 0);
635static SENSOR_DEVICE_ATTR_RO(humidity1_alarm, humidity1_alarm, 0);
636static SENSOR_DEVICE_ATTR_RW(heater_enable, heater_enable, 0);
637static SENSOR_DEVICE_ATTR_RW(update_interval, update_interval, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
638
639static struct attribute *sht3x_attrs[] = {
640	&sensor_dev_attr_temp1_input.dev_attr.attr,
641	&sensor_dev_attr_humidity1_input.dev_attr.attr,
642	&sensor_dev_attr_temp1_max.dev_attr.attr,
643	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
644	&sensor_dev_attr_humidity1_max.dev_attr.attr,
645	&sensor_dev_attr_humidity1_max_hyst.dev_attr.attr,
646	&sensor_dev_attr_temp1_min.dev_attr.attr,
647	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
648	&sensor_dev_attr_humidity1_min.dev_attr.attr,
649	&sensor_dev_attr_humidity1_min_hyst.dev_attr.attr,
650	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
651	&sensor_dev_attr_humidity1_alarm.dev_attr.attr,
652	&sensor_dev_attr_heater_enable.dev_attr.attr,
653	&sensor_dev_attr_update_interval.dev_attr.attr,
654	NULL
655};
656
657static struct attribute *sts3x_attrs[] = {
658	&sensor_dev_attr_temp1_input.dev_attr.attr,
659	NULL
660};
661
662ATTRIBUTE_GROUPS(sht3x);
663ATTRIBUTE_GROUPS(sts3x);
664
665static const struct i2c_device_id sht3x_ids[];
666
667static int sht3x_probe(struct i2c_client *client)
668{
669	int ret;
670	struct sht3x_data *data;
671	struct device *hwmon_dev;
672	struct i2c_adapter *adap = client->adapter;
673	struct device *dev = &client->dev;
674	const struct attribute_group **attribute_groups;
675
676	/*
677	 * we require full i2c support since the sht3x uses multi-byte read and
678	 * writes as well as multi-byte commands which are not supported by
679	 * the smbus protocol
680	 */
681	if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
682		return -ENODEV;
683
684	ret = i2c_master_send(client, sht3x_cmd_clear_status_reg,
685			      SHT3X_CMD_LENGTH);
686	if (ret != SHT3X_CMD_LENGTH)
687		return ret < 0 ? ret : -ENODEV;
688
689	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
690	if (!data)
691		return -ENOMEM;
692
693	data->setup.blocking_io = false;
694	data->setup.high_precision = true;
695	data->mode = 0;
696	data->last_update = jiffies - msecs_to_jiffies(3000);
697	data->client = client;
698	crc8_populate_msb(sht3x_crc8_table, SHT3X_CRC8_POLYNOMIAL);
699
700	if (client->dev.platform_data)
701		data->setup = *(struct sht3x_platform_data *)dev->platform_data;
702
703	sht3x_select_command(data);
704
705	mutex_init(&data->i2c_lock);
706	mutex_init(&data->data_lock);
707
708	/*
709	 * An attempt to read limits register too early
710	 * causes a NACK response from the chip.
711	 * Waiting for an empirical delay of 500 us solves the issue.
712	 */
713	usleep_range(500, 600);
714
715	ret = limits_update(data);
716	if (ret)
717		return ret;
718
719	if (i2c_match_id(sht3x_ids, client)->driver_data == sts3x)
720		attribute_groups = sts3x_groups;
721	else
722		attribute_groups = sht3x_groups;
723
724	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
725							   client->name,
726							   data,
727							   attribute_groups);
728
729	if (IS_ERR(hwmon_dev))
730		dev_dbg(dev, "unable to register hwmon device\n");
731
732	return PTR_ERR_OR_ZERO(hwmon_dev);
733}
734
735/* device ID table */
736static const struct i2c_device_id sht3x_ids[] = {
737	{"sht3x", sht3x},
738	{"sts3x", sts3x},
739	{}
740};
741
742MODULE_DEVICE_TABLE(i2c, sht3x_ids);
743
744static struct i2c_driver sht3x_i2c_driver = {
745	.driver.name = "sht3x",
746	.probe_new   = sht3x_probe,
747	.id_table    = sht3x_ids,
748};
749
750module_i2c_driver(sht3x_i2c_driver);
751
752MODULE_AUTHOR("David Frey <david.frey@sensirion.com>");
753MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
754MODULE_DESCRIPTION("Sensirion SHT3x humidity and temperature sensor driver");
755MODULE_LICENSE("GPL");