2 * STMicroelectronics hts221 sensor driver
4 * Copyright 2016 STMicroelectronics Inc.
6 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
8 * Licensed under the GPL-2.
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>
17 #include <asm/unaligned.h>
21 #define HTS221_REG_WHOAMI_ADDR 0x0f
22 #define HTS221_REG_WHOAMI_VAL 0xbc
24 #define HTS221_REG_CNTRL1_ADDR 0x20
25 #define HTS221_REG_CNTRL2_ADDR 0x21
26 #define HTS221_REG_CNTRL3_ADDR 0x22
28 #define HTS221_REG_AVG_ADDR 0x10
29 #define HTS221_REG_H_OUT_L 0x28
30 #define HTS221_REG_T_OUT_L 0x2a
32 #define HTS221_HUMIDITY_AVG_MASK 0x07
33 #define HTS221_TEMP_AVG_MASK 0x38
35 #define HTS221_ODR_MASK 0x03
36 #define HTS221_BDU_MASK BIT(2)
37 #define HTS221_ENABLE_MASK BIT(7)
39 #define HTS221_DRDY_MASK BIT(2)
42 /* calibration registers */
43 #define HTS221_REG_0RH_CAL_X_H 0x36
44 #define HTS221_REG_1RH_CAL_X_H 0x3a
45 #define HTS221_REG_0RH_CAL_Y_H 0x30
46 #define HTS221_REG_1RH_CAL_Y_H 0x31
47 #define HTS221_REG_0T_CAL_X_L 0x3c
48 #define HTS221_REG_1T_CAL_X_L 0x3e
49 #define HTS221_REG_0T_CAL_Y_H 0x32
50 #define HTS221_REG_1T_CAL_Y_H 0x33
51 #define HTS221_REG_T1_T0_CAL_Y_H 0x35
58 #define HTS221_AVG_DEPTH 8
62 u16 avg_avl[HTS221_AVG_DEPTH];
65 static const struct hts221_odr hts221_odr_table[] = {
66 { 1, 0x01 }, /* 1Hz */
67 { 7, 0x02 }, /* 7Hz */
68 { 13, 0x03 }, /* 12.5Hz */
71 static const struct hts221_avg hts221_avg_list[] = {
73 .addr = HTS221_REG_AVG_ADDR,
74 .mask = HTS221_HUMIDITY_AVG_MASK,
87 .addr = HTS221_REG_AVG_ADDR,
88 .mask = HTS221_TEMP_AVG_MASK,
102 static const struct iio_chan_spec hts221_channels[] = {
104 .type = IIO_HUMIDITYRELATIVE,
105 .address = HTS221_REG_H_OUT_L,
106 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
107 BIT(IIO_CHAN_INFO_OFFSET) |
108 BIT(IIO_CHAN_INFO_SCALE) |
109 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
110 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
116 .endianness = IIO_LE,
121 .address = HTS221_REG_T_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),
132 .endianness = IIO_LE,
135 IIO_CHAN_SOFT_TIMESTAMP(2),
138 static int hts221_write_with_mask(struct hts221_hw *hw, u8 addr, u8 mask,
144 mutex_lock(&hw->lock);
146 err = hw->tf->read(hw->dev, addr, sizeof(data), &data);
148 dev_err(hw->dev, "failed to read %02x register\n", addr);
152 data = (data & ~mask) | ((val << __ffs(mask)) & mask);
154 err = hw->tf->write(hw->dev, addr, sizeof(data), &data);
156 dev_err(hw->dev, "failed to write %02x register\n", addr);
159 mutex_unlock(&hw->lock);
164 static int hts221_check_whoami(struct hts221_hw *hw)
169 err = hw->tf->read(hw->dev, HTS221_REG_WHOAMI_ADDR, sizeof(data),
172 dev_err(hw->dev, "failed to read whoami register\n");
176 if (data != HTS221_REG_WHOAMI_VAL) {
177 dev_err(hw->dev, "wrong whoami {%02x vs %02x}\n",
178 data, HTS221_REG_WHOAMI_VAL);
185 int hts221_config_drdy(struct hts221_hw *hw, bool enable)
189 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL3_ADDR,
190 HTS221_DRDY_MASK, enable);
192 return err < 0 ? err : 0;
195 static int hts221_update_odr(struct hts221_hw *hw, u8 odr)
199 for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
200 if (hts221_odr_table[i].hz == odr)
203 if (i == ARRAY_SIZE(hts221_odr_table))
206 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
207 HTS221_ODR_MASK, hts221_odr_table[i].val);
211 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
212 HTS221_ENABLE_MASK, 1);
221 static int hts221_update_avg(struct hts221_hw *hw,
222 enum hts221_sensor_type type,
226 const struct hts221_avg *avg = &hts221_avg_list[type];
228 for (i = 0; i < HTS221_AVG_DEPTH; i++)
229 if (avg->avg_avl[i] == val)
232 if (i == HTS221_AVG_DEPTH)
235 err = hts221_write_with_mask(hw, avg->addr, avg->mask, i);
239 hw->sensors[type].cur_avg_idx = i;
244 static ssize_t hts221_sysfs_sampling_freq(struct device *dev,
245 struct device_attribute *attr,
251 for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
252 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
253 hts221_odr_table[i].hz);
260 hts221_sysfs_rh_oversampling_avail(struct device *dev,
261 struct device_attribute *attr,
264 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_H];
268 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
269 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
277 hts221_sysfs_temp_oversampling_avail(struct device *dev,
278 struct device_attribute *attr,
281 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_T];
285 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
286 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
293 int hts221_power_on(struct hts221_hw *hw)
297 err = hts221_update_odr(hw, hw->odr);
306 int hts221_power_off(struct hts221_hw *hw)
311 err = hw->tf->write(hw->dev, HTS221_REG_CNTRL1_ADDR, sizeof(data),
321 static int hts221_parse_temp_caldata(struct hts221_hw *hw)
323 int err, *slope, *b_gen;
324 s16 cal_x0, cal_x1, cal_y0, cal_y1;
327 err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_Y_H,
328 sizeof(cal0), &cal0);
332 err = hw->tf->read(hw->dev, HTS221_REG_T1_T0_CAL_Y_H,
333 sizeof(cal1), &cal1);
336 cal_y0 = (le16_to_cpu(cal1 & 0x3) << 8) | cal0;
338 err = hw->tf->read(hw->dev, HTS221_REG_1T_CAL_Y_H,
339 sizeof(cal0), &cal0);
342 cal_y1 = (((cal1 & 0xc) >> 2) << 8) | cal0;
344 err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_X_L, sizeof(cal_x0),
348 cal_x0 = le16_to_cpu(cal_x0);
350 err = hw->tf->read(hw->dev, HTS221_REG_1T_CAL_X_L, sizeof(cal_x1),
354 cal_x1 = le16_to_cpu(cal_x1);
356 slope = &hw->sensors[HTS221_SENSOR_T].slope;
357 b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen;
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) /
367 static int hts221_parse_rh_caldata(struct hts221_hw *hw)
369 int err, *slope, *b_gen;
370 s16 cal_x0, cal_x1, cal_y0, cal_y1;
373 err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_Y_H, sizeof(data),
379 err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_Y_H, sizeof(data),
385 err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_X_H, sizeof(cal_x0),
389 cal_x0 = le16_to_cpu(cal_x0);
391 err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_X_H, sizeof(cal_x1),
395 cal_x1 = le16_to_cpu(cal_x1);
397 slope = &hw->sensors[HTS221_SENSOR_H].slope;
398 b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen;
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) /
408 static int hts221_get_sensor_scale(struct hts221_hw *hw,
409 enum iio_chan_type ch_type,
416 case IIO_HUMIDITYRELATIVE:
417 data = hw->sensors[HTS221_SENSOR_H].slope;
418 div = (1 << 4) * 1000;
421 data = hw->sensors[HTS221_SENSOR_T].slope;
422 div = (1 << 6) * 1000;
428 tmp = div_s64(data * 1000000000LL, div);
429 tmp = div_s64_rem(tmp, 1000000000LL, &rem);
434 return IIO_VAL_INT_PLUS_NANO;
437 static int hts221_get_sensor_offset(struct hts221_hw *hw,
438 enum iio_chan_type ch_type,
445 case IIO_HUMIDITYRELATIVE:
446 data = hw->sensors[HTS221_SENSOR_H].b_gen;
447 div = hw->sensors[HTS221_SENSOR_H].slope;
450 data = hw->sensors[HTS221_SENSOR_T].b_gen;
451 div = hw->sensors[HTS221_SENSOR_T].slope;
457 tmp = div_s64(data * 1000000000LL, div);
458 tmp = div_s64_rem(tmp, 1000000000LL, &rem);
463 return IIO_VAL_INT_PLUS_NANO;
466 static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
468 u8 data[HTS221_DATA_SIZE];
471 err = hts221_power_on(hw);
477 err = hw->tf->read(hw->dev, addr, sizeof(data), data);
481 hts221_power_off(hw);
483 *val = (s16)get_unaligned_le16(data);
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)
492 struct hts221_hw *hw = iio_priv(iio_dev);
495 ret = iio_device_claim_direct_mode(iio_dev);
500 case IIO_CHAN_INFO_RAW:
501 ret = hts221_read_oneshot(hw, ch->address, val);
503 case IIO_CHAN_INFO_SCALE:
504 ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
506 case IIO_CHAN_INFO_OFFSET:
507 ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
509 case IIO_CHAN_INFO_SAMP_FREQ:
513 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
515 const struct hts221_avg *avg;
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];
525 avg = &hts221_avg_list[HTS221_SENSOR_T];
526 idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
527 *val = avg->avg_avl[idx];
541 iio_device_release_direct_mode(iio_dev);
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)
550 struct hts221_hw *hw = iio_priv(iio_dev);
553 ret = iio_device_claim_direct_mode(iio_dev);
558 case IIO_CHAN_INFO_SAMP_FREQ:
559 ret = hts221_update_odr(hw, val);
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);
567 ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
579 iio_device_release_direct_mode(iio_dev);
584 static int hts221_validate_trigger(struct iio_dev *iio_dev,
585 struct iio_trigger *trig)
587 struct hts221_hw *hw = iio_priv(iio_dev);
589 return hw->trig == trig ? 0 : -EINVAL;
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);
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,
605 static const struct attribute_group hts221_attribute_group = {
606 .attrs = hts221_attributes,
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,
617 static const unsigned long hts221_scan_masks[] = {0x3, 0x0};
619 int hts221_probe(struct iio_dev *iio_dev)
621 struct hts221_hw *hw = iio_priv(iio_dev);
625 mutex_init(&hw->lock);
627 err = hts221_check_whoami(hw);
631 hw->odr = hts221_odr_table[0].hz;
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;
641 /* enable Block Data Update */
642 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
647 /* configure humidity sensor */
648 err = hts221_parse_rh_caldata(hw);
650 dev_err(hw->dev, "failed to get rh calibration data\n");
654 data = hts221_avg_list[HTS221_SENSOR_H].avg_avl[3];
655 err = hts221_update_avg(hw, HTS221_SENSOR_H, data);
657 dev_err(hw->dev, "failed to set rh oversampling ratio\n");
661 /* configure temperature sensor */
662 err = hts221_parse_temp_caldata(hw);
665 "failed to get temperature calibration data\n");
669 data = hts221_avg_list[HTS221_SENSOR_T].avg_avl[3];
670 err = hts221_update_avg(hw, HTS221_SENSOR_T, data);
673 "failed to set temperature oversampling ratio\n");
678 err = hts221_allocate_buffers(hw);
682 err = hts221_allocate_trigger(hw);
687 return devm_iio_device_register(hw->dev, iio_dev);
689 EXPORT_SYMBOL(hts221_probe);
691 static int __maybe_unused hts221_suspend(struct device *dev)
693 struct iio_dev *iio_dev = dev_get_drvdata(dev);
694 struct hts221_hw *hw = iio_priv(iio_dev);
698 err = hw->tf->write(hw->dev, HTS221_REG_CNTRL1_ADDR, sizeof(data),
701 return err < 0 ? err : 0;
704 static int __maybe_unused hts221_resume(struct device *dev)
706 struct iio_dev *iio_dev = dev_get_drvdata(dev);
707 struct hts221_hw *hw = iio_priv(iio_dev);
711 err = hts221_update_odr(hw, hw->odr);
716 const struct dev_pm_ops hts221_pm_ops = {
717 SET_SYSTEM_SLEEP_PM_OPS(hts221_suspend, hts221_resume)
719 EXPORT_SYMBOL(hts221_pm_ops);
721 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
722 MODULE_DESCRIPTION("STMicroelectronics hts221 sensor driver");
723 MODULE_LICENSE("GPL v2");