sht4x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) Linumiz 2021
  4. *
  5. * sht4x.c - Linux hwmon driver for SHT4x Temperature and Humidity sensor
  6. *
  7. * Author: Navin Sankar Velliangiri <navin@linumiz.com>
  8. */
  9. #include <linux/crc8.h>
  10. #include <linux/delay.h>
  11. #include <linux/hwmon.h>
  12. #include <linux/hwmon-sysfs.h>
  13. #include <linux/i2c.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/module.h>
  16. /*
  17. * Poll intervals (in milliseconds)
  18. */
  19. #define SHT4X_MIN_POLL_INTERVAL 2000
  20. /*
  21. * I2C command delays (in microseconds)
  22. */
  23. #define SHT4X_MEAS_DELAY_HPM 8200 /* see t_MEAS,h in datasheet */
  24. #define SHT4X_DELAY_EXTRA 10000
  25. /*
  26. * Command Bytes
  27. */
  28. #define SHT4X_CMD_MEASURE_HPM 0b11111101
  29. #define SHT4X_CMD_RESET 0b10010100
  30. #define SHT4X_CMD_HEATER_20_1 0b00011110
  31. #define SHT4X_CMD_HEATER_20_01 0b00010101
  32. #define SHT4X_CMD_HEATER_110_1 0b00101111
  33. #define SHT4X_CMD_HEATER_110_01 0b00100100
  34. #define SHT4X_CMD_HEATER_200_1 0b00111001
  35. #define SHT4X_CMD_HEATER_200_01 0b00110010
  36. #define SHT4X_CMD_LEN 1
  37. #define SHT4X_CRC8_LEN 1
  38. #define SHT4X_WORD_LEN 2
  39. #define SHT4X_RESPONSE_LENGTH 6
  40. #define SHT4X_CRC8_POLYNOMIAL 0x31
  41. #define SHT4X_CRC8_INIT 0xff
  42. #define SHT4X_MIN_TEMPERATURE -45000
  43. #define SHT4X_MAX_TEMPERATURE 125000
  44. #define SHT4X_MIN_HUMIDITY 0
  45. #define SHT4X_MAX_HUMIDITY 100000
  46. DECLARE_CRC8_TABLE(sht4x_crc8_table);
  47. /**
  48. * struct sht4x_data - All the data required to operate an SHT4X chip
  49. * @client: the i2c client associated with the SHT4X
  50. * @heating_complete: the time that the last heating finished
  51. * @data_pending: true if and only if there are measurements to retrieve after heating
  52. * @heater_power: the power at which the heater will be started
  53. * @heater_time: the time for which the heater will remain turned on
  54. * @valid: validity of fields below
  55. * @update_interval: the minimum poll interval
  56. * @last_updated: the previous time that the SHT4X was polled
  57. * @temperature: the latest temperature value received from the SHT4X
  58. * @humidity: the latest humidity value received from the SHT4X
  59. */
  60. struct sht4x_data {
  61. struct i2c_client *client;
  62. unsigned long heating_complete; /* in jiffies */
  63. bool data_pending;
  64. u32 heater_power; /* in milli-watts */
  65. u32 heater_time; /* in milli-seconds */
  66. bool valid; /* validity of fields below */
  67. long update_interval; /* in milli-seconds */
  68. long last_updated; /* in jiffies */
  69. s32 temperature;
  70. s32 humidity;
  71. };
  72. /**
  73. * sht4x_read_values() - read and parse the raw data from the SHT4X
  74. * @data: the struct sht4x_data to use for the lock
  75. * Return: 0 if successful, -ERRNO if not
  76. */
  77. static int sht4x_read_values(struct sht4x_data *data)
  78. {
  79. int ret;
  80. u16 t_ticks, rh_ticks;
  81. unsigned long next_update;
  82. struct i2c_client *client = data->client;
  83. u8 crc;
  84. u8 cmd[SHT4X_CMD_LEN] = {SHT4X_CMD_MEASURE_HPM};
  85. u8 raw_data[SHT4X_RESPONSE_LENGTH];
  86. unsigned long curr_jiffies;
  87. curr_jiffies = jiffies;
  88. if (time_before(curr_jiffies, data->heating_complete))
  89. msleep(jiffies_to_msecs(data->heating_complete - curr_jiffies));
  90. if (data->data_pending &&
  91. time_before(jiffies, data->heating_complete + data->update_interval)) {
  92. data->data_pending = false;
  93. } else {
  94. next_update = data->last_updated +
  95. msecs_to_jiffies(data->update_interval);
  96. if (data->valid && time_before_eq(jiffies, next_update))
  97. return 0;
  98. ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
  99. if (ret < 0)
  100. return ret;
  101. usleep_range(SHT4X_MEAS_DELAY_HPM, SHT4X_MEAS_DELAY_HPM + SHT4X_DELAY_EXTRA);
  102. }
  103. ret = i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH);
  104. if (ret != SHT4X_RESPONSE_LENGTH) {
  105. if (ret >= 0)
  106. ret = -ENODATA;
  107. return ret;
  108. }
  109. t_ticks = raw_data[0] << 8 | raw_data[1];
  110. rh_ticks = raw_data[3] << 8 | raw_data[4];
  111. crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
  112. if (crc != raw_data[2]) {
  113. dev_err(&client->dev, "data integrity check failed\n");
  114. return -EIO;
  115. }
  116. crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
  117. if (crc != raw_data[5]) {
  118. dev_err(&client->dev, "data integrity check failed\n");
  119. return -EIO;
  120. }
  121. data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000;
  122. data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000;
  123. data->last_updated = jiffies;
  124. data->valid = true;
  125. return 0;
  126. }
  127. static ssize_t sht4x_interval_write(struct sht4x_data *data, long val)
  128. {
  129. data->update_interval = clamp_val(val, SHT4X_MIN_POLL_INTERVAL, INT_MAX);
  130. return 0;
  131. }
  132. /* sht4x_interval_read() - read the minimum poll interval in milliseconds */
  133. static size_t sht4x_interval_read(struct sht4x_data *data, long *val)
  134. {
  135. *val = data->update_interval;
  136. return 0;
  137. }
  138. /* sht4x_temperature1_read() - read the temperature in millidegrees */
  139. static int sht4x_temperature1_read(struct sht4x_data *data, long *val)
  140. {
  141. int ret;
  142. ret = sht4x_read_values(data);
  143. if (ret < 0)
  144. return ret;
  145. *val = data->temperature;
  146. return 0;
  147. }
  148. /* sht4x_humidity1_read() - read a relative humidity in millipercent */
  149. static int sht4x_humidity1_read(struct sht4x_data *data, long *val)
  150. {
  151. int ret;
  152. ret = sht4x_read_values(data);
  153. if (ret < 0)
  154. return ret;
  155. *val = data->humidity;
  156. return 0;
  157. }
  158. static umode_t sht4x_hwmon_visible(const void *data,
  159. enum hwmon_sensor_types type,
  160. u32 attr, int channel)
  161. {
  162. switch (type) {
  163. case hwmon_temp:
  164. case hwmon_humidity:
  165. return 0444;
  166. case hwmon_chip:
  167. return 0644;
  168. default:
  169. return 0;
  170. }
  171. }
  172. static int sht4x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  173. u32 attr, int channel, long *val)
  174. {
  175. struct sht4x_data *data = dev_get_drvdata(dev);
  176. switch (type) {
  177. case hwmon_temp:
  178. return sht4x_temperature1_read(data, val);
  179. case hwmon_humidity:
  180. return sht4x_humidity1_read(data, val);
  181. case hwmon_chip:
  182. return sht4x_interval_read(data, val);
  183. default:
  184. return -EOPNOTSUPP;
  185. }
  186. }
  187. static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
  188. u32 attr, int channel, long val)
  189. {
  190. struct sht4x_data *data = dev_get_drvdata(dev);
  191. switch (type) {
  192. case hwmon_chip:
  193. return sht4x_interval_write(data, val);
  194. default:
  195. return -EOPNOTSUPP;
  196. }
  197. }
  198. static ssize_t heater_enable_show(struct device *dev,
  199. struct device_attribute *attr,
  200. char *buf)
  201. {
  202. struct sht4x_data *data = dev_get_drvdata(dev);
  203. return sysfs_emit(buf, "%u\n", time_before(jiffies, data->heating_complete));
  204. }
  205. static ssize_t heater_enable_store(struct device *dev,
  206. struct device_attribute *attr,
  207. const char *buf,
  208. size_t count)
  209. {
  210. struct sht4x_data *data = dev_get_drvdata(dev);
  211. bool status;
  212. ssize_t ret;
  213. u8 cmd;
  214. u32 heating_time_bound;
  215. ret = kstrtobool(buf, &status);
  216. if (ret)
  217. return ret;
  218. if (!status)
  219. return -EINVAL;
  220. if (data->heater_time == 100) {
  221. if (data->heater_power == 20)
  222. cmd = SHT4X_CMD_HEATER_20_01;
  223. else if (data->heater_power == 110)
  224. cmd = SHT4X_CMD_HEATER_110_01;
  225. else /* data->heater_power == 200 */
  226. cmd = SHT4X_CMD_HEATER_200_01;
  227. heating_time_bound = 110;
  228. } else { /* data->heater_time == 1000 */
  229. if (data->heater_power == 20)
  230. cmd = SHT4X_CMD_HEATER_20_1;
  231. else if (data->heater_power == 110)
  232. cmd = SHT4X_CMD_HEATER_110_1;
  233. else /* data->heater_power == 200 */
  234. cmd = SHT4X_CMD_HEATER_200_1;
  235. heating_time_bound = 1100;
  236. }
  237. if (time_before(jiffies, data->heating_complete))
  238. return -EBUSY;
  239. ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
  240. if (ret < 0)
  241. return ret;
  242. data->heating_complete = jiffies + msecs_to_jiffies(heating_time_bound);
  243. data->data_pending = true;
  244. return 0;
  245. }
  246. static ssize_t heater_power_show(struct device *dev,
  247. struct device_attribute *attr,
  248. char *buf)
  249. {
  250. struct sht4x_data *data = dev_get_drvdata(dev);
  251. return sysfs_emit(buf, "%u\n", data->heater_power);
  252. }
  253. static ssize_t heater_power_store(struct device *dev,
  254. struct device_attribute *attr,
  255. const char *buf,
  256. size_t count)
  257. {
  258. struct sht4x_data *data = dev_get_drvdata(dev);
  259. u32 power;
  260. ssize_t ret;
  261. ret = kstrtou32(buf, 10, &power);
  262. if (ret)
  263. return ret;
  264. if (power != 20 && power != 110 && power != 200)
  265. return -EINVAL;
  266. data->heater_power = power;
  267. return count;
  268. }
  269. static ssize_t heater_time_show(struct device *dev,
  270. struct device_attribute *attr,
  271. char *buf)
  272. {
  273. struct sht4x_data *data = dev_get_drvdata(dev);
  274. return sysfs_emit(buf, "%u\n", data->heater_time);
  275. }
  276. static ssize_t heater_time_store(struct device *dev,
  277. struct device_attribute *attr,
  278. const char *buf,
  279. size_t count)
  280. {
  281. struct sht4x_data *data = dev_get_drvdata(dev);
  282. u32 time;
  283. ssize_t ret;
  284. ret = kstrtou32(buf, 10, &time);
  285. if (ret)
  286. return ret;
  287. if (time != 100 && time != 1000)
  288. return -EINVAL;
  289. data->heater_time = time;
  290. return count;
  291. }
  292. static DEVICE_ATTR_RW(heater_enable);
  293. static DEVICE_ATTR_RW(heater_power);
  294. static DEVICE_ATTR_RW(heater_time);
  295. static struct attribute *sht4x_attrs[] = {
  296. &dev_attr_heater_enable.attr,
  297. &dev_attr_heater_power.attr,
  298. &dev_attr_heater_time.attr,
  299. NULL
  300. };
  301. ATTRIBUTE_GROUPS(sht4x);
  302. static const struct hwmon_channel_info * const sht4x_info[] = {
  303. HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
  304. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
  305. HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT),
  306. NULL,
  307. };
  308. static const struct hwmon_ops sht4x_hwmon_ops = {
  309. .is_visible = sht4x_hwmon_visible,
  310. .read = sht4x_hwmon_read,
  311. .write = sht4x_hwmon_write,
  312. };
  313. static const struct hwmon_chip_info sht4x_chip_info = {
  314. .ops = &sht4x_hwmon_ops,
  315. .info = sht4x_info,
  316. };
  317. static int sht4x_probe(struct i2c_client *client)
  318. {
  319. struct device *device = &client->dev;
  320. struct device *hwmon_dev;
  321. struct sht4x_data *data;
  322. u8 cmd[] = {SHT4X_CMD_RESET};
  323. int ret;
  324. /*
  325. * we require full i2c support since the sht4x uses multi-byte read and
  326. * writes as well as multi-byte commands which are not supported by
  327. * the smbus protocol
  328. */
  329. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  330. return -EOPNOTSUPP;
  331. data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL);
  332. if (!data)
  333. return -ENOMEM;
  334. data->update_interval = SHT4X_MIN_POLL_INTERVAL;
  335. data->client = client;
  336. data->heater_power = 200;
  337. data->heater_time = 1000;
  338. data->heating_complete = jiffies;
  339. crc8_populate_msb(sht4x_crc8_table, SHT4X_CRC8_POLYNOMIAL);
  340. ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
  341. if (ret < 0)
  342. return ret;
  343. if (ret != SHT4X_CMD_LEN)
  344. return -EIO;
  345. hwmon_dev = devm_hwmon_device_register_with_info(device,
  346. client->name,
  347. data,
  348. &sht4x_chip_info,
  349. sht4x_groups);
  350. return PTR_ERR_OR_ZERO(hwmon_dev);
  351. }
  352. static const struct i2c_device_id sht4x_id[] = {
  353. { "sht4x" },
  354. { },
  355. };
  356. MODULE_DEVICE_TABLE(i2c, sht4x_id);
  357. static const struct of_device_id sht4x_of_match[] = {
  358. { .compatible = "sensirion,sht4x" },
  359. { }
  360. };
  361. MODULE_DEVICE_TABLE(of, sht4x_of_match);
  362. static struct i2c_driver sht4x_driver = {
  363. .driver = {
  364. .name = "sht4x",
  365. .of_match_table = sht4x_of_match,
  366. },
  367. .probe = sht4x_probe,
  368. .id_table = sht4x_id,
  369. };
  370. module_i2c_driver(sht4x_driver);
  371. MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>");
  372. MODULE_DESCRIPTION("Sensirion SHT4x humidity and temperature sensor driver");
  373. MODULE_LICENSE("GPL v2");