sht3x.c 22 KB

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