1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * hdc3020.c - Support for the TI HDC3020,HDC3021 and HDC3022
4 * temperature + relative humidity sensors
5 *
6 * Copyright (C) 2023
7 *
8 * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH
9 *
10 * Datasheet: https://www.ti.com/lit/ds/symlink/hdc3020.pdf
11 */
12
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>
28
29 #include <linux/unaligned.h>
30
31 #include <linux/iio/events.h>
32 #include <linux/iio/iio.h>
33
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
54
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
59
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)
64
65 #define HDC3020_READ_RETRY_TIMES 10
66 #define HDC3020_BUSY_DELAY_MS 10
67
68 #define HDC3020_CRC8_POLYNOMIAL 0x31
69
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
74
75 struct hdc3020_data {
76 struct i2c_client *client;
77 struct gpio_desc *reset_gpio;
78 struct regulator *vdd_supply;
79 /*
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).
84 */
85 struct mutex lock;
86 };
87
88 static const int hdc3020_heater_vals[] = {0, 1, 0x3FFF};
89
90 static const struct iio_event_spec hdc3020_t_rh_event[] = {
91 {
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),
96 },
97 {
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),
102 },
103 };
104
105 static const struct iio_chan_spec hdc3020_channels[] = {
106 {
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),
113 },
114 {
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),
121 },
122 {
123 /*
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.
127 */
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,
132 },
133 };
134
135 DECLARE_CRC8_TABLE(hdc3020_crc8_table);
136
hdc3020_write_bytes(struct hdc3020_data * data,u8 * buf,u8 len)137 static int hdc3020_write_bytes(struct hdc3020_data *data, u8 *buf, u8 len)
138 {
139 struct i2c_client *client = data->client;
140 struct i2c_msg msg;
141 int ret, cnt;
142
143 msg.addr = client->addr;
144 msg.flags = 0;
145 msg.buf = buf;
146 msg.len = len;
147
148 /*
149 * During the measurement process, HDC3020 will not return data.
150 * So wait for a while and try again
151 */
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;
156
157 mdelay(HDC3020_BUSY_DELAY_MS);
158 }
159 dev_err(&client->dev, "Could not write sensor command\n");
160
161 return -ETIMEDOUT;
162 }
163
164 static
hdc3020_read_bytes(struct hdc3020_data * data,u16 reg,u8 * buf,int len)165 int hdc3020_read_bytes(struct hdc3020_data *data, u16 reg, u8 *buf, int len)
166 {
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,
176 },
177 [1] = {
178 .addr = client->addr,
179 .flags = I2C_M_RD,
180 .buf = buf,
181 .len = len,
182 },
183 };
184
185 put_unaligned_be16(reg, reg_buf);
186 /*
187 * During the measurement process, HDC3020 will not return data.
188 * So wait for a while and try again
189 */
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;
194
195 mdelay(HDC3020_BUSY_DELAY_MS);
196 }
197 dev_err(&client->dev, "Could not read sensor data\n");
198
199 return -ETIMEDOUT;
200 }
201
hdc3020_read_be16(struct hdc3020_data * data,u16 reg)202 static int hdc3020_read_be16(struct hdc3020_data *data, u16 reg)
203 {
204 u8 crc, buf[3];
205 int ret;
206
207 ret = hdc3020_read_bytes(data, reg, buf, 3);
208 if (ret < 0)
209 return ret;
210
211 crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
212 if (crc != buf[2])
213 return -EINVAL;
214
215 return get_unaligned_be16(buf);
216 }
217
hdc3020_exec_cmd(struct hdc3020_data * data,u16 reg)218 static int hdc3020_exec_cmd(struct hdc3020_data *data, u16 reg)
219 {
220 u8 reg_buf[2];
221
222 put_unaligned_be16(reg, reg_buf);
223 return hdc3020_write_bytes(data, reg_buf, 2);
224 }
225
hdc3020_read_measurement(struct hdc3020_data * data,enum iio_chan_type type,int * val)226 static int hdc3020_read_measurement(struct hdc3020_data *data,
227 enum iio_chan_type type, int *val)
228 {
229 u8 crc, buf[6];
230 int ret;
231
232 ret = hdc3020_read_bytes(data, HDC3020_R_T_RH_AUTO, buf, 6);
233 if (ret < 0)
234 return ret;
235
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;
240
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;
245
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;
252
253 return 0;
254 }
255
hdc3020_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
259 {
260 struct hdc3020_data *data = iio_priv(indio_dev);
261 int ret;
262
263 if (chan->type != IIO_TEMP && chan->type != IIO_HUMIDITYRELATIVE)
264 return -EINVAL;
265
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;
272
273 return IIO_VAL_INT;
274 }
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);
281
282 if (ret < 0)
283 return ret;
284
285 *val = ret;
286 return IIO_VAL_INT;
287 }
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);
294
295 if (ret < 0)
296 return ret;
297
298 *val = ret;
299 return IIO_VAL_INT;
300 }
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;
308
309 case IIO_CHAN_INFO_OFFSET:
310 if (chan->type != IIO_TEMP)
311 return -EINVAL;
312
313 *val = -16852;
314 return IIO_VAL_INT;
315
316 default:
317 return -EINVAL;
318 }
319 }
320
hdc3020_read_available(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)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)
325 {
326 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_CURRENT)
327 return -EINVAL;
328
329 *vals = hdc3020_heater_vals;
330 *type = IIO_VAL_INT;
331
332 return IIO_AVAIL_RANGE;
333 }
334
hdc3020_update_heater(struct hdc3020_data * data,int val)335 static int hdc3020_update_heater(struct hdc3020_data *data, int val)
336 {
337 u8 buf[5];
338 int ret;
339
340 if (val < hdc3020_heater_vals[0] || val > hdc3020_heater_vals[2])
341 return -EINVAL;
342
343 if (!val)
344 hdc3020_exec_cmd(data, HDC3020_HEATER_DISABLE);
345
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;
352
353 return hdc3020_exec_cmd(data, HDC3020_HEATER_ENABLE);
354 }
355
hdc3020_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)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)
359 {
360 struct hdc3020_data *data = iio_priv(indio_dev);
361
362 switch (mask) {
363 case IIO_CHAN_INFO_RAW:
364 if (chan->type != IIO_CURRENT)
365 return -EINVAL;
366
367 guard(mutex)(&data->lock);
368 return hdc3020_update_heater(data, val);
369 }
370
371 return -EINVAL;
372 }
373
hdc3020_thresh_get_temp(u16 thresh)374 static int hdc3020_thresh_get_temp(u16 thresh)
375 {
376 int temp;
377
378 /*
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.
383 */
384 temp = FIELD_GET(HDC3020_THRESH_TEMP_MASK, thresh) <<
385 HDC3020_THRESH_TEMP_TRUNC_SHIFT;
386
387 return -2949075 + (175 * temp);
388 }
389
hdc3020_thresh_get_hum(u16 thresh)390 static int hdc3020_thresh_get_hum(u16 thresh)
391 {
392 int hum;
393
394 /*
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.
399 */
400 hum = FIELD_GET(HDC3020_THRESH_HUM_MASK, thresh) <<
401 HDC3020_THRESH_HUM_TRUNC_SHIFT;
402
403 return hum * 100;
404 }
405
hdc3020_thresh_set_temp(int s_temp,u16 curr_thresh)406 static u16 hdc3020_thresh_set_temp(int s_temp, u16 curr_thresh)
407 {
408 u64 temp;
409 u16 thresh;
410
411 /*
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.
415 */
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);
421
422 return thresh;
423 }
424
hdc3020_thresh_set_hum(int s_hum,u16 curr_thresh)425 static u16 hdc3020_thresh_set_hum(int s_hum, u16 curr_thresh)
426 {
427 u64 hum;
428 u16 thresh;
429
430 /*
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.
434 */
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);
439
440 return thresh;
441 }
442
443 static
hdc3020_thresh_clr(s64 s_thresh,s64 s_hyst,enum iio_event_direction dir)444 int hdc3020_thresh_clr(s64 s_thresh, s64 s_hyst, enum iio_event_direction dir)
445 {
446 s64 s_clr;
447
448 /*
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.
452 */
453 if (dir == IIO_EV_DIR_RISING)
454 s_clr = s_thresh - s_hyst;
455 else
456 s_clr = s_thresh + s_hyst;
457
458 /* Divide by 65535 to get units of micro */
459 return div_s64(s_clr, 65535);
460 }
461
_hdc3020_write_thresh(struct hdc3020_data * data,u16 reg,u16 val)462 static int _hdc3020_write_thresh(struct hdc3020_data *data, u16 reg, u16 val)
463 {
464 u8 buf[5];
465
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);
469
470 return hdc3020_write_bytes(data, buf, 5);
471 }
472
hdc3020_write_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)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)
479 {
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;
484
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;
496 }
497
498 guard(mutex)(&data->lock);
499 ret = hdc3020_read_be16(data, reg_thresh_rd);
500 if (ret < 0)
501 return ret;
502
503 thresh = ret;
504 ret = hdc3020_read_be16(data, reg_clr_rd);
505 if (ret < 0)
506 return ret;
507
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;
522
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:
533 /*
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.
537 */
538 s_thresh = (s64)hdc3020_thresh_get_temp(thresh) * 1000000;
539 /*
540 * Units of s_val are in micro degree celsius, scale by
541 * 65535 to get same units as s_thresh.
542 */
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;
553 }
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;
564
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:
575 /*
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.
579 */
580 s_thresh = (s64)hdc3020_thresh_get_hum(thresh) * 1000000;
581 /*
582 * Units of s_val are in micro percent, scale by 65535
583 * to get same units as s_thresh.
584 */
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;
594 }
595 break;
596 default:
597 return -EOPNOTSUPP;
598 }
599
600 return _hdc3020_write_thresh(data, reg, reg_val);
601 }
602
hdc3020_read_thresh(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)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)
609 {
610 struct hdc3020_data *data = iio_priv(indio_dev);
611 u16 reg_thresh, reg_clr;
612 int thresh, clr, ret;
613
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;
621 }
622
623 guard(mutex)(&data->lock);
624 ret = hdc3020_read_be16(data, reg_thresh);
625 if (ret < 0)
626 return ret;
627
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;
639
640 clr = hdc3020_thresh_get_temp(ret);
641 *val = abs(thresh - clr);
642 break;
643 default:
644 return -EOPNOTSUPP;
645 }
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;
658
659 clr = hdc3020_thresh_get_hum(ret);
660 *val = abs(thresh - clr);
661 break;
662 default:
663 return -EOPNOTSUPP;
664 }
665 *val2 = 65535;
666 return IIO_VAL_FRACTIONAL;
667 default:
668 return -EOPNOTSUPP;
669 }
670 }
671
hdc3020_interrupt_handler(int irq,void * private)672 static irqreturn_t hdc3020_interrupt_handler(int irq, void *private)
673 {
674 struct iio_dev *indio_dev = private;
675 struct hdc3020_data *data;
676 s64 time;
677 int ret;
678
679 data = iio_priv(indio_dev);
680 ret = hdc3020_read_be16(data, HDC3020_R_STATUS);
681 if (ret < 0)
682 return IRQ_HANDLED;
683
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;
687
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);
696
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);
704
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);
712
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);
720
721 return IRQ_HANDLED;
722 }
723
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,
730 };
731
hdc3020_power_off(struct hdc3020_data * data)732 static int hdc3020_power_off(struct hdc3020_data *data)
733 {
734 hdc3020_exec_cmd(data, HDC3020_EXIT_AUTO);
735
736 if (data->reset_gpio)
737 gpiod_set_value_cansleep(data->reset_gpio, 1);
738
739 return regulator_disable(data->vdd_supply);
740 }
741
hdc3020_power_on(struct hdc3020_data * data)742 static int hdc3020_power_on(struct hdc3020_data *data)
743 {
744 int ret;
745
746 ret = regulator_enable(data->vdd_supply);
747 if (ret)
748 return ret;
749
750 fsleep(5000);
751
752 if (data->reset_gpio) {
753 gpiod_set_value_cansleep(data->reset_gpio, 0);
754 fsleep(3000);
755 }
756
757 if (data->client->irq) {
758 /*
759 * The alert output is activated by default upon power up,
760 * hardware reset, and soft reset. Clear the status register.
761 */
762 ret = hdc3020_exec_cmd(data, HDC3020_S_STATUS);
763 if (ret) {
764 hdc3020_power_off(data);
765 return ret;
766 }
767 }
768
769 ret = hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0);
770 if (ret)
771 hdc3020_power_off(data);
772
773 return ret;
774 }
775
hdc3020_exit(void * data)776 static void hdc3020_exit(void *data)
777 {
778 hdc3020_power_off(data);
779 }
780
hdc3020_probe(struct i2c_client * client)781 static int hdc3020_probe(struct i2c_client *client)
782 {
783 struct iio_dev *indio_dev;
784 struct hdc3020_data *data;
785 int ret;
786
787 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
788 return -EOPNOTSUPP;
789
790 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
791 if (!indio_dev)
792 return -ENOMEM;
793
794 dev_set_drvdata(&client->dev, indio_dev);
795
796 data = iio_priv(indio_dev);
797 data->client = client;
798 mutex_init(&data->lock);
799
800 crc8_populate_msb(hdc3020_crc8_table, HDC3020_CRC8_POLYNOMIAL);
801
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);
807
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");
812
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");
818
819 ret = hdc3020_power_on(data);
820 if (ret)
821 return dev_err_probe(&client->dev, ret, "Power on failed\n");
822
823 ret = devm_add_action_or_reset(&data->client->dev, hdc3020_exit, data);
824 if (ret)
825 return ret;
826
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");
835 }
836
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");
840
841 return 0;
842 }
843
hdc3020_suspend(struct device * dev)844 static int hdc3020_suspend(struct device *dev)
845 {
846 struct iio_dev *iio_dev = dev_get_drvdata(dev);
847 struct hdc3020_data *data = iio_priv(iio_dev);
848
849 return hdc3020_power_off(data);
850 }
851
hdc3020_resume(struct device * dev)852 static int hdc3020_resume(struct device *dev)
853 {
854 struct iio_dev *iio_dev = dev_get_drvdata(dev);
855 struct hdc3020_data *data = iio_priv(iio_dev);
856
857 return hdc3020_power_on(data);
858 }
859
860 static DEFINE_SIMPLE_DEV_PM_OPS(hdc3020_pm_ops, hdc3020_suspend, hdc3020_resume);
861
862 static const struct i2c_device_id hdc3020_id[] = {
863 { "hdc3020" },
864 { "hdc3021" },
865 { "hdc3022" },
866 { }
867 };
868 MODULE_DEVICE_TABLE(i2c, hdc3020_id);
869
870 static const struct of_device_id hdc3020_dt_ids[] = {
871 { .compatible = "ti,hdc3020" },
872 { .compatible = "ti,hdc3021" },
873 { .compatible = "ti,hdc3022" },
874 { }
875 };
876 MODULE_DEVICE_TABLE(of, hdc3020_dt_ids);
877
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,
883 },
884 .probe = hdc3020_probe,
885 .id_table = hdc3020_id,
886 };
887 module_i2c_driver(hdc3020_driver);
888
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");
893