0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/slab.h>
0016 #include <linux/i2c.h>
0017 #include <linux/hwmon.h>
0018 #include <linux/hwmon-sysfs.h>
0019 #include <linux/err.h>
0020 #include <linux/mutex.h>
0021 #include <linux/device.h>
0022 #include <linux/delay.h>
0023 #include <linux/jiffies.h>
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 struct hih6130 {
0036 struct i2c_client *client;
0037 struct mutex lock;
0038 bool valid;
0039 unsigned long last_update;
0040 int temperature;
0041 int humidity;
0042 size_t write_length;
0043 };
0044
0045
0046
0047
0048
0049
0050 static inline int hih6130_temp_ticks_to_millicelsius(int ticks)
0051 {
0052 ticks = ticks >> 2;
0053
0054
0055
0056
0057 return (DIV_ROUND_CLOSEST(ticks * 1650, 16382) - 400) * 100;
0058 }
0059
0060
0061
0062
0063
0064
0065 static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks)
0066 {
0067 ticks &= ~0xC000;
0068
0069
0070
0071
0072 return DIV_ROUND_CLOSEST(ticks * 1000, 16382) * 100;
0073 }
0074
0075
0076
0077
0078
0079
0080
0081 static int hih6130_update_measurements(struct device *dev)
0082 {
0083 struct hih6130 *hih6130 = dev_get_drvdata(dev);
0084 struct i2c_client *client = hih6130->client;
0085 int ret = 0;
0086 int t;
0087 unsigned char tmp[4];
0088 struct i2c_msg msgs[1] = {
0089 {
0090 .addr = client->addr,
0091 .flags = I2C_M_RD,
0092 .len = 4,
0093 .buf = tmp,
0094 }
0095 };
0096
0097 mutex_lock(&hih6130->lock);
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110 if (time_after(jiffies, hih6130->last_update + HZ) || !hih6130->valid) {
0111
0112
0113
0114
0115
0116
0117
0118
0119 tmp[0] = 0;
0120 ret = i2c_master_send(client, tmp, hih6130->write_length);
0121 if (ret < 0)
0122 goto out;
0123
0124
0125 msleep(40);
0126
0127 ret = i2c_transfer(client->adapter, msgs, 1);
0128 if (ret < 0)
0129 goto out;
0130
0131 if ((tmp[0] & 0xC0) != 0) {
0132 dev_err(&client->dev, "Error while reading measurement result\n");
0133 ret = -EIO;
0134 goto out;
0135 }
0136
0137 t = (tmp[0] << 8) + tmp[1];
0138 hih6130->humidity = hih6130_rh_ticks_to_per_cent_mille(t);
0139
0140 t = (tmp[2] << 8) + tmp[3];
0141 hih6130->temperature = hih6130_temp_ticks_to_millicelsius(t);
0142
0143 hih6130->last_update = jiffies;
0144 hih6130->valid = true;
0145 }
0146 out:
0147 mutex_unlock(&hih6130->lock);
0148
0149 return ret >= 0 ? 0 : ret;
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 static ssize_t hih6130_temperature_show(struct device *dev,
0162 struct device_attribute *attr,
0163 char *buf)
0164 {
0165 struct hih6130 *hih6130 = dev_get_drvdata(dev);
0166 int ret;
0167
0168 ret = hih6130_update_measurements(dev);
0169 if (ret < 0)
0170 return ret;
0171 return sprintf(buf, "%d\n", hih6130->temperature);
0172 }
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 static ssize_t hih6130_humidity_show(struct device *dev,
0184 struct device_attribute *attr, char *buf)
0185 {
0186 struct hih6130 *hih6130 = dev_get_drvdata(dev);
0187 int ret;
0188
0189 ret = hih6130_update_measurements(dev);
0190 if (ret < 0)
0191 return ret;
0192 return sprintf(buf, "%d\n", hih6130->humidity);
0193 }
0194
0195
0196 static SENSOR_DEVICE_ATTR_RO(temp1_input, hih6130_temperature, 0);
0197 static SENSOR_DEVICE_ATTR_RO(humidity1_input, hih6130_humidity, 0);
0198
0199 static struct attribute *hih6130_attrs[] = {
0200 &sensor_dev_attr_temp1_input.dev_attr.attr,
0201 &sensor_dev_attr_humidity1_input.dev_attr.attr,
0202 NULL
0203 };
0204
0205 ATTRIBUTE_GROUPS(hih6130);
0206
0207 static int hih6130_probe(struct i2c_client *client)
0208 {
0209 struct device *dev = &client->dev;
0210 struct hih6130 *hih6130;
0211 struct device *hwmon_dev;
0212
0213 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0214 dev_err(&client->dev, "adapter does not support true I2C\n");
0215 return -ENODEV;
0216 }
0217
0218 hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL);
0219 if (!hih6130)
0220 return -ENOMEM;
0221
0222 hih6130->client = client;
0223 mutex_init(&hih6130->lock);
0224
0225 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
0226 hih6130->write_length = 1;
0227
0228 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0229 hih6130,
0230 hih6130_groups);
0231 return PTR_ERR_OR_ZERO(hwmon_dev);
0232 }
0233
0234
0235 static const struct i2c_device_id hih6130_id[] = {
0236 { "hih6130", 0 },
0237 { }
0238 };
0239 MODULE_DEVICE_TABLE(i2c, hih6130_id);
0240
0241 static const struct of_device_id __maybe_unused hih6130_of_match[] = {
0242 { .compatible = "honeywell,hih6130", },
0243 { }
0244 };
0245 MODULE_DEVICE_TABLE(of, hih6130_of_match);
0246
0247 static struct i2c_driver hih6130_driver = {
0248 .driver = {
0249 .name = "hih6130",
0250 .of_match_table = of_match_ptr(hih6130_of_match),
0251 },
0252 .probe_new = hih6130_probe,
0253 .id_table = hih6130_id,
0254 };
0255
0256 module_i2c_driver(hih6130_driver);
0257
0258 MODULE_AUTHOR("Iain Paton <ipaton0@gmail.com>");
0259 MODULE_DESCRIPTION("Honeywell HIH-6130 humidity and temperature sensor driver");
0260 MODULE_LICENSE("GPL");