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 #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 */
43 /* integration time in us */
44 static const int hdc100x_int_time
[][3] = {
45 { 6350, 3650, 0 }, /* IIO_TEMP channel*/
46 { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
49 /* HDC100X_REG_CONFIG shift and mask values */
53 } hdc100x_resolution_shift
[2] = {
54 { /* IIO_TEMP channel */
58 { /* IIO_HUMIDITYRELATIVE channel */
64 static IIO_CONST_ATTR(temp_integration_time_available
,
67 static IIO_CONST_ATTR(humidityrelative_integration_time_available
,
68 "0.0025 0.00385 0.0065");
70 static IIO_CONST_ATTR(out_current_heater_raw_available
,
73 static struct attribute
*hdc100x_attributes
[] = {
74 &iio_const_attr_temp_integration_time_available
.dev_attr
.attr
,
75 &iio_const_attr_humidityrelative_integration_time_available
.dev_attr
.attr
,
76 &iio_const_attr_out_current_heater_raw_available
.dev_attr
.attr
,
80 static const struct attribute_group hdc100x_attribute_group
= {
81 .attrs
= hdc100x_attributes
,
84 static const struct iio_chan_spec hdc100x_channels
[] = {
87 .address
= HDC100X_REG_TEMP
,
88 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
89 BIT(IIO_CHAN_INFO_SCALE
) |
90 BIT(IIO_CHAN_INFO_INT_TIME
) |
91 BIT(IIO_CHAN_INFO_OFFSET
),
101 .type
= IIO_HUMIDITYRELATIVE
,
102 .address
= HDC100X_REG_HUMIDITY
,
103 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
104 BIT(IIO_CHAN_INFO_SCALE
) |
105 BIT(IIO_CHAN_INFO_INT_TIME
),
111 .endianness
= IIO_BE
,
116 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
117 .extend_name
= "heater",
121 IIO_CHAN_SOFT_TIMESTAMP(2),
124 static const unsigned long hdc100x_scan_masks
[] = {0x3, 0};
126 static int hdc100x_update_config(struct hdc100x_data
*data
, int mask
, int val
)
128 int tmp
= (~mask
& data
->config
) | val
;
131 ret
= i2c_smbus_write_word_swapped(data
->client
,
132 HDC100X_REG_CONFIG
, tmp
);
139 static int hdc100x_set_it_time(struct hdc100x_data
*data
, int chan
, int val2
)
141 int shift
= hdc100x_resolution_shift
[chan
].shift
;
145 for (i
= 0; i
< ARRAY_SIZE(hdc100x_int_time
[chan
]); i
++) {
146 if (val2
&& val2
== hdc100x_int_time
[chan
][i
]) {
147 ret
= hdc100x_update_config(data
,
148 hdc100x_resolution_shift
[chan
].mask
<< shift
,
151 data
->adc_int_us
[chan
] = val2
;
159 static int hdc100x_get_measurement(struct hdc100x_data
*data
,
160 struct iio_chan_spec
const *chan
)
162 struct i2c_client
*client
= data
->client
;
163 int delay
= data
->adc_int_us
[chan
->address
];
167 /* start measurement */
168 ret
= i2c_smbus_write_byte(client
, chan
->address
);
170 dev_err(&client
->dev
, "cannot start measurement");
174 /* wait for integration time to pass */
175 usleep_range(delay
, delay
+ 1000);
177 /* read measurement */
178 ret
= i2c_master_recv(data
->client
, (char *)&val
, sizeof(val
));
180 dev_err(&client
->dev
, "cannot read sensor data\n");
183 return be16_to_cpu(val
);
186 static int hdc100x_get_heater_status(struct hdc100x_data
*data
)
188 return !!(data
->config
& HDC100X_REG_CONFIG_HEATER_EN
);
191 static int hdc100x_read_raw(struct iio_dev
*indio_dev
,
192 struct iio_chan_spec
const *chan
, int *val
,
193 int *val2
, long mask
)
195 struct hdc100x_data
*data
= iio_priv(indio_dev
);
198 case IIO_CHAN_INFO_RAW
: {
201 mutex_lock(&data
->lock
);
202 if (chan
->type
== IIO_CURRENT
) {
203 *val
= hdc100x_get_heater_status(data
);
206 ret
= iio_device_claim_direct_mode(indio_dev
);
208 mutex_unlock(&data
->lock
);
212 ret
= hdc100x_get_measurement(data
, chan
);
213 iio_device_release_direct_mode(indio_dev
);
219 mutex_unlock(&data
->lock
);
222 case IIO_CHAN_INFO_INT_TIME
:
224 *val2
= data
->adc_int_us
[chan
->address
];
225 return IIO_VAL_INT_PLUS_MICRO
;
226 case IIO_CHAN_INFO_SCALE
:
227 if (chan
->type
== IIO_TEMP
) {
230 return IIO_VAL_FRACTIONAL
;
234 return IIO_VAL_FRACTIONAL
;
237 case IIO_CHAN_INFO_OFFSET
:
240 return IIO_VAL_INT_PLUS_MICRO
;
246 static int hdc100x_write_raw(struct iio_dev
*indio_dev
,
247 struct iio_chan_spec
const *chan
,
248 int val
, int val2
, long mask
)
250 struct hdc100x_data
*data
= iio_priv(indio_dev
);
254 case IIO_CHAN_INFO_INT_TIME
:
258 mutex_lock(&data
->lock
);
259 ret
= hdc100x_set_it_time(data
, chan
->address
, val2
);
260 mutex_unlock(&data
->lock
);
262 case IIO_CHAN_INFO_RAW
:
263 if (chan
->type
!= IIO_CURRENT
|| val2
!= 0)
266 mutex_lock(&data
->lock
);
267 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_HEATER_EN
,
268 val
? HDC100X_REG_CONFIG_HEATER_EN
: 0);
269 mutex_unlock(&data
->lock
);
276 static int hdc100x_buffer_postenable(struct iio_dev
*indio_dev
)
278 struct hdc100x_data
*data
= iio_priv(indio_dev
);
281 /* Buffer is enabled. First set ACQ Mode, then attach poll func */
282 mutex_lock(&data
->lock
);
283 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
,
284 HDC100X_REG_CONFIG_ACQ_MODE
);
285 mutex_unlock(&data
->lock
);
289 return iio_triggered_buffer_postenable(indio_dev
);
292 static int hdc100x_buffer_predisable(struct iio_dev
*indio_dev
)
294 struct hdc100x_data
*data
= iio_priv(indio_dev
);
297 /* First detach poll func, then reset ACQ mode. OK to disable buffer */
298 ret
= iio_triggered_buffer_predisable(indio_dev
);
302 mutex_lock(&data
->lock
);
303 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
, 0);
304 mutex_unlock(&data
->lock
);
309 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops
= {
310 .postenable
= hdc100x_buffer_postenable
,
311 .predisable
= hdc100x_buffer_predisable
,
314 static irqreturn_t
hdc100x_trigger_handler(int irq
, void *p
)
316 struct iio_poll_func
*pf
= p
;
317 struct iio_dev
*indio_dev
= pf
->indio_dev
;
318 struct hdc100x_data
*data
= iio_priv(indio_dev
);
319 struct i2c_client
*client
= data
->client
;
320 int delay
= data
->adc_int_us
[0] + data
->adc_int_us
[1];
322 s16 buf
[8]; /* 2x s16 + padding + 8 byte timestamp */
324 /* dual read starts at temp register */
325 mutex_lock(&data
->lock
);
326 ret
= i2c_smbus_write_byte(client
, HDC100X_REG_TEMP
);
328 dev_err(&client
->dev
, "cannot start measurement\n");
331 usleep_range(delay
, delay
+ 1000);
333 ret
= i2c_master_recv(client
, (u8
*)buf
, 4);
335 dev_err(&client
->dev
, "cannot read sensor data\n");
339 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
340 iio_get_time_ns(indio_dev
));
342 mutex_unlock(&data
->lock
);
343 iio_trigger_notify_done(indio_dev
->trig
);
348 static const struct iio_info hdc100x_info
= {
349 .read_raw
= hdc100x_read_raw
,
350 .write_raw
= hdc100x_write_raw
,
351 .attrs
= &hdc100x_attribute_group
,
354 static int hdc100x_probe(struct i2c_client
*client
,
355 const struct i2c_device_id
*id
)
357 struct iio_dev
*indio_dev
;
358 struct hdc100x_data
*data
;
361 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
|
362 I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_I2C
))
365 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
369 data
= iio_priv(indio_dev
);
370 i2c_set_clientdata(client
, indio_dev
);
371 data
->client
= client
;
372 mutex_init(&data
->lock
);
374 indio_dev
->dev
.parent
= &client
->dev
;
375 indio_dev
->name
= dev_name(&client
->dev
);
376 indio_dev
->modes
= INDIO_DIRECT_MODE
;
377 indio_dev
->info
= &hdc100x_info
;
379 indio_dev
->channels
= hdc100x_channels
;
380 indio_dev
->num_channels
= ARRAY_SIZE(hdc100x_channels
);
381 indio_dev
->available_scan_masks
= hdc100x_scan_masks
;
383 /* be sure we are in a known state */
384 hdc100x_set_it_time(data
, 0, hdc100x_int_time
[0][0]);
385 hdc100x_set_it_time(data
, 1, hdc100x_int_time
[1][0]);
386 hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
, 0);
388 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
389 hdc100x_trigger_handler
,
390 &hdc_buffer_setup_ops
);
392 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
395 ret
= iio_device_register(indio_dev
);
397 iio_triggered_buffer_cleanup(indio_dev
);
402 static int hdc100x_remove(struct i2c_client
*client
)
404 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
406 iio_device_unregister(indio_dev
);
407 iio_triggered_buffer_cleanup(indio_dev
);
412 static const struct i2c_device_id hdc100x_id
[] = {
421 MODULE_DEVICE_TABLE(i2c
, hdc100x_id
);
423 static const struct of_device_id hdc100x_dt_ids
[] = {
424 { .compatible
= "ti,hdc1000" },
425 { .compatible
= "ti,hdc1008" },
426 { .compatible
= "ti,hdc1010" },
427 { .compatible
= "ti,hdc1050" },
428 { .compatible
= "ti,hdc1080" },
431 MODULE_DEVICE_TABLE(of
, hdc100x_dt_ids
);
433 static struct i2c_driver hdc100x_driver
= {
436 .of_match_table
= of_match_ptr(hdc100x_dt_ids
),
438 .probe
= hdc100x_probe
,
439 .remove
= hdc100x_remove
,
440 .id_table
= hdc100x_id
,
442 module_i2c_driver(hdc100x_driver
);
444 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
445 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
446 MODULE_LICENSE("GPL");