sht21.c 8.1 KB

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