htu21.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Measurement Specialties HTU21D humidity and temperature sensor driver
  3. *
  4. * Copyright (C) 2013 William Markezana <william.markezana@meas-spec.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/err.h>
  24. #include <linux/mutex.h>
  25. #include <linux/device.h>
  26. #include <linux/jiffies.h>
  27. /* HTU21 Commands */
  28. #define HTU21_T_MEASUREMENT_HM 0xE3
  29. #define HTU21_RH_MEASUREMENT_HM 0xE5
  30. struct htu21 {
  31. struct i2c_client *client;
  32. struct mutex lock;
  33. bool valid;
  34. unsigned long last_update;
  35. int temperature;
  36. int humidity;
  37. };
  38. static inline int htu21_temp_ticks_to_millicelsius(int ticks)
  39. {
  40. ticks &= ~0x0003; /* clear status bits */
  41. /*
  42. * Formula T = -46.85 + 175.72 * ST / 2^16 from datasheet p14,
  43. * optimized for integer fixed point (3 digits) arithmetic
  44. */
  45. return ((21965 * ticks) >> 13) - 46850;
  46. }
  47. static inline int htu21_rh_ticks_to_per_cent_mille(int ticks)
  48. {
  49. ticks &= ~0x0003; /* clear status bits */
  50. /*
  51. * Formula RH = -6 + 125 * SRH / 2^16 from datasheet p14,
  52. * optimized for integer fixed point (3 digits) arithmetic
  53. */
  54. return ((15625 * ticks) >> 13) - 6000;
  55. }
  56. static int htu21_update_measurements(struct device *dev)
  57. {
  58. struct htu21 *htu21 = dev_get_drvdata(dev);
  59. struct i2c_client *client = htu21->client;
  60. int ret = 0;
  61. mutex_lock(&htu21->lock);
  62. if (time_after(jiffies, htu21->last_update + HZ / 2) ||
  63. !htu21->valid) {
  64. ret = i2c_smbus_read_word_swapped(client,
  65. HTU21_T_MEASUREMENT_HM);
  66. if (ret < 0)
  67. goto out;
  68. htu21->temperature = htu21_temp_ticks_to_millicelsius(ret);
  69. ret = i2c_smbus_read_word_swapped(client,
  70. HTU21_RH_MEASUREMENT_HM);
  71. if (ret < 0)
  72. goto out;
  73. htu21->humidity = htu21_rh_ticks_to_per_cent_mille(ret);
  74. htu21->last_update = jiffies;
  75. htu21->valid = true;
  76. }
  77. out:
  78. mutex_unlock(&htu21->lock);
  79. return ret >= 0 ? 0 : ret;
  80. }
  81. static ssize_t htu21_show_temperature(struct device *dev,
  82. struct device_attribute *attr, char *buf)
  83. {
  84. struct htu21 *htu21 = dev_get_drvdata(dev);
  85. int ret;
  86. ret = htu21_update_measurements(dev);
  87. if (ret < 0)
  88. return ret;
  89. return sprintf(buf, "%d\n", htu21->temperature);
  90. }
  91. static ssize_t htu21_show_humidity(struct device *dev,
  92. struct device_attribute *attr, char *buf)
  93. {
  94. struct htu21 *htu21 = dev_get_drvdata(dev);
  95. int ret;
  96. ret = htu21_update_measurements(dev);
  97. if (ret < 0)
  98. return ret;
  99. return sprintf(buf, "%d\n", htu21->humidity);
  100. }
  101. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  102. htu21_show_temperature, NULL, 0);
  103. static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
  104. htu21_show_humidity, NULL, 0);
  105. static struct attribute *htu21_attrs[] = {
  106. &sensor_dev_attr_temp1_input.dev_attr.attr,
  107. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  108. NULL
  109. };
  110. ATTRIBUTE_GROUPS(htu21);
  111. static int htu21_probe(struct i2c_client *client,
  112. const struct i2c_device_id *id)
  113. {
  114. struct device *dev = &client->dev;
  115. struct htu21 *htu21;
  116. struct device *hwmon_dev;
  117. if (!i2c_check_functionality(client->adapter,
  118. I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  119. dev_err(&client->dev,
  120. "adapter does not support SMBus word transactions\n");
  121. return -ENODEV;
  122. }
  123. htu21 = devm_kzalloc(dev, sizeof(*htu21), GFP_KERNEL);
  124. if (!htu21)
  125. return -ENOMEM;
  126. htu21->client = client;
  127. mutex_init(&htu21->lock);
  128. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  129. htu21,
  130. htu21_groups);
  131. return PTR_ERR_OR_ZERO(hwmon_dev);
  132. }
  133. static const struct i2c_device_id htu21_id[] = {
  134. { "htu21", 0 },
  135. { }
  136. };
  137. MODULE_DEVICE_TABLE(i2c, htu21_id);
  138. static struct i2c_driver htu21_driver = {
  139. .class = I2C_CLASS_HWMON,
  140. .driver = {
  141. .name = "htu21",
  142. },
  143. .probe = htu21_probe,
  144. .id_table = htu21_id,
  145. };
  146. module_i2c_driver(htu21_driver);
  147. MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  148. MODULE_DESCRIPTION("MEAS HTU21D humidity and temperature sensor driver");
  149. MODULE_LICENSE("GPL");