htu21.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 device *hwmon_dev;
  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 i2c_client *client)
  57. {
  58. int ret = 0;
  59. struct htu21 *htu21 = i2c_get_clientdata(client);
  60. mutex_lock(&htu21->lock);
  61. if (time_after(jiffies, htu21->last_update + HZ / 2) ||
  62. !htu21->valid) {
  63. ret = i2c_smbus_read_word_swapped(client,
  64. HTU21_T_MEASUREMENT_HM);
  65. if (ret < 0)
  66. goto out;
  67. htu21->temperature = htu21_temp_ticks_to_millicelsius(ret);
  68. ret = i2c_smbus_read_word_swapped(client,
  69. HTU21_RH_MEASUREMENT_HM);
  70. if (ret < 0)
  71. goto out;
  72. htu21->humidity = htu21_rh_ticks_to_per_cent_mille(ret);
  73. htu21->last_update = jiffies;
  74. htu21->valid = true;
  75. }
  76. out:
  77. mutex_unlock(&htu21->lock);
  78. return ret >= 0 ? 0 : ret;
  79. }
  80. static ssize_t htu21_show_temperature(struct device *dev,
  81. struct device_attribute *attr, char *buf)
  82. {
  83. struct i2c_client *client = to_i2c_client(dev);
  84. struct htu21 *htu21 = i2c_get_clientdata(client);
  85. int ret = htu21_update_measurements(client);
  86. if (ret < 0)
  87. return ret;
  88. return sprintf(buf, "%d\n", htu21->temperature);
  89. }
  90. static ssize_t htu21_show_humidity(struct device *dev,
  91. struct device_attribute *attr, char *buf)
  92. {
  93. struct i2c_client *client = to_i2c_client(dev);
  94. struct htu21 *htu21 = i2c_get_clientdata(client);
  95. int ret = htu21_update_measurements(client);
  96. if (ret < 0)
  97. return ret;
  98. return sprintf(buf, "%d\n", htu21->humidity);
  99. }
  100. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  101. htu21_show_temperature, NULL, 0);
  102. static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
  103. htu21_show_humidity, NULL, 0);
  104. static struct attribute *htu21_attributes[] = {
  105. &sensor_dev_attr_temp1_input.dev_attr.attr,
  106. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  107. NULL
  108. };
  109. static const struct attribute_group htu21_group = {
  110. .attrs = htu21_attributes,
  111. };
  112. static int htu21_probe(struct i2c_client *client,
  113. const struct i2c_device_id *id)
  114. {
  115. struct htu21 *htu21;
  116. int err;
  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(&client->dev, sizeof(*htu21), GFP_KERNEL);
  124. if (!htu21)
  125. return -ENOMEM;
  126. i2c_set_clientdata(client, htu21);
  127. mutex_init(&htu21->lock);
  128. err = sysfs_create_group(&client->dev.kobj, &htu21_group);
  129. if (err) {
  130. dev_dbg(&client->dev, "could not create sysfs files\n");
  131. return err;
  132. }
  133. htu21->hwmon_dev = hwmon_device_register(&client->dev);
  134. if (IS_ERR(htu21->hwmon_dev)) {
  135. dev_dbg(&client->dev, "unable to register hwmon device\n");
  136. err = PTR_ERR(htu21->hwmon_dev);
  137. goto error;
  138. }
  139. dev_info(&client->dev, "initialized\n");
  140. return 0;
  141. error:
  142. sysfs_remove_group(&client->dev.kobj, &htu21_group);
  143. return err;
  144. }
  145. static int htu21_remove(struct i2c_client *client)
  146. {
  147. struct htu21 *htu21 = i2c_get_clientdata(client);
  148. hwmon_device_unregister(htu21->hwmon_dev);
  149. sysfs_remove_group(&client->dev.kobj, &htu21_group);
  150. return 0;
  151. }
  152. static const struct i2c_device_id htu21_id[] = {
  153. { "htu21", 0 },
  154. { }
  155. };
  156. MODULE_DEVICE_TABLE(i2c, htu21_id);
  157. static struct i2c_driver htu21_driver = {
  158. .class = I2C_CLASS_HWMON,
  159. .driver = {
  160. .name = "htu21",
  161. },
  162. .probe = htu21_probe,
  163. .remove = htu21_remove,
  164. .id_table = htu21_id,
  165. };
  166. module_i2c_driver(htu21_driver);
  167. MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  168. MODULE_DESCRIPTION("MEAS HTU21D humidity and temperature sensor driver");
  169. MODULE_LICENSE("GPL");