1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics hts221 sensor driver
5 * Copyright 2016 STMicroelectronics Inc.
7 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/delay.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/bitfield.h>
22 #define HTS221_REG_WHOAMI_ADDR 0x0f
23 #define HTS221_REG_WHOAMI_VAL 0xbc
25 #define HTS221_REG_CNTRL1_ADDR 0x20
26 #define HTS221_REG_CNTRL2_ADDR 0x21
28 #define HTS221_ODR_MASK 0x03
29 #define HTS221_BDU_MASK BIT(2)
30 #define HTS221_ENABLE_MASK BIT(7)
32 /* calibration registers */
33 #define HTS221_REG_0RH_CAL_X_H 0x36
34 #define HTS221_REG_1RH_CAL_X_H 0x3a
35 #define HTS221_REG_0RH_CAL_Y_H 0x30
36 #define HTS221_REG_1RH_CAL_Y_H 0x31
37 #define HTS221_REG_0T_CAL_X_L 0x3c
38 #define HTS221_REG_1T_CAL_X_L 0x3e
39 #define HTS221_REG_0T_CAL_Y_H 0x32
40 #define HTS221_REG_1T_CAL_Y_H 0x33
41 #define HTS221_REG_T1_T0_CAL_Y_H 0x35
48 #define HTS221_AVG_DEPTH 8
52 u16 avg_avl
[HTS221_AVG_DEPTH
];
55 static const struct hts221_odr hts221_odr_table
[] = {
56 { 1, 0x01 }, /* 1Hz */
57 { 7, 0x02 }, /* 7Hz */
58 { 13, 0x03 }, /* 12.5Hz */
61 static const struct hts221_avg hts221_avg_list
[] = {
92 static const struct iio_chan_spec hts221_channels
[] = {
94 .type
= IIO_HUMIDITYRELATIVE
,
96 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
97 BIT(IIO_CHAN_INFO_OFFSET
) |
98 BIT(IIO_CHAN_INFO_SCALE
) |
99 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
100 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
106 .endianness
= IIO_LE
,
112 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
113 BIT(IIO_CHAN_INFO_OFFSET
) |
114 BIT(IIO_CHAN_INFO_SCALE
) |
115 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
116 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
122 .endianness
= IIO_LE
,
125 IIO_CHAN_SOFT_TIMESTAMP(2),
128 static int hts221_check_whoami(struct hts221_hw
*hw
)
132 err
= regmap_read(hw
->regmap
, HTS221_REG_WHOAMI_ADDR
, &data
);
134 dev_err(hw
->dev
, "failed to read whoami register\n");
138 if (data
!= HTS221_REG_WHOAMI_VAL
) {
139 dev_err(hw
->dev
, "wrong whoami {%02x vs %02x}\n",
140 data
, HTS221_REG_WHOAMI_VAL
);
147 static int hts221_update_odr(struct hts221_hw
*hw
, u8 odr
)
151 for (i
= 0; i
< ARRAY_SIZE(hts221_odr_table
); i
++)
152 if (hts221_odr_table
[i
].hz
== odr
)
155 if (i
== ARRAY_SIZE(hts221_odr_table
))
158 err
= regmap_update_bits(hw
->regmap
, HTS221_REG_CNTRL1_ADDR
,
160 FIELD_PREP(HTS221_ODR_MASK
,
161 hts221_odr_table
[i
].val
));
170 static int hts221_update_avg(struct hts221_hw
*hw
,
171 enum hts221_sensor_type type
,
174 const struct hts221_avg
*avg
= &hts221_avg_list
[type
];
177 for (i
= 0; i
< HTS221_AVG_DEPTH
; i
++)
178 if (avg
->avg_avl
[i
] == val
)
181 if (i
== HTS221_AVG_DEPTH
)
184 data
= ((i
<< __ffs(avg
->mask
)) & avg
->mask
);
185 err
= regmap_update_bits(hw
->regmap
, avg
->addr
,
190 hw
->sensors
[type
].cur_avg_idx
= i
;
195 static ssize_t
hts221_sysfs_sampling_freq(struct device
*dev
,
196 struct device_attribute
*attr
,
202 for (i
= 0; i
< ARRAY_SIZE(hts221_odr_table
); i
++)
203 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
204 hts221_odr_table
[i
].hz
);
211 hts221_sysfs_rh_oversampling_avail(struct device
*dev
,
212 struct device_attribute
*attr
,
215 const struct hts221_avg
*avg
= &hts221_avg_list
[HTS221_SENSOR_H
];
219 for (i
= 0; i
< ARRAY_SIZE(avg
->avg_avl
); i
++)
220 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
228 hts221_sysfs_temp_oversampling_avail(struct device
*dev
,
229 struct device_attribute
*attr
,
232 const struct hts221_avg
*avg
= &hts221_avg_list
[HTS221_SENSOR_T
];
236 for (i
= 0; i
< ARRAY_SIZE(avg
->avg_avl
); i
++)
237 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
244 int hts221_set_enable(struct hts221_hw
*hw
, bool enable
)
248 err
= regmap_update_bits(hw
->regmap
, HTS221_REG_CNTRL1_ADDR
,
250 FIELD_PREP(HTS221_ENABLE_MASK
, enable
));
254 hw
->enabled
= enable
;
259 static int hts221_parse_temp_caldata(struct hts221_hw
*hw
)
261 int err
, *slope
, *b_gen
, cal0
, cal1
;
262 s16 cal_x0
, cal_x1
, cal_y0
, cal_y1
;
265 err
= regmap_read(hw
->regmap
, HTS221_REG_0T_CAL_Y_H
, &cal0
);
269 err
= regmap_read(hw
->regmap
, HTS221_REG_T1_T0_CAL_Y_H
, &cal1
);
272 cal_y0
= ((cal1
& 0x3) << 8) | cal0
;
274 err
= regmap_read(hw
->regmap
, HTS221_REG_1T_CAL_Y_H
, &cal0
);
277 cal_y1
= (((cal1
& 0xc) >> 2) << 8) | cal0
;
279 err
= regmap_bulk_read(hw
->regmap
, HTS221_REG_0T_CAL_X_L
,
283 cal_x0
= le16_to_cpu(val
);
285 err
= regmap_bulk_read(hw
->regmap
, HTS221_REG_1T_CAL_X_L
,
289 cal_x1
= le16_to_cpu(val
);
291 slope
= &hw
->sensors
[HTS221_SENSOR_T
].slope
;
292 b_gen
= &hw
->sensors
[HTS221_SENSOR_T
].b_gen
;
294 *slope
= ((cal_y1
- cal_y0
) * 8000) / (cal_x1
- cal_x0
);
295 *b_gen
= (((s32
)cal_x1
* cal_y0
- (s32
)cal_x0
* cal_y1
) * 1000) /
302 static int hts221_parse_rh_caldata(struct hts221_hw
*hw
)
304 int err
, *slope
, *b_gen
, data
;
305 s16 cal_x0
, cal_x1
, cal_y0
, cal_y1
;
308 err
= regmap_read(hw
->regmap
, HTS221_REG_0RH_CAL_Y_H
, &data
);
313 err
= regmap_read(hw
->regmap
, HTS221_REG_1RH_CAL_Y_H
, &data
);
318 err
= regmap_bulk_read(hw
->regmap
, HTS221_REG_0RH_CAL_X_H
,
322 cal_x0
= le16_to_cpu(val
);
324 err
= regmap_bulk_read(hw
->regmap
, HTS221_REG_1RH_CAL_X_H
,
328 cal_x1
= le16_to_cpu(val
);
330 slope
= &hw
->sensors
[HTS221_SENSOR_H
].slope
;
331 b_gen
= &hw
->sensors
[HTS221_SENSOR_H
].b_gen
;
333 *slope
= ((cal_y1
- cal_y0
) * 8000) / (cal_x1
- cal_x0
);
334 *b_gen
= (((s32
)cal_x1
* cal_y0
- (s32
)cal_x0
* cal_y1
) * 1000) /
341 static int hts221_get_sensor_scale(struct hts221_hw
*hw
,
342 enum iio_chan_type ch_type
,
349 case IIO_HUMIDITYRELATIVE
:
350 data
= hw
->sensors
[HTS221_SENSOR_H
].slope
;
351 div
= (1 << 4) * 1000;
354 data
= hw
->sensors
[HTS221_SENSOR_T
].slope
;
355 div
= (1 << 6) * 1000;
361 tmp
= div_s64(data
* 1000000000LL, div
);
362 tmp
= div_s64_rem(tmp
, 1000000000LL, &rem
);
367 return IIO_VAL_INT_PLUS_NANO
;
370 static int hts221_get_sensor_offset(struct hts221_hw
*hw
,
371 enum iio_chan_type ch_type
,
378 case IIO_HUMIDITYRELATIVE
:
379 data
= hw
->sensors
[HTS221_SENSOR_H
].b_gen
;
380 div
= hw
->sensors
[HTS221_SENSOR_H
].slope
;
383 data
= hw
->sensors
[HTS221_SENSOR_T
].b_gen
;
384 div
= hw
->sensors
[HTS221_SENSOR_T
].slope
;
390 tmp
= div_s64(data
* 1000000000LL, div
);
391 tmp
= div_s64_rem(tmp
, 1000000000LL, &rem
);
396 return IIO_VAL_INT_PLUS_NANO
;
399 static int hts221_read_oneshot(struct hts221_hw
*hw
, u8 addr
, int *val
)
404 err
= hts221_set_enable(hw
, true);
410 err
= regmap_bulk_read(hw
->regmap
, addr
, &data
, sizeof(data
));
414 hts221_set_enable(hw
, false);
416 *val
= (s16
)le16_to_cpu(data
);
421 static int hts221_read_raw(struct iio_dev
*iio_dev
,
422 struct iio_chan_spec
const *ch
,
423 int *val
, int *val2
, long mask
)
425 struct hts221_hw
*hw
= iio_priv(iio_dev
);
428 ret
= iio_device_claim_direct_mode(iio_dev
);
433 case IIO_CHAN_INFO_RAW
:
434 ret
= hts221_read_oneshot(hw
, ch
->address
, val
);
436 case IIO_CHAN_INFO_SCALE
:
437 ret
= hts221_get_sensor_scale(hw
, ch
->type
, val
, val2
);
439 case IIO_CHAN_INFO_OFFSET
:
440 ret
= hts221_get_sensor_offset(hw
, ch
->type
, val
, val2
);
442 case IIO_CHAN_INFO_SAMP_FREQ
:
446 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
: {
448 const struct hts221_avg
*avg
;
451 case IIO_HUMIDITYRELATIVE
:
452 avg
= &hts221_avg_list
[HTS221_SENSOR_H
];
453 idx
= hw
->sensors
[HTS221_SENSOR_H
].cur_avg_idx
;
454 *val
= avg
->avg_avl
[idx
];
458 avg
= &hts221_avg_list
[HTS221_SENSOR_T
];
459 idx
= hw
->sensors
[HTS221_SENSOR_T
].cur_avg_idx
;
460 *val
= avg
->avg_avl
[idx
];
474 iio_device_release_direct_mode(iio_dev
);
479 static int hts221_write_raw(struct iio_dev
*iio_dev
,
480 struct iio_chan_spec
const *chan
,
481 int val
, int val2
, long mask
)
483 struct hts221_hw
*hw
= iio_priv(iio_dev
);
486 ret
= iio_device_claim_direct_mode(iio_dev
);
491 case IIO_CHAN_INFO_SAMP_FREQ
:
492 ret
= hts221_update_odr(hw
, val
);
494 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
495 switch (chan
->type
) {
496 case IIO_HUMIDITYRELATIVE
:
497 ret
= hts221_update_avg(hw
, HTS221_SENSOR_H
, val
);
500 ret
= hts221_update_avg(hw
, HTS221_SENSOR_T
, val
);
512 iio_device_release_direct_mode(iio_dev
);
517 static int hts221_validate_trigger(struct iio_dev
*iio_dev
,
518 struct iio_trigger
*trig
)
520 struct hts221_hw
*hw
= iio_priv(iio_dev
);
522 return hw
->trig
== trig
? 0 : -EINVAL
;
525 static IIO_DEVICE_ATTR(in_humidity_oversampling_ratio_available
, S_IRUGO
,
526 hts221_sysfs_rh_oversampling_avail
, NULL
, 0);
527 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available
, S_IRUGO
,
528 hts221_sysfs_temp_oversampling_avail
, NULL
, 0);
529 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hts221_sysfs_sampling_freq
);
531 static struct attribute
*hts221_attributes
[] = {
532 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
533 &iio_dev_attr_in_humidity_oversampling_ratio_available
.dev_attr
.attr
,
534 &iio_dev_attr_in_temp_oversampling_ratio_available
.dev_attr
.attr
,
538 static const struct attribute_group hts221_attribute_group
= {
539 .attrs
= hts221_attributes
,
542 static const struct iio_info hts221_info
= {
543 .attrs
= &hts221_attribute_group
,
544 .read_raw
= hts221_read_raw
,
545 .write_raw
= hts221_write_raw
,
546 .validate_trigger
= hts221_validate_trigger
,
549 static const unsigned long hts221_scan_masks
[] = {0x3, 0x0};
551 static int hts221_init_regulators(struct device
*dev
)
555 err
= devm_regulator_get_enable(dev
, "vdd");
557 return dev_err_probe(dev
, err
, "failed to get vdd regulator\n");
564 int hts221_probe(struct device
*dev
, int irq
, const char *name
,
565 struct regmap
*regmap
)
567 struct iio_dev
*iio_dev
;
568 struct hts221_hw
*hw
;
572 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*hw
));
576 dev_set_drvdata(dev
, iio_dev
);
578 hw
= iio_priv(iio_dev
);
584 err
= hts221_init_regulators(dev
);
588 err
= hts221_check_whoami(hw
);
592 iio_dev
->modes
= INDIO_DIRECT_MODE
;
593 iio_dev
->available_scan_masks
= hts221_scan_masks
;
594 iio_dev
->channels
= hts221_channels
;
595 iio_dev
->num_channels
= ARRAY_SIZE(hts221_channels
);
596 iio_dev
->name
= HTS221_DEV_NAME
;
597 iio_dev
->info
= &hts221_info
;
599 /* enable Block Data Update */
600 err
= regmap_update_bits(hw
->regmap
, HTS221_REG_CNTRL1_ADDR
,
602 FIELD_PREP(HTS221_BDU_MASK
, 1));
606 err
= hts221_update_odr(hw
, hts221_odr_table
[0].hz
);
610 /* configure humidity sensor */
611 err
= hts221_parse_rh_caldata(hw
);
613 dev_err(hw
->dev
, "failed to get rh calibration data\n");
617 data
= hts221_avg_list
[HTS221_SENSOR_H
].avg_avl
[3];
618 err
= hts221_update_avg(hw
, HTS221_SENSOR_H
, data
);
620 dev_err(hw
->dev
, "failed to set rh oversampling ratio\n");
624 /* configure temperature sensor */
625 err
= hts221_parse_temp_caldata(hw
);
628 "failed to get temperature calibration data\n");
632 data
= hts221_avg_list
[HTS221_SENSOR_T
].avg_avl
[3];
633 err
= hts221_update_avg(hw
, HTS221_SENSOR_T
, data
);
636 "failed to set temperature oversampling ratio\n");
641 err
= hts221_allocate_buffers(iio_dev
);
645 err
= hts221_allocate_trigger(iio_dev
);
650 return devm_iio_device_register(hw
->dev
, iio_dev
);
652 EXPORT_SYMBOL_NS(hts221_probe
, "IIO_HTS221");
654 static int hts221_suspend(struct device
*dev
)
656 struct iio_dev
*iio_dev
= dev_get_drvdata(dev
);
657 struct hts221_hw
*hw
= iio_priv(iio_dev
);
659 return regmap_update_bits(hw
->regmap
, HTS221_REG_CNTRL1_ADDR
,
661 FIELD_PREP(HTS221_ENABLE_MASK
, false));
664 static int hts221_resume(struct device
*dev
)
666 struct iio_dev
*iio_dev
= dev_get_drvdata(dev
);
667 struct hts221_hw
*hw
= iio_priv(iio_dev
);
671 err
= regmap_update_bits(hw
->regmap
, HTS221_REG_CNTRL1_ADDR
,
673 FIELD_PREP(HTS221_ENABLE_MASK
,
678 EXPORT_NS_SIMPLE_DEV_PM_OPS(hts221_pm_ops
, hts221_suspend
, hts221_resume
,
681 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
682 MODULE_DESCRIPTION("STMicroelectronics hts221 sensor driver");
683 MODULE_LICENSE("GPL v2");