sht3x.c 21 KB

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