hih6130.c 8.1 KB

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