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 /* enable Block Data Update */
207 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
212 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
213 HTS221_ODR_MASK, hts221_odr_table[i].val);
217 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
218 HTS221_ENABLE_MASK, 1);
227 static int hts221_update_avg(struct hts221_hw *hw,
228 enum hts221_sensor_type type,
232 const struct hts221_avg *avg = &hts221_avg_list[type];
234 for (i = 0; i < HTS221_AVG_DEPTH; i++)
235 if (avg->avg_avl[i] == val)
238 if (i == HTS221_AVG_DEPTH)
241 err = hts221_write_with_mask(hw, avg->addr, avg->mask, i);
245 hw->sensors[type].cur_avg_idx = i;
250 static ssize_t hts221_sysfs_sampling_freq(struct device *dev,
251 struct device_attribute *attr,
257 for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
258 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
259 hts221_odr_table[i].hz);
266 hts221_sysfs_rh_oversampling_avail(struct device *dev,
267 struct device_attribute *attr,
270 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_H];
274 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
275 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
283 hts221_sysfs_temp_oversampling_avail(struct device *dev,
284 struct device_attribute *attr,
287 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_T];
291 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
292 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
299 int hts221_power_on(struct hts221_hw *hw)
303 err = hts221_update_odr(hw, hw->odr);
312 int hts221_power_off(struct hts221_hw *hw)
317 err = hw->tf->write(hw->dev, HTS221_REG_CNTRL1_ADDR, sizeof(data),
327 static int hts221_parse_temp_caldata(struct hts221_hw *hw)
329 int err, *slope, *b_gen;
330 s16 cal_x0, cal_x1, cal_y0, cal_y1;
333 err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_Y_H,
334 sizeof(cal0), &cal0);
338 err = hw->tf->read(hw->dev, HTS221_REG_T1_T0_CAL_Y_H,
339 sizeof(cal1), &cal1);
342 cal_y0 = (le16_to_cpu(cal1 & 0x3) << 8) | cal0;
344 err = hw->tf->read(hw->dev, HTS221_REG_1T_CAL_Y_H,
345 sizeof(cal0), &cal0);
348 cal_y1 = (((cal1 & 0xc) >> 2) << 8) | cal0;
350 err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_X_L, sizeof(cal_x0),
354 cal_x0 = le16_to_cpu(cal_x0);
356 err = hw->tf->read(hw->dev, HTS221_REG_1T_CAL_X_L, sizeof(cal_x1),
360 cal_x1 = le16_to_cpu(cal_x1);
362 slope = &hw->sensors[HTS221_SENSOR_T].slope;
363 b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen;
365 *slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
366 *b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
373 static int hts221_parse_rh_caldata(struct hts221_hw *hw)
375 int err, *slope, *b_gen;
376 s16 cal_x0, cal_x1, cal_y0, cal_y1;
379 err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_Y_H, sizeof(data),
385 err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_Y_H, sizeof(data),
391 err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_X_H, sizeof(cal_x0),
395 cal_x0 = le16_to_cpu(cal_x0);
397 err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_X_H, sizeof(cal_x1),
401 cal_x1 = le16_to_cpu(cal_x1);
403 slope = &hw->sensors[HTS221_SENSOR_H].slope;
404 b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen;
406 *slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0);
407 *b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) /
414 static int hts221_get_sensor_scale(struct hts221_hw *hw,
415 enum iio_chan_type ch_type,
422 case IIO_HUMIDITYRELATIVE:
423 data = hw->sensors[HTS221_SENSOR_H].slope;
424 div = (1 << 4) * 1000;
427 data = hw->sensors[HTS221_SENSOR_T].slope;
428 div = (1 << 6) * 1000;
434 tmp = div_s64(data * 1000000000LL, div);
435 tmp = div_s64_rem(tmp, 1000000000LL, &rem);
440 return IIO_VAL_INT_PLUS_NANO;
443 static int hts221_get_sensor_offset(struct hts221_hw *hw,
444 enum iio_chan_type ch_type,
451 case IIO_HUMIDITYRELATIVE:
452 data = hw->sensors[HTS221_SENSOR_H].b_gen;
453 div = hw->sensors[HTS221_SENSOR_H].slope;
456 data = hw->sensors[HTS221_SENSOR_T].b_gen;
457 div = hw->sensors[HTS221_SENSOR_T].slope;
463 tmp = div_s64(data * 1000000000LL, div);
464 tmp = div_s64_rem(tmp, 1000000000LL, &rem);
469 return IIO_VAL_INT_PLUS_NANO;
472 static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
474 u8 data[HTS221_DATA_SIZE];
477 err = hts221_power_on(hw);
483 err = hw->tf->read(hw->dev, addr, sizeof(data), data);
487 hts221_power_off(hw);
489 *val = (s16)get_unaligned_le16(data);
494 static int hts221_read_raw(struct iio_dev *iio_dev,
495 struct iio_chan_spec const *ch,
496 int *val, int *val2, long mask)
498 struct hts221_hw *hw = iio_priv(iio_dev);
501 ret = iio_device_claim_direct_mode(iio_dev);
506 case IIO_CHAN_INFO_RAW:
507 ret = hts221_read_oneshot(hw, ch->address, val);
509 case IIO_CHAN_INFO_SCALE:
510 ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
512 case IIO_CHAN_INFO_OFFSET:
513 ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
515 case IIO_CHAN_INFO_SAMP_FREQ:
519 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
521 const struct hts221_avg *avg;
524 case IIO_HUMIDITYRELATIVE:
525 avg = &hts221_avg_list[HTS221_SENSOR_H];
526 idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx;
527 *val = avg->avg_avl[idx];
531 avg = &hts221_avg_list[HTS221_SENSOR_T];
532 idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
533 *val = avg->avg_avl[idx];
547 iio_device_release_direct_mode(iio_dev);
552 static int hts221_write_raw(struct iio_dev *iio_dev,
553 struct iio_chan_spec const *chan,
554 int val, int val2, long mask)
556 struct hts221_hw *hw = iio_priv(iio_dev);
559 ret = iio_device_claim_direct_mode(iio_dev);
564 case IIO_CHAN_INFO_SAMP_FREQ:
565 ret = hts221_update_odr(hw, val);
567 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
568 switch (chan->type) {
569 case IIO_HUMIDITYRELATIVE:
570 ret = hts221_update_avg(hw, HTS221_SENSOR_H, val);
573 ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
585 iio_device_release_direct_mode(iio_dev);
590 static int hts221_validate_trigger(struct iio_dev *iio_dev,
591 struct iio_trigger *trig)
593 struct hts221_hw *hw = iio_priv(iio_dev);
595 return hw->trig == trig ? 0 : -EINVAL;
598 static IIO_DEVICE_ATTR(in_humidity_oversampling_ratio_available, S_IRUGO,
599 hts221_sysfs_rh_oversampling_avail, NULL, 0);
600 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available, S_IRUGO,
601 hts221_sysfs_temp_oversampling_avail, NULL, 0);
602 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hts221_sysfs_sampling_freq);
604 static struct attribute *hts221_attributes[] = {
605 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
606 &iio_dev_attr_in_humidity_oversampling_ratio_available.dev_attr.attr,
607 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
611 static const struct attribute_group hts221_attribute_group = {
612 .attrs = hts221_attributes,
615 static const struct iio_info hts221_info = {
616 .driver_module = THIS_MODULE,
617 .attrs = &hts221_attribute_group,
618 .read_raw = hts221_read_raw,
619 .write_raw = hts221_write_raw,
620 .validate_trigger = hts221_validate_trigger,
623 static const unsigned long hts221_scan_masks[] = {0x3, 0x0};
625 int hts221_probe(struct iio_dev *iio_dev)
627 struct hts221_hw *hw = iio_priv(iio_dev);
631 mutex_init(&hw->lock);
633 err = hts221_check_whoami(hw);
637 hw->odr = hts221_odr_table[0].hz;
639 iio_dev->modes = INDIO_DIRECT_MODE;
640 iio_dev->dev.parent = hw->dev;
641 iio_dev->available_scan_masks = hts221_scan_masks;
642 iio_dev->channels = hts221_channels;
643 iio_dev->num_channels = ARRAY_SIZE(hts221_channels);
644 iio_dev->name = HTS221_DEV_NAME;
645 iio_dev->info = &hts221_info;
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");