sht21.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 device *hwmon_dev;
  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. * @client: I2C client device
  83. *
  84. * Returns 0 on success, else negative errno.
  85. */
  86. static int sht21_update_measurements(struct i2c_client *client)
  87. {
  88. int ret = 0;
  89. struct sht21 *sht21 = i2c_get_clientdata(client);
  90. mutex_lock(&sht21->lock);
  91. /*
  92. * Data sheet 2.4:
  93. * SHT2x should not be active for more than 10% of the time - e.g.
  94. * maximum two measurements per second at 12bit accuracy shall be made.
  95. */
  96. if (time_after(jiffies, sht21->last_update + HZ / 2) || !sht21->valid) {
  97. ret = i2c_smbus_read_word_swapped(client,
  98. SHT21_TRIG_T_MEASUREMENT_HM);
  99. if (ret < 0)
  100. goto out;
  101. sht21->temperature = sht21_temp_ticks_to_millicelsius(ret);
  102. ret = i2c_smbus_read_word_swapped(client,
  103. SHT21_TRIG_RH_MEASUREMENT_HM);
  104. if (ret < 0)
  105. goto out;
  106. sht21->humidity = sht21_rh_ticks_to_per_cent_mille(ret);
  107. sht21->last_update = jiffies;
  108. sht21->valid = 1;
  109. }
  110. out:
  111. mutex_unlock(&sht21->lock);
  112. return ret >= 0 ? 0 : ret;
  113. }
  114. /**
  115. * sht21_show_temperature() - show temperature measurement value in sysfs
  116. * @dev: device
  117. * @attr: device attribute
  118. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  119. *
  120. * Will be called on read access to temp1_input sysfs attribute.
  121. * Returns number of bytes written into buffer, negative errno on error.
  122. */
  123. static ssize_t sht21_show_temperature(struct device *dev,
  124. struct device_attribute *attr,
  125. char *buf)
  126. {
  127. struct i2c_client *client = to_i2c_client(dev);
  128. struct sht21 *sht21 = i2c_get_clientdata(client);
  129. int ret = sht21_update_measurements(client);
  130. if (ret < 0)
  131. return ret;
  132. return sprintf(buf, "%d\n", sht21->temperature);
  133. }
  134. /**
  135. * sht21_show_humidity() - show humidity measurement value in sysfs
  136. * @dev: device
  137. * @attr: device attribute
  138. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  139. *
  140. * Will be called on read access to humidity1_input sysfs attribute.
  141. * Returns number of bytes written into buffer, negative errno on error.
  142. */
  143. static ssize_t sht21_show_humidity(struct device *dev,
  144. struct device_attribute *attr,
  145. char *buf)
  146. {
  147. struct i2c_client *client = to_i2c_client(dev);
  148. struct sht21 *sht21 = i2c_get_clientdata(client);
  149. int ret = sht21_update_measurements(client);
  150. if (ret < 0)
  151. return ret;
  152. return sprintf(buf, "%d\n", sht21->humidity);
  153. }
  154. /* sysfs attributes */
  155. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature,
  156. NULL, 0);
  157. static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity,
  158. NULL, 0);
  159. static struct attribute *sht21_attributes[] = {
  160. &sensor_dev_attr_temp1_input.dev_attr.attr,
  161. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  162. NULL
  163. };
  164. static const struct attribute_group sht21_attr_group = {
  165. .attrs = sht21_attributes,
  166. };
  167. /**
  168. * sht21_probe() - probe device
  169. * @client: I2C client device
  170. * @id: device ID
  171. *
  172. * Called by the I2C core when an entry in the ID table matches a
  173. * device's name.
  174. * Returns 0 on success.
  175. */
  176. static int sht21_probe(struct i2c_client *client,
  177. const struct i2c_device_id *id)
  178. {
  179. struct sht21 *sht21;
  180. int err;
  181. if (!i2c_check_functionality(client->adapter,
  182. I2C_FUNC_SMBUS_WORD_DATA)) {
  183. dev_err(&client->dev,
  184. "adapter does not support SMBus word transactions\n");
  185. return -ENODEV;
  186. }
  187. sht21 = devm_kzalloc(&client->dev, sizeof(*sht21), GFP_KERNEL);
  188. if (!sht21)
  189. return -ENOMEM;
  190. i2c_set_clientdata(client, sht21);
  191. mutex_init(&sht21->lock);
  192. err = sysfs_create_group(&client->dev.kobj, &sht21_attr_group);
  193. if (err) {
  194. dev_dbg(&client->dev, "could not create sysfs files\n");
  195. return err;
  196. }
  197. sht21->hwmon_dev = hwmon_device_register(&client->dev);
  198. if (IS_ERR(sht21->hwmon_dev)) {
  199. dev_dbg(&client->dev, "unable to register hwmon device\n");
  200. err = PTR_ERR(sht21->hwmon_dev);
  201. goto fail_remove_sysfs;
  202. }
  203. dev_info(&client->dev, "initialized\n");
  204. return 0;
  205. fail_remove_sysfs:
  206. sysfs_remove_group(&client->dev.kobj, &sht21_attr_group);
  207. return err;
  208. }
  209. /**
  210. * sht21_remove() - remove device
  211. * @client: I2C client device
  212. */
  213. static int sht21_remove(struct i2c_client *client)
  214. {
  215. struct sht21 *sht21 = i2c_get_clientdata(client);
  216. hwmon_device_unregister(sht21->hwmon_dev);
  217. sysfs_remove_group(&client->dev.kobj, &sht21_attr_group);
  218. return 0;
  219. }
  220. /* Device ID table */
  221. static const struct i2c_device_id sht21_id[] = {
  222. { "sht21", 0 },
  223. { }
  224. };
  225. MODULE_DEVICE_TABLE(i2c, sht21_id);
  226. static struct i2c_driver sht21_driver = {
  227. .driver.name = "sht21",
  228. .probe = sht21_probe,
  229. .remove = sht21_remove,
  230. .id_table = sht21_id,
  231. };
  232. module_i2c_driver(sht21_driver);
  233. MODULE_AUTHOR("Urs Fleisch <urs.fleisch@sensirion.com>");
  234. MODULE_DESCRIPTION("Sensirion SHT21 humidity and temperature sensor driver");
  235. MODULE_LICENSE("GPL");