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