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);
157 /* read measurement */
158 ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
160 dev_err(&client->dev, "cannot read sensor data\n");
163 return be16_to_cpu(val);
166 static int hdc100x_get_heater_status(struct hdc100x_data *data)
168 return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
171 static int hdc100x_read_raw(struct iio_dev *indio_dev,
172 struct iio_chan_spec const *chan, int *val,
173 int *val2, long mask)
175 struct hdc100x_data *data = iio_priv(indio_dev);
178 case IIO_CHAN_INFO_RAW: {
181 mutex_lock(&data->lock);
182 if (chan->type == IIO_CURRENT) {
183 *val = hdc100x_get_heater_status(data);
186 ret = hdc100x_get_measurement(data, chan);
192 mutex_unlock(&data->lock);
195 case IIO_CHAN_INFO_INT_TIME:
197 *val2 = data->adc_int_us[chan->address];
198 return IIO_VAL_INT_PLUS_MICRO;
199 case IIO_CHAN_INFO_SCALE:
200 if (chan->type == IIO_TEMP) {
203 return IIO_VAL_FRACTIONAL;
207 return IIO_VAL_FRACTIONAL;
210 case IIO_CHAN_INFO_OFFSET:
213 return IIO_VAL_INT_PLUS_MICRO;
219 static int hdc100x_write_raw(struct iio_dev *indio_dev,
220 struct iio_chan_spec const *chan,
221 int val, int val2, long mask)
223 struct hdc100x_data *data = iio_priv(indio_dev);
227 case IIO_CHAN_INFO_INT_TIME:
231 mutex_lock(&data->lock);
232 ret = hdc100x_set_it_time(data, chan->address, val2);
233 mutex_unlock(&data->lock);
235 case IIO_CHAN_INFO_RAW:
236 if (chan->type != IIO_CURRENT || val2 != 0)
239 mutex_lock(&data->lock);
240 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
241 val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
242 mutex_unlock(&data->lock);
249 static const struct iio_info hdc100x_info = {
250 .read_raw = hdc100x_read_raw,
251 .write_raw = hdc100x_write_raw,
252 .attrs = &hdc100x_attribute_group,
253 .driver_module = THIS_MODULE,
256 static int hdc100x_probe(struct i2c_client *client,
257 const struct i2c_device_id *id)
259 struct iio_dev *indio_dev;
260 struct hdc100x_data *data;
262 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
263 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
266 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
270 data = iio_priv(indio_dev);
271 i2c_set_clientdata(client, indio_dev);
272 data->client = client;
273 mutex_init(&data->lock);
275 indio_dev->dev.parent = &client->dev;
276 indio_dev->name = dev_name(&client->dev);
277 indio_dev->modes = INDIO_DIRECT_MODE;
278 indio_dev->info = &hdc100x_info;
280 indio_dev->channels = hdc100x_channels;
281 indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
283 /* be sure we are in a known state */
284 hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
285 hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
287 return devm_iio_device_register(&client->dev, indio_dev);
290 static const struct i2c_device_id hdc100x_id[] = {
294 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
296 static struct i2c_driver hdc100x_driver = {
300 .probe = hdc100x_probe,
301 .id_table = hdc100x_id,
303 module_i2c_driver(hdc100x_driver);
305 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
306 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
307 MODULE_LICENSE("GPL");