sht21.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  36. * struct sht21 - SHT21 device specific data
  37. * @hwmon_dev: device registered with hwmon
  38. * @lock: mutex to protect measurement values
  39. * @valid: only 0 before first measurement is taken
  40. * @last_update: time of last update (jiffies)
  41. * @temperature: cached temperature measurement value
  42. * @humidity: cached humidity measurement value
  43. */
  44. struct sht21 {
  45. struct i2c_client *client;
  46. struct mutex lock;
  47. char valid;
  48. unsigned long last_update;
  49. int temperature;
  50. int humidity;
  51. };
  52. /**
  53. * sht21_temp_ticks_to_millicelsius() - convert raw temperature ticks to
  54. * milli celsius
  55. * @ticks: temperature ticks value received from sensor
  56. */
  57. static inline int sht21_temp_ticks_to_millicelsius(int ticks)
  58. {
  59. ticks &= ~0x0003; /* clear status bits */
  60. /*
  61. * Formula T = -46.85 + 175.72 * ST / 2^16 from data sheet 6.2,
  62. * optimized for integer fixed point (3 digits) arithmetic
  63. */
  64. return ((21965 * ticks) >> 13) - 46850;
  65. }
  66. /**
  67. * sht21_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
  68. * one-thousandths of a percent relative humidity
  69. * @ticks: humidity ticks value received from sensor
  70. */
  71. static inline int sht21_rh_ticks_to_per_cent_mille(int ticks)
  72. {
  73. ticks &= ~0x0003; /* clear status bits */
  74. /*
  75. * Formula RH = -6 + 125 * SRH / 2^16 from data sheet 6.1,
  76. * optimized for integer fixed point (3 digits) arithmetic
  77. */
  78. return ((15625 * ticks) >> 13) - 6000;
  79. }
  80. /**
  81. * sht21_update_measurements() - get updated measurements from device
  82. * @dev: device
  83. *
  84. * Returns 0 on success, else negative errno.
  85. */
  86. static int sht21_update_measurements(struct device *dev)
  87. {
  88. int ret = 0;
  89. struct sht21 *sht21 = dev_get_drvdata(dev);
  90. struct i2c_client *client = sht21->client;
  91. mutex_lock(&sht21->lock);
  92. /*
  93. * Data sheet 2.4:
  94. * SHT2x should not be active for more than 10% of the time - e.g.
  95. * maximum two measurements per second at 12bit accuracy shall be made.
  96. */
  97. if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
  98. ret = i2c_smbus_read_word_swapped(client,
  99. SHT21_TRIG_T_MEASUREMENT_HM);
  100. if (ret < 0)
  101. goto out;
  102. sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
  103. ret = i2c_smbus_read_word_swapped(client,
  104. SHT21_TRIG_RH_MEASUREMENT_HM);
  105. if (ret < 0)
  106. goto out;
  107. sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
  108. sht21->last_update = jiffies;
  109. sht21->valid = 1;
  110. }
  111. out:
  112. mutex_unlock(&sht21->lock);
  113. return ret >= 0 ? 0 : ret;
  114. }
  115. /**
  116. * sht21_show_temperature() - show temperature measurement value in sysfs
  117. * @dev: device
  118. * @attr: device attribute
  119. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  120. *
  121. * Will be called on read access to temp1_input sysfs attribute.
  122. * Returns number of bytes written into buffer, negative errno on error.
  123. */
  124. static ssize_t sht21_show_temperature(struct device *dev,
  125. struct device_attribute *attr,
  126. char *buf)
  127. {
  128. struct sht21 *sht21 = dev_get_drvdata(dev);
  129. int ret;
  130. ret = sht21_update_measurements(dev);
  131. if (ret < 0)
  132. return ret;
  133. return sprintf(buf, "%d\n", sht21->temperature);
  134. }
  135. /**
  136. * sht21_show_humidity() - show humidity measurement value in sysfs
  137. * @dev: device
  138. * @attr: device attribute
  139. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  140. *
  141. * Will be called on read access to humidity1_input sysfs attribute.
  142. * Returns number of bytes written into buffer, negative errno on error.
  143. */
  144. static ssize_t sht21_show_humidity(struct device *dev,
  145. struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct sht21 *sht21 = dev_get_drvdata(dev);
  149. int ret;
  150. ret = sht21_update_measurements(dev);
  151. if (ret < 0)
  152. return ret;
  153. return sprintf(buf, "%d\n", sht21->humidity);
  154. }
  155. /* sysfs attributes */
  156. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
  157. NULL, 0);
  158. static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
  159. NULL, 0);
  160. static struct attribute *sht21_attrs[] = {
  161. &sensor_dev_attr_temp1_input.dev_attr.attr,
  162. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  163. NULL
  164. };
  165. ATTRIBUTE_GROUPS(sht21);
  166. static int sht21_probe(struct i2c_client *client,
  167. const struct i2c_device_id *id)
  168. {
  169. struct device *dev = &client->dev;
  170. struct device *hwmon_dev;
  171. struct sht21 *sht21;
  172. if (!i2c_check_functionality(client->adapter,
  173. I2C_FUNC_SMBUS_WORD_DATA)) {
  174. dev_err(&client->dev,
  175. "adapter does not support SMBus word transactions\n");
  176. return -ENODEV;
  177. }
  178. sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL);
  179. if (!sht21)
  180. return -ENOMEM;
  181. sht21->client = client;
  182. mutex_init(&sht21->lock);
  183. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  184. sht21, sht21_groups);
  185. return PTR_ERR_OR_ZERO(hwmon_dev);
  186. }
  187. /* Device ID table */
  188. static const struct i2c_device_id sht21_id[] = {
  189. { "sht21", 0 },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(i2c, sht21_id);
  193. static struct i2c_driver sht21_driver = {
  194. .driver.name = "sht21",
  195. .probe = sht21_probe,
  196. .id_table = sht21_id,
  197. };
  198. module_i2c_driver(sht21_driver);
  199. MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
  200. MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
  201. MODULE_LICENSE("GPL");