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