hih6130.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. */
  45. struct hih6130 {
  46. struct device *hwmon_dev;
  47. struct mutex lock;
  48. bool valid;
  49. unsigned long last_update;
  50. int temperature;
  51. int humidity;
  52. };
  53. /**
  54. * hih6130_temp_ticks_to_millicelsius() - convert raw temperature ticks to
  55. * milli celsius
  56. * @ticks: temperature ticks value received from sensor
  57. */
  58. static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
  59. {
  60. ticks = ticks >> 2;
  61. /*
  62. * from data sheet section 5.0
  63. * Formula T = ( ticks / ( 2^14 - 2 ) ) * 165 -40
  64. */
  65. return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
  66. }
  67. /**
  68. * hih6130_rh_ticks_to_per_cent_mille() - convert raw humidity ticks to
  69. * one-thousandths of a percent relative humidity
  70. * @ticks: humidity ticks value received from sensor
  71. */
  72. static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
  73. {
  74. ticks &= ~0xC000; /* clear status bits */
  75. /*
  76. * from data sheet section 4.0
  77. * Formula RH = ( ticks / ( 2^14 -2 ) ) * 100
  78. */
  79. return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
  80. }
  81. /**
  82. * hih6130_update_measurements() - get updated measurements from device
  83. * @client: I2C client device
  84. *
  85. * Returns 0 on success, else negative errno.
  86. */
  87. static int hih6130_update_measurements(struct i2c_client *client)
  88. {
  89. int ret = 0;
  90. int t;
  91. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  92. unsigned char tmp[4];
  93. struct i2c_msg msgs[1] = {
  94. {
  95. .addr = client->addr,
  96. .flags = I2C_M_RD,
  97. .len = 4,
  98. .buf = tmp,
  99. }
  100. };
  101. mutex_lock(&hih6130->lock);
  102. /*
  103. * While the measurement can be completed in ~40ms the sensor takes
  104. * much longer to react to a change in external conditions. How quickly
  105. * it reacts depends on airflow and other factors outwith our control.
  106. * The datasheet specifies maximum 'Response time' for humidity at 8s
  107. * and temperature at 30s under specified conditions.
  108. * We therefore choose to only read the sensor at most once per second.
  109. * This trades off pointless activity polling the sensor much faster
  110. * than it can react against better response times in conditions more
  111. * favourable than specified in the datasheet.
  112. */
  113. if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
  114. /* write to slave address, no data, to request a measurement */
  115. ret = i2c_master_send(client, tmp, 0);
  116. if (ret < 0)
  117. goto out;
  118. /* measurement cycle time is ~36.65msec */
  119. msleep(40);
  120. ret = i2c_transfer(client->adapter, msgs, 1);
  121. if (ret < 0)
  122. goto out;
  123. if ((tmp[0] & 0xC0) != 0) {
  124. dev_err(&client->dev, "Error while reading measurement result\n");
  125. ret = -EIO;
  126. goto out;
  127. }
  128. t = (tmp[0] << 8) + tmp[1];
  129. hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
  130. t = (tmp[2] << 8) + tmp[3];
  131. hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
  132. hih6130->last_update = jiffies;
  133. hih6130->valid = true;
  134. }
  135. out:
  136. mutex_unlock(&hih6130->lock);
  137. return ret >= 0 ? 0 : ret;
  138. }
  139. /**
  140. * hih6130_show_temperature() - show temperature measurement value in sysfs
  141. * @dev: device
  142. * @attr: device attribute
  143. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  144. *
  145. * Will be called on read access to temp1_input sysfs attribute.
  146. * Returns number of bytes written into buffer, negative errno on error.
  147. */
  148. static ssize_t hih6130_show_temperature(struct device *dev,
  149. struct device_attribute *attr,
  150. char *buf)
  151. {
  152. struct i2c_client *client = to_i2c_client(dev);
  153. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  154. int ret = hih6130_update_measurements(client);
  155. if (ret < 0)
  156. return ret;
  157. return sprintf(buf, "%d\n", hih6130->temperature);
  158. }
  159. /**
  160. * hih6130_show_humidity() - show humidity measurement value in sysfs
  161. * @dev: device
  162. * @attr: device attribute
  163. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  164. *
  165. * Will be called on read access to humidity1_input sysfs attribute.
  166. * Returns number of bytes written into buffer, negative errno on error.
  167. */
  168. static ssize_t hih6130_show_humidity(struct device *dev,
  169. struct device_attribute *attr, char *buf)
  170. {
  171. struct i2c_client *client = to_i2c_client(dev);
  172. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  173. int ret = hih6130_update_measurements(client);
  174. if (ret < 0)
  175. return ret;
  176. return sprintf(buf, "%d\n", hih6130->humidity);
  177. }
  178. /* sysfs attributes */
  179. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature,
  180. NULL, 0);
  181. static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity,
  182. NULL, 0);
  183. static struct attribute *hih6130_attributes[] = {
  184. &sensor_dev_attr_temp1_input.dev_attr.attr,
  185. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  186. NULL
  187. };
  188. static const struct attribute_group hih6130_attr_group = {
  189. .attrs = hih6130_attributes,
  190. };
  191. /**
  192. * hih6130_probe() - probe device
  193. * @client: I2C client device
  194. * @id: device ID
  195. *
  196. * Called by the I2C core when an entry in the ID table matches a
  197. * device's name.
  198. * Returns 0 on success.
  199. */
  200. static int hih6130_probe(struct i2c_client *client,
  201. const struct i2c_device_id *id)
  202. {
  203. struct hih6130 *hih6130;
  204. int err;
  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(&client->dev, sizeof(*hih6130), GFP_KERNEL);
  210. if (!hih6130)
  211. return -ENOMEM;
  212. i2c_set_clientdata(client, hih6130);
  213. mutex_init(&hih6130->lock);
  214. err = sysfs_create_group(&client->dev.kobj, &hih6130_attr_group);
  215. if (err) {
  216. dev_dbg(&client->dev, "could not create sysfs files\n");
  217. return err;
  218. }
  219. hih6130->hwmon_dev = hwmon_device_register(&client->dev);
  220. if (IS_ERR(hih6130->hwmon_dev)) {
  221. dev_dbg(&client->dev, "unable to register hwmon device\n");
  222. err = PTR_ERR(hih6130->hwmon_dev);
  223. goto fail_remove_sysfs;
  224. }
  225. return 0;
  226. fail_remove_sysfs:
  227. sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
  228. return err;
  229. }
  230. /**
  231. * hih6130_remove() - remove device
  232. * @client: I2C client device
  233. */
  234. static int hih6130_remove(struct i2c_client *client)
  235. {
  236. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  237. hwmon_device_unregister(hih6130->hwmon_dev);
  238. sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
  239. return 0;
  240. }
  241. /* Device ID table */
  242. static const struct i2c_device_id hih6130_id[] = {
  243. { "hih6130", 0 },
  244. { }
  245. };
  246. MODULE_DEVICE_TABLE(i2c, hih6130_id);
  247. static struct i2c_driver hih6130_driver = {
  248. .driver.name = "hih6130",
  249. .probe = hih6130_probe,
  250. .remove = hih6130_remove,
  251. .id_table = hih6130_id,
  252. };
  253. module_i2c_driver(hih6130_driver);
  254. MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
  255. MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
  256. MODULE_LICENSE("GPL");