hih6130.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Honeywell HIH-6130/HIH-6131 humidity and temperature sensor driver
  3. *
  4. * Copyright (C) 2012 Iain Paton <ipaton0@gmail.com>
  5. *
  6. * heavily based on the sht21 driver
  7. * Copyright (C) 2010 Urs Fleisch <urs.fleisch@sensirion.com>
  8. *
  9. * Data sheets available (2012-06-22) at
  10. * http://sensing.honeywell.com/index.php?ci_id=3106&la_id=1&defId=44872
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/i2c.h>
  16. #include <linux/hwmon.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/err.h>
  19. #include <linux/mutex.h>
  20. #include <linux/device.h>
  21. #include <linux/delay.h>
  22. #include <linux/jiffies.h>
  23. /**
  24. * struct hih6130 - HIH-6130 device specific data
  25. * @client: pointer to I2C client device
  26. * @lock: mutex to protect measurement values
  27. * @valid: only false before first measurement is taken
  28. * @last_update: time of last update (jiffies)
  29. * @temperature: cached temperature measurement value
  30. * @humidity: cached humidity measurement value
  31. * @write_length: length for I2C measurement request
  32. */
  33. struct hih6130 {
  34. struct i2c_client *client;
  35. struct mutex lock;
  36. bool valid;
  37. unsigned long last_update;
  38. int temperature;
  39. int humidity;
  40. size_t write_length;
  41. };
  42. /**
  43. * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
  44. * milli celsius
  45. * @ticks: temperature ticks value received from sensor
  46. */
  47. static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
  48. {
  49. ticks = ticks >> 2;
  50. /*
  51. * from data sheet section 5.0
  52. * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
  53. */
  54. return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
  55. }
  56. /**
  57. * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
  58. * one-thousandths of a percent relative humidity
  59. * @ticks: humidity ticks value received from sensor
  60. */
  61. static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
  62. {
  63. ticks &= ~0xC000; /* clear status bits */
  64. /*
  65. * from data sheet section 4.0
  66. * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
  67. */
  68. return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
  69. }
  70. /**
  71. * hih6130_update_measurements() - get updated measurements from device
  72. * @dev: device
  73. *
  74. * Returns 0 on success, else negative errno.
  75. */
  76. static int hih6130_update_measurements(struct device *dev)
  77. {
  78. struct hih6130 *hih6130 = dev_get_drvdata(dev);
  79. struct i2c_client *client = hih6130->client;
  80. int ret = 0;
  81. int t;
  82. unsigned char tmp[4];
  83. struct i2c_msg msgs[1] = {
  84. {
  85. .addr = client->addr,
  86. .flags = I2C_M_RD,
  87. .len = 4,
  88. .buf = tmp,
  89. }
  90. };
  91. mutex_lock(&hih6130->lock);
  92. /*
  93. * While the measurement can be completed in ~40ms the sensor takes
  94. * much longer to react to a change in external conditions. How quickly
  95. * it reacts depends on airflow and other factors outwith our control.
  96. * The datasheet specifies maximum 'Response time' for humidity at 8s
  97. * and temperature at 30s under specified conditions.
  98. * We therefore choose to only read the sensor at most once per second.
  99. * This trades off pointless activity polling the sensor much faster
  100. * than it can react against better response times in conditions more
  101. * favourable than specified in the datasheet.
  102. */
  103. if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
  104. /*
  105. * Write to slave address to request a measurement.
  106. * According with the datasheet it should be with no data, but
  107. * for systems with I2C bus drivers that do not allow zero
  108. * length packets we write one dummy byte to allow sensor
  109. * measurements on them.
  110. */
  111. tmp[0] = 0;
  112. ret = i2c_master_send(client, tmp, hih6130->write_length);
  113. if (ret < 0)
  114. goto out;
  115. /* measurement cycle time is ~36.65msec */
  116. msleep(40);
  117. ret = i2c_transfer(client->adapter, msgs, 1);
  118. if (ret < 0)
  119. goto out;
  120. if ((tmp[0] & 0xC0) != 0) {
  121. dev_err(&client->dev, "Error while reading measurement result\n");
  122. ret = -EIO;
  123. goto out;
  124. }
  125. t = (tmp[0] << 8) + tmp[1];
  126. hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
  127. t = (tmp[2] << 8) + tmp[3];
  128. hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
  129. hih6130->last_update = jiffies;
  130. hih6130->valid = true;
  131. }
  132. out:
  133. mutex_unlock(&hih6130->lock);
  134. return ret >= 0 ? 0 : ret;
  135. }
  136. /**
  137. * hih6130_show_temperature() - show temperature measurement value in sysfs
  138. * @dev: device
  139. * @attr: device attribute
  140. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  141. *
  142. * Will be called on read access to temp1_input sysfs attribute.
  143. * Returns number of bytes written into buffer, negative errno on error.
  144. */
  145. static ssize_t hih6130_temperature_show(struct device *dev,
  146. struct device_attribute *attr,
  147. char *buf)
  148. {
  149. struct hih6130 *hih6130 = dev_get_drvdata(dev);
  150. int ret;
  151. ret = hih6130_update_measurements(dev);
  152. if (ret < 0)
  153. return ret;
  154. return sprintf(buf, "%d\n", hih6130->temperature);
  155. }
  156. /**
  157. * hih6130_show_humidity() - show humidity measurement value in sysfs
  158. * @dev: device
  159. * @attr: device attribute
  160. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  161. *
  162. * Will be called on read access to humidity1_input sysfs attribute.
  163. * Returns number of bytes written into buffer, negative errno on error.
  164. */
  165. static ssize_t hih6130_humidity_show(struct device *dev,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct hih6130 *hih6130 = dev_get_drvdata(dev);
  169. int ret;
  170. ret = hih6130_update_measurements(dev);
  171. if (ret < 0)
  172. return ret;
  173. return sprintf(buf, "%d\n", hih6130->humidity);
  174. }
  175. /* sysfs attributes */
  176. static SENSOR_DEVICE_ATTR_RO(temp1_input, hih6130_temperature, 0);
  177. static SENSOR_DEVICE_ATTR_RO(humidity1_input, hih6130_humidity, 0);
  178. static struct attribute *hih6130_attrs[] = {
  179. &sensor_dev_attr_temp1_input.dev_attr.attr,
  180. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  181. NULL
  182. };
  183. ATTRIBUTE_GROUPS(hih6130);
  184. static int hih6130_probe(struct i2c_client *client)
  185. {
  186. struct device *dev = &client->dev;
  187. struct hih6130 *hih6130;
  188. struct device *hwmon_dev;
  189. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  190. dev_err(&client->dev, "adapter does not support true I2C\n");
  191. return -ENODEV;
  192. }
  193. hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL);
  194. if (!hih6130)
  195. return -ENOMEM;
  196. hih6130->client = client;
  197. mutex_init(&hih6130->lock);
  198. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
  199. hih6130->write_length = 1;
  200. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  201. hih6130,
  202. hih6130_groups);
  203. return PTR_ERR_OR_ZERO(hwmon_dev);
  204. }
  205. /* Device ID table */
  206. static const struct i2c_device_id hih6130_id[] = {
  207. { "hih6130", 0 },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(i2c, hih6130_id);
  211. static const struct of_device_id __maybe_unused hih6130_of_match[] = {
  212. { .compatible = "honeywell,hih6130", },
  213. { }
  214. };
  215. MODULE_DEVICE_TABLE(of, hih6130_of_match);
  216. static struct i2c_driver hih6130_driver = {
  217. .driver = {
  218. .name = "hih6130",
  219. .of_match_table = of_match_ptr(hih6130_of_match),
  220. },
  221. .probe_new = hih6130_probe,
  222. .id_table = hih6130_id,
  223. };
  224. module_i2c_driver(hih6130_driver);
  225. MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
  226. MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
  227. MODULE_LICENSE("GPL");