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>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/triggered_buffer.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 */
45 /* integration time in us */
46 static const int hdc100x_int_time
[][3] = {
47 { 6350, 3650, 0 }, /* IIO_TEMP channel*/
48 { 6500, 3850, 2500 }, /* IIO_HUMIDITYRELATIVE channel */
51 /* HDC100X_REG_CONFIG shift and mask values */
55 } hdc100x_resolution_shift
[2] = {
56 { /* IIO_TEMP channel */
60 { /* IIO_HUMIDITYRELATIVE channel */
66 static IIO_CONST_ATTR(temp_integration_time_available
,
69 static IIO_CONST_ATTR(humidityrelative_integration_time_available
,
70 "0.0025 0.00385 0.0065");
72 static IIO_CONST_ATTR(out_current_heater_raw_available
,
75 static struct attribute
*hdc100x_attributes
[] = {
76 &iio_const_attr_temp_integration_time_available
.dev_attr
.attr
,
77 &iio_const_attr_humidityrelative_integration_time_available
.dev_attr
.attr
,
78 &iio_const_attr_out_current_heater_raw_available
.dev_attr
.attr
,
82 static struct attribute_group hdc100x_attribute_group
= {
83 .attrs
= hdc100x_attributes
,
86 static const struct iio_chan_spec hdc100x_channels
[] = {
89 .address
= HDC100X_REG_TEMP
,
90 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
91 BIT(IIO_CHAN_INFO_SCALE
) |
92 BIT(IIO_CHAN_INFO_INT_TIME
) |
93 BIT(IIO_CHAN_INFO_OFFSET
),
103 .type
= IIO_HUMIDITYRELATIVE
,
104 .address
= HDC100X_REG_HUMIDITY
,
105 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
106 BIT(IIO_CHAN_INFO_SCALE
) |
107 BIT(IIO_CHAN_INFO_INT_TIME
),
113 .endianness
= IIO_BE
,
118 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
119 .extend_name
= "heater",
123 IIO_CHAN_SOFT_TIMESTAMP(2),
126 static const unsigned long hdc100x_scan_masks
[] = {0x3, 0};
128 static int hdc100x_update_config(struct hdc100x_data
*data
, int mask
, int val
)
130 int tmp
= (~mask
& data
->config
) | val
;
133 ret
= i2c_smbus_write_word_swapped(data
->client
,
134 HDC100X_REG_CONFIG
, tmp
);
141 static int hdc100x_set_it_time(struct hdc100x_data
*data
, int chan
, int val2
)
143 int shift
= hdc100x_resolution_shift
[chan
].shift
;
147 for (i
= 0; i
< ARRAY_SIZE(hdc100x_int_time
[chan
]); i
++) {
148 if (val2
&& val2
== hdc100x_int_time
[chan
][i
]) {
149 ret
= hdc100x_update_config(data
,
150 hdc100x_resolution_shift
[chan
].mask
<< shift
,
153 data
->adc_int_us
[chan
] = val2
;
161 static int hdc100x_get_measurement(struct hdc100x_data
*data
,
162 struct iio_chan_spec
const *chan
)
164 struct i2c_client
*client
= data
->client
;
165 int delay
= data
->adc_int_us
[chan
->address
];
169 /* start measurement */
170 ret
= i2c_smbus_write_byte(client
, chan
->address
);
172 dev_err(&client
->dev
, "cannot start measurement");
176 /* wait for integration time to pass */
177 usleep_range(delay
, delay
+ 1000);
179 /* read measurement */
180 ret
= i2c_master_recv(data
->client
, (char *)&val
, sizeof(val
));
182 dev_err(&client
->dev
, "cannot read sensor data\n");
185 return be16_to_cpu(val
);
188 static int hdc100x_get_heater_status(struct hdc100x_data
*data
)
190 return !!(data
->config
& HDC100X_REG_CONFIG_HEATER_EN
);
193 static int hdc100x_read_raw(struct iio_dev
*indio_dev
,
194 struct iio_chan_spec
const *chan
, int *val
,
195 int *val2
, long mask
)
197 struct hdc100x_data
*data
= iio_priv(indio_dev
);
200 case IIO_CHAN_INFO_RAW
: {
203 mutex_lock(&data
->lock
);
204 if (chan
->type
== IIO_CURRENT
) {
205 *val
= hdc100x_get_heater_status(data
);
208 ret
= iio_device_claim_direct_mode(indio_dev
);
210 mutex_unlock(&data
->lock
);
214 ret
= hdc100x_get_measurement(data
, chan
);
215 iio_device_release_direct_mode(indio_dev
);
221 mutex_unlock(&data
->lock
);
224 case IIO_CHAN_INFO_INT_TIME
:
226 *val2
= data
->adc_int_us
[chan
->address
];
227 return IIO_VAL_INT_PLUS_MICRO
;
228 case IIO_CHAN_INFO_SCALE
:
229 if (chan
->type
== IIO_TEMP
) {
232 return IIO_VAL_FRACTIONAL
;
236 return IIO_VAL_FRACTIONAL
;
239 case IIO_CHAN_INFO_OFFSET
:
242 return IIO_VAL_INT_PLUS_MICRO
;
248 static int hdc100x_write_raw(struct iio_dev
*indio_dev
,
249 struct iio_chan_spec
const *chan
,
250 int val
, int val2
, long mask
)
252 struct hdc100x_data
*data
= iio_priv(indio_dev
);
256 case IIO_CHAN_INFO_INT_TIME
:
260 mutex_lock(&data
->lock
);
261 ret
= hdc100x_set_it_time(data
, chan
->address
, val2
);
262 mutex_unlock(&data
->lock
);
264 case IIO_CHAN_INFO_RAW
:
265 if (chan
->type
!= IIO_CURRENT
|| val2
!= 0)
268 mutex_lock(&data
->lock
);
269 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_HEATER_EN
,
270 val
? HDC100X_REG_CONFIG_HEATER_EN
: 0);
271 mutex_unlock(&data
->lock
);
278 static int hdc100x_buffer_postenable(struct iio_dev
*indio_dev
)
280 struct hdc100x_data
*data
= iio_priv(indio_dev
);
283 /* Buffer is enabled. First set ACQ Mode, then attach poll func */
284 mutex_lock(&data
->lock
);
285 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
,
286 HDC100X_REG_CONFIG_ACQ_MODE
);
287 mutex_unlock(&data
->lock
);
291 return iio_triggered_buffer_postenable(indio_dev
);
294 static int hdc100x_buffer_predisable(struct iio_dev
*indio_dev
)
296 struct hdc100x_data
*data
= iio_priv(indio_dev
);
299 /* First detach poll func, then reset ACQ mode. OK to disable buffer */
300 ret
= iio_triggered_buffer_predisable(indio_dev
);
304 mutex_lock(&data
->lock
);
305 ret
= hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
, 0);
306 mutex_unlock(&data
->lock
);
311 static const struct iio_buffer_setup_ops hdc_buffer_setup_ops
= {
312 .postenable
= hdc100x_buffer_postenable
,
313 .predisable
= hdc100x_buffer_predisable
,
316 static irqreturn_t
hdc100x_trigger_handler(int irq
, void *p
)
318 struct iio_poll_func
*pf
= p
;
319 struct iio_dev
*indio_dev
= pf
->indio_dev
;
320 struct hdc100x_data
*data
= iio_priv(indio_dev
);
321 struct i2c_client
*client
= data
->client
;
322 int delay
= data
->adc_int_us
[0] + data
->adc_int_us
[1];
324 s16 buf
[8]; /* 2x s16 + padding + 8 byte timestamp */
326 /* dual read starts at temp register */
327 mutex_lock(&data
->lock
);
328 ret
= i2c_smbus_write_byte(client
, HDC100X_REG_TEMP
);
330 dev_err(&client
->dev
, "cannot start measurement\n");
333 usleep_range(delay
, delay
+ 1000);
335 ret
= i2c_master_recv(client
, (u8
*)buf
, 4);
337 dev_err(&client
->dev
, "cannot read sensor data\n");
341 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
342 iio_get_time_ns(indio_dev
));
344 mutex_unlock(&data
->lock
);
345 iio_trigger_notify_done(indio_dev
->trig
);
350 static const struct iio_info hdc100x_info
= {
351 .read_raw
= hdc100x_read_raw
,
352 .write_raw
= hdc100x_write_raw
,
353 .attrs
= &hdc100x_attribute_group
,
354 .driver_module
= THIS_MODULE
,
357 static int hdc100x_probe(struct i2c_client
*client
,
358 const struct i2c_device_id
*id
)
360 struct iio_dev
*indio_dev
;
361 struct hdc100x_data
*data
;
364 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
|
365 I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_I2C
))
368 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
372 data
= iio_priv(indio_dev
);
373 i2c_set_clientdata(client
, indio_dev
);
374 data
->client
= client
;
375 mutex_init(&data
->lock
);
377 indio_dev
->dev
.parent
= &client
->dev
;
378 indio_dev
->name
= dev_name(&client
->dev
);
379 indio_dev
->modes
= INDIO_DIRECT_MODE
;
380 indio_dev
->info
= &hdc100x_info
;
382 indio_dev
->channels
= hdc100x_channels
;
383 indio_dev
->num_channels
= ARRAY_SIZE(hdc100x_channels
);
384 indio_dev
->available_scan_masks
= hdc100x_scan_masks
;
386 /* be sure we are in a known state */
387 hdc100x_set_it_time(data
, 0, hdc100x_int_time
[0][0]);
388 hdc100x_set_it_time(data
, 1, hdc100x_int_time
[1][0]);
389 hdc100x_update_config(data
, HDC100X_REG_CONFIG_ACQ_MODE
, 0);
391 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
392 hdc100x_trigger_handler
,
393 &hdc_buffer_setup_ops
);
395 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
398 ret
= iio_device_register(indio_dev
);
400 iio_triggered_buffer_cleanup(indio_dev
);
405 static int hdc100x_remove(struct i2c_client
*client
)
407 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
409 iio_device_unregister(indio_dev
);
410 iio_triggered_buffer_cleanup(indio_dev
);
415 static const struct i2c_device_id hdc100x_id
[] = {
419 MODULE_DEVICE_TABLE(i2c
, hdc100x_id
);
421 static struct i2c_driver hdc100x_driver
= {
425 .probe
= hdc100x_probe
,
426 .remove
= hdc100x_remove
,
427 .id_table
= hdc100x_id
,
429 module_i2c_driver(hdc100x_driver
);
431 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
432 MODULE_DESCRIPTION("TI HDC100x humidity and temperature sensor driver");
433 MODULE_LICENSE("GPL");