1 // SPDX-License-Identifier: GPL-2.0+
3 * hdc3020.c - Support for the TI HDC3020,HDC3021 and HDC3022
4 * temperature + relative humidity sensors
8 * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH
10 * Datasheet: https://www.ti.com/lit/ds/symlink/hdc3020.pdf
13 #include <linux/bitfield.h>
14 #include <linux/bitops.h>
15 #include <linux/cleanup.h>
16 #include <linux/crc8.h>
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/math64.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/units.h>
29 #include <linux/unaligned.h>
31 #include <linux/iio/events.h>
32 #include <linux/iio/iio.h>
34 #define HDC3020_S_AUTO_10HZ_MOD0 0x2737
35 #define HDC3020_S_STATUS 0x3041
36 #define HDC3020_HEATER_DISABLE 0x3066
37 #define HDC3020_HEATER_ENABLE 0x306D
38 #define HDC3020_HEATER_CONFIG 0x306E
39 #define HDC3020_EXIT_AUTO 0x3093
40 #define HDC3020_S_T_RH_THRESH_LOW 0x6100
41 #define HDC3020_S_T_RH_THRESH_LOW_CLR 0x610B
42 #define HDC3020_S_T_RH_THRESH_HIGH_CLR 0x6116
43 #define HDC3020_S_T_RH_THRESH_HIGH 0x611D
44 #define HDC3020_R_T_RH_AUTO 0xE000
45 #define HDC3020_R_T_LOW_AUTO 0xE002
46 #define HDC3020_R_T_HIGH_AUTO 0xE003
47 #define HDC3020_R_RH_LOW_AUTO 0xE004
48 #define HDC3020_R_RH_HIGH_AUTO 0xE005
49 #define HDC3020_R_T_RH_THRESH_LOW 0xE102
50 #define HDC3020_R_T_RH_THRESH_LOW_CLR 0xE109
51 #define HDC3020_R_T_RH_THRESH_HIGH_CLR 0xE114
52 #define HDC3020_R_T_RH_THRESH_HIGH 0xE11F
53 #define HDC3020_R_STATUS 0xF32D
55 #define HDC3020_THRESH_TEMP_MASK GENMASK(8, 0)
56 #define HDC3020_THRESH_TEMP_TRUNC_SHIFT 7
57 #define HDC3020_THRESH_HUM_MASK GENMASK(15, 9)
58 #define HDC3020_THRESH_HUM_TRUNC_SHIFT 9
60 #define HDC3020_STATUS_T_LOW_ALERT BIT(6)
61 #define HDC3020_STATUS_T_HIGH_ALERT BIT(7)
62 #define HDC3020_STATUS_RH_LOW_ALERT BIT(8)
63 #define HDC3020_STATUS_RH_HIGH_ALERT BIT(9)
65 #define HDC3020_READ_RETRY_TIMES 10
66 #define HDC3020_BUSY_DELAY_MS 10
68 #define HDC3020_CRC8_POLYNOMIAL 0x31
70 #define HDC3020_MIN_TEMP_MICRO -39872968
71 #define HDC3020_MAX_TEMP_MICRO 124875639
72 #define HDC3020_MAX_TEMP_HYST_MICRO 164748607
73 #define HDC3020_MAX_HUM_MICRO 99220264
76 struct i2c_client
*client
;
77 struct gpio_desc
*reset_gpio
;
78 struct regulator
*vdd_supply
;
80 * Ensure that the sensor configuration (currently only heater is
81 * supported) will not be changed during the process of reading
82 * sensor data (this driver will try HDC3020_READ_RETRY_TIMES times
83 * if the device does not respond).
88 static const int hdc3020_heater_vals
[] = {0, 1, 0x3FFF};
90 static const struct iio_event_spec hdc3020_t_rh_event
[] = {
92 .type
= IIO_EV_TYPE_THRESH
,
93 .dir
= IIO_EV_DIR_RISING
,
94 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
95 BIT(IIO_EV_INFO_HYSTERESIS
),
98 .type
= IIO_EV_TYPE_THRESH
,
99 .dir
= IIO_EV_DIR_FALLING
,
100 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
101 BIT(IIO_EV_INFO_HYSTERESIS
),
105 static const struct iio_chan_spec hdc3020_channels
[] = {
108 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
109 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_PEAK
) |
110 BIT(IIO_CHAN_INFO_TROUGH
) | BIT(IIO_CHAN_INFO_OFFSET
),
111 .event_spec
= hdc3020_t_rh_event
,
112 .num_event_specs
= ARRAY_SIZE(hdc3020_t_rh_event
),
115 .type
= IIO_HUMIDITYRELATIVE
,
116 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
117 BIT(IIO_CHAN_INFO_SCALE
) | BIT(IIO_CHAN_INFO_PEAK
) |
118 BIT(IIO_CHAN_INFO_TROUGH
),
119 .event_spec
= hdc3020_t_rh_event
,
120 .num_event_specs
= ARRAY_SIZE(hdc3020_t_rh_event
),
124 * For setting the internal heater, which can be switched on to
125 * prevent or remove any condensation that may develop when the
126 * ambient environment approaches its dew point temperature.
129 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
130 .info_mask_separate_available
= BIT(IIO_CHAN_INFO_RAW
),
135 DECLARE_CRC8_TABLE(hdc3020_crc8_table
);
137 static int hdc3020_write_bytes(struct hdc3020_data
*data
, u8
*buf
, u8 len
)
139 struct i2c_client
*client
= data
->client
;
143 msg
.addr
= client
->addr
;
149 * During the measurement process, HDC3020 will not return data.
150 * So wait for a while and try again
152 for (cnt
= 0; cnt
< HDC3020_READ_RETRY_TIMES
; cnt
++) {
153 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
157 mdelay(HDC3020_BUSY_DELAY_MS
);
159 dev_err(&client
->dev
, "Could not write sensor command\n");
165 int hdc3020_read_bytes(struct hdc3020_data
*data
, u16 reg
, u8
*buf
, int len
)
169 struct i2c_client
*client
= data
->client
;
170 struct i2c_msg msg
[2] = {
172 .addr
= client
->addr
,
178 .addr
= client
->addr
,
185 put_unaligned_be16(reg
, reg_buf
);
187 * During the measurement process, HDC3020 will not return data.
188 * So wait for a while and try again
190 for (cnt
= 0; cnt
< HDC3020_READ_RETRY_TIMES
; cnt
++) {
191 ret
= i2c_transfer(client
->adapter
, msg
, 2);
195 mdelay(HDC3020_BUSY_DELAY_MS
);
197 dev_err(&client
->dev
, "Could not read sensor data\n");
202 static int hdc3020_read_be16(struct hdc3020_data
*data
, u16 reg
)
207 ret
= hdc3020_read_bytes(data
, reg
, buf
, 3);
211 crc
= crc8(hdc3020_crc8_table
, buf
, 2, CRC8_INIT_VALUE
);
215 return get_unaligned_be16(buf
);
218 static int hdc3020_exec_cmd(struct hdc3020_data
*data
, u16 reg
)
222 put_unaligned_be16(reg
, reg_buf
);
223 return hdc3020_write_bytes(data
, reg_buf
, 2);
226 static int hdc3020_read_measurement(struct hdc3020_data
*data
,
227 enum iio_chan_type type
, int *val
)
232 ret
= hdc3020_read_bytes(data
, HDC3020_R_T_RH_AUTO
, buf
, 6);
236 /* CRC check of the temperature measurement */
237 crc
= crc8(hdc3020_crc8_table
, buf
, 2, CRC8_INIT_VALUE
);
241 /* CRC check of the relative humidity measurement */
242 crc
= crc8(hdc3020_crc8_table
, buf
+ 3, 2, CRC8_INIT_VALUE
);
246 if (type
== IIO_TEMP
)
247 *val
= get_unaligned_be16(buf
);
248 else if (type
== IIO_HUMIDITYRELATIVE
)
249 *val
= get_unaligned_be16(&buf
[3]);
256 static int hdc3020_read_raw(struct iio_dev
*indio_dev
,
257 struct iio_chan_spec
const *chan
, int *val
,
258 int *val2
, long mask
)
260 struct hdc3020_data
*data
= iio_priv(indio_dev
);
263 if (chan
->type
!= IIO_TEMP
&& chan
->type
!= IIO_HUMIDITYRELATIVE
)
267 case IIO_CHAN_INFO_RAW
: {
268 guard(mutex
)(&data
->lock
);
269 ret
= hdc3020_read_measurement(data
, chan
->type
, val
);
275 case IIO_CHAN_INFO_PEAK
: {
276 guard(mutex
)(&data
->lock
);
277 if (chan
->type
== IIO_TEMP
)
278 ret
= hdc3020_read_be16(data
, HDC3020_R_T_HIGH_AUTO
);
280 ret
= hdc3020_read_be16(data
, HDC3020_R_RH_HIGH_AUTO
);
288 case IIO_CHAN_INFO_TROUGH
: {
289 guard(mutex
)(&data
->lock
);
290 if (chan
->type
== IIO_TEMP
)
291 ret
= hdc3020_read_be16(data
, HDC3020_R_T_LOW_AUTO
);
293 ret
= hdc3020_read_be16(data
, HDC3020_R_RH_LOW_AUTO
);
301 case IIO_CHAN_INFO_SCALE
:
303 if (chan
->type
== IIO_TEMP
)
307 return IIO_VAL_FRACTIONAL
;
309 case IIO_CHAN_INFO_OFFSET
:
310 if (chan
->type
!= IIO_TEMP
)
321 static int hdc3020_read_available(struct iio_dev
*indio_dev
,
322 struct iio_chan_spec
const *chan
,
324 int *type
, int *length
, long mask
)
326 if (mask
!= IIO_CHAN_INFO_RAW
|| chan
->type
!= IIO_CURRENT
)
329 *vals
= hdc3020_heater_vals
;
332 return IIO_AVAIL_RANGE
;
335 static int hdc3020_update_heater(struct hdc3020_data
*data
, int val
)
340 if (val
< hdc3020_heater_vals
[0] || val
> hdc3020_heater_vals
[2])
344 hdc3020_exec_cmd(data
, HDC3020_HEATER_DISABLE
);
346 put_unaligned_be16(HDC3020_HEATER_CONFIG
, buf
);
347 put_unaligned_be16(val
& GENMASK(13, 0), &buf
[2]);
348 buf
[4] = crc8(hdc3020_crc8_table
, buf
+ 2, 2, CRC8_INIT_VALUE
);
349 ret
= hdc3020_write_bytes(data
, buf
, 5);
353 return hdc3020_exec_cmd(data
, HDC3020_HEATER_ENABLE
);
356 static int hdc3020_write_raw(struct iio_dev
*indio_dev
,
357 struct iio_chan_spec
const *chan
,
358 int val
, int val2
, long mask
)
360 struct hdc3020_data
*data
= iio_priv(indio_dev
);
363 case IIO_CHAN_INFO_RAW
:
364 if (chan
->type
!= IIO_CURRENT
)
367 guard(mutex
)(&data
->lock
);
368 return hdc3020_update_heater(data
, val
);
374 static int hdc3020_thresh_get_temp(u16 thresh
)
379 * Get the temperature threshold from 9 LSBs, shift them to get
380 * the truncated temperature threshold representation and
381 * calculate the threshold according to the formula in the
382 * datasheet. Result is degree celsius scaled by 65535.
384 temp
= FIELD_GET(HDC3020_THRESH_TEMP_MASK
, thresh
) <<
385 HDC3020_THRESH_TEMP_TRUNC_SHIFT
;
387 return -2949075 + (175 * temp
);
390 static int hdc3020_thresh_get_hum(u16 thresh
)
395 * Get the humidity threshold from 7 MSBs, shift them to get the
396 * truncated humidity threshold representation and calculate the
397 * threshold according to the formula in the datasheet. Result is
398 * percent scaled by 65535.
400 hum
= FIELD_GET(HDC3020_THRESH_HUM_MASK
, thresh
) <<
401 HDC3020_THRESH_HUM_TRUNC_SHIFT
;
406 static u16
hdc3020_thresh_set_temp(int s_temp
, u16 curr_thresh
)
412 * Calculate temperature threshold, shift it down to get the
413 * truncated threshold representation in the 9LSBs while keeping
414 * the current humidity threshold in the 7 MSBs.
416 temp
= (u64
)(s_temp
+ 45000000) * 65535ULL;
417 temp
= div_u64(temp
, 1000000 * 175) >> HDC3020_THRESH_TEMP_TRUNC_SHIFT
;
418 thresh
= FIELD_PREP(HDC3020_THRESH_TEMP_MASK
, temp
);
419 thresh
|= (FIELD_GET(HDC3020_THRESH_HUM_MASK
, curr_thresh
) <<
420 HDC3020_THRESH_HUM_TRUNC_SHIFT
);
425 static u16
hdc3020_thresh_set_hum(int s_hum
, u16 curr_thresh
)
431 * Calculate humidity threshold, shift it down and up to get the
432 * truncated threshold representation in the 7MSBs while keeping
433 * the current temperature threshold in the 9 LSBs.
435 hum
= (u64
)(s_hum
) * 65535ULL;
436 hum
= div_u64(hum
, 1000000 * 100) >> HDC3020_THRESH_HUM_TRUNC_SHIFT
;
437 thresh
= FIELD_PREP(HDC3020_THRESH_HUM_MASK
, hum
);
438 thresh
|= FIELD_GET(HDC3020_THRESH_TEMP_MASK
, curr_thresh
);
444 int hdc3020_thresh_clr(s64 s_thresh
, s64 s_hyst
, enum iio_event_direction dir
)
449 * Include directions when calculation the clear value,
450 * since hysteresis is unsigned by definition and the
451 * clear value is an absolute value which is signed.
453 if (dir
== IIO_EV_DIR_RISING
)
454 s_clr
= s_thresh
- s_hyst
;
456 s_clr
= s_thresh
+ s_hyst
;
458 /* Divide by 65535 to get units of micro */
459 return div_s64(s_clr
, 65535);
462 static int _hdc3020_write_thresh(struct hdc3020_data
*data
, u16 reg
, u16 val
)
466 put_unaligned_be16(reg
, buf
);
467 put_unaligned_be16(val
, buf
+ 2);
468 buf
[4] = crc8(hdc3020_crc8_table
, buf
+ 2, 2, CRC8_INIT_VALUE
);
470 return hdc3020_write_bytes(data
, buf
, 5);
473 static int hdc3020_write_thresh(struct iio_dev
*indio_dev
,
474 const struct iio_chan_spec
*chan
,
475 enum iio_event_type type
,
476 enum iio_event_direction dir
,
477 enum iio_event_info info
,
480 struct hdc3020_data
*data
= iio_priv(indio_dev
);
481 u16 reg
, reg_val
, reg_thresh_rd
, reg_clr_rd
, reg_thresh_wr
, reg_clr_wr
;
482 s64 s_thresh
, s_hyst
, s_clr
;
483 int s_val
, thresh
, clr
, ret
;
485 /* Select threshold registers */
486 if (dir
== IIO_EV_DIR_RISING
) {
487 reg_thresh_rd
= HDC3020_R_T_RH_THRESH_HIGH
;
488 reg_thresh_wr
= HDC3020_S_T_RH_THRESH_HIGH
;
489 reg_clr_rd
= HDC3020_R_T_RH_THRESH_HIGH_CLR
;
490 reg_clr_wr
= HDC3020_S_T_RH_THRESH_HIGH_CLR
;
492 reg_thresh_rd
= HDC3020_R_T_RH_THRESH_LOW
;
493 reg_thresh_wr
= HDC3020_S_T_RH_THRESH_LOW
;
494 reg_clr_rd
= HDC3020_R_T_RH_THRESH_LOW_CLR
;
495 reg_clr_wr
= HDC3020_S_T_RH_THRESH_LOW_CLR
;
498 guard(mutex
)(&data
->lock
);
499 ret
= hdc3020_read_be16(data
, reg_thresh_rd
);
504 ret
= hdc3020_read_be16(data
, reg_clr_rd
);
509 /* Scale value to include decimal part into calculations */
510 s_val
= (val
< 0) ? (val
* 1000000 - val2
) : (val
* 1000000 + val2
);
511 switch (chan
->type
) {
514 case IIO_EV_INFO_VALUE
:
515 s_val
= max(s_val
, HDC3020_MIN_TEMP_MICRO
);
516 s_val
= min(s_val
, HDC3020_MAX_TEMP_MICRO
);
518 reg_val
= hdc3020_thresh_set_temp(s_val
, thresh
);
519 ret
= _hdc3020_write_thresh(data
, reg
, reg_val
);
523 /* Calculate old hysteresis */
524 s_thresh
= (s64
)hdc3020_thresh_get_temp(thresh
) * 1000000;
525 s_clr
= (s64
)hdc3020_thresh_get_temp(clr
) * 1000000;
526 s_hyst
= div_s64(abs(s_thresh
- s_clr
), 65535);
527 /* Set new threshold */
529 /* Set old hysteresis */
532 case IIO_EV_INFO_HYSTERESIS
:
534 * Function hdc3020_thresh_get_temp returns temperature
535 * in degree celsius scaled by 65535. Scale by 1000000
536 * to be able to subtract scaled hysteresis value.
538 s_thresh
= (s64
)hdc3020_thresh_get_temp(thresh
) * 1000000;
540 * Units of s_val are in micro degree celsius, scale by
541 * 65535 to get same units as s_thresh.
543 s_val
= min(abs(s_val
), HDC3020_MAX_TEMP_HYST_MICRO
);
544 s_hyst
= (s64
)s_val
* 65535;
545 s_clr
= hdc3020_thresh_clr(s_thresh
, s_hyst
, dir
);
546 s_clr
= max(s_clr
, HDC3020_MIN_TEMP_MICRO
);
547 s_clr
= min(s_clr
, HDC3020_MAX_TEMP_MICRO
);
549 reg_val
= hdc3020_thresh_set_temp(s_clr
, clr
);
555 case IIO_HUMIDITYRELATIVE
:
556 s_val
= (s_val
< 0) ? 0 : min(s_val
, HDC3020_MAX_HUM_MICRO
);
558 case IIO_EV_INFO_VALUE
:
560 reg_val
= hdc3020_thresh_set_hum(s_val
, thresh
);
561 ret
= _hdc3020_write_thresh(data
, reg
, reg_val
);
565 /* Calculate old hysteresis */
566 s_thresh
= (s64
)hdc3020_thresh_get_hum(thresh
) * 1000000;
567 s_clr
= (s64
)hdc3020_thresh_get_hum(clr
) * 1000000;
568 s_hyst
= div_s64(abs(s_thresh
- s_clr
), 65535);
569 /* Set new threshold */
571 /* Try to set old hysteresis */
572 s_val
= min(abs(s_hyst
), HDC3020_MAX_HUM_MICRO
);
574 case IIO_EV_INFO_HYSTERESIS
:
576 * Function hdc3020_thresh_get_hum returns relative
577 * humidity in percent scaled by 65535. Scale by 1000000
578 * to be able to subtract scaled hysteresis value.
580 s_thresh
= (s64
)hdc3020_thresh_get_hum(thresh
) * 1000000;
582 * Units of s_val are in micro percent, scale by 65535
583 * to get same units as s_thresh.
585 s_hyst
= (s64
)s_val
* 65535;
586 s_clr
= hdc3020_thresh_clr(s_thresh
, s_hyst
, dir
);
587 s_clr
= max(s_clr
, 0);
588 s_clr
= min(s_clr
, HDC3020_MAX_HUM_MICRO
);
590 reg_val
= hdc3020_thresh_set_hum(s_clr
, clr
);
600 return _hdc3020_write_thresh(data
, reg
, reg_val
);
603 static int hdc3020_read_thresh(struct iio_dev
*indio_dev
,
604 const struct iio_chan_spec
*chan
,
605 enum iio_event_type type
,
606 enum iio_event_direction dir
,
607 enum iio_event_info info
,
610 struct hdc3020_data
*data
= iio_priv(indio_dev
);
611 u16 reg_thresh
, reg_clr
;
612 int thresh
, clr
, ret
;
614 /* Select threshold registers */
615 if (dir
== IIO_EV_DIR_RISING
) {
616 reg_thresh
= HDC3020_R_T_RH_THRESH_HIGH
;
617 reg_clr
= HDC3020_R_T_RH_THRESH_HIGH_CLR
;
619 reg_thresh
= HDC3020_R_T_RH_THRESH_LOW
;
620 reg_clr
= HDC3020_R_T_RH_THRESH_LOW_CLR
;
623 guard(mutex
)(&data
->lock
);
624 ret
= hdc3020_read_be16(data
, reg_thresh
);
628 switch (chan
->type
) {
630 thresh
= hdc3020_thresh_get_temp(ret
);
632 case IIO_EV_INFO_VALUE
:
635 case IIO_EV_INFO_HYSTERESIS
:
636 ret
= hdc3020_read_be16(data
, reg_clr
);
640 clr
= hdc3020_thresh_get_temp(ret
);
641 *val
= abs(thresh
- clr
);
647 return IIO_VAL_FRACTIONAL
;
648 case IIO_HUMIDITYRELATIVE
:
649 thresh
= hdc3020_thresh_get_hum(ret
);
651 case IIO_EV_INFO_VALUE
:
654 case IIO_EV_INFO_HYSTERESIS
:
655 ret
= hdc3020_read_be16(data
, reg_clr
);
659 clr
= hdc3020_thresh_get_hum(ret
);
660 *val
= abs(thresh
- clr
);
666 return IIO_VAL_FRACTIONAL
;
672 static irqreturn_t
hdc3020_interrupt_handler(int irq
, void *private)
674 struct iio_dev
*indio_dev
= private;
675 struct hdc3020_data
*data
;
679 data
= iio_priv(indio_dev
);
680 ret
= hdc3020_read_be16(data
, HDC3020_R_STATUS
);
684 if (!(ret
& (HDC3020_STATUS_T_HIGH_ALERT
| HDC3020_STATUS_T_LOW_ALERT
|
685 HDC3020_STATUS_RH_HIGH_ALERT
| HDC3020_STATUS_RH_LOW_ALERT
)))
688 time
= iio_get_time_ns(indio_dev
);
689 if (ret
& HDC3020_STATUS_T_HIGH_ALERT
)
690 iio_push_event(indio_dev
,
691 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
697 if (ret
& HDC3020_STATUS_T_LOW_ALERT
)
698 iio_push_event(indio_dev
,
699 IIO_MOD_EVENT_CODE(IIO_TEMP
, 0,
705 if (ret
& HDC3020_STATUS_RH_HIGH_ALERT
)
706 iio_push_event(indio_dev
,
707 IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE
, 0,
713 if (ret
& HDC3020_STATUS_RH_LOW_ALERT
)
714 iio_push_event(indio_dev
,
715 IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE
, 0,
724 static const struct iio_info hdc3020_info
= {
725 .read_raw
= hdc3020_read_raw
,
726 .write_raw
= hdc3020_write_raw
,
727 .read_avail
= hdc3020_read_available
,
728 .read_event_value
= hdc3020_read_thresh
,
729 .write_event_value
= hdc3020_write_thresh
,
732 static int hdc3020_power_off(struct hdc3020_data
*data
)
734 hdc3020_exec_cmd(data
, HDC3020_EXIT_AUTO
);
736 if (data
->reset_gpio
)
737 gpiod_set_value_cansleep(data
->reset_gpio
, 1);
739 return regulator_disable(data
->vdd_supply
);
742 static int hdc3020_power_on(struct hdc3020_data
*data
)
746 ret
= regulator_enable(data
->vdd_supply
);
752 if (data
->reset_gpio
) {
753 gpiod_set_value_cansleep(data
->reset_gpio
, 0);
757 if (data
->client
->irq
) {
759 * The alert output is activated by default upon power up,
760 * hardware reset, and soft reset. Clear the status register.
762 ret
= hdc3020_exec_cmd(data
, HDC3020_S_STATUS
);
764 hdc3020_power_off(data
);
769 ret
= hdc3020_exec_cmd(data
, HDC3020_S_AUTO_10HZ_MOD0
);
771 hdc3020_power_off(data
);
776 static void hdc3020_exit(void *data
)
778 hdc3020_power_off(data
);
781 static int hdc3020_probe(struct i2c_client
*client
)
783 struct iio_dev
*indio_dev
;
784 struct hdc3020_data
*data
;
787 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
790 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
794 dev_set_drvdata(&client
->dev
, indio_dev
);
796 data
= iio_priv(indio_dev
);
797 data
->client
= client
;
798 mutex_init(&data
->lock
);
800 crc8_populate_msb(hdc3020_crc8_table
, HDC3020_CRC8_POLYNOMIAL
);
802 indio_dev
->name
= "hdc3020";
803 indio_dev
->modes
= INDIO_DIRECT_MODE
;
804 indio_dev
->info
= &hdc3020_info
;
805 indio_dev
->channels
= hdc3020_channels
;
806 indio_dev
->num_channels
= ARRAY_SIZE(hdc3020_channels
);
808 data
->vdd_supply
= devm_regulator_get(&client
->dev
, "vdd");
809 if (IS_ERR(data
->vdd_supply
))
810 return dev_err_probe(&client
->dev
, PTR_ERR(data
->vdd_supply
),
811 "Unable to get VDD regulator\n");
813 data
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
815 if (IS_ERR(data
->reset_gpio
))
816 return dev_err_probe(&client
->dev
, PTR_ERR(data
->reset_gpio
),
817 "Cannot get reset GPIO\n");
819 ret
= hdc3020_power_on(data
);
821 return dev_err_probe(&client
->dev
, ret
, "Power on failed\n");
823 ret
= devm_add_action_or_reset(&data
->client
->dev
, hdc3020_exit
, data
);
828 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
829 NULL
, hdc3020_interrupt_handler
,
830 IRQF_ONESHOT
, "hdc3020",
833 return dev_err_probe(&client
->dev
, ret
,
834 "Failed to request IRQ\n");
837 ret
= devm_iio_device_register(&data
->client
->dev
, indio_dev
);
839 return dev_err_probe(&client
->dev
, ret
, "Failed to add device");
844 static int hdc3020_suspend(struct device
*dev
)
846 struct iio_dev
*iio_dev
= dev_get_drvdata(dev
);
847 struct hdc3020_data
*data
= iio_priv(iio_dev
);
849 return hdc3020_power_off(data
);
852 static int hdc3020_resume(struct device
*dev
)
854 struct iio_dev
*iio_dev
= dev_get_drvdata(dev
);
855 struct hdc3020_data
*data
= iio_priv(iio_dev
);
857 return hdc3020_power_on(data
);
860 static DEFINE_SIMPLE_DEV_PM_OPS(hdc3020_pm_ops
, hdc3020_suspend
, hdc3020_resume
);
862 static const struct i2c_device_id hdc3020_id
[] = {
868 MODULE_DEVICE_TABLE(i2c
, hdc3020_id
);
870 static const struct of_device_id hdc3020_dt_ids
[] = {
871 { .compatible
= "ti,hdc3020" },
872 { .compatible
= "ti,hdc3021" },
873 { .compatible
= "ti,hdc3022" },
876 MODULE_DEVICE_TABLE(of
, hdc3020_dt_ids
);
878 static struct i2c_driver hdc3020_driver
= {
881 .pm
= pm_sleep_ptr(&hdc3020_pm_ops
),
882 .of_match_table
= hdc3020_dt_ids
,
884 .probe
= hdc3020_probe
,
885 .id_table
= hdc3020_id
,
887 module_i2c_driver(hdc3020_driver
);
889 MODULE_AUTHOR("Javier Carrasco <javier.carrasco.cruz@gmail.com>");
890 MODULE_AUTHOR("Li peiyu <579lpy@gmail.com>");
891 MODULE_DESCRIPTION("TI HDC3020 humidity and temperature sensor driver");
892 MODULE_LICENSE("GPL");