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
27 #define HTS221_REG_AVG_ADDR 0x10
28 #define HTS221_REG_H_OUT_L 0x28
29 #define HTS221_REG_T_OUT_L 0x2a
31 #define HTS221_HUMIDITY_AVG_MASK 0x07
32 #define HTS221_TEMP_AVG_MASK 0x38
34 #define HTS221_ODR_MASK 0x03
35 #define HTS221_BDU_MASK BIT(2)
36 #define HTS221_ENABLE_MASK BIT(7)
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
54 #define HTS221_AVG_DEPTH 8
58 u16 avg_avl
[HTS221_AVG_DEPTH
];
61 static const struct hts221_odr hts221_odr_table
[] = {
62 { 1, 0x01 }, /* 1Hz */
63 { 7, 0x02 }, /* 7Hz */
64 { 13, 0x03 }, /* 12.5Hz */
67 static const struct hts221_avg hts221_avg_list
[] = {
69 .addr
= HTS221_REG_AVG_ADDR
,
70 .mask
= HTS221_HUMIDITY_AVG_MASK
,
83 .addr
= HTS221_REG_AVG_ADDR
,
84 .mask
= HTS221_TEMP_AVG_MASK
,
98 static const struct iio_chan_spec hts221_channels
[] = {
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
),
112 .endianness
= IIO_LE
,
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
),
128 .endianness
= IIO_LE
,
131 IIO_CHAN_SOFT_TIMESTAMP(2),
134 int hts221_write_with_mask(struct hts221_hw
*hw
, u8 addr
, u8 mask
, u8 val
)
139 mutex_lock(&hw
->lock
);
141 err
= hw
->tf
->read(hw
->dev
, addr
, sizeof(data
), &data
);
143 dev_err(hw
->dev
, "failed to read %02x register\n", addr
);
147 data
= (data
& ~mask
) | ((val
<< __ffs(mask
)) & mask
);
149 err
= hw
->tf
->write(hw
->dev
, addr
, sizeof(data
), &data
);
151 dev_err(hw
->dev
, "failed to write %02x register\n", addr
);
154 mutex_unlock(&hw
->lock
);
159 static int hts221_check_whoami(struct hts221_hw
*hw
)
164 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_WHOAMI_ADDR
, sizeof(data
),
167 dev_err(hw
->dev
, "failed to read whoami register\n");
171 if (data
!= HTS221_REG_WHOAMI_VAL
) {
172 dev_err(hw
->dev
, "wrong whoami {%02x vs %02x}\n",
173 data
, HTS221_REG_WHOAMI_VAL
);
180 static int hts221_update_odr(struct hts221_hw
*hw
, u8 odr
)
184 for (i
= 0; i
< ARRAY_SIZE(hts221_odr_table
); i
++)
185 if (hts221_odr_table
[i
].hz
== odr
)
188 if (i
== ARRAY_SIZE(hts221_odr_table
))
191 err
= hts221_write_with_mask(hw
, HTS221_REG_CNTRL1_ADDR
,
192 HTS221_ODR_MASK
, hts221_odr_table
[i
].val
);
201 static int hts221_update_avg(struct hts221_hw
*hw
,
202 enum hts221_sensor_type type
,
206 const struct hts221_avg
*avg
= &hts221_avg_list
[type
];
208 for (i
= 0; i
< HTS221_AVG_DEPTH
; i
++)
209 if (avg
->avg_avl
[i
] == val
)
212 if (i
== HTS221_AVG_DEPTH
)
215 err
= hts221_write_with_mask(hw
, avg
->addr
, avg
->mask
, i
);
219 hw
->sensors
[type
].cur_avg_idx
= i
;
224 static ssize_t
hts221_sysfs_sampling_freq(struct device
*dev
,
225 struct device_attribute
*attr
,
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
);
240 hts221_sysfs_rh_oversampling_avail(struct device
*dev
,
241 struct device_attribute
*attr
,
244 const struct hts221_avg
*avg
= &hts221_avg_list
[HTS221_SENSOR_H
];
248 for (i
= 0; i
< ARRAY_SIZE(avg
->avg_avl
); i
++)
249 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
257 hts221_sysfs_temp_oversampling_avail(struct device
*dev
,
258 struct device_attribute
*attr
,
261 const struct hts221_avg
*avg
= &hts221_avg_list
[HTS221_SENSOR_T
];
265 for (i
= 0; i
< ARRAY_SIZE(avg
->avg_avl
); i
++)
266 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
273 int hts221_set_enable(struct hts221_hw
*hw
, bool enable
)
277 err
= hts221_write_with_mask(hw
, HTS221_REG_CNTRL1_ADDR
,
278 HTS221_ENABLE_MASK
, enable
);
282 hw
->enabled
= enable
;
287 static int hts221_parse_temp_caldata(struct hts221_hw
*hw
)
289 int err
, *slope
, *b_gen
;
290 s16 cal_x0
, cal_x1
, cal_y0
, cal_y1
;
293 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_0T_CAL_Y_H
,
294 sizeof(cal0
), &cal0
);
298 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_T1_T0_CAL_Y_H
,
299 sizeof(cal1
), &cal1
);
302 cal_y0
= (le16_to_cpu(cal1
& 0x3) << 8) | cal0
;
304 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_1T_CAL_Y_H
,
305 sizeof(cal0
), &cal0
);
308 cal_y1
= (((cal1
& 0xc) >> 2) << 8) | cal0
;
310 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_0T_CAL_X_L
, sizeof(cal_x0
),
314 cal_x0
= le16_to_cpu(cal_x0
);
316 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_1T_CAL_X_L
, sizeof(cal_x1
),
320 cal_x1
= le16_to_cpu(cal_x1
);
322 slope
= &hw
->sensors
[HTS221_SENSOR_T
].slope
;
323 b_gen
= &hw
->sensors
[HTS221_SENSOR_T
].b_gen
;
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) /
333 static int hts221_parse_rh_caldata(struct hts221_hw
*hw
)
335 int err
, *slope
, *b_gen
;
336 s16 cal_x0
, cal_x1
, cal_y0
, cal_y1
;
339 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_0RH_CAL_Y_H
, sizeof(data
),
345 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_1RH_CAL_Y_H
, sizeof(data
),
351 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_0RH_CAL_X_H
, sizeof(cal_x0
),
355 cal_x0
= le16_to_cpu(cal_x0
);
357 err
= hw
->tf
->read(hw
->dev
, HTS221_REG_1RH_CAL_X_H
, sizeof(cal_x1
),
361 cal_x1
= le16_to_cpu(cal_x1
);
363 slope
= &hw
->sensors
[HTS221_SENSOR_H
].slope
;
364 b_gen
= &hw
->sensors
[HTS221_SENSOR_H
].b_gen
;
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) /
374 static int hts221_get_sensor_scale(struct hts221_hw
*hw
,
375 enum iio_chan_type ch_type
,
382 case IIO_HUMIDITYRELATIVE
:
383 data
= hw
->sensors
[HTS221_SENSOR_H
].slope
;
384 div
= (1 << 4) * 1000;
387 data
= hw
->sensors
[HTS221_SENSOR_T
].slope
;
388 div
= (1 << 6) * 1000;
394 tmp
= div_s64(data
* 1000000000LL, div
);
395 tmp
= div_s64_rem(tmp
, 1000000000LL, &rem
);
400 return IIO_VAL_INT_PLUS_NANO
;
403 static int hts221_get_sensor_offset(struct hts221_hw
*hw
,
404 enum iio_chan_type ch_type
,
411 case IIO_HUMIDITYRELATIVE
:
412 data
= hw
->sensors
[HTS221_SENSOR_H
].b_gen
;
413 div
= hw
->sensors
[HTS221_SENSOR_H
].slope
;
416 data
= hw
->sensors
[HTS221_SENSOR_T
].b_gen
;
417 div
= hw
->sensors
[HTS221_SENSOR_T
].slope
;
423 tmp
= div_s64(data
* 1000000000LL, div
);
424 tmp
= div_s64_rem(tmp
, 1000000000LL, &rem
);
429 return IIO_VAL_INT_PLUS_NANO
;
432 static int hts221_read_oneshot(struct hts221_hw
*hw
, u8 addr
, int *val
)
434 u8 data
[HTS221_DATA_SIZE
];
437 err
= hts221_set_enable(hw
, true);
443 err
= hw
->tf
->read(hw
->dev
, addr
, sizeof(data
), data
);
447 hts221_set_enable(hw
, false);
449 *val
= (s16
)get_unaligned_le16(data
);
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
)
458 struct hts221_hw
*hw
= iio_priv(iio_dev
);
461 ret
= iio_device_claim_direct_mode(iio_dev
);
466 case IIO_CHAN_INFO_RAW
:
467 ret
= hts221_read_oneshot(hw
, ch
->address
, val
);
469 case IIO_CHAN_INFO_SCALE
:
470 ret
= hts221_get_sensor_scale(hw
, ch
->type
, val
, val2
);
472 case IIO_CHAN_INFO_OFFSET
:
473 ret
= hts221_get_sensor_offset(hw
, ch
->type
, val
, val2
);
475 case IIO_CHAN_INFO_SAMP_FREQ
:
479 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
: {
481 const struct hts221_avg
*avg
;
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
];
491 avg
= &hts221_avg_list
[HTS221_SENSOR_T
];
492 idx
= hw
->sensors
[HTS221_SENSOR_T
].cur_avg_idx
;
493 *val
= avg
->avg_avl
[idx
];
507 iio_device_release_direct_mode(iio_dev
);
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
)
516 struct hts221_hw
*hw
= iio_priv(iio_dev
);
519 ret
= iio_device_claim_direct_mode(iio_dev
);
524 case IIO_CHAN_INFO_SAMP_FREQ
:
525 ret
= hts221_update_odr(hw
, val
);
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
);
533 ret
= hts221_update_avg(hw
, HTS221_SENSOR_T
, val
);
545 iio_device_release_direct_mode(iio_dev
);
550 static int hts221_validate_trigger(struct iio_dev
*iio_dev
,
551 struct iio_trigger
*trig
)
553 struct hts221_hw
*hw
= iio_priv(iio_dev
);
555 return hw
->trig
== trig
? 0 : -EINVAL
;
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
);
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
,
571 static const struct attribute_group hts221_attribute_group
= {
572 .attrs
= hts221_attributes
,
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
,
582 static const unsigned long hts221_scan_masks
[] = {0x3, 0x0};
584 int hts221_probe(struct device
*dev
, int irq
, const char *name
,
585 const struct hts221_transfer_function
*tf_ops
)
587 struct iio_dev
*iio_dev
;
588 struct hts221_hw
*hw
;
592 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*hw
));
596 dev_set_drvdata(dev
, (void *)iio_dev
);
598 hw
= iio_priv(iio_dev
);
604 mutex_init(&hw
->lock
);
606 err
= hts221_check_whoami(hw
);
610 iio_dev
->modes
= INDIO_DIRECT_MODE
;
611 iio_dev
->dev
.parent
= hw
->dev
;
612 iio_dev
->available_scan_masks
= hts221_scan_masks
;
613 iio_dev
->channels
= hts221_channels
;
614 iio_dev
->num_channels
= ARRAY_SIZE(hts221_channels
);
615 iio_dev
->name
= HTS221_DEV_NAME
;
616 iio_dev
->info
= &hts221_info
;
618 /* enable Block Data Update */
619 err
= hts221_write_with_mask(hw
, HTS221_REG_CNTRL1_ADDR
,
624 err
= hts221_update_odr(hw
, hts221_odr_table
[0].hz
);
628 /* configure humidity sensor */
629 err
= hts221_parse_rh_caldata(hw
);
631 dev_err(hw
->dev
, "failed to get rh calibration data\n");
635 data
= hts221_avg_list
[HTS221_SENSOR_H
].avg_avl
[3];
636 err
= hts221_update_avg(hw
, HTS221_SENSOR_H
, data
);
638 dev_err(hw
->dev
, "failed to set rh oversampling ratio\n");
642 /* configure temperature sensor */
643 err
= hts221_parse_temp_caldata(hw
);
646 "failed to get temperature calibration data\n");
650 data
= hts221_avg_list
[HTS221_SENSOR_T
].avg_avl
[3];
651 err
= hts221_update_avg(hw
, HTS221_SENSOR_T
, data
);
654 "failed to set temperature oversampling ratio\n");
659 err
= hts221_allocate_buffers(hw
);
663 err
= hts221_allocate_trigger(hw
);
668 return devm_iio_device_register(hw
->dev
, iio_dev
);
670 EXPORT_SYMBOL(hts221_probe
);
672 static int __maybe_unused
hts221_suspend(struct device
*dev
)
674 struct iio_dev
*iio_dev
= dev_get_drvdata(dev
);
675 struct hts221_hw
*hw
= iio_priv(iio_dev
);
678 err
= hts221_write_with_mask(hw
, HTS221_REG_CNTRL1_ADDR
,
679 HTS221_ENABLE_MASK
, false);
681 return err
< 0 ? err
: 0;
684 static int __maybe_unused
hts221_resume(struct device
*dev
)
686 struct iio_dev
*iio_dev
= dev_get_drvdata(dev
);
687 struct hts221_hw
*hw
= iio_priv(iio_dev
);
691 err
= hts221_write_with_mask(hw
, HTS221_REG_CNTRL1_ADDR
,
692 HTS221_ENABLE_MASK
, true);
697 const struct dev_pm_ops hts221_pm_ops
= {
698 SET_SYSTEM_SLEEP_PM_OPS(hts221_suspend
, hts221_resume
)
700 EXPORT_SYMBOL(hts221_pm_ops
);
702 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
703 MODULE_DESCRIPTION("STMicroelectronics hts221 sensor driver");
704 MODULE_LICENSE("GPL v2");