hih6130.c 7.6 KB

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