sht3x.c 20 KB

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