hih6130.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 device *hwmon_dev;
  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. * @client: I2C client device
  86. *
  87. * Returns 0 on success, else negative errno.
  88. */
  89. static int hih6130_update_measurements(struct i2c_client *client)
  90. {
  91. int ret = 0;
  92. int t;
  93. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  94. unsigned char tmp[4];
  95. struct i2c_msg msgs[1] = {
  96. {
  97. .addr = client->addr,
  98. .flags = I2C_M_RD,
  99. .len = 4,
  100. .buf = tmp,
  101. }
  102. };
  103. mutex_lock(&hih6130->lock);
  104. /*
  105. * While the measurement can be completed in ~40ms the sensor takes
  106. * much longer to react to a change in external conditions. How quickly
  107. * it reacts depends on airflow and other factors outwith our control.
  108. * The datasheet specifies maximum 'Response time' for humidity at 8s
  109. * and temperature at 30s under specified conditions.
  110. * We therefore choose to only read the sensor at most once per second.
  111. * This trades off pointless activity polling the sensor much faster
  112. * than it can react against better response times in conditions more
  113. * favourable than specified in the datasheet.
  114. */
  115. if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
  116. /*
  117. * Write to slave address to request a measurement.
  118. * According with the datasheet it should be with no data, but
  119. * for systems with I2C bus drivers that do not allow zero
  120. * length packets we write one dummy byte to allow sensor
  121. * measurements on them.
  122. */
  123. tmp[0] = 0;
  124. ret = i2c_master_send(client, tmp, hih6130->write_length);
  125. if (ret < 0)
  126. goto out;
  127. /* measurement cycle time is ~36.65msec */
  128. msleep(40);
  129. ret = i2c_transfer(client->adapter, msgs, 1);
  130. if (ret < 0)
  131. goto out;
  132. if ((tmp[0] & 0xC0) != 0) {
  133. dev_err(&client->dev, "Error while reading measurement result\n");
  134. ret = -EIO;
  135. goto out;
  136. }
  137. t = (tmp[0] << 8) + tmp[1];
  138. hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
  139. t = (tmp[2] << 8) + tmp[3];
  140. hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
  141. hih6130->last_update = jiffies;
  142. hih6130->valid = true;
  143. }
  144. out:
  145. mutex_unlock(&hih6130->lock);
  146. return ret >= 0 ? 0 : ret;
  147. }
  148. /**
  149. * hih6130_show_temperature() - show temperature measurement value in sysfs
  150. * @dev: device
  151. * @attr: device attribute
  152. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  153. *
  154. * Will be called on read access to temp1_input sysfs attribute.
  155. * Returns number of bytes written into buffer, negative errno on error.
  156. */
  157. static ssize_t hih6130_show_temperature(struct device *dev,
  158. struct device_attribute *attr,
  159. char *buf)
  160. {
  161. struct i2c_client *client = to_i2c_client(dev);
  162. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  163. int ret = hih6130_update_measurements(client);
  164. if (ret < 0)
  165. return ret;
  166. return sprintf(buf, "%d\n", hih6130->temperature);
  167. }
  168. /**
  169. * hih6130_show_humidity() - show humidity measurement value in sysfs
  170. * @dev: device
  171. * @attr: device attribute
  172. * @buf: sysfs buffer (PAGE_SIZE) where measurement values are written to
  173. *
  174. * Will be called on read access to humidity1_input sysfs attribute.
  175. * Returns number of bytes written into buffer, negative errno on error.
  176. */
  177. static ssize_t hih6130_show_humidity(struct device *dev,
  178. struct device_attribute *attr, char *buf)
  179. {
  180. struct i2c_client *client = to_i2c_client(dev);
  181. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  182. int ret = hih6130_update_measurements(client);
  183. if (ret < 0)
  184. return ret;
  185. return sprintf(buf, "%d\n", hih6130->humidity);
  186. }
  187. /* sysfs attributes */
  188. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature,
  189. NULL, 0);
  190. static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity,
  191. NULL, 0);
  192. static struct attribute *hih6130_attributes[] = {
  193. &sensor_dev_attr_temp1_input.dev_attr.attr,
  194. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  195. NULL
  196. };
  197. static const struct attribute_group hih6130_attr_group = {
  198. .attrs = hih6130_attributes,
  199. };
  200. /**
  201. * hih6130_probe() - probe device
  202. * @client: I2C client device
  203. * @id: device ID
  204. *
  205. * Called by the I2C core when an entry in the ID table matches a
  206. * device's name.
  207. * Returns 0 on success.
  208. */
  209. static int hih6130_probe(struct i2c_client *client,
  210. const struct i2c_device_id *id)
  211. {
  212. struct hih6130 *hih6130;
  213. int err;
  214. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  215. dev_err(&client->dev, "adapter does not support true I2C\n");
  216. return -ENODEV;
  217. }
  218. hih6130 = devm_kzalloc(&client->dev, sizeof(*hih6130), GFP_KERNEL);
  219. if (!hih6130)
  220. return -ENOMEM;
  221. i2c_set_clientdata(client, hih6130);
  222. mutex_init(&hih6130->lock);
  223. err = sysfs_create_group(&client->dev.kobj, &hih6130_attr_group);
  224. if (err) {
  225. dev_dbg(&client->dev, "could not create sysfs files\n");
  226. return err;
  227. }
  228. hih6130->hwmon_dev = hwmon_device_register(&client->dev);
  229. if (IS_ERR(hih6130->hwmon_dev)) {
  230. dev_dbg(&client->dev, "unable to register hwmon device\n");
  231. err = PTR_ERR(hih6130->hwmon_dev);
  232. goto fail_remove_sysfs;
  233. }
  234. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
  235. hih6130->write_length = 1;
  236. return 0;
  237. fail_remove_sysfs:
  238. sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
  239. return err;
  240. }
  241. /**
  242. * hih6130_remove() - remove device
  243. * @client: I2C client device
  244. */
  245. static int hih6130_remove(struct i2c_client *client)
  246. {
  247. struct hih6130 *hih6130 = i2c_get_clientdata(client);
  248. hwmon_device_unregister(hih6130->hwmon_dev);
  249. sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group);
  250. return 0;
  251. }
  252. /* Device ID table */
  253. static const struct i2c_device_id hih6130_id[] = {
  254. { "hih6130", 0 },
  255. { }
  256. };
  257. MODULE_DEVICE_TABLE(i2c, hih6130_id);
  258. static struct i2c_driver hih6130_driver = {
  259. .driver.name = "hih6130",
  260. .probe = hih6130_probe,
  261. .remove = hih6130_remove,
  262. .id_table = hih6130_id,
  263. };
  264. module_i2c_driver(hih6130_driver);
  265. MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
  266. MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
  267. MODULE_LICENSE("GPL");