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.
17 * http://www.ti.com/product/HDC1000/datasheet
18 * http://www.ti.com/product/HDC1008/datasheet
19 * http://www.ti.com/product/HDC1010/datasheet
20 * http://www.ti.com/product/HDC1050/datasheet
21 * http://www.ti.com/product/HDC1080/datasheet
24 #include <linux/delay.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/i2c.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/trigger_consumer.h>
33 #include <linux/iio/triggered_buffer.h>
35 #include <linux/time.h>
37 #define HDC100X_REG_TEMP 0x00
38 #define HDC100X_REG_HUMIDITY 0x01
40 #define HDC100X_REG_CONFIG 0x02
41 #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)
42 #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
45 struct i2c_client *client;
49 /* integration time of the sensor */
51 /* Ensure natural alignment of timestamp */
58 /* integration time in us */
59 static const int hdc100x_int_time[][3] = {
60 { 6350, 3650, 0 }, /* IIO_TEMP channel*/
61 { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
64 /* HDC100X_REG_CONFIG shift and mask values */
68 } hdc100x_resolution_shift[2] = {
69 { /* IIO_TEMP channel */
73 { /* IIO_HUMIDITYRELATIVE channel */
79 static IIO_CONST_ATTR(temp_integration_time_available,
82 static IIO_CONST_ATTR(humidityrelative_integration_time_available,
83 "0.0025 0.00385 0.0065");
85 static IIO_CONST_ATTR(out_current_heater_raw_available,
88 static struct attribute *hdc100x_attributes[] = {
89 &iio_const_attr_temp_integration_time_available.dev_attr.attr,
90 &iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr,
91 &iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
95 static const struct attribute_group hdc100x_attribute_group = {
96 .attrs = hdc100x_attributes,
99 static const struct iio_chan_spec hdc100x_channels[] = {
102 .address = HDC100X_REG_TEMP,
103 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
104 BIT(IIO_CHAN_INFO_SCALE) |
105 BIT(IIO_CHAN_INFO_INT_TIME) |
106 BIT(IIO_CHAN_INFO_OFFSET),
112 .endianness = IIO_BE,
116 .type = IIO_HUMIDITYRELATIVE,
117 .address = HDC100X_REG_HUMIDITY,
118 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
119 BIT(IIO_CHAN_INFO_SCALE) |
120 BIT(IIO_CHAN_INFO_INT_TIME),
126 .endianness = IIO_BE,
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
132 .extend_name = "heater",
136 IIO_CHAN_SOFT_TIMESTAMP(2),
139 static const unsigned long hdc100x_scan_masks[] = {0x3, 0};
141 static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
143 int tmp = (~mask & data->config) | val;
146 ret = i2c_smbus_write_word_swapped(data->client,
147 HDC100X_REG_CONFIG, tmp);
154 static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
156 int shift = hdc100x_resolution_shift[chan].shift;
160 for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) {
161 if (val2 && val2 == hdc100x_int_time[chan][i]) {
162 ret = hdc100x_update_config(data,
163 hdc100x_resolution_shift[chan].mask << shift,
166 data->adc_int_us[chan] = val2;
174 static int hdc100x_get_measurement(struct hdc100x_data *data,
175 struct iio_chan_spec const *chan)
177 struct i2c_client *client = data->client;
178 int delay = data->adc_int_us[chan->address] + 1*USEC_PER_MSEC;
182 /* start measurement */
183 ret = i2c_smbus_write_byte(client, chan->address);
185 dev_err(&client->dev, "cannot start measurement");
189 /* wait for integration time to pass */
190 usleep_range(delay, delay + 1000);
192 /* read measurement */
193 ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
195 dev_err(&client->dev, "cannot read sensor data\n");
198 return be16_to_cpu(val);
201 static int hdc100x_get_heater_status(struct hdc100x_data *data)
203 return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
206 static int hdc100x_read_raw(struct iio_dev *indio_dev,
207 struct iio_chan_spec const *chan, int *val,
208 int *val2, long mask)
210 struct hdc100x_data *data = iio_priv(indio_dev);
213 case IIO_CHAN_INFO_RAW: {
216 mutex_lock(&data->lock);
217 if (chan->type == IIO_CURRENT) {
218 *val = hdc100x_get_heater_status(data);
221 ret = iio_device_claim_direct_mode(indio_dev);
223 mutex_unlock(&data->lock);
227 ret = hdc100x_get_measurement(data, chan);
228 iio_device_release_direct_mode(indio_dev);
234 mutex_unlock(&data->lock);
237 case IIO_CHAN_INFO_INT_TIME:
239 *val2 = data->adc_int_us[chan->address];
240 return IIO_VAL_INT_PLUS_MICRO;
241 case IIO_CHAN_INFO_SCALE:
242 if (chan->type == IIO_TEMP) {
245 return IIO_VAL_FRACTIONAL;
249 return IIO_VAL_FRACTIONAL;
252 case IIO_CHAN_INFO_OFFSET:
255 return IIO_VAL_INT_PLUS_MICRO;
261 static int hdc100x_write_raw(struct iio_dev *indio_dev,
262 struct iio_chan_spec const *chan,
263 int val, int val2, long mask)
265 struct hdc100x_data *data = iio_priv(indio_dev);
269 case IIO_CHAN_INFO_INT_TIME:
273 mutex_lock(&data->lock);
274 ret = hdc100x_set_it_time(data, chan->address, val2);
275 mutex_unlock(&data->lock);
277 case IIO_CHAN_INFO_RAW:
278 if (chan->type != IIO_CURRENT || val2 != 0)
281 mutex_lock(&data->lock);
282 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
283 val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
284 mutex_unlock(&data->lock);
291 static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
293 struct hdc100x_data *data = iio_priv(indio_dev);
296 /* Buffer is enabled. First set ACQ Mode, then attach poll func */
297 mutex_lock(&data->lock);
298 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
299 HDC100X_REG_CONFIG_ACQ_MODE);
300 mutex_unlock(&data->lock);
304 return iio_triggered_buffer_postenable(indio_dev);
307 static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
309 struct hdc100x_data *data = iio_priv(indio_dev);
312 /* First detach poll func, then reset ACQ mode. OK to disable buffer */
313 ret = iio_triggered_buffer_predisable(indio_dev);
317 mutex_lock(&data->lock);
318 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
319 mutex_unlock(&data->lock);
324 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
325 .postenable = hdc100x_buffer_postenable,
326 .predisable = hdc100x_buffer_predisable,
329 static irqreturn_t hdc100x_trigger_handler(int irq, void *p)
331 struct iio_poll_func *pf = p;
332 struct iio_dev *indio_dev = pf->indio_dev;
333 struct hdc100x_data *data = iio_priv(indio_dev);
334 struct i2c_client *client = data->client;
335 int delay = data->adc_int_us[0] + data->adc_int_us[1] + 2*USEC_PER_MSEC;
338 /* dual read starts at temp register */
339 mutex_lock(&data->lock);
340 ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP);
342 dev_err(&client->dev, "cannot start measurement\n");
345 usleep_range(delay, delay + 1000);
347 ret = i2c_master_recv(client, (u8 *)data->scan.channels, 4);
349 dev_err(&client->dev, "cannot read sensor data\n");
353 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
354 iio_get_time_ns(indio_dev));
356 mutex_unlock(&data->lock);
357 iio_trigger_notify_done(indio_dev->trig);
362 static const struct iio_info hdc100x_info = {
363 .read_raw = hdc100x_read_raw,
364 .write_raw = hdc100x_write_raw,
365 .attrs = &hdc100x_attribute_group,
366 .driver_module = THIS_MODULE,
369 static int hdc100x_probe(struct i2c_client *client,
370 const struct i2c_device_id *id)
372 struct iio_dev *indio_dev;
373 struct hdc100x_data *data;
376 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
377 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
380 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
384 data = iio_priv(indio_dev);
385 i2c_set_clientdata(client, indio_dev);
386 data->client = client;
387 mutex_init(&data->lock);
389 indio_dev->dev.parent = &client->dev;
390 indio_dev->name = dev_name(&client->dev);
391 indio_dev->modes = INDIO_DIRECT_MODE;
392 indio_dev->info = &hdc100x_info;
394 indio_dev->channels = hdc100x_channels;
395 indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
396 indio_dev->available_scan_masks = hdc100x_scan_masks;
398 /* be sure we are in a known state */
399 hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
400 hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
401 hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
403 ret = iio_triggered_buffer_setup(indio_dev, NULL,
404 hdc100x_trigger_handler,
405 &hdc_buffer_setup_ops);
407 dev_err(&client->dev, "iio triggered buffer setup failed\n");
410 ret = iio_device_register(indio_dev);
412 iio_triggered_buffer_cleanup(indio_dev);
417 static int hdc100x_remove(struct i2c_client *client)
419 struct iio_dev *indio_dev = i2c_get_clientdata(client);
421 iio_device_unregister(indio_dev);
422 iio_triggered_buffer_cleanup(indio_dev);
427 static const struct i2c_device_id hdc100x_id[] = {
436 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
438 static const struct of_device_id hdc100x_dt_ids[] = {
439 { .compatible = "ti,hdc1000" },
440 { .compatible = "ti,hdc1008" },
441 { .compatible = "ti,hdc1010" },
442 { .compatible = "ti,hdc1050" },
443 { .compatible = "ti,hdc1080" },
446 MODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
448 static struct i2c_driver hdc100x_driver = {
451 .of_match_table = of_match_ptr(hdc100x_dt_ids),
453 .probe = hdc100x_probe,
454 .remove = hdc100x_remove,
455 .id_table = hdc100x_id,
457 module_i2c_driver(hdc100x_driver);
459 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
460 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
461 MODULE_LICENSE("GPL");