xref: /openbmc/linux/drivers/iio/humidity/hts221_core.c (revision 812f77b749a8ae11f58dacf0d3ed65e7ede47458)
1 /*
2  * STMicroelectronics hts221 sensor driver
3  *
4  * Copyright 2016 STMicroelectronics Inc.
5  *
6  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7  *
8  * Licensed under the GPL-2.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/delay.h>
16 #include <linux/pm.h>
17 #include <asm/unaligned.h>
18 
19 #include "hts221.h"
20 
21 #define HTS221_REG_WHOAMI_ADDR		0x0f
22 #define HTS221_REG_WHOAMI_VAL		0xbc
23 
24 #define HTS221_REG_CNTRL1_ADDR		0x20
25 #define HTS221_REG_CNTRL2_ADDR		0x21
26 
27 #define HTS221_REG_AVG_ADDR		0x10
28 #define HTS221_REG_H_OUT_L		0x28
29 #define HTS221_REG_T_OUT_L		0x2a
30 
31 #define HTS221_HUMIDITY_AVG_MASK	0x07
32 #define HTS221_TEMP_AVG_MASK		0x38
33 
34 #define HTS221_ODR_MASK			0x03
35 #define HTS221_BDU_MASK			BIT(2)
36 #define HTS221_ENABLE_MASK		BIT(7)
37 
38 /* calibration registers */
39 #define HTS221_REG_0RH_CAL_X_H		0x36
40 #define HTS221_REG_1RH_CAL_X_H		0x3a
41 #define HTS221_REG_0RH_CAL_Y_H		0x30
42 #define HTS221_REG_1RH_CAL_Y_H		0x31
43 #define HTS221_REG_0T_CAL_X_L		0x3c
44 #define HTS221_REG_1T_CAL_X_L		0x3e
45 #define HTS221_REG_0T_CAL_Y_H		0x32
46 #define HTS221_REG_1T_CAL_Y_H		0x33
47 #define HTS221_REG_T1_T0_CAL_Y_H	0x35
48 
49 struct hts221_odr {
50 	u8 hz;
51 	u8 val;
52 };
53 
54 #define HTS221_AVG_DEPTH		8
55 struct hts221_avg {
56 	u8 addr;
57 	u8 mask;
58 	u16 avg_avl[HTS221_AVG_DEPTH];
59 };
60 
61 static const struct hts221_odr hts221_odr_table[] = {
62 	{  1, 0x01 },	/* 1Hz */
63 	{  7, 0x02 },	/* 7Hz */
64 	{ 13, 0x03 },	/* 12.5Hz */
65 };
66 
67 static const struct hts221_avg hts221_avg_list[] = {
68 	{
69 		.addr = HTS221_REG_AVG_ADDR,
70 		.mask = HTS221_HUMIDITY_AVG_MASK,
71 		.avg_avl = {
72 			4, /* 0.4 %RH */
73 			8, /* 0.3 %RH */
74 			16, /* 0.2 %RH */
75 			32, /* 0.15 %RH */
76 			64, /* 0.1 %RH */
77 			128, /* 0.07 %RH */
78 			256, /* 0.05 %RH */
79 			512, /* 0.03 %RH */
80 		},
81 	},
82 	{
83 		.addr = HTS221_REG_AVG_ADDR,
84 		.mask = HTS221_TEMP_AVG_MASK,
85 		.avg_avl = {
86 			2, /* 0.08 degC */
87 			4, /* 0.05 degC */
88 			8, /* 0.04 degC */
89 			16, /* 0.03 degC */
90 			32, /* 0.02 degC */
91 			64, /* 0.015 degC */
92 			128, /* 0.01 degC */
93 			256, /* 0.007 degC */
94 		},
95 	},
96 };
97 
98 static const struct iio_chan_spec hts221_channels[] = {
99 	{
100 		.type = IIO_HUMIDITYRELATIVE,
101 		.address = HTS221_REG_H_OUT_L,
102 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
103 				      BIT(IIO_CHAN_INFO_OFFSET) |
104 				      BIT(IIO_CHAN_INFO_SCALE) |
105 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
106 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
107 		.scan_index = 0,
108 		.scan_type = {
109 			.sign = 's',
110 			.realbits = 16,
111 			.storagebits = 16,
112 			.endianness = IIO_LE,
113 		},
114 	},
115 	{
116 		.type = IIO_TEMP,
117 		.address = HTS221_REG_T_OUT_L,
118 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
119 				      BIT(IIO_CHAN_INFO_OFFSET) |
120 				      BIT(IIO_CHAN_INFO_SCALE) |
121 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
122 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
123 		.scan_index = 1,
124 		.scan_type = {
125 			.sign = 's',
126 			.realbits = 16,
127 			.storagebits = 16,
128 			.endianness = IIO_LE,
129 		},
130 	},
131 	IIO_CHAN_SOFT_TIMESTAMP(2),
132 };
133 
134 int hts221_write_with_mask(struct hts221_hw *hw, u8 addr, u8 mask, u8 val)
135 {
136 	u8 data;
137 	int err;
138 
139 	mutex_lock(&hw->lock);
140 
141 	err = hw->tf->read(hw->dev, addr, sizeof(data), &data);
142 	if (err < 0) {
143 		dev_err(hw->dev, "failed to read %02x register\n", addr);
144 		goto unlock;
145 	}
146 
147 	data = (data & ~mask) | ((val << __ffs(mask)) & mask);
148 
149 	err = hw->tf->write(hw->dev, addr, sizeof(data), &data);
150 	if (err < 0)
151 		dev_err(hw->dev, "failed to write %02x register\n", addr);
152 
153 unlock:
154 	mutex_unlock(&hw->lock);
155 
156 	return err;
157 }
158 
159 static int hts221_check_whoami(struct hts221_hw *hw)
160 {
161 	u8 data;
162 	int err;
163 
164 	err = hw->tf->read(hw->dev, HTS221_REG_WHOAMI_ADDR, sizeof(data),
165 			   &data);
166 	if (err < 0) {
167 		dev_err(hw->dev, "failed to read whoami register\n");
168 		return err;
169 	}
170 
171 	if (data != HTS221_REG_WHOAMI_VAL) {
172 		dev_err(hw->dev, "wrong whoami {%02x vs %02x}\n",
173 			data, HTS221_REG_WHOAMI_VAL);
174 		return -ENODEV;
175 	}
176 
177 	return 0;
178 }
179 
180 static int hts221_update_odr(struct hts221_hw *hw, u8 odr)
181 {
182 	int i, err;
183 
184 	for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
185 		if (hts221_odr_table[i].hz == odr)
186 			break;
187 
188 	if (i == ARRAY_SIZE(hts221_odr_table))
189 		return -EINVAL;
190 
191 	err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
192 				     HTS221_ODR_MASK, hts221_odr_table[i].val);
193 	if (err < 0)
194 		return err;
195 
196 	hw->odr = odr;
197 
198 	return 0;
199 }
200 
201 static int hts221_update_avg(struct hts221_hw *hw,
202 			     enum hts221_sensor_type type,
203 			     u16 val)
204 {
205 	int i, err;
206 	const struct hts221_avg *avg = &hts221_avg_list[type];
207 
208 	for (i = 0; i < HTS221_AVG_DEPTH; i++)
209 		if (avg->avg_avl[i] == val)
210 			break;
211 
212 	if (i == HTS221_AVG_DEPTH)
213 		return -EINVAL;
214 
215 	err = hts221_write_with_mask(hw, avg->addr, avg->mask, i);
216 	if (err < 0)
217 		return err;
218 
219 	hw->sensors[type].cur_avg_idx = i;
220 
221 	return 0;
222 }
223 
224 static ssize_t hts221_sysfs_sampling_freq(struct device *dev,
225 					  struct device_attribute *attr,
226 					  char *buf)
227 {
228 	int i;
229 	ssize_t len = 0;
230 
231 	for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
232 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
233 				 hts221_odr_table[i].hz);
234 	buf[len - 1] = '\n';
235 
236 	return len;
237 }
238 
239 static ssize_t
240 hts221_sysfs_rh_oversampling_avail(struct device *dev,
241 				   struct device_attribute *attr,
242 				   char *buf)
243 {
244 	const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_H];
245 	ssize_t len = 0;
246 	int i;
247 
248 	for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
249 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
250 				 avg->avg_avl[i]);
251 	buf[len - 1] = '\n';
252 
253 	return len;
254 }
255 
256 static ssize_t
257 hts221_sysfs_temp_oversampling_avail(struct device *dev,
258 				     struct device_attribute *attr,
259 				     char *buf)
260 {
261 	const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_T];
262 	ssize_t len = 0;
263 	int i;
264 
265 	for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
266 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
267 				 avg->avg_avl[i]);
268 	buf[len - 1] = '\n';
269 
270 	return len;
271 }
272 
273 int hts221_set_enable(struct hts221_hw *hw, bool enable)
274 {
275 	int err;
276 
277 	err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
278 				     HTS221_ENABLE_MASK, enable);
279 	if (err < 0)
280 		return err;
281 
282 	hw->enabled = enable;
283 
284 	return 0;
285 }
286 
287 static int hts221_parse_temp_caldata(struct hts221_hw *hw)
288 {
289 	int err, *slope, *b_gen;
290 	s16 cal_x0, cal_x1, cal_y0, cal_y1;
291 	u8 cal0, cal1;
292 
293 	err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_Y_H,
294 			   sizeof(cal0), &cal0);
295 	if (err < 0)
296 		return err;
297 
298 	err = hw->tf->read(hw->dev, HTS221_REG_T1_T0_CAL_Y_H,
299 			   sizeof(cal1), &cal1);
300 	if (err < 0)
301 		return err;
302 	cal_y0 = (le16_to_cpu(cal1 & 0x3) << 8) | cal0;
303 
304 	err = hw->tf->read(hw->dev, HTS221_REG_1T_CAL_Y_H,
305 			   sizeof(cal0), &cal0);
306 	if (err < 0)
307 		return err;
308 	cal_y1 = (((cal1 & 0xc) >> 2) << 8) | cal0;
309 
310 	err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_X_L, sizeof(cal_x0),
311 			   (u8 *)&cal_x0);
312 	if (err < 0)
313 		return err;
314 	cal_x0 = le16_to_cpu(cal_x0);
315 
316 	err = hw->tf->read(hw->dev, HTS221_REG_1T_CAL_X_L, sizeof(cal_x1),
317 			   (u8 *)&cal_x1);
318 	if (err < 0)
319 		return err;
320 	cal_x1 = le16_to_cpu(cal_x1);
321 
322 	slope = &hw->sensors[HTS221_SENSOR_T].slope;
323 	b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen;
324 
325 	*slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
326 	*b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
327 		 (cal_x1 - cal_x0);
328 	*b_gen *= 8;
329 
330 	return 0;
331 }
332 
333 static int hts221_parse_rh_caldata(struct hts221_hw *hw)
334 {
335 	int err, *slope, *b_gen;
336 	s16 cal_x0, cal_x1, cal_y0, cal_y1;
337 	u8 data;
338 
339 	err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_Y_H, sizeof(data),
340 			   &data);
341 	if (err < 0)
342 		return err;
343 	cal_y0 = data;
344 
345 	err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_Y_H, sizeof(data),
346 			   &data);
347 	if (err < 0)
348 		return err;
349 	cal_y1 = data;
350 
351 	err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_X_H, sizeof(cal_x0),
352 			   (u8 *)&cal_x0);
353 	if (err < 0)
354 		return err;
355 	cal_x0 = le16_to_cpu(cal_x0);
356 
357 	err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_X_H, sizeof(cal_x1),
358 			   (u8 *)&cal_x1);
359 	if (err < 0)
360 		return err;
361 	cal_x1 = le16_to_cpu(cal_x1);
362 
363 	slope = &hw->sensors[HTS221_SENSOR_H].slope;
364 	b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen;
365 
366 	*slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
367 	*b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
368 		 (cal_x1 - cal_x0);
369 	*b_gen *= 8;
370 
371 	return 0;
372 }
373 
374 static int hts221_get_sensor_scale(struct hts221_hw *hw,
375 				   enum iio_chan_type ch_type,
376 				   int *val, int *val2)
377 {
378 	s64 tmp;
379 	s32 rem, div, data;
380 
381 	switch (ch_type) {
382 	case IIO_HUMIDITYRELATIVE:
383 		data = hw->sensors[HTS221_SENSOR_H].slope;
384 		div = (1 << 4) * 1000;
385 		break;
386 	case IIO_TEMP:
387 		data = hw->sensors[HTS221_SENSOR_T].slope;
388 		div = (1 << 6) * 1000;
389 		break;
390 	default:
391 		return -EINVAL;
392 	}
393 
394 	tmp = div_s64(data * 1000000000LL, div);
395 	tmp = div_s64_rem(tmp, 1000000000LL, &rem);
396 
397 	*val = tmp;
398 	*val2 = rem;
399 
400 	return IIO_VAL_INT_PLUS_NANO;
401 }
402 
403 static int hts221_get_sensor_offset(struct hts221_hw *hw,
404 				    enum iio_chan_type ch_type,
405 				    int *val, int *val2)
406 {
407 	s64 tmp;
408 	s32 rem, div, data;
409 
410 	switch (ch_type) {
411 	case IIO_HUMIDITYRELATIVE:
412 		data = hw->sensors[HTS221_SENSOR_H].b_gen;
413 		div = hw->sensors[HTS221_SENSOR_H].slope;
414 		break;
415 	case IIO_TEMP:
416 		data = hw->sensors[HTS221_SENSOR_T].b_gen;
417 		div = hw->sensors[HTS221_SENSOR_T].slope;
418 		break;
419 	default:
420 		return -EINVAL;
421 	}
422 
423 	tmp = div_s64(data * 1000000000LL, div);
424 	tmp = div_s64_rem(tmp, 1000000000LL, &rem);
425 
426 	*val = tmp;
427 	*val2 = rem;
428 
429 	return IIO_VAL_INT_PLUS_NANO;
430 }
431 
432 static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
433 {
434 	u8 data[HTS221_DATA_SIZE];
435 	int err;
436 
437 	err = hts221_set_enable(hw, true);
438 	if (err < 0)
439 		return err;
440 
441 	msleep(50);
442 
443 	err = hw->tf->read(hw->dev, addr, sizeof(data), data);
444 	if (err < 0)
445 		return err;
446 
447 	hts221_set_enable(hw, false);
448 
449 	*val = (s16)get_unaligned_le16(data);
450 
451 	return IIO_VAL_INT;
452 }
453 
454 static int hts221_read_raw(struct iio_dev *iio_dev,
455 			   struct iio_chan_spec const *ch,
456 			   int *val, int *val2, long mask)
457 {
458 	struct hts221_hw *hw = iio_priv(iio_dev);
459 	int ret;
460 
461 	ret = iio_device_claim_direct_mode(iio_dev);
462 	if (ret)
463 		return ret;
464 
465 	switch (mask) {
466 	case IIO_CHAN_INFO_RAW:
467 		ret = hts221_read_oneshot(hw, ch->address, val);
468 		break;
469 	case IIO_CHAN_INFO_SCALE:
470 		ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
471 		break;
472 	case IIO_CHAN_INFO_OFFSET:
473 		ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
474 		break;
475 	case IIO_CHAN_INFO_SAMP_FREQ:
476 		*val = hw->odr;
477 		ret = IIO_VAL_INT;
478 		break;
479 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
480 		u8 idx;
481 		const struct hts221_avg *avg;
482 
483 		switch (ch->type) {
484 		case IIO_HUMIDITYRELATIVE:
485 			avg = &hts221_avg_list[HTS221_SENSOR_H];
486 			idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx;
487 			*val = avg->avg_avl[idx];
488 			ret = IIO_VAL_INT;
489 			break;
490 		case IIO_TEMP:
491 			avg = &hts221_avg_list[HTS221_SENSOR_T];
492 			idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
493 			*val = avg->avg_avl[idx];
494 			ret = IIO_VAL_INT;
495 			break;
496 		default:
497 			ret = -EINVAL;
498 			break;
499 		}
500 		break;
501 	}
502 	default:
503 		ret = -EINVAL;
504 		break;
505 	}
506 
507 	iio_device_release_direct_mode(iio_dev);
508 
509 	return ret;
510 }
511 
512 static int hts221_write_raw(struct iio_dev *iio_dev,
513 			    struct iio_chan_spec const *chan,
514 			    int val, int val2, long mask)
515 {
516 	struct hts221_hw *hw = iio_priv(iio_dev);
517 	int ret;
518 
519 	ret = iio_device_claim_direct_mode(iio_dev);
520 	if (ret)
521 		return ret;
522 
523 	switch (mask) {
524 	case IIO_CHAN_INFO_SAMP_FREQ:
525 		ret = hts221_update_odr(hw, val);
526 		break;
527 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
528 		switch (chan->type) {
529 		case IIO_HUMIDITYRELATIVE:
530 			ret = hts221_update_avg(hw, HTS221_SENSOR_H, val);
531 			break;
532 		case IIO_TEMP:
533 			ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
534 			break;
535 		default:
536 			ret = -EINVAL;
537 			break;
538 		}
539 		break;
540 	default:
541 		ret = -EINVAL;
542 		break;
543 	}
544 
545 	iio_device_release_direct_mode(iio_dev);
546 
547 	return ret;
548 }
549 
550 static int hts221_validate_trigger(struct iio_dev *iio_dev,
551 				   struct iio_trigger *trig)
552 {
553 	struct hts221_hw *hw = iio_priv(iio_dev);
554 
555 	return hw->trig == trig ? 0 : -EINVAL;
556 }
557 
558 static IIO_DEVICE_ATTR(in_humidity_oversampling_ratio_available, S_IRUGO,
559 		       hts221_sysfs_rh_oversampling_avail, NULL, 0);
560 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available, S_IRUGO,
561 		       hts221_sysfs_temp_oversampling_avail, NULL, 0);
562 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hts221_sysfs_sampling_freq);
563 
564 static struct attribute *hts221_attributes[] = {
565 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
566 	&iio_dev_attr_in_humidity_oversampling_ratio_available.dev_attr.attr,
567 	&iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
568 	NULL,
569 };
570 
571 static const struct attribute_group hts221_attribute_group = {
572 	.attrs = hts221_attributes,
573 };
574 
575 static const struct iio_info hts221_info = {
576 	.attrs = &hts221_attribute_group,
577 	.read_raw = hts221_read_raw,
578 	.write_raw = hts221_write_raw,
579 	.validate_trigger = hts221_validate_trigger,
580 };
581 
582 static const unsigned long hts221_scan_masks[] = {0x3, 0x0};
583 
584 int hts221_probe(struct iio_dev *iio_dev)
585 {
586 	struct hts221_hw *hw = iio_priv(iio_dev);
587 	int err;
588 	u8 data;
589 
590 	mutex_init(&hw->lock);
591 
592 	err = hts221_check_whoami(hw);
593 	if (err < 0)
594 		return err;
595 
596 	iio_dev->modes = INDIO_DIRECT_MODE;
597 	iio_dev->dev.parent = hw->dev;
598 	iio_dev->available_scan_masks = hts221_scan_masks;
599 	iio_dev->channels = hts221_channels;
600 	iio_dev->num_channels = ARRAY_SIZE(hts221_channels);
601 	iio_dev->name = HTS221_DEV_NAME;
602 	iio_dev->info = &hts221_info;
603 
604 	/* enable Block Data Update */
605 	err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
606 				     HTS221_BDU_MASK, 1);
607 	if (err < 0)
608 		return err;
609 
610 	err = hts221_update_odr(hw, hts221_odr_table[0].hz);
611 	if (err < 0)
612 		return err;
613 
614 	/* configure humidity sensor */
615 	err = hts221_parse_rh_caldata(hw);
616 	if (err < 0) {
617 		dev_err(hw->dev, "failed to get rh calibration data\n");
618 		return err;
619 	}
620 
621 	data = hts221_avg_list[HTS221_SENSOR_H].avg_avl[3];
622 	err = hts221_update_avg(hw, HTS221_SENSOR_H, data);
623 	if (err < 0) {
624 		dev_err(hw->dev, "failed to set rh oversampling ratio\n");
625 		return err;
626 	}
627 
628 	/* configure temperature sensor */
629 	err = hts221_parse_temp_caldata(hw);
630 	if (err < 0) {
631 		dev_err(hw->dev,
632 			"failed to get temperature calibration data\n");
633 		return err;
634 	}
635 
636 	data = hts221_avg_list[HTS221_SENSOR_T].avg_avl[3];
637 	err = hts221_update_avg(hw, HTS221_SENSOR_T, data);
638 	if (err < 0) {
639 		dev_err(hw->dev,
640 			"failed to set temperature oversampling ratio\n");
641 		return err;
642 	}
643 
644 	if (hw->irq > 0) {
645 		err = hts221_allocate_buffers(hw);
646 		if (err < 0)
647 			return err;
648 
649 		err = hts221_allocate_trigger(hw);
650 		if (err)
651 			return err;
652 	}
653 
654 	return devm_iio_device_register(hw->dev, iio_dev);
655 }
656 EXPORT_SYMBOL(hts221_probe);
657 
658 static int __maybe_unused hts221_suspend(struct device *dev)
659 {
660 	struct iio_dev *iio_dev = dev_get_drvdata(dev);
661 	struct hts221_hw *hw = iio_priv(iio_dev);
662 	int err;
663 
664 	err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
665 				     HTS221_ENABLE_MASK, false);
666 
667 	return err < 0 ? err : 0;
668 }
669 
670 static int __maybe_unused hts221_resume(struct device *dev)
671 {
672 	struct iio_dev *iio_dev = dev_get_drvdata(dev);
673 	struct hts221_hw *hw = iio_priv(iio_dev);
674 	int err = 0;
675 
676 	if (hw->enabled)
677 		err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
678 					     HTS221_ENABLE_MASK, true);
679 
680 	return err;
681 }
682 
683 const struct dev_pm_ops hts221_pm_ops = {
684 	SET_SYSTEM_SLEEP_PM_OPS(hts221_suspend, hts221_resume)
685 };
686 EXPORT_SYMBOL(hts221_pm_ops);
687 
688 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
689 MODULE_DESCRIPTION("STMicroelectronics hts221 sensor driver");
690 MODULE_LICENSE("GPL v2");
691