Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / humidity / hts221_core.c
blobd3f7904766bd3f477343c91d45d3280e8c23ac1c
1 /*
2 * STMicroelectronics hts221 sensor driver
4 * Copyright 2016 STMicroelectronics Inc.
6 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
8 * Licensed under the GPL-2.
9 */
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 <linux/pm.h>
17 #include <asm/unaligned.h>
19 #include "hts221.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
49 struct hts221_odr {
50 u8 hz;
51 u8 val;
54 #define HTS221_AVG_DEPTH 8
55 struct hts221_avg {
56 u8 addr;
57 u8 mask;
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,
71 .avg_avl = {
72 4, /* 0.4 %RH */
73 8, /* 0.3 %RH */
74 16, /* 0.2 %RH */
75 32, /* 0.15 %RH */
76 64, /* 0.1 %RH */
77 128, /* 0.07 %RH */
78 256, /* 0.05 %RH */
79 512, /* 0.03 %RH */
83 .addr = HTS221_REG_AVG_ADDR,
84 .mask = HTS221_TEMP_AVG_MASK,
85 .avg_avl = {
86 2, /* 0.08 degC */
87 4, /* 0.05 degC */
88 8, /* 0.04 degC */
89 16, /* 0.03 degC */
90 32, /* 0.02 degC */
91 64, /* 0.015 degC */
92 128, /* 0.01 degC */
93 256, /* 0.007 degC */
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),
107 .scan_index = 0,
108 .scan_type = {
109 .sign = 's',
110 .realbits = 16,
111 .storagebits = 16,
112 .endianness = IIO_LE,
116 .type = IIO_TEMP,
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),
123 .scan_index = 1,
124 .scan_type = {
125 .sign = 's',
126 .realbits = 16,
127 .storagebits = 16,
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)
136 u8 data;
137 int err;
139 mutex_lock(&hw->lock);
141 err = hw->tf->read(hw->dev, addr, sizeof(data), &data);
142 if (err < 0) {
143 dev_err(hw->dev, "failed to read %02x register\n", addr);
144 goto unlock;
147 data = (data & ~mask) | ((val << __ffs(mask)) & mask);
149 err = hw->tf->write(hw->dev, addr, sizeof(data), &data);
150 if (err < 0)
151 dev_err(hw->dev, "failed to write %02x register\n", addr);
153 unlock:
154 mutex_unlock(&hw->lock);
156 return err;
159 static int hts221_check_whoami(struct hts221_hw *hw)
161 u8 data;
162 int err;
164 err = hw->tf->read(hw->dev, HTS221_REG_WHOAMI_ADDR, sizeof(data),
165 &data);
166 if (err < 0) {
167 dev_err(hw->dev, "failed to read whoami register\n");
168 return err;
171 if (data != HTS221_REG_WHOAMI_VAL) {
172 dev_err(hw->dev, "wrong whoami {%02x vs %02x}\n",
173 data, HTS221_REG_WHOAMI_VAL);
174 return -ENODEV;
177 return 0;
180 static int hts221_update_odr(struct hts221_hw *hw, u8 odr)
182 int i, err;
184 for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++)
185 if (hts221_odr_table[i].hz == odr)
186 break;
188 if (i == ARRAY_SIZE(hts221_odr_table))
189 return -EINVAL;
191 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
192 HTS221_ODR_MASK, hts221_odr_table[i].val);
193 if (err < 0)
194 return err;
196 hw->odr = odr;
198 return 0;
201 static int hts221_update_avg(struct hts221_hw *hw,
202 enum hts221_sensor_type type,
203 u16 val)
205 int i, err;
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)
210 break;
212 if (i == HTS221_AVG_DEPTH)
213 return -EINVAL;
215 err = hts221_write_with_mask(hw, avg->addr, avg->mask, i);
216 if (err < 0)
217 return err;
219 hw->sensors[type].cur_avg_idx = i;
221 return 0;
224 static ssize_t hts221_sysfs_sampling_freq(struct device *dev,
225 struct device_attribute *attr,
226 char *buf)
228 int i;
229 ssize_t len = 0;
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);
234 buf[len - 1] = '\n';
236 return len;
239 static ssize_t
240 hts221_sysfs_rh_oversampling_avail(struct device *dev,
241 struct device_attribute *attr,
242 char *buf)
244 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_H];
245 ssize_t len = 0;
246 int i;
248 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
249 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
250 avg->avg_avl[i]);
251 buf[len - 1] = '\n';
253 return len;
256 static ssize_t
257 hts221_sysfs_temp_oversampling_avail(struct device *dev,
258 struct device_attribute *attr,
259 char *buf)
261 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_T];
262 ssize_t len = 0;
263 int i;
265 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++)
266 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
267 avg->avg_avl[i]);
268 buf[len - 1] = '\n';
270 return len;
273 int hts221_set_enable(struct hts221_hw *hw, bool enable)
275 int err;
277 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
278 HTS221_ENABLE_MASK, enable);
279 if (err < 0)
280 return err;
282 hw->enabled = enable;
284 return 0;
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;
291 u8 cal0, cal1;
293 err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_Y_H,
294 sizeof(cal0), &cal0);
295 if (err < 0)
296 return err;
298 err = hw->tf->read(hw->dev, HTS221_REG_T1_T0_CAL_Y_H,
299 sizeof(cal1), &cal1);
300 if (err < 0)
301 return err;
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);
306 if (err < 0)
307 return err;
308 cal_y1 = (((cal1 & 0xc) >> 2) << 8) | cal0;
310 err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_X_L, sizeof(cal_x0),
311 (u8 *)&cal_x0);
312 if (err < 0)
313 return err;
314 cal_x0 = le16_to_cpu(cal_x0);
316 err = hw->tf->read(hw->dev, HTS221_REG_1T_CAL_X_L, sizeof(cal_x1),
317 (u8 *)&cal_x1);
318 if (err < 0)
319 return err;
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) /
327 (cal_x1 - cal_x0);
328 *b_gen *= 8;
330 return 0;
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;
337 u8 data;
339 err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_Y_H, sizeof(data),
340 &data);
341 if (err < 0)
342 return err;
343 cal_y0 = data;
345 err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_Y_H, sizeof(data),
346 &data);
347 if (err < 0)
348 return err;
349 cal_y1 = data;
351 err = hw->tf->read(hw->dev, HTS221_REG_0RH_CAL_X_H, sizeof(cal_x0),
352 (u8 *)&cal_x0);
353 if (err < 0)
354 return err;
355 cal_x0 = le16_to_cpu(cal_x0);
357 err = hw->tf->read(hw->dev, HTS221_REG_1RH_CAL_X_H, sizeof(cal_x1),
358 (u8 *)&cal_x1);
359 if (err < 0)
360 return err;
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) /
368 (cal_x1 - cal_x0);
369 *b_gen *= 8;
371 return 0;
374 static int hts221_get_sensor_scale(struct hts221_hw *hw,
375 enum iio_chan_type ch_type,
376 int *val, int *val2)
378 s64 tmp;
379 s32 rem, div, data;
381 switch (ch_type) {
382 case IIO_HUMIDITYRELATIVE:
383 data = hw->sensors[HTS221_SENSOR_H].slope;
384 div = (1 << 4) * 1000;
385 break;
386 case IIO_TEMP:
387 data = hw->sensors[HTS221_SENSOR_T].slope;
388 div = (1 << 6) * 1000;
389 break;
390 default:
391 return -EINVAL;
394 tmp = div_s64(data * 1000000000LL, div);
395 tmp = div_s64_rem(tmp, 1000000000LL, &rem);
397 *val = tmp;
398 *val2 = 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,
405 int *val, int *val2)
407 s64 tmp;
408 s32 rem, div, data;
410 switch (ch_type) {
411 case IIO_HUMIDITYRELATIVE:
412 data = hw->sensors[HTS221_SENSOR_H].b_gen;
413 div = hw->sensors[HTS221_SENSOR_H].slope;
414 break;
415 case IIO_TEMP:
416 data = hw->sensors[HTS221_SENSOR_T].b_gen;
417 div = hw->sensors[HTS221_SENSOR_T].slope;
418 break;
419 default:
420 return -EINVAL;
423 tmp = div_s64(data * 1000000000LL, div);
424 tmp = div_s64_rem(tmp, 1000000000LL, &rem);
426 *val = tmp;
427 *val2 = 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];
435 int err;
437 err = hts221_set_enable(hw, true);
438 if (err < 0)
439 return err;
441 msleep(50);
443 err = hw->tf->read(hw->dev, addr, sizeof(data), data);
444 if (err < 0)
445 return err;
447 hts221_set_enable(hw, false);
449 *val = (s16)get_unaligned_le16(data);
451 return IIO_VAL_INT;
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);
459 int ret;
461 ret = iio_device_claim_direct_mode(iio_dev);
462 if (ret)
463 return ret;
465 switch (mask) {
466 case IIO_CHAN_INFO_RAW:
467 ret = hts221_read_oneshot(hw, ch->address, val);
468 break;
469 case IIO_CHAN_INFO_SCALE:
470 ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
471 break;
472 case IIO_CHAN_INFO_OFFSET:
473 ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
474 break;
475 case IIO_CHAN_INFO_SAMP_FREQ:
476 *val = hw->odr;
477 ret = IIO_VAL_INT;
478 break;
479 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
480 u8 idx;
481 const struct hts221_avg *avg;
483 switch (ch->type) {
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];
488 ret = IIO_VAL_INT;
489 break;
490 case IIO_TEMP:
491 avg = &hts221_avg_list[HTS221_SENSOR_T];
492 idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
493 *val = avg->avg_avl[idx];
494 ret = IIO_VAL_INT;
495 break;
496 default:
497 ret = -EINVAL;
498 break;
500 break;
502 default:
503 ret = -EINVAL;
504 break;
507 iio_device_release_direct_mode(iio_dev);
509 return ret;
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);
517 int ret;
519 ret = iio_device_claim_direct_mode(iio_dev);
520 if (ret)
521 return ret;
523 switch (mask) {
524 case IIO_CHAN_INFO_SAMP_FREQ:
525 ret = hts221_update_odr(hw, val);
526 break;
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);
531 break;
532 case IIO_TEMP:
533 ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
534 break;
535 default:
536 ret = -EINVAL;
537 break;
539 break;
540 default:
541 ret = -EINVAL;
542 break;
545 iio_device_release_direct_mode(iio_dev);
547 return ret;
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,
568 NULL,
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;
589 int err;
590 u8 data;
592 iio_dev = devm_iio_device_alloc(dev, sizeof(*hw));
593 if (!iio_dev)
594 return -ENOMEM;
596 dev_set_drvdata(dev, (void *)iio_dev);
598 hw = iio_priv(iio_dev);
599 hw->name = name;
600 hw->dev = dev;
601 hw->irq = irq;
602 hw->tf = tf_ops;
604 mutex_init(&hw->lock);
606 err = hts221_check_whoami(hw);
607 if (err < 0)
608 return err;
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,
620 HTS221_BDU_MASK, 1);
621 if (err < 0)
622 return err;
624 err = hts221_update_odr(hw, hts221_odr_table[0].hz);
625 if (err < 0)
626 return err;
628 /* configure humidity sensor */
629 err = hts221_parse_rh_caldata(hw);
630 if (err < 0) {
631 dev_err(hw->dev, "failed to get rh calibration data\n");
632 return err;
635 data = hts221_avg_list[HTS221_SENSOR_H].avg_avl[3];
636 err = hts221_update_avg(hw, HTS221_SENSOR_H, data);
637 if (err < 0) {
638 dev_err(hw->dev, "failed to set rh oversampling ratio\n");
639 return err;
642 /* configure temperature sensor */
643 err = hts221_parse_temp_caldata(hw);
644 if (err < 0) {
645 dev_err(hw->dev,
646 "failed to get temperature calibration data\n");
647 return err;
650 data = hts221_avg_list[HTS221_SENSOR_T].avg_avl[3];
651 err = hts221_update_avg(hw, HTS221_SENSOR_T, data);
652 if (err < 0) {
653 dev_err(hw->dev,
654 "failed to set temperature oversampling ratio\n");
655 return err;
658 if (hw->irq > 0) {
659 err = hts221_allocate_buffers(hw);
660 if (err < 0)
661 return err;
663 err = hts221_allocate_trigger(hw);
664 if (err)
665 return err;
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);
676 int err;
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);
688 int err = 0;
690 if (hw->enabled)
691 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR,
692 HTS221_ENABLE_MASK, true);
694 return err;
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");