2 * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/delay.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 #define HDC100X_REG_TEMP 0x00
27 #define HDC100X_REG_HUMIDITY 0x01
29 #define HDC100X_REG_CONFIG 0x02
30 #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
33 struct i2c_client *client;
37 /* integration time of the sensor */
41 /* integration time in us */
42 static const int hdc100x_int_time[][3] = {
43 { 6350, 3650, 0 }, /* IIO_TEMP channel*/
44 { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
47 /* HDC100X_REG_CONFIG shift and mask values */
51 } hdc100x_resolution_shift[2] = {
52 { /* IIO_TEMP channel */
56 { /* IIO_HUMIDITYRELATIVE channel */
62 static IIO_CONST_ATTR(temp_integration_time_available,
65 static IIO_CONST_ATTR(humidityrelative_integration_time_available,
66 "0.0025 0.00385 0.0065");
68 static IIO_CONST_ATTR(out_current_heater_raw_available,
71 static struct attribute *hdc100x_attributes[] = {
72 &iio_const_attr_temp_integration_time_available.dev_attr.attr,
73 &iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr,
74 &iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
78 static struct attribute_group hdc100x_attribute_group = {
79 .attrs = hdc100x_attributes,
82 static const struct iio_chan_spec hdc100x_channels[] = {
85 .address = HDC100X_REG_TEMP,
86 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
87 BIT(IIO_CHAN_INFO_SCALE) |
88 BIT(IIO_CHAN_INFO_INT_TIME) |
89 BIT(IIO_CHAN_INFO_OFFSET),
92 .type = IIO_HUMIDITYRELATIVE,
93 .address = HDC100X_REG_HUMIDITY,
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
95 BIT(IIO_CHAN_INFO_SCALE) |
96 BIT(IIO_CHAN_INFO_INT_TIME)
100 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
101 .extend_name = "heater",
106 static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
108 int tmp = (~mask & data->config) | val;
111 ret = i2c_smbus_write_word_swapped(data->client,
112 HDC100X_REG_CONFIG, tmp);
119 static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
121 int shift = hdc100x_resolution_shift[chan].shift;
125 for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) {
126 if (val2 && val2 == hdc100x_int_time[chan][i]) {
127 ret = hdc100x_update_config(data,
128 hdc100x_resolution_shift[chan].mask << shift,
131 data->adc_int_us[chan] = val2;
139 static int hdc100x_get_measurement(struct hdc100x_data *data,
140 struct iio_chan_spec const *chan)
142 struct i2c_client *client = data->client;
143 int delay = data->adc_int_us[chan->address];
147 /* start measurement */
148 ret = i2c_smbus_write_byte(client, chan->address);
150 dev_err(&client->dev, "cannot start measurement");
154 /* wait for integration time to pass */
155 usleep_range(delay, delay + 1000);
158 * i2c_smbus_read_word_data cannot() be used here due to the command
159 * value not being understood and causes NAKs preventing any reading
160 * from being accessed.
162 ret = i2c_smbus_read_byte(client);
164 dev_err(&client->dev, "cannot read high byte measurement");
169 ret = i2c_smbus_read_byte(client);
171 dev_err(&client->dev, "cannot read low byte measurement");
179 static int hdc100x_get_heater_status(struct hdc100x_data *data)
181 return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
184 static int hdc100x_read_raw(struct iio_dev *indio_dev,
185 struct iio_chan_spec const *chan, int *val,
186 int *val2, long mask)
188 struct hdc100x_data *data = iio_priv(indio_dev);
191 case IIO_CHAN_INFO_RAW: {
194 mutex_lock(&data->lock);
195 if (chan->type == IIO_CURRENT) {
196 *val = hdc100x_get_heater_status(data);
199 ret = hdc100x_get_measurement(data, chan);
205 mutex_unlock(&data->lock);
208 case IIO_CHAN_INFO_INT_TIME:
210 *val2 = data->adc_int_us[chan->address];
211 return IIO_VAL_INT_PLUS_MICRO;
212 case IIO_CHAN_INFO_SCALE:
213 if (chan->type == IIO_TEMP) {
216 return IIO_VAL_FRACTIONAL;
220 return IIO_VAL_INT_PLUS_MICRO;
223 case IIO_CHAN_INFO_OFFSET:
226 return IIO_VAL_INT_PLUS_MICRO;
232 static int hdc100x_write_raw(struct iio_dev *indio_dev,
233 struct iio_chan_spec const *chan,
234 int val, int val2, long mask)
236 struct hdc100x_data *data = iio_priv(indio_dev);
240 case IIO_CHAN_INFO_INT_TIME:
244 mutex_lock(&data->lock);
245 ret = hdc100x_set_it_time(data, chan->address, val2);
246 mutex_unlock(&data->lock);
248 case IIO_CHAN_INFO_RAW:
249 if (chan->type != IIO_CURRENT || val2 != 0)
252 mutex_lock(&data->lock);
253 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
254 val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
255 mutex_unlock(&data->lock);
262 static const struct iio_info hdc100x_info = {
263 .read_raw = hdc100x_read_raw,
264 .write_raw = hdc100x_write_raw,
265 .attrs = &hdc100x_attribute_group,
266 .driver_module = THIS_MODULE,
269 static int hdc100x_probe(struct i2c_client *client,
270 const struct i2c_device_id *id)
272 struct iio_dev *indio_dev;
273 struct hdc100x_data *data;
275 if (!i2c_check_functionality(client->adapter,
276 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
279 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
283 data = iio_priv(indio_dev);
284 i2c_set_clientdata(client, indio_dev);
285 data->client = client;
286 mutex_init(&data->lock);
288 indio_dev->dev.parent = &client->dev;
289 indio_dev->name = dev_name(&client->dev);
290 indio_dev->modes = INDIO_DIRECT_MODE;
291 indio_dev->info = &hdc100x_info;
293 indio_dev->channels = hdc100x_channels;
294 indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
296 /* be sure we are in a known state */
297 hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
298 hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
300 return devm_iio_device_register(&client->dev, indio_dev);
303 static const struct i2c_device_id hdc100x_id[] = {
307 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
309 static struct i2c_driver hdc100x_driver = {
313 .probe = hdc100x_probe,
314 .id_table = hdc100x_id,
316 module_i2c_driver(hdc100x_driver);
318 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
319 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
320 MODULE_LICENSE("GPL");