sht21.c 7.4 KB

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