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