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