sht21.c 8.1 KB

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