1 // SPDX-License-Identifier: GPL-2.0+
3 * hdc100x.c - Support for the TI HDC100x temperature + humidity sensors
5 * Copyright (C) 2015, 2018
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
9 * http://www.ti.com/product/HDC1000/datasheet
10 * http://www.ti.com/product/HDC1008/datasheet
11 * http://www.ti.com/product/HDC1010/datasheet
12 * http://www.ti.com/product/HDC1050/datasheet
13 * http://www.ti.com/product/HDC1080/datasheet
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/i2c.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
27 #include <linux/time.h>
29 #define HDC100X_REG_TEMP 0x00
30 #define HDC100X_REG_HUMIDITY 0x01
32 #define HDC100X_REG_CONFIG 0x02
33 #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)
34 #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
37 struct i2c_client *client;
41 /* integration time of the sensor */
43 /* Ensure natural alignment of timestamp */
50 /* integration time in us */
51 static const int hdc100x_int_time[][3] = {
52 { 6350, 3650, 0 }, /* IIO_TEMP channel*/
53 { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
56 /* HDC100X_REG_CONFIG shift and mask values */
60 } hdc100x_resolution_shift[2] = {
61 { /* IIO_TEMP channel */
65 { /* IIO_HUMIDITYRELATIVE channel */
71 static IIO_CONST_ATTR(temp_integration_time_available,
74 static IIO_CONST_ATTR(humidityrelative_integration_time_available,
75 "0.0025 0.00385 0.0065");
77 static IIO_CONST_ATTR(out_current_heater_raw_available,
80 static struct attribute *hdc100x_attributes[] = {
81 &iio_const_attr_temp_integration_time_available.dev_attr.attr,
82 &iio_const_attr_humidityrelative_integration_time_available.dev_attr.attr,
83 &iio_const_attr_out_current_heater_raw_available.dev_attr.attr,
87 static const struct attribute_group hdc100x_attribute_group = {
88 .attrs = hdc100x_attributes,
91 static const struct iio_chan_spec hdc100x_channels[] = {
94 .address = HDC100X_REG_TEMP,
95 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
96 BIT(IIO_CHAN_INFO_SCALE) |
97 BIT(IIO_CHAN_INFO_INT_TIME) |
98 BIT(IIO_CHAN_INFO_OFFSET),
104 .endianness = IIO_BE,
108 .type = IIO_HUMIDITYRELATIVE,
109 .address = HDC100X_REG_HUMIDITY,
110 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
111 BIT(IIO_CHAN_INFO_SCALE) |
112 BIT(IIO_CHAN_INFO_INT_TIME),
118 .endianness = IIO_BE,
123 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
124 .extend_name = "heater",
128 IIO_CHAN_SOFT_TIMESTAMP(2),
131 static const unsigned long hdc100x_scan_masks[] = {0x3, 0};
133 static int hdc100x_update_config(struct hdc100x_data *data, int mask, int val)
135 int tmp = (~mask & data->config) | val;
138 ret = i2c_smbus_write_word_swapped(data->client,
139 HDC100X_REG_CONFIG, tmp);
146 static int hdc100x_set_it_time(struct hdc100x_data *data, int chan, int val2)
148 int shift = hdc100x_resolution_shift[chan].shift;
152 for (i = 0; i < ARRAY_SIZE(hdc100x_int_time[chan]); i++) {
153 if (val2 && val2 == hdc100x_int_time[chan][i]) {
154 ret = hdc100x_update_config(data,
155 hdc100x_resolution_shift[chan].mask << shift,
158 data->adc_int_us[chan] = val2;
166 static int hdc100x_get_measurement(struct hdc100x_data *data,
167 struct iio_chan_spec const *chan)
169 struct i2c_client *client = data->client;
170 int delay = data->adc_int_us[chan->address] + 1*USEC_PER_MSEC;
174 /* start measurement */
175 ret = i2c_smbus_write_byte(client, chan->address);
177 dev_err(&client->dev, "cannot start measurement");
181 /* wait for integration time to pass */
182 usleep_range(delay, delay + 1000);
184 /* read measurement */
185 ret = i2c_master_recv(data->client, (char *)&val, sizeof(val));
187 dev_err(&client->dev, "cannot read sensor data\n");
190 return be16_to_cpu(val);
193 static int hdc100x_get_heater_status(struct hdc100x_data *data)
195 return !!(data->config & HDC100X_REG_CONFIG_HEATER_EN);
198 static int hdc100x_read_raw(struct iio_dev *indio_dev,
199 struct iio_chan_spec const *chan, int *val,
200 int *val2, long mask)
202 struct hdc100x_data *data = iio_priv(indio_dev);
205 case IIO_CHAN_INFO_RAW: {
208 mutex_lock(&data->lock);
209 if (chan->type == IIO_CURRENT) {
210 *val = hdc100x_get_heater_status(data);
213 ret = iio_device_claim_direct_mode(indio_dev);
215 mutex_unlock(&data->lock);
219 ret = hdc100x_get_measurement(data, chan);
220 iio_device_release_direct_mode(indio_dev);
226 mutex_unlock(&data->lock);
229 case IIO_CHAN_INFO_INT_TIME:
231 *val2 = data->adc_int_us[chan->address];
232 return IIO_VAL_INT_PLUS_MICRO;
233 case IIO_CHAN_INFO_SCALE:
234 if (chan->type == IIO_TEMP) {
237 return IIO_VAL_FRACTIONAL;
241 return IIO_VAL_FRACTIONAL;
244 case IIO_CHAN_INFO_OFFSET:
247 return IIO_VAL_INT_PLUS_MICRO;
253 static int hdc100x_write_raw(struct iio_dev *indio_dev,
254 struct iio_chan_spec const *chan,
255 int val, int val2, long mask)
257 struct hdc100x_data *data = iio_priv(indio_dev);
261 case IIO_CHAN_INFO_INT_TIME:
265 mutex_lock(&data->lock);
266 ret = hdc100x_set_it_time(data, chan->address, val2);
267 mutex_unlock(&data->lock);
269 case IIO_CHAN_INFO_RAW:
270 if (chan->type != IIO_CURRENT || val2 != 0)
273 mutex_lock(&data->lock);
274 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_HEATER_EN,
275 val ? HDC100X_REG_CONFIG_HEATER_EN : 0);
276 mutex_unlock(&data->lock);
283 static int hdc100x_buffer_postenable(struct iio_dev *indio_dev)
285 struct hdc100x_data *data = iio_priv(indio_dev);
288 /* Buffer is enabled. First set ACQ Mode, then attach poll func */
289 mutex_lock(&data->lock);
290 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE,
291 HDC100X_REG_CONFIG_ACQ_MODE);
292 mutex_unlock(&data->lock);
296 return iio_triggered_buffer_postenable(indio_dev);
299 static int hdc100x_buffer_predisable(struct iio_dev *indio_dev)
301 struct hdc100x_data *data = iio_priv(indio_dev);
304 /* First detach poll func, then reset ACQ mode. OK to disable buffer */
305 ret = iio_triggered_buffer_predisable(indio_dev);
309 mutex_lock(&data->lock);
310 ret = hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
311 mutex_unlock(&data->lock);
316 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops = {
317 .postenable = hdc100x_buffer_postenable,
318 .predisable = hdc100x_buffer_predisable,
321 static irqreturn_t hdc100x_trigger_handler(int irq, void *p)
323 struct iio_poll_func *pf = p;
324 struct iio_dev *indio_dev = pf->indio_dev;
325 struct hdc100x_data *data = iio_priv(indio_dev);
326 struct i2c_client *client = data->client;
327 int delay = data->adc_int_us[0] + data->adc_int_us[1] + 2*USEC_PER_MSEC;
330 /* dual read starts at temp register */
331 mutex_lock(&data->lock);
332 ret = i2c_smbus_write_byte(client, HDC100X_REG_TEMP);
334 dev_err(&client->dev, "cannot start measurement\n");
337 usleep_range(delay, delay + 1000);
339 ret = i2c_master_recv(client, (u8 *)data->scan.channels, 4);
341 dev_err(&client->dev, "cannot read sensor data\n");
345 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
346 iio_get_time_ns(indio_dev));
348 mutex_unlock(&data->lock);
349 iio_trigger_notify_done(indio_dev->trig);
354 static const struct iio_info hdc100x_info = {
355 .read_raw = hdc100x_read_raw,
356 .write_raw = hdc100x_write_raw,
357 .attrs = &hdc100x_attribute_group,
360 static int hdc100x_probe(struct i2c_client *client,
361 const struct i2c_device_id *id)
363 struct iio_dev *indio_dev;
364 struct hdc100x_data *data;
367 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
368 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))
371 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
375 data = iio_priv(indio_dev);
376 i2c_set_clientdata(client, indio_dev);
377 data->client = client;
378 mutex_init(&data->lock);
380 indio_dev->dev.parent = &client->dev;
381 indio_dev->name = dev_name(&client->dev);
382 indio_dev->modes = INDIO_DIRECT_MODE;
383 indio_dev->info = &hdc100x_info;
385 indio_dev->channels = hdc100x_channels;
386 indio_dev->num_channels = ARRAY_SIZE(hdc100x_channels);
387 indio_dev->available_scan_masks = hdc100x_scan_masks;
389 /* be sure we are in a known state */
390 hdc100x_set_it_time(data, 0, hdc100x_int_time[0][0]);
391 hdc100x_set_it_time(data, 1, hdc100x_int_time[1][0]);
392 hdc100x_update_config(data, HDC100X_REG_CONFIG_ACQ_MODE, 0);
394 ret = iio_triggered_buffer_setup(indio_dev, NULL,
395 hdc100x_trigger_handler,
396 &hdc_buffer_setup_ops);
398 dev_err(&client->dev, "iio triggered buffer setup failed\n");
401 ret = iio_device_register(indio_dev);
403 iio_triggered_buffer_cleanup(indio_dev);
408 static int hdc100x_remove(struct i2c_client *client)
410 struct iio_dev *indio_dev = i2c_get_clientdata(client);
412 iio_device_unregister(indio_dev);
413 iio_triggered_buffer_cleanup(indio_dev);
418 static const struct i2c_device_id hdc100x_id[] = {
427 MODULE_DEVICE_TABLE(i2c, hdc100x_id);
429 static const struct of_device_id hdc100x_dt_ids[] = {
430 { .compatible = "ti,hdc1000" },
431 { .compatible = "ti,hdc1008" },
432 { .compatible = "ti,hdc1010" },
433 { .compatible = "ti,hdc1050" },
434 { .compatible = "ti,hdc1080" },
437 MODULE_DEVICE_TABLE(of, hdc100x_dt_ids);
439 static struct i2c_driver hdc100x_driver = {
442 .of_match_table = of_match_ptr(hdc100x_dt_ids),
444 .probe = hdc100x_probe,
445 .remove = hdc100x_remove,
446 .id_table = hdc100x_id,
448 module_i2c_driver(hdc100x_driver);
450 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
451 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
452 MODULE_LICENSE("GPL");