module: Convert symbol namespace to string literal
[linux.git] / drivers / iio / humidity / hdc3020.c
blobffb25596d3a8bad01d1f84a9a972561266f65d76
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * hdc3020.c - Support for the TI HDC3020,HDC3021 and HDC3022
4 * temperature + relative humidity sensors
6 * Copyright (C) 2023
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>
25 #include <linux/pm.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
75 struct hdc3020_data {
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).
85 struct mutex lock;
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[] = {
107 .type = IIO_TEMP,
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.
128 .type = IIO_CURRENT,
129 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
130 .info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
131 .output = 1,
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;
140 struct i2c_msg msg;
141 int ret, cnt;
143 msg.addr = client->addr;
144 msg.flags = 0;
145 msg.buf = buf;
146 msg.len = len;
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);
154 if (ret == 1)
155 return 0;
157 mdelay(HDC3020_BUSY_DELAY_MS);
159 dev_err(&client->dev, "Could not write sensor command\n");
161 return -ETIMEDOUT;
164 static
165 int hdc3020_read_bytes(struct hdc3020_data *data, u16 reg, u8 *buf, int len)
167 u8 reg_buf[2];
168 int ret, cnt;
169 struct i2c_client *client = data->client;
170 struct i2c_msg msg[2] = {
171 [0] = {
172 .addr = client->addr,
173 .flags = 0,
174 .buf = reg_buf,
175 .len = 2,
177 [1] = {
178 .addr = client->addr,
179 .flags = I2C_M_RD,
180 .buf = buf,
181 .len = len,
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);
192 if (ret == 2)
193 return 0;
195 mdelay(HDC3020_BUSY_DELAY_MS);
197 dev_err(&client->dev, "Could not read sensor data\n");
199 return -ETIMEDOUT;
202 static int hdc3020_read_be16(struct hdc3020_data *data, u16 reg)
204 u8 crc, buf[3];
205 int ret;
207 ret = hdc3020_read_bytes(data, reg, buf, 3);
208 if (ret < 0)
209 return ret;
211 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
212 if (crc != buf[2])
213 return -EINVAL;
215 return get_unaligned_be16(buf);
218 static int hdc3020_exec_cmd(struct hdc3020_data *data, u16 reg)
220 u8 reg_buf[2];
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)
229 u8 crc, buf[6];
230 int ret;
232 ret = hdc3020_read_bytes(data, HDC3020_R_T_RH_AUTO, buf, 6);
233 if (ret < 0)
234 return ret;
236 /* CRC check of the temperature measurement */
237 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
238 if (crc != buf[2])
239 return -EINVAL;
241 /* CRC check of the relative humidity measurement */
242 crc = crc8(hdc3020_crc8_table, buf + 3, 2, CRC8_INIT_VALUE);
243 if (crc != buf[5])
244 return -EINVAL;
246 if (type == IIO_TEMP)
247 *val = get_unaligned_be16(buf);
248 else if (type == IIO_HUMIDITYRELATIVE)
249 *val = get_unaligned_be16(&buf[3]);
250 else
251 return -EINVAL;
253 return 0;
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);
261 int ret;
263 if (chan->type != IIO_TEMP && chan->type != IIO_HUMIDITYRELATIVE)
264 return -EINVAL;
266 switch (mask) {
267 case IIO_CHAN_INFO_RAW: {
268 guard(mutex)(&data->lock);
269 ret = hdc3020_read_measurement(data, chan->type, val);
270 if (ret < 0)
271 return ret;
273 return IIO_VAL_INT;
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);
279 else
280 ret = hdc3020_read_be16(data, HDC3020_R_RH_HIGH_AUTO);
282 if (ret < 0)
283 return ret;
285 *val = ret;
286 return IIO_VAL_INT;
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);
292 else
293 ret = hdc3020_read_be16(data, HDC3020_R_RH_LOW_AUTO);
295 if (ret < 0)
296 return ret;
298 *val = ret;
299 return IIO_VAL_INT;
301 case IIO_CHAN_INFO_SCALE:
302 *val2 = 65536;
303 if (chan->type == IIO_TEMP)
304 *val = 175;
305 else
306 *val = 100;
307 return IIO_VAL_FRACTIONAL;
309 case IIO_CHAN_INFO_OFFSET:
310 if (chan->type != IIO_TEMP)
311 return -EINVAL;
313 *val = -16852;
314 return IIO_VAL_INT;
316 default:
317 return -EINVAL;
321 static int hdc3020_read_available(struct iio_dev *indio_dev,
322 struct iio_chan_spec const *chan,
323 const int **vals,
324 int *type, int *length, long mask)
326 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_CURRENT)
327 return -EINVAL;
329 *vals = hdc3020_heater_vals;
330 *type = IIO_VAL_INT;
332 return IIO_AVAIL_RANGE;
335 static int hdc3020_update_heater(struct hdc3020_data *data, int val)
337 u8 buf[5];
338 int ret;
340 if (val < hdc3020_heater_vals[0] || val > hdc3020_heater_vals[2])
341 return -EINVAL;
343 if (!val)
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);
350 if (ret < 0)
351 return ret;
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);
362 switch (mask) {
363 case IIO_CHAN_INFO_RAW:
364 if (chan->type != IIO_CURRENT)
365 return -EINVAL;
367 guard(mutex)(&data->lock);
368 return hdc3020_update_heater(data, val);
371 return -EINVAL;
374 static int hdc3020_thresh_get_temp(u16 thresh)
376 int temp;
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)
392 int hum;
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;
403 return hum * 100;
406 static u16 hdc3020_thresh_set_temp(int s_temp, u16 curr_thresh)
408 u64 temp;
409 u16 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);
422 return thresh;
425 static u16 hdc3020_thresh_set_hum(int s_hum, u16 curr_thresh)
427 u64 hum;
428 u16 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);
440 return thresh;
443 static
444 int hdc3020_thresh_clr(s64 s_thresh, s64 s_hyst, enum iio_event_direction dir)
446 s64 s_clr;
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;
455 else
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)
464 u8 buf[5];
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,
478 int val, int val2)
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;
491 } else {
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);
500 if (ret < 0)
501 return ret;
503 thresh = ret;
504 ret = hdc3020_read_be16(data, reg_clr_rd);
505 if (ret < 0)
506 return ret;
508 clr = ret;
509 /* Scale value to include decimal part into calculations */
510 s_val = (val < 0) ? (val * 1000000 - val2) : (val * 1000000 + val2);
511 switch (chan->type) {
512 case IIO_TEMP:
513 switch (info) {
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);
517 reg = reg_thresh_wr;
518 reg_val = hdc3020_thresh_set_temp(s_val, thresh);
519 ret = _hdc3020_write_thresh(data, reg, reg_val);
520 if (ret < 0)
521 return ret;
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 */
528 thresh = reg_val;
529 /* Set old hysteresis */
530 s_val = s_hyst;
531 fallthrough;
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);
548 reg = reg_clr_wr;
549 reg_val = hdc3020_thresh_set_temp(s_clr, clr);
550 break;
551 default:
552 return -EOPNOTSUPP;
554 break;
555 case IIO_HUMIDITYRELATIVE:
556 s_val = (s_val < 0) ? 0 : min(s_val, HDC3020_MAX_HUM_MICRO);
557 switch (info) {
558 case IIO_EV_INFO_VALUE:
559 reg = reg_thresh_wr;
560 reg_val = hdc3020_thresh_set_hum(s_val, thresh);
561 ret = _hdc3020_write_thresh(data, reg, reg_val);
562 if (ret < 0)
563 return ret;
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 */
570 thresh = reg_val;
571 /* Try to set old hysteresis */
572 s_val = min(abs(s_hyst), HDC3020_MAX_HUM_MICRO);
573 fallthrough;
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);
589 reg = reg_clr_wr;
590 reg_val = hdc3020_thresh_set_hum(s_clr, clr);
591 break;
592 default:
593 return -EOPNOTSUPP;
595 break;
596 default:
597 return -EOPNOTSUPP;
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,
608 int *val, int *val2)
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;
618 } else {
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);
625 if (ret < 0)
626 return ret;
628 switch (chan->type) {
629 case IIO_TEMP:
630 thresh = hdc3020_thresh_get_temp(ret);
631 switch (info) {
632 case IIO_EV_INFO_VALUE:
633 *val = thresh;
634 break;
635 case IIO_EV_INFO_HYSTERESIS:
636 ret = hdc3020_read_be16(data, reg_clr);
637 if (ret < 0)
638 return ret;
640 clr = hdc3020_thresh_get_temp(ret);
641 *val = abs(thresh - clr);
642 break;
643 default:
644 return -EOPNOTSUPP;
646 *val2 = 65535;
647 return IIO_VAL_FRACTIONAL;
648 case IIO_HUMIDITYRELATIVE:
649 thresh = hdc3020_thresh_get_hum(ret);
650 switch (info) {
651 case IIO_EV_INFO_VALUE:
652 *val = thresh;
653 break;
654 case IIO_EV_INFO_HYSTERESIS:
655 ret = hdc3020_read_be16(data, reg_clr);
656 if (ret < 0)
657 return ret;
659 clr = hdc3020_thresh_get_hum(ret);
660 *val = abs(thresh - clr);
661 break;
662 default:
663 return -EOPNOTSUPP;
665 *val2 = 65535;
666 return IIO_VAL_FRACTIONAL;
667 default:
668 return -EOPNOTSUPP;
672 static irqreturn_t hdc3020_interrupt_handler(int irq, void *private)
674 struct iio_dev *indio_dev = private;
675 struct hdc3020_data *data;
676 s64 time;
677 int ret;
679 data = iio_priv(indio_dev);
680 ret = hdc3020_read_be16(data, HDC3020_R_STATUS);
681 if (ret < 0)
682 return IRQ_HANDLED;
684 if (!(ret & (HDC3020_STATUS_T_HIGH_ALERT | HDC3020_STATUS_T_LOW_ALERT |
685 HDC3020_STATUS_RH_HIGH_ALERT | HDC3020_STATUS_RH_LOW_ALERT)))
686 return IRQ_NONE;
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,
692 IIO_NO_MOD,
693 IIO_EV_TYPE_THRESH,
694 IIO_EV_DIR_RISING),
695 time);
697 if (ret & HDC3020_STATUS_T_LOW_ALERT)
698 iio_push_event(indio_dev,
699 IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
700 IIO_NO_MOD,
701 IIO_EV_TYPE_THRESH,
702 IIO_EV_DIR_FALLING),
703 time);
705 if (ret & HDC3020_STATUS_RH_HIGH_ALERT)
706 iio_push_event(indio_dev,
707 IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE, 0,
708 IIO_NO_MOD,
709 IIO_EV_TYPE_THRESH,
710 IIO_EV_DIR_RISING),
711 time);
713 if (ret & HDC3020_STATUS_RH_LOW_ALERT)
714 iio_push_event(indio_dev,
715 IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE, 0,
716 IIO_NO_MOD,
717 IIO_EV_TYPE_THRESH,
718 IIO_EV_DIR_FALLING),
719 time);
721 return IRQ_HANDLED;
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)
744 int ret;
746 ret = regulator_enable(data->vdd_supply);
747 if (ret)
748 return ret;
750 fsleep(5000);
752 if (data->reset_gpio) {
753 gpiod_set_value_cansleep(data->reset_gpio, 0);
754 fsleep(3000);
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);
763 if (ret) {
764 hdc3020_power_off(data);
765 return ret;
769 ret = hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0);
770 if (ret)
771 hdc3020_power_off(data);
773 return ret;
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;
785 int ret;
787 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
788 return -EOPNOTSUPP;
790 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
791 if (!indio_dev)
792 return -ENOMEM;
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",
814 GPIOD_OUT_HIGH);
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);
820 if (ret)
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);
824 if (ret)
825 return ret;
827 if (client->irq) {
828 ret = devm_request_threaded_irq(&client->dev, client->irq,
829 NULL, hdc3020_interrupt_handler,
830 IRQF_ONESHOT, "hdc3020",
831 indio_dev);
832 if (ret)
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);
838 if (ret)
839 return dev_err_probe(&client->dev, ret, "Failed to add device");
841 return 0;
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[] = {
863 { "hdc3020" },
864 { "hdc3021" },
865 { "hdc3022" },
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 = {
879 .driver = {
880 .name = "hdc3020",
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");