sht21.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Sensirion SHT21 humidity and temperature sensor driver
  3. *
  4. * Copyright (C) 2010 Urs Fleisch <[email protected]>
  5. *
  6. * Data sheet available at https://www.sensirion.com/file/datasheet_sht21
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/i2c.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/hwmon-sysfs.h>
  14. #include <linux/err.h>
  15. #include <linux/mutex.h>
  16. #include <linux/device.h>
  17. #include <linux/jiffies.h>
  18. /* I2C command bytes */
  19. #define SHT21_TRIG_T_MEASUREMENT_HM 0xe3
  20. #define SHT21_TRIG_RH_MEASUREMENT_HM 0xe5
  21. #define SHT21_READ_SNB_CMD1 0xFA
  22. #define SHT21_READ_SNB_CMD2 0x0F
  23. #define SHT21_READ_SNAC_CMD1 0xFC
  24. #define SHT21_READ_SNAC_CMD2 0xC9
  25. /**
  26. * struct sht21 - SHT21 device specific data
  27. * @client: I2C client device
  28. * @lock: mutex to protect measurement values
  29. * @last_update: time of last update (jiffies)
  30. * @temperature: cached temperature measurement value
  31. * @humidity: cached humidity measurement value
  32. * @valid: only 0 before first measurement is taken
  33. * @eic: cached electronic identification code text
  34. */
  35. struct sht21 {
  36. struct i2c_client *client;
  37. struct mutex lock;
  38. unsigned long last_update;
  39. int temperature;
  40. int humidity;
  41. bool valid;
  42. char eic[18];
  43. };
  44. /**
  45. * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
  46. * milli celsius
  47. * @ticks: temperature ticks value received from sensor
  48. */
  49. static inline int sht21_temp_ticks_to_millicelsius(int ticks)
  50. {
  51. ticks &= ~0x0003; /* clear status bits */
  52. /*
  53. * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
  54. * optimized for integer fixed point (3 digits) arithmetic
  55. */
  56. return ((21965 * ticks) >> 13) - 46850;
  57. }
  58. /**
  59. * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
  60. * one-thousandths of a percent relative humidity
  61. * @ticks: humidity ticks value received from sensor
  62. */
  63. static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
  64. {
  65. ticks &= ~0x0003; /* clear status bits */
  66. /*
  67. * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
  68. * optimized for integer fixed point (3 digits) arithmetic
  69. */
  70. return ((15625 * ticks) >> 13) - 6000;
  71. }
  72. /**
  73. * sht21_update_measurements() - get updated measurements from device
  74. * @dev: device
  75. *
  76. * Returns 0 on success, else negative errno.
  77. */
  78. static int sht21_update_measurements(struct device *dev)
  79. {
  80. int ret = 0;
  81. struct sht21 *sht21 = dev_get_drvdata(dev);
  82. struct i2c_client *client = sht21->client;
  83. mutex_lock(&sht21->lock);
  84. /*
  85. * Data sheet 2.4:
  86. * SHT2x should not be active for more than 10% of the time - e.g.
  87. * maximum two measurements per second at 12bit accuracy shall be made.
  88. */
  89. if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
  90. ret = i2c_smbus_read_word_swapped(client,
  91. SHT21_TRIG_T_MEASUREMENT_HM);
  92. if (ret < 0)
  93. goto out;
  94. sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
  95. ret = i2c_smbus_read_word_swapped(client,
  96. SHT21_TRIG_RH_MEASUREMENT_HM);
  97. if (ret < 0)
  98. goto out;
  99. sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
  100. sht21->last_update = jiffies;
  101. sht21->valid = true;
  102. }
  103. out:
  104. mutex_unlock(&sht21->lock);
  105. return ret >= 0 ? 0 : ret;
  106. }
  107. /**
  108. * sht21_show_temperature() - show temperature measurement value in sysfs
  109. * @dev: device
  110. * @attr: device attribute
  111. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  112. *
  113. * Will be called on read access to temp1_input sysfs attribute.
  114. * Returns number of bytes written into buffer, negative errno on error.
  115. */
  116. static ssize_t sht21_temperature_show(struct device *dev,
  117. struct device_attribute *attr,
  118. char *buf)
  119. {
  120. struct sht21 *sht21 = dev_get_drvdata(dev);
  121. int ret;
  122. ret = sht21_update_measurements(dev);
  123. if (ret < 0)
  124. return ret;
  125. return sprintf(buf, "%d\n", sht21->temperature);
  126. }
  127. /**
  128. * sht21_show_humidity() - show humidity measurement value in sysfs
  129. * @dev: device
  130. * @attr: device attribute
  131. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  132. *
  133. * Will be called on read access to humidity1_input sysfs attribute.
  134. * Returns number of bytes written into buffer, negative errno on error.
  135. */
  136. static ssize_t sht21_humidity_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. struct sht21 *sht21 = dev_get_drvdata(dev);
  140. int ret;
  141. ret = sht21_update_measurements(dev);
  142. if (ret < 0)
  143. return ret;
  144. return sprintf(buf, "%d\n", sht21->humidity);
  145. }
  146. static ssize_t eic_read(struct sht21 *sht21)
  147. {
  148. struct i2c_client *client = sht21->client;
  149. u8 tx[2];
  150. u8 rx[8];
  151. u8 eic[8];
  152. struct i2c_msg msgs[2] = {
  153. {
  154. .addr = client->addr,
  155. .flags = 0,
  156. .len = 2,
  157. .buf = tx,
  158. },
  159. {
  160. .addr = client->addr,
  161. .flags = I2C_M_RD,
  162. .len = 8,
  163. .buf = rx,
  164. },
  165. };
  166. int ret;
  167. tx[0] = SHT21_READ_SNB_CMD1;
  168. tx[1] = SHT21_READ_SNB_CMD2;
  169. ret = i2c_transfer(client->adapter, msgs, 2);
  170. if (ret < 0)
  171. goto out;
  172. eic[2] = rx[0];
  173. eic[3] = rx[2];
  174. eic[4] = rx[4];
  175. eic[5] = rx[6];
  176. tx[0] = SHT21_READ_SNAC_CMD1;
  177. tx[1] = SHT21_READ_SNAC_CMD2;
  178. msgs[1].len = 6;
  179. ret = i2c_transfer(client->adapter, msgs, 2);
  180. if (ret < 0)
  181. goto out;
  182. eic[0] = rx[3];
  183. eic[1] = rx[4];
  184. eic[6] = rx[0];
  185. eic[7] = rx[1];
  186. ret = snprintf(sht21->eic, sizeof(sht21->eic),
  187. "%02x%02x%02x%02x%02x%02x%02x%02x\n",
  188. eic[0], eic[1], eic[2], eic[3],
  189. eic[4], eic[5], eic[6], eic[7]);
  190. out:
  191. if (ret < 0)
  192. sht21->eic[0] = 0;
  193. return ret;
  194. }
  195. /**
  196. * eic_show() - show Electronic Identification Code in sysfs
  197. * @dev: device
  198. * @attr: device attribute
  199. * @buf: sysfs buffer (PAGE_SIZE) where EIC is written
  200. *
  201. * Will be called on read access to eic sysfs attribute.
  202. * Returns number of bytes written into buffer, negative errno on error.
  203. */
  204. static ssize_t eic_show(struct device *dev,
  205. struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct sht21 *sht21 = dev_get_drvdata(dev);
  209. int ret;
  210. ret = sizeof(sht21->eic) - 1;
  211. mutex_lock(&sht21->lock);
  212. if (!sht21->eic[0])
  213. ret = eic_read(sht21);
  214. if (ret > 0)
  215. memcpy(buf, sht21->eic, ret);
  216. mutex_unlock(&sht21->lock);
  217. return ret;
  218. }
  219. /* sysfs attributes */
  220. static SENSOR_DEVICE_ATTR_RO(temp1_input, sht21_temperature, 0);
  221. static SENSOR_DEVICE_ATTR_RO(humidity1_input, sht21_humidity, 0);
  222. static DEVICE_ATTR_RO(eic);
  223. static struct attribute *sht21_attrs[] = {
  224. &sensor_dev_attr_temp1_input.dev_attr.attr,
  225. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  226. &dev_attr_eic.attr,
  227. NULL
  228. };
  229. ATTRIBUTE_GROUPS(sht21);
  230. static int sht21_probe(struct i2c_client *client)
  231. {
  232. struct device *dev = &client->dev;
  233. struct device *hwmon_dev;
  234. struct sht21 *sht21;
  235. if (!i2c_check_functionality(client->adapter,
  236. I2C_FUNC_SMBUS_WORD_DATA)) {
  237. dev_err(&client->dev,
  238. "adapter does not support SMBus word transactions\n");
  239. return -ENODEV;
  240. }
  241. sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
  242. if (!sht21)
  243. return -ENOMEM;
  244. sht21->client = client;
  245. mutex_init(&sht21->lock);
  246. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  247. sht21, sht21_groups);
  248. return PTR_ERR_OR_ZERO(hwmon_dev);
  249. }
  250. /* Device ID table */
  251. static const struct i2c_device_id sht21_id[] = {
  252. { "sht21", 0 },
  253. { }
  254. };
  255. MODULE_DEVICE_TABLE(i2c, sht21_id);
  256. static struct i2c_driver sht21_driver = {
  257. .driver.name = "sht21",
  258. .probe_new = sht21_probe,
  259. .id_table = sht21_id,
  260. };
  261. module_i2c_driver(sht21_driver);
  262. MODULE_AUTHOR("Urs Fleisch <[email protected]>");
  263. MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
  264. MODULE_LICENSE("GPL");