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 * https://www.ti.com/product/HDC1000/datasheet
10 * https://www.ti.com/product/HDC1008/datasheet
11 * https://www.ti.com/product/HDC1010/datasheet
12 * https://www.ti.com/product/HDC1050/datasheet
13 * https://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 #define HDC100X_REG_TEMP 0x00
28 #define HDC100X_REG_HUMIDITY 0x01
30 #define HDC100X_REG_CONFIG 0x02
31 #define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)
32 #define HDC100X_REG_CONFIG_HEATER_EN BIT(13)
35 struct i2c_client
*client
;
39 /* integration time of the sensor */
41 /* Ensure natural alignment of timestamp */
48 /* integration time in us */
49 static const int hdc100x_int_time
[][3] = {
50 { 6350, 3650, 0 }, /* IIO_TEMP channel*/
51 { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
54 /* HDC100X_REG_CONFIG shift and mask values */
58 } hdc100x_resolution_shift
[2] = {
59 { /* IIO_TEMP channel */
63 { /* IIO_HUMIDITYRELATIVE channel */
69 static IIO_CONST_ATTR(temp_integration_time_available
,
72 static IIO_CONST_ATTR(humidityrelative_integration_time_available
,
73 "0.0025 0.00385 0.0065");
75 static IIO_CONST_ATTR(out_current_heater_raw_available
,
78 static struct attribute
*hdc100x_attributes
[] = {
79 &iio_const_attr_temp_integration_time_available
.dev_attr
.attr
,
80 &iio_const_attr_humidityrelative_integration_time_available
.dev_attr
.attr
,
81 &iio_const_attr_out_current_heater_raw_available
.dev_attr
.attr
,
85 static const struct attribute_group hdc100x_attribute_group
= {
86 .attrs
= hdc100x_attributes
,
89 static const struct iio_chan_spec hdc100x_channels
[] = {
92 .address
= HDC100X_REG_TEMP
,
93 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
94 BIT(IIO_CHAN_INFO_SCALE
) |
95 BIT(IIO_CHAN_INFO_INT_TIME
) |
96 BIT(IIO_CHAN_INFO_OFFSET
),
102 .endianness
= IIO_BE
,
106 .type
= IIO_HUMIDITYRELATIVE
,
107 .address
= HDC100X_REG_HUMIDITY
,
108 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
109 BIT(IIO_CHAN_INFO_SCALE
) |
110 BIT(IIO_CHAN_INFO_INT_TIME
),
116 .endianness
= IIO_BE
,
121 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
122 .extend_name
= "heater",
126 IIO_CHAN_SOFT_TIMESTAMP(2),
129 static const unsigned long hdc100x_scan_masks
[] = {0x3, 0};
131 static int hdc100x_update_config(struct hdc100x_data
*data
, int mask
, int val
)
133 int tmp
= (~mask
& data
->config
) | val
;
136 ret
= i2c_smbus_write_word_swapped(data
->client
,
137 HDC100X_REG_CONFIG
, tmp
);
144 static int hdc100x_set_it_time(struct hdc100x_data
*data
, int chan
, int val2
)
146 int shift
= hdc100x_resolution_shift
[chan
].shift
;
150 for (i
= 0; i
< ARRAY_SIZE(hdc100x_int_time
[chan
]); i
++) {
151 if (val2
&& val2
== hdc100x_int_time
[chan
][i
]) {
152 ret
= hdc100x_update_config(data
,
153 hdc100x_resolution_shift
[chan
].mask
<< shift
,
156 data
->adc_int_us
[chan
] = val2
;
164 static int hdc100x_get_measurement(struct hdc100x_data
*data
,
165 struct iio_chan_spec
const *chan
)
167 struct i2c_client
*client
= data
->client
;
168 int delay
= data
->adc_int_us
[chan
->address
];
172 /* start measurement */
173 ret
= i2c_smbus_write_byte(client
, chan
->address
);
175 dev_err(&client
->dev
, "cannot start measurement");
179 /* wait for integration time to pass */
180 usleep_range(delay
, delay
+ 1000);
182 /* read measurement */
183 ret
= i2c_master_recv(data
->client
, (char *)&val
, sizeof(val
));
185 dev_err(&client
->dev
, "cannot read sensor data\n");
188 return be16_to_cpu(val
);
191 static int hdc100x_get_heater_status(struct hdc100x_data
*data
)
193 return !!(data
->config
& HDC100X_REG_CONFIG_HEATER_EN
);
196 static int hdc100x_read_raw(struct iio_dev
*indio_dev
,
197 struct iio_chan_spec
const *chan
, int *val
,
198 int *val2
, long mask
)
200 struct hdc100x_data
*data
= iio_priv(indio_dev
);
203 case IIO_CHAN_INFO_RAW
: {
206 mutex_lock(&data
->lock
);
207 if (chan
->type
== IIO_CURRENT
) {
208 *val
= hdc100x_get_heater_status(data
);
211 ret
= iio_device_claim_direct_mode(indio_dev
);
213 mutex_unlock(&data
->lock
);
217 ret
= hdc100x_get_measurement(data
, chan
);
218 iio_device_release_direct_mode(indio_dev
);
224 mutex_unlock(&data
->lock
);
227 case IIO_CHAN_INFO_INT_TIME
:
229 *val2
= data
->adc_int_us
[chan
->address
];
230 return IIO_VAL_INT_PLUS_MICRO
;
231 case IIO_CHAN_INFO_SCALE
:
232 if (chan
->type
== IIO_TEMP
) {
235 return IIO_VAL_FRACTIONAL
;
239 return IIO_VAL_FRACTIONAL
;
242 case IIO_CHAN_INFO_OFFSET
:
245 return IIO_VAL_INT_PLUS_MICRO
;
251 static int hdc100x_write_raw(struct iio_dev
*indio_dev
,
252 struct iio_chan_spec
const *chan
,
253 int val
, int val2
, long mask
)
255 struct hdc100x_data
*data
= iio_priv(indio_dev
);
259 case IIO_CHAN_INFO_INT_TIME
:
263 mutex_lock(&data
->lock
);
264 ret
= hdc100x_set_it_time(data
, chan
->address
, val2
);
265 mutex_unlock(&data
->lock
);
267 case IIO_CHAN_INFO_RAW
:
268 if (chan
->type
!= IIO_CURRENT
|| val2
!= 0)
271 mutex_lock(&data
->lock
);
272 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_HEATER_EN
,
273 val
? HDC100X_REG_CONFIG_HEATER_EN
: 0);
274 mutex_unlock(&data
->lock
);
281 static int hdc100x_buffer_postenable(struct iio_dev
*indio_dev
)
283 struct hdc100x_data
*data
= iio_priv(indio_dev
);
286 /* Buffer is enabled. First set ACQ Mode, then attach poll func */
287 mutex_lock(&data
->lock
);
288 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
,
289 HDC100X_REG_CONFIG_ACQ_MODE
);
290 mutex_unlock(&data
->lock
);
295 static int hdc100x_buffer_predisable(struct iio_dev
*indio_dev
)
297 struct hdc100x_data
*data
= iio_priv(indio_dev
);
300 mutex_lock(&data
->lock
);
301 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
, 0);
302 mutex_unlock(&data
->lock
);
307 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops
= {
308 .postenable
= hdc100x_buffer_postenable
,
309 .predisable
= hdc100x_buffer_predisable
,
312 static irqreturn_t
hdc100x_trigger_handler(int irq
, void *p
)
314 struct iio_poll_func
*pf
= p
;
315 struct iio_dev
*indio_dev
= pf
->indio_dev
;
316 struct hdc100x_data
*data
= iio_priv(indio_dev
);
317 struct i2c_client
*client
= data
->client
;
318 int delay
= data
->adc_int_us
[0] + data
->adc_int_us
[1];
321 /* dual read starts at temp register */
322 mutex_lock(&data
->lock
);
323 ret
= i2c_smbus_write_byte(client
, HDC100X_REG_TEMP
);
325 dev_err(&client
->dev
, "cannot start measurement\n");
328 usleep_range(delay
, delay
+ 1000);
330 ret
= i2c_master_recv(client
, (u8
*)data
->scan
.channels
, 4);
332 dev_err(&client
->dev
, "cannot read sensor data\n");
336 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
337 iio_get_time_ns(indio_dev
));
339 mutex_unlock(&data
->lock
);
340 iio_trigger_notify_done(indio_dev
->trig
);
345 static const struct iio_info hdc100x_info
= {
346 .read_raw
= hdc100x_read_raw
,
347 .write_raw
= hdc100x_write_raw
,
348 .attrs
= &hdc100x_attribute_group
,
351 static int hdc100x_probe(struct i2c_client
*client
,
352 const struct i2c_device_id
*id
)
354 struct iio_dev
*indio_dev
;
355 struct hdc100x_data
*data
;
358 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
|
359 I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_I2C
))
362 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
366 data
= iio_priv(indio_dev
);
367 i2c_set_clientdata(client
, indio_dev
);
368 data
->client
= client
;
369 mutex_init(&data
->lock
);
371 indio_dev
->name
= dev_name(&client
->dev
);
372 indio_dev
->modes
= INDIO_DIRECT_MODE
;
373 indio_dev
->info
= &hdc100x_info
;
375 indio_dev
->channels
= hdc100x_channels
;
376 indio_dev
->num_channels
= ARRAY_SIZE(hdc100x_channels
);
377 indio_dev
->available_scan_masks
= hdc100x_scan_masks
;
379 /* be sure we are in a known state */
380 hdc100x_set_it_time(data
, 0, hdc100x_int_time
[0][0]);
381 hdc100x_set_it_time(data
, 1, hdc100x_int_time
[1][0]);
382 hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
, 0);
384 ret
= devm_iio_triggered_buffer_setup(&client
->dev
,
386 hdc100x_trigger_handler
,
387 &hdc_buffer_setup_ops
);
389 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
393 return devm_iio_device_register(&client
->dev
, indio_dev
);
396 static const struct i2c_device_id hdc100x_id
[] = {
405 MODULE_DEVICE_TABLE(i2c
, hdc100x_id
);
407 static const struct of_device_id hdc100x_dt_ids
[] = {
408 { .compatible
= "ti,hdc1000" },
409 { .compatible
= "ti,hdc1008" },
410 { .compatible
= "ti,hdc1010" },
411 { .compatible
= "ti,hdc1050" },
412 { .compatible
= "ti,hdc1080" },
415 MODULE_DEVICE_TABLE(of
, hdc100x_dt_ids
);
417 static struct i2c_driver hdc100x_driver
= {
420 .of_match_table
= of_match_ptr(hdc100x_dt_ids
),
422 .probe
= hdc100x_probe
,
423 .id_table
= hdc100x_id
,
425 module_i2c_driver(hdc100x_driver
);
427 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
428 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
429 MODULE_LICENSE("GPL");