xref: /linux/drivers/iio/humidity/hdc3020.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
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/module.h>
23 #include <linux/mutex.h>
24 #include <linux/pm.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/units.h>
27 
28 #include <asm/unaligned.h>
29 
30 #include <linux/iio/events.h>
31 #include <linux/iio/iio.h>
32 
33 #define HDC3020_S_AUTO_10HZ_MOD0	0x2737
34 #define HDC3020_S_STATUS		0x3041
35 #define HDC3020_HEATER_DISABLE		0x3066
36 #define HDC3020_HEATER_ENABLE		0x306D
37 #define HDC3020_HEATER_CONFIG		0x306E
38 #define HDC3020_EXIT_AUTO		0x3093
39 #define HDC3020_S_T_RH_THRESH_LOW	0x6100
40 #define HDC3020_S_T_RH_THRESH_LOW_CLR	0x610B
41 #define HDC3020_S_T_RH_THRESH_HIGH_CLR	0x6116
42 #define HDC3020_S_T_RH_THRESH_HIGH	0x611D
43 #define HDC3020_R_T_RH_AUTO		0xE000
44 #define HDC3020_R_T_LOW_AUTO		0xE002
45 #define HDC3020_R_T_HIGH_AUTO		0xE003
46 #define HDC3020_R_RH_LOW_AUTO		0xE004
47 #define HDC3020_R_RH_HIGH_AUTO		0xE005
48 #define HDC3020_R_T_RH_THRESH_LOW	0xE102
49 #define HDC3020_R_T_RH_THRESH_LOW_CLR	0xE109
50 #define HDC3020_R_T_RH_THRESH_HIGH_CLR	0xE114
51 #define HDC3020_R_T_RH_THRESH_HIGH	0xE11F
52 #define HDC3020_R_STATUS		0xF32D
53 
54 #define HDC3020_THRESH_TEMP_MASK	GENMASK(8, 0)
55 #define HDC3020_THRESH_TEMP_TRUNC_SHIFT	7
56 #define HDC3020_THRESH_HUM_MASK		GENMASK(15, 9)
57 #define HDC3020_THRESH_HUM_TRUNC_SHIFT	9
58 
59 #define HDC3020_STATUS_T_LOW_ALERT	BIT(6)
60 #define HDC3020_STATUS_T_HIGH_ALERT	BIT(7)
61 #define HDC3020_STATUS_RH_LOW_ALERT	BIT(8)
62 #define HDC3020_STATUS_RH_HIGH_ALERT	BIT(9)
63 
64 #define HDC3020_READ_RETRY_TIMES	10
65 #define HDC3020_BUSY_DELAY_MS		10
66 
67 #define HDC3020_CRC8_POLYNOMIAL		0x31
68 
69 #define HDC3020_MIN_TEMP		-40
70 #define HDC3020_MAX_TEMP		125
71 
72 struct hdc3020_data {
73 	struct i2c_client *client;
74 	struct gpio_desc *reset_gpio;
75 	struct regulator *vdd_supply;
76 	/*
77 	 * Ensure that the sensor configuration (currently only heater is
78 	 * supported) will not be changed during the process of reading
79 	 * sensor data (this driver will try HDC3020_READ_RETRY_TIMES times
80 	 * if the device does not respond).
81 	 */
82 	struct mutex lock;
83 };
84 
85 static const int hdc3020_heater_vals[] = {0, 1, 0x3FFF};
86 
87 static const struct iio_event_spec hdc3020_t_rh_event[] = {
88 	{
89 		.type = IIO_EV_TYPE_THRESH,
90 		.dir = IIO_EV_DIR_RISING,
91 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
92 		BIT(IIO_EV_INFO_HYSTERESIS),
93 	},
94 	{
95 		.type = IIO_EV_TYPE_THRESH,
96 		.dir = IIO_EV_DIR_FALLING,
97 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
98 		BIT(IIO_EV_INFO_HYSTERESIS),
99 	},
100 };
101 
102 static const struct iio_chan_spec hdc3020_channels[] = {
103 	{
104 		.type = IIO_TEMP,
105 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
106 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_PEAK) |
107 		BIT(IIO_CHAN_INFO_TROUGH) | BIT(IIO_CHAN_INFO_OFFSET),
108 		.event_spec = hdc3020_t_rh_event,
109 		.num_event_specs = ARRAY_SIZE(hdc3020_t_rh_event),
110 	},
111 	{
112 		.type = IIO_HUMIDITYRELATIVE,
113 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
114 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_PEAK) |
115 		BIT(IIO_CHAN_INFO_TROUGH),
116 		.event_spec = hdc3020_t_rh_event,
117 		.num_event_specs = ARRAY_SIZE(hdc3020_t_rh_event),
118 	},
119 	{
120 		/*
121 		 * For setting the internal heater, which can be switched on to
122 		 * prevent or remove any condensation that may develop when the
123 		 * ambient environment approaches its dew point temperature.
124 		 */
125 		.type = IIO_CURRENT,
126 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
127 		.info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW),
128 		.output = 1,
129 	},
130 };
131 
132 DECLARE_CRC8_TABLE(hdc3020_crc8_table);
133 
134 static int hdc3020_write_bytes(struct hdc3020_data *data, u8 *buf, u8 len)
135 {
136 	struct i2c_client *client = data->client;
137 	struct i2c_msg msg;
138 	int ret, cnt;
139 
140 	msg.addr = client->addr;
141 	msg.flags = 0;
142 	msg.buf = buf;
143 	msg.len = len;
144 
145 	/*
146 	 * During the measurement process, HDC3020 will not return data.
147 	 * So wait for a while and try again
148 	 */
149 	for (cnt = 0; cnt < HDC3020_READ_RETRY_TIMES; cnt++) {
150 		ret = i2c_transfer(client->adapter, &msg, 1);
151 		if (ret == 1)
152 			return 0;
153 
154 		mdelay(HDC3020_BUSY_DELAY_MS);
155 	}
156 	dev_err(&client->dev, "Could not write sensor command\n");
157 
158 	return -ETIMEDOUT;
159 }
160 
161 static
162 int hdc3020_read_bytes(struct hdc3020_data *data, u16 reg, u8 *buf, int len)
163 {
164 	u8 reg_buf[2];
165 	int ret, cnt;
166 	struct i2c_client *client = data->client;
167 	struct i2c_msg msg[2] = {
168 		[0] = {
169 			.addr = client->addr,
170 			.flags = 0,
171 			.buf = reg_buf,
172 			.len = 2,
173 		},
174 		[1] = {
175 			.addr = client->addr,
176 			.flags = I2C_M_RD,
177 			.buf = buf,
178 			.len = len,
179 		},
180 	};
181 
182 	put_unaligned_be16(reg, reg_buf);
183 	/*
184 	 * During the measurement process, HDC3020 will not return data.
185 	 * So wait for a while and try again
186 	 */
187 	for (cnt = 0; cnt < HDC3020_READ_RETRY_TIMES; cnt++) {
188 		ret = i2c_transfer(client->adapter, msg, 2);
189 		if (ret == 2)
190 			return 0;
191 
192 		mdelay(HDC3020_BUSY_DELAY_MS);
193 	}
194 	dev_err(&client->dev, "Could not read sensor data\n");
195 
196 	return -ETIMEDOUT;
197 }
198 
199 static int hdc3020_read_be16(struct hdc3020_data *data, u16 reg)
200 {
201 	u8 crc, buf[3];
202 	int ret;
203 
204 	ret = hdc3020_read_bytes(data, reg, buf, 3);
205 	if (ret < 0)
206 		return ret;
207 
208 	crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
209 	if (crc != buf[2])
210 		return -EINVAL;
211 
212 	return get_unaligned_be16(buf);
213 }
214 
215 static int hdc3020_exec_cmd(struct hdc3020_data *data, u16 reg)
216 {
217 	u8 reg_buf[2];
218 
219 	put_unaligned_be16(reg, reg_buf);
220 	return hdc3020_write_bytes(data, reg_buf, 2);
221 }
222 
223 static int hdc3020_read_measurement(struct hdc3020_data *data,
224 				    enum iio_chan_type type, int *val)
225 {
226 	u8 crc, buf[6];
227 	int ret;
228 
229 	ret = hdc3020_read_bytes(data, HDC3020_R_T_RH_AUTO, buf, 6);
230 	if (ret < 0)
231 		return ret;
232 
233 	/* CRC check of the temperature measurement */
234 	crc = crc8(hdc3020_crc8_table, buf, 2, CRC8_INIT_VALUE);
235 	if (crc != buf[2])
236 		return -EINVAL;
237 
238 	/* CRC check of the relative humidity measurement */
239 	crc = crc8(hdc3020_crc8_table, buf + 3, 2, CRC8_INIT_VALUE);
240 	if (crc != buf[5])
241 		return -EINVAL;
242 
243 	if (type == IIO_TEMP)
244 		*val = get_unaligned_be16(buf);
245 	else if (type == IIO_HUMIDITYRELATIVE)
246 		*val = get_unaligned_be16(&buf[3]);
247 	else
248 		return -EINVAL;
249 
250 	return 0;
251 }
252 
253 static int hdc3020_read_raw(struct iio_dev *indio_dev,
254 			    struct iio_chan_spec const *chan, int *val,
255 			    int *val2, long mask)
256 {
257 	struct hdc3020_data *data = iio_priv(indio_dev);
258 	int ret;
259 
260 	if (chan->type != IIO_TEMP && chan->type != IIO_HUMIDITYRELATIVE)
261 		return -EINVAL;
262 
263 	switch (mask) {
264 	case IIO_CHAN_INFO_RAW: {
265 		guard(mutex)(&data->lock);
266 		ret = hdc3020_read_measurement(data, chan->type, val);
267 		if (ret < 0)
268 			return ret;
269 
270 		return IIO_VAL_INT;
271 	}
272 	case IIO_CHAN_INFO_PEAK: {
273 		guard(mutex)(&data->lock);
274 		if (chan->type == IIO_TEMP)
275 			ret = hdc3020_read_be16(data, HDC3020_R_T_HIGH_AUTO);
276 		else
277 			ret = hdc3020_read_be16(data, HDC3020_R_RH_HIGH_AUTO);
278 
279 		if (ret < 0)
280 			return ret;
281 
282 		*val = ret;
283 		return IIO_VAL_INT;
284 	}
285 	case IIO_CHAN_INFO_TROUGH: {
286 		guard(mutex)(&data->lock);
287 		if (chan->type == IIO_TEMP)
288 			ret = hdc3020_read_be16(data, HDC3020_R_T_LOW_AUTO);
289 		else
290 			ret = hdc3020_read_be16(data, HDC3020_R_RH_LOW_AUTO);
291 
292 		if (ret < 0)
293 			return ret;
294 
295 		*val = ret;
296 		return IIO_VAL_INT;
297 	}
298 	case IIO_CHAN_INFO_SCALE:
299 		*val2 = 65536;
300 		if (chan->type == IIO_TEMP)
301 			*val = 175;
302 		else
303 			*val = 100;
304 		return IIO_VAL_FRACTIONAL;
305 
306 	case IIO_CHAN_INFO_OFFSET:
307 		if (chan->type != IIO_TEMP)
308 			return -EINVAL;
309 
310 		*val = -16852;
311 		return IIO_VAL_INT;
312 
313 	default:
314 		return -EINVAL;
315 	}
316 }
317 
318 static int hdc3020_read_available(struct iio_dev *indio_dev,
319 				  struct iio_chan_spec const *chan,
320 				  const int **vals,
321 				  int *type, int *length, long mask)
322 {
323 	if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_CURRENT)
324 		return -EINVAL;
325 
326 	*vals = hdc3020_heater_vals;
327 	*type = IIO_VAL_INT;
328 
329 	return IIO_AVAIL_RANGE;
330 }
331 
332 static int hdc3020_update_heater(struct hdc3020_data *data, int val)
333 {
334 	u8 buf[5];
335 	int ret;
336 
337 	if (val < hdc3020_heater_vals[0] || val > hdc3020_heater_vals[2])
338 		return -EINVAL;
339 
340 	if (!val)
341 		hdc3020_exec_cmd(data, HDC3020_HEATER_DISABLE);
342 
343 	put_unaligned_be16(HDC3020_HEATER_CONFIG, buf);
344 	put_unaligned_be16(val & GENMASK(13, 0), &buf[2]);
345 	buf[4] = crc8(hdc3020_crc8_table, buf + 2, 2, CRC8_INIT_VALUE);
346 	ret = hdc3020_write_bytes(data, buf, 5);
347 	if (ret < 0)
348 		return ret;
349 
350 	return hdc3020_exec_cmd(data, HDC3020_HEATER_ENABLE);
351 }
352 
353 static int hdc3020_write_raw(struct iio_dev *indio_dev,
354 			     struct iio_chan_spec const *chan,
355 			     int val, int val2, long mask)
356 {
357 	struct hdc3020_data *data = iio_priv(indio_dev);
358 
359 	switch (mask) {
360 	case IIO_CHAN_INFO_RAW:
361 		if (chan->type != IIO_CURRENT)
362 			return -EINVAL;
363 
364 		guard(mutex)(&data->lock);
365 		return hdc3020_update_heater(data, val);
366 	}
367 
368 	return -EINVAL;
369 }
370 
371 static int hdc3020_write_thresh(struct iio_dev *indio_dev,
372 				const struct iio_chan_spec *chan,
373 				enum iio_event_type type,
374 				enum iio_event_direction dir,
375 				enum iio_event_info info,
376 				int val, int val2)
377 {
378 	struct hdc3020_data *data = iio_priv(indio_dev);
379 	u8 buf[5];
380 	u64 tmp;
381 	u16 reg;
382 	int ret;
383 
384 	/* Supported temperature range is from –40 to 125 degree celsius */
385 	if (val < HDC3020_MIN_TEMP || val > HDC3020_MAX_TEMP)
386 		return -EINVAL;
387 
388 	/* Select threshold register */
389 	if (info == IIO_EV_INFO_VALUE) {
390 		if (dir == IIO_EV_DIR_RISING)
391 			reg = HDC3020_S_T_RH_THRESH_HIGH;
392 		else
393 			reg = HDC3020_S_T_RH_THRESH_LOW;
394 	} else {
395 		if (dir == IIO_EV_DIR_RISING)
396 			reg = HDC3020_S_T_RH_THRESH_HIGH_CLR;
397 		else
398 			reg = HDC3020_S_T_RH_THRESH_LOW_CLR;
399 	}
400 
401 	guard(mutex)(&data->lock);
402 	ret = hdc3020_read_be16(data, reg);
403 	if (ret < 0)
404 		return ret;
405 
406 	switch (chan->type) {
407 	case IIO_TEMP:
408 		/*
409 		 * Calculate temperature threshold, shift it down to get the
410 		 * truncated threshold representation in the 9LSBs while keeping
411 		 * the current humidity threshold in the 7 MSBs.
412 		 */
413 		tmp = ((u64)(((val + 45) * MICRO) + val2)) * 65535ULL;
414 		tmp = div_u64(tmp, MICRO * 175);
415 		val = tmp >> HDC3020_THRESH_TEMP_TRUNC_SHIFT;
416 		val = FIELD_PREP(HDC3020_THRESH_TEMP_MASK, val);
417 		val |= (FIELD_GET(HDC3020_THRESH_HUM_MASK, ret) <<
418 			HDC3020_THRESH_HUM_TRUNC_SHIFT);
419 		break;
420 	case IIO_HUMIDITYRELATIVE:
421 		/*
422 		 * Calculate humidity threshold, shift it down and up to get the
423 		 * truncated threshold representation in the 7MSBs while keeping
424 		 * the current temperature threshold in the 9 LSBs.
425 		 */
426 		tmp = ((u64)((val * MICRO) + val2)) * 65535ULL;
427 		tmp = div_u64(tmp, MICRO * 100);
428 		val = tmp >> HDC3020_THRESH_HUM_TRUNC_SHIFT;
429 		val = FIELD_PREP(HDC3020_THRESH_HUM_MASK, val);
430 		val |= FIELD_GET(HDC3020_THRESH_TEMP_MASK, ret);
431 		break;
432 	default:
433 		return -EOPNOTSUPP;
434 	}
435 
436 	put_unaligned_be16(reg, buf);
437 	put_unaligned_be16(val, buf + 2);
438 	buf[4] = crc8(hdc3020_crc8_table, buf + 2, 2, CRC8_INIT_VALUE);
439 	return hdc3020_write_bytes(data, buf, 5);
440 }
441 
442 static int hdc3020_read_thresh(struct iio_dev *indio_dev,
443 			       const struct iio_chan_spec *chan,
444 			       enum iio_event_type type,
445 			       enum iio_event_direction dir,
446 			       enum iio_event_info info,
447 			       int *val, int *val2)
448 {
449 	struct hdc3020_data *data = iio_priv(indio_dev);
450 	u16 reg;
451 	int ret;
452 
453 	/* Select threshold register */
454 	if (info == IIO_EV_INFO_VALUE) {
455 		if (dir == IIO_EV_DIR_RISING)
456 			reg = HDC3020_R_T_RH_THRESH_HIGH;
457 		else
458 			reg = HDC3020_R_T_RH_THRESH_LOW;
459 	} else {
460 		if (dir == IIO_EV_DIR_RISING)
461 			reg = HDC3020_R_T_RH_THRESH_HIGH_CLR;
462 		else
463 			reg = HDC3020_R_T_RH_THRESH_LOW_CLR;
464 	}
465 
466 	guard(mutex)(&data->lock);
467 	ret = hdc3020_read_be16(data, reg);
468 	if (ret < 0)
469 		return ret;
470 
471 	switch (chan->type) {
472 	case IIO_TEMP:
473 		/*
474 		 * Get the temperature threshold from 9 LSBs, shift them to get
475 		 * the truncated temperature threshold representation and
476 		 * calculate the threshold according to the formula in the
477 		 * datasheet.
478 		 */
479 		*val = FIELD_GET(HDC3020_THRESH_TEMP_MASK, ret);
480 		*val = *val << HDC3020_THRESH_TEMP_TRUNC_SHIFT;
481 		*val = -2949075 + (175 * (*val));
482 		*val2 = 65535;
483 		return IIO_VAL_FRACTIONAL;
484 	case IIO_HUMIDITYRELATIVE:
485 		/*
486 		 * Get the humidity threshold from 7 MSBs, shift them to get the
487 		 * truncated humidity threshold representation and calculate the
488 		 * threshold according to the formula in the datasheet.
489 		 */
490 		*val = FIELD_GET(HDC3020_THRESH_HUM_MASK, ret);
491 		*val = (*val << HDC3020_THRESH_HUM_TRUNC_SHIFT) * 100;
492 		*val2 = 65535;
493 		return IIO_VAL_FRACTIONAL;
494 	default:
495 		return -EOPNOTSUPP;
496 	}
497 }
498 
499 static irqreturn_t hdc3020_interrupt_handler(int irq, void *private)
500 {
501 	struct iio_dev *indio_dev = private;
502 	struct hdc3020_data *data;
503 	s64 time;
504 	int ret;
505 
506 	data = iio_priv(indio_dev);
507 	ret = hdc3020_read_be16(data, HDC3020_R_STATUS);
508 	if (ret < 0)
509 		return IRQ_HANDLED;
510 
511 	if (!(ret & (HDC3020_STATUS_T_HIGH_ALERT | HDC3020_STATUS_T_LOW_ALERT |
512 		HDC3020_STATUS_RH_HIGH_ALERT | HDC3020_STATUS_RH_LOW_ALERT)))
513 		return IRQ_NONE;
514 
515 	time = iio_get_time_ns(indio_dev);
516 	if (ret & HDC3020_STATUS_T_HIGH_ALERT)
517 		iio_push_event(indio_dev,
518 			       IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
519 						  IIO_NO_MOD,
520 						  IIO_EV_TYPE_THRESH,
521 						  IIO_EV_DIR_RISING),
522 						  time);
523 
524 	if (ret & HDC3020_STATUS_T_LOW_ALERT)
525 		iio_push_event(indio_dev,
526 			       IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
527 						  IIO_NO_MOD,
528 						  IIO_EV_TYPE_THRESH,
529 						  IIO_EV_DIR_FALLING),
530 						  time);
531 
532 	if (ret & HDC3020_STATUS_RH_HIGH_ALERT)
533 		iio_push_event(indio_dev,
534 			       IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE, 0,
535 						  IIO_NO_MOD,
536 						  IIO_EV_TYPE_THRESH,
537 						  IIO_EV_DIR_RISING),
538 						  time);
539 
540 	if (ret & HDC3020_STATUS_RH_LOW_ALERT)
541 		iio_push_event(indio_dev,
542 			       IIO_MOD_EVENT_CODE(IIO_HUMIDITYRELATIVE, 0,
543 						  IIO_NO_MOD,
544 						  IIO_EV_TYPE_THRESH,
545 						  IIO_EV_DIR_FALLING),
546 						  time);
547 
548 	return IRQ_HANDLED;
549 }
550 
551 static const struct iio_info hdc3020_info = {
552 	.read_raw = hdc3020_read_raw,
553 	.write_raw = hdc3020_write_raw,
554 	.read_avail = hdc3020_read_available,
555 	.read_event_value = hdc3020_read_thresh,
556 	.write_event_value = hdc3020_write_thresh,
557 };
558 
559 static int hdc3020_power_off(struct hdc3020_data *data)
560 {
561 	hdc3020_exec_cmd(data, HDC3020_EXIT_AUTO);
562 
563 	if (data->reset_gpio)
564 		gpiod_set_value_cansleep(data->reset_gpio, 1);
565 
566 	return regulator_disable(data->vdd_supply);
567 }
568 
569 static int hdc3020_power_on(struct hdc3020_data *data)
570 {
571 	int ret;
572 
573 	ret = regulator_enable(data->vdd_supply);
574 	if (ret)
575 		return ret;
576 
577 	fsleep(5000);
578 
579 	if (data->reset_gpio) {
580 		gpiod_set_value_cansleep(data->reset_gpio, 0);
581 		fsleep(3000);
582 	}
583 
584 	if (data->client->irq) {
585 		/*
586 		 * The alert output is activated by default upon power up,
587 		 * hardware reset, and soft reset. Clear the status register.
588 		 */
589 		ret = hdc3020_exec_cmd(data, HDC3020_S_STATUS);
590 		if (ret) {
591 			hdc3020_power_off(data);
592 			return ret;
593 		}
594 	}
595 
596 	ret = hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0);
597 	if (ret)
598 		hdc3020_power_off(data);
599 
600 	return ret;
601 }
602 
603 static void hdc3020_exit(void *data)
604 {
605 	hdc3020_power_off(data);
606 }
607 
608 static int hdc3020_probe(struct i2c_client *client)
609 {
610 	struct iio_dev *indio_dev;
611 	struct hdc3020_data *data;
612 	int ret;
613 
614 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
615 		return -EOPNOTSUPP;
616 
617 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
618 	if (!indio_dev)
619 		return -ENOMEM;
620 
621 	dev_set_drvdata(&client->dev, indio_dev);
622 
623 	data = iio_priv(indio_dev);
624 	data->client = client;
625 	mutex_init(&data->lock);
626 
627 	crc8_populate_msb(hdc3020_crc8_table, HDC3020_CRC8_POLYNOMIAL);
628 
629 	indio_dev->name = "hdc3020";
630 	indio_dev->modes = INDIO_DIRECT_MODE;
631 	indio_dev->info = &hdc3020_info;
632 	indio_dev->channels = hdc3020_channels;
633 	indio_dev->num_channels = ARRAY_SIZE(hdc3020_channels);
634 
635 	data->vdd_supply = devm_regulator_get(&client->dev, "vdd");
636 	if (IS_ERR(data->vdd_supply))
637 		return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply),
638 				     "Unable to get VDD regulator\n");
639 
640 	data->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
641 						   GPIOD_OUT_HIGH);
642 	if (IS_ERR(data->reset_gpio))
643 		return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio),
644 				     "Cannot get reset GPIO\n");
645 
646 	ret = hdc3020_power_on(data);
647 	if (ret)
648 		return dev_err_probe(&client->dev, ret, "Power on failed\n");
649 
650 	ret = devm_add_action_or_reset(&data->client->dev, hdc3020_exit, data);
651 	if (ret)
652 		return ret;
653 
654 	if (client->irq) {
655 		ret = devm_request_threaded_irq(&client->dev, client->irq,
656 						NULL, hdc3020_interrupt_handler,
657 						IRQF_ONESHOT, "hdc3020",
658 						indio_dev);
659 		if (ret)
660 			return dev_err_probe(&client->dev, ret,
661 					     "Failed to request IRQ\n");
662 	}
663 
664 	ret = devm_iio_device_register(&data->client->dev, indio_dev);
665 	if (ret)
666 		return dev_err_probe(&client->dev, ret, "Failed to add device");
667 
668 	return 0;
669 }
670 
671 static int hdc3020_suspend(struct device *dev)
672 {
673 	struct iio_dev *iio_dev = dev_get_drvdata(dev);
674 	struct hdc3020_data *data = iio_priv(iio_dev);
675 
676 	return hdc3020_power_off(data);
677 }
678 
679 static int hdc3020_resume(struct device *dev)
680 {
681 	struct iio_dev *iio_dev = dev_get_drvdata(dev);
682 	struct hdc3020_data *data = iio_priv(iio_dev);
683 
684 	return hdc3020_power_on(data);
685 }
686 
687 static DEFINE_SIMPLE_DEV_PM_OPS(hdc3020_pm_ops, hdc3020_suspend, hdc3020_resume);
688 
689 static const struct i2c_device_id hdc3020_id[] = {
690 	{ "hdc3020" },
691 	{ "hdc3021" },
692 	{ "hdc3022" },
693 	{ }
694 };
695 MODULE_DEVICE_TABLE(i2c, hdc3020_id);
696 
697 static const struct of_device_id hdc3020_dt_ids[] = {
698 	{ .compatible = "ti,hdc3020" },
699 	{ .compatible = "ti,hdc3021" },
700 	{ .compatible = "ti,hdc3022" },
701 	{ }
702 };
703 MODULE_DEVICE_TABLE(of, hdc3020_dt_ids);
704 
705 static struct i2c_driver hdc3020_driver = {
706 	.driver = {
707 		.name = "hdc3020",
708 		.pm = pm_sleep_ptr(&hdc3020_pm_ops),
709 		.of_match_table = hdc3020_dt_ids,
710 	},
711 	.probe = hdc3020_probe,
712 	.id_table = hdc3020_id,
713 };
714 module_i2c_driver(hdc3020_driver);
715 
716 MODULE_AUTHOR("Javier Carrasco <javier.carrasco.cruz@gmail.com>");
717 MODULE_AUTHOR("Li peiyu <579lpy@gmail.com>");
718 MODULE_DESCRIPTION("TI HDC3020 humidity and temperature sensor driver");
719 MODULE_LICENSE("GPL");
720