1 /* 2 * STMicroelectronics hts221 sensor driver 3 * 4 * Copyright 2016 STMicroelectronics Inc. 5 * 6 * Lorenzo Bianconi <lorenzo.bianconi@st.com> 7 * 8 * Licensed under the GPL-2. 9 */ 10 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 <linux/regmap.h> 18 #include <linux/bitfield.h> 19 20 #include "hts221.h" 21 22 #define HTS221_REG_WHOAMI_ADDR 0x0f 23 #define HTS221_REG_WHOAMI_VAL 0xbc 24 25 #define HTS221_REG_CNTRL1_ADDR 0x20 26 #define HTS221_REG_CNTRL2_ADDR 0x21 27 28 #define HTS221_REG_AVG_ADDR 0x10 29 #define HTS221_REG_H_OUT_L 0x28 30 #define HTS221_REG_T_OUT_L 0x2a 31 32 #define HTS221_HUMIDITY_AVG_MASK 0x07 33 #define HTS221_TEMP_AVG_MASK 0x38 34 35 #define HTS221_ODR_MASK 0x03 36 #define HTS221_BDU_MASK BIT(2) 37 #define HTS221_ENABLE_MASK BIT(7) 38 39 /* calibration registers */ 40 #define HTS221_REG_0RH_CAL_X_H 0x36 41 #define HTS221_REG_1RH_CAL_X_H 0x3a 42 #define HTS221_REG_0RH_CAL_Y_H 0x30 43 #define HTS221_REG_1RH_CAL_Y_H 0x31 44 #define HTS221_REG_0T_CAL_X_L 0x3c 45 #define HTS221_REG_1T_CAL_X_L 0x3e 46 #define HTS221_REG_0T_CAL_Y_H 0x32 47 #define HTS221_REG_1T_CAL_Y_H 0x33 48 #define HTS221_REG_T1_T0_CAL_Y_H 0x35 49 50 struct hts221_odr { 51 u8 hz; 52 u8 val; 53 }; 54 55 #define HTS221_AVG_DEPTH 8 56 struct hts221_avg { 57 u8 addr; 58 u8 mask; 59 u16 avg_avl[HTS221_AVG_DEPTH]; 60 }; 61 62 static const struct hts221_odr hts221_odr_table[] = { 63 { 1, 0x01 }, /* 1Hz */ 64 { 7, 0x02 }, /* 7Hz */ 65 { 13, 0x03 }, /* 12.5Hz */ 66 }; 67 68 static const struct hts221_avg hts221_avg_list[] = { 69 { 70 .addr = HTS221_REG_AVG_ADDR, 71 .mask = HTS221_HUMIDITY_AVG_MASK, 72 .avg_avl = { 73 4, /* 0.4 %RH */ 74 8, /* 0.3 %RH */ 75 16, /* 0.2 %RH */ 76 32, /* 0.15 %RH */ 77 64, /* 0.1 %RH */ 78 128, /* 0.07 %RH */ 79 256, /* 0.05 %RH */ 80 512, /* 0.03 %RH */ 81 }, 82 }, 83 { 84 .addr = HTS221_REG_AVG_ADDR, 85 .mask = HTS221_TEMP_AVG_MASK, 86 .avg_avl = { 87 2, /* 0.08 degC */ 88 4, /* 0.05 degC */ 89 8, /* 0.04 degC */ 90 16, /* 0.03 degC */ 91 32, /* 0.02 degC */ 92 64, /* 0.015 degC */ 93 128, /* 0.01 degC */ 94 256, /* 0.007 degC */ 95 }, 96 }, 97 }; 98 99 static const struct iio_chan_spec hts221_channels[] = { 100 { 101 .type = IIO_HUMIDITYRELATIVE, 102 .address = HTS221_REG_H_OUT_L, 103 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 104 BIT(IIO_CHAN_INFO_OFFSET) | 105 BIT(IIO_CHAN_INFO_SCALE) | 106 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 107 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 108 .scan_index = 0, 109 .scan_type = { 110 .sign = 's', 111 .realbits = 16, 112 .storagebits = 16, 113 .endianness = IIO_LE, 114 }, 115 }, 116 { 117 .type = IIO_TEMP, 118 .address = HTS221_REG_T_OUT_L, 119 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 120 BIT(IIO_CHAN_INFO_OFFSET) | 121 BIT(IIO_CHAN_INFO_SCALE) | 122 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 123 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 124 .scan_index = 1, 125 .scan_type = { 126 .sign = 's', 127 .realbits = 16, 128 .storagebits = 16, 129 .endianness = IIO_LE, 130 }, 131 }, 132 IIO_CHAN_SOFT_TIMESTAMP(2), 133 }; 134 135 static int hts221_check_whoami(struct hts221_hw *hw) 136 { 137 int err, data; 138 139 err = regmap_read(hw->regmap, HTS221_REG_WHOAMI_ADDR, &data); 140 if (err < 0) { 141 dev_err(hw->dev, "failed to read whoami register\n"); 142 return err; 143 } 144 145 if (data != HTS221_REG_WHOAMI_VAL) { 146 dev_err(hw->dev, "wrong whoami {%02x vs %02x}\n", 147 data, HTS221_REG_WHOAMI_VAL); 148 return -ENODEV; 149 } 150 151 return 0; 152 } 153 154 static int hts221_update_odr(struct hts221_hw *hw, u8 odr) 155 { 156 int i, err; 157 158 for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++) 159 if (hts221_odr_table[i].hz == odr) 160 break; 161 162 if (i == ARRAY_SIZE(hts221_odr_table)) 163 return -EINVAL; 164 165 err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR, 166 HTS221_ODR_MASK, 167 FIELD_PREP(HTS221_ODR_MASK, 168 hts221_odr_table[i].val)); 169 if (err < 0) 170 return err; 171 172 hw->odr = odr; 173 174 return 0; 175 } 176 177 static int hts221_update_avg(struct hts221_hw *hw, 178 enum hts221_sensor_type type, 179 u16 val) 180 { 181 const struct hts221_avg *avg = &hts221_avg_list[type]; 182 int i, err, data; 183 184 for (i = 0; i < HTS221_AVG_DEPTH; i++) 185 if (avg->avg_avl[i] == val) 186 break; 187 188 if (i == HTS221_AVG_DEPTH) 189 return -EINVAL; 190 191 data = ((i << __ffs(avg->mask)) & avg->mask); 192 err = regmap_update_bits(hw->regmap, avg->addr, 193 avg->mask, data); 194 if (err < 0) 195 return err; 196 197 hw->sensors[type].cur_avg_idx = i; 198 199 return 0; 200 } 201 202 static ssize_t hts221_sysfs_sampling_freq(struct device *dev, 203 struct device_attribute *attr, 204 char *buf) 205 { 206 int i; 207 ssize_t len = 0; 208 209 for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++) 210 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", 211 hts221_odr_table[i].hz); 212 buf[len - 1] = '\n'; 213 214 return len; 215 } 216 217 static ssize_t 218 hts221_sysfs_rh_oversampling_avail(struct device *dev, 219 struct device_attribute *attr, 220 char *buf) 221 { 222 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_H]; 223 ssize_t len = 0; 224 int i; 225 226 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++) 227 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", 228 avg->avg_avl[i]); 229 buf[len - 1] = '\n'; 230 231 return len; 232 } 233 234 static ssize_t 235 hts221_sysfs_temp_oversampling_avail(struct device *dev, 236 struct device_attribute *attr, 237 char *buf) 238 { 239 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_T]; 240 ssize_t len = 0; 241 int i; 242 243 for (i = 0; i < ARRAY_SIZE(avg->avg_avl); i++) 244 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", 245 avg->avg_avl[i]); 246 buf[len - 1] = '\n'; 247 248 return len; 249 } 250 251 int hts221_set_enable(struct hts221_hw *hw, bool enable) 252 { 253 int err; 254 255 err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR, 256 HTS221_ENABLE_MASK, 257 FIELD_PREP(HTS221_ENABLE_MASK, enable)); 258 if (err < 0) 259 return err; 260 261 hw->enabled = enable; 262 263 return 0; 264 } 265 266 static int hts221_parse_temp_caldata(struct hts221_hw *hw) 267 { 268 int err, *slope, *b_gen, cal0, cal1; 269 s16 cal_x0, cal_x1, cal_y0, cal_y1; 270 __le16 val; 271 272 err = regmap_read(hw->regmap, HTS221_REG_0T_CAL_Y_H, &cal0); 273 if (err < 0) 274 return err; 275 276 err = regmap_read(hw->regmap, HTS221_REG_T1_T0_CAL_Y_H, &cal1); 277 if (err < 0) 278 return err; 279 cal_y0 = ((cal1 & 0x3) << 8) | cal0; 280 281 err = regmap_read(hw->regmap, HTS221_REG_1T_CAL_Y_H, &cal0); 282 if (err < 0) 283 return err; 284 cal_y1 = (((cal1 & 0xc) >> 2) << 8) | cal0; 285 286 err = regmap_bulk_read(hw->regmap, HTS221_REG_0T_CAL_X_L, 287 &val, sizeof(val)); 288 if (err < 0) 289 return err; 290 cal_x0 = le16_to_cpu(val); 291 292 err = regmap_bulk_read(hw->regmap, HTS221_REG_1T_CAL_X_L, 293 &val, sizeof(val)); 294 if (err < 0) 295 return err; 296 cal_x1 = le16_to_cpu(val); 297 298 slope = &hw->sensors[HTS221_SENSOR_T].slope; 299 b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen; 300 301 *slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0); 302 *b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) / 303 (cal_x1 - cal_x0); 304 *b_gen *= 8; 305 306 return 0; 307 } 308 309 static int hts221_parse_rh_caldata(struct hts221_hw *hw) 310 { 311 int err, *slope, *b_gen, data; 312 s16 cal_x0, cal_x1, cal_y0, cal_y1; 313 __le16 val; 314 315 err = regmap_read(hw->regmap, HTS221_REG_0RH_CAL_Y_H, &data); 316 if (err < 0) 317 return err; 318 cal_y0 = data; 319 320 err = regmap_read(hw->regmap, HTS221_REG_1RH_CAL_Y_H, &data); 321 if (err < 0) 322 return err; 323 cal_y1 = data; 324 325 err = regmap_bulk_read(hw->regmap, HTS221_REG_0RH_CAL_X_H, 326 &val, sizeof(val)); 327 if (err < 0) 328 return err; 329 cal_x0 = le16_to_cpu(val); 330 331 err = regmap_bulk_read(hw->regmap, HTS221_REG_1RH_CAL_X_H, 332 &val, sizeof(val)); 333 if (err < 0) 334 return err; 335 cal_x1 = le16_to_cpu(val); 336 337 slope = &hw->sensors[HTS221_SENSOR_H].slope; 338 b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen; 339 340 *slope = ((cal_y1 - cal_y0) * 8000) / (cal_x1 - cal_x0); 341 *b_gen = (((s32)cal_x1 * cal_y0 - (s32)cal_x0 * cal_y1) * 1000) / 342 (cal_x1 - cal_x0); 343 *b_gen *= 8; 344 345 return 0; 346 } 347 348 static int hts221_get_sensor_scale(struct hts221_hw *hw, 349 enum iio_chan_type ch_type, 350 int *val, int *val2) 351 { 352 s64 tmp; 353 s32 rem, div, data; 354 355 switch (ch_type) { 356 case IIO_HUMIDITYRELATIVE: 357 data = hw->sensors[HTS221_SENSOR_H].slope; 358 div = (1 << 4) * 1000; 359 break; 360 case IIO_TEMP: 361 data = hw->sensors[HTS221_SENSOR_T].slope; 362 div = (1 << 6) * 1000; 363 break; 364 default: 365 return -EINVAL; 366 } 367 368 tmp = div_s64(data * 1000000000LL, div); 369 tmp = div_s64_rem(tmp, 1000000000LL, &rem); 370 371 *val = tmp; 372 *val2 = rem; 373 374 return IIO_VAL_INT_PLUS_NANO; 375 } 376 377 static int hts221_get_sensor_offset(struct hts221_hw *hw, 378 enum iio_chan_type ch_type, 379 int *val, int *val2) 380 { 381 s64 tmp; 382 s32 rem, div, data; 383 384 switch (ch_type) { 385 case IIO_HUMIDITYRELATIVE: 386 data = hw->sensors[HTS221_SENSOR_H].b_gen; 387 div = hw->sensors[HTS221_SENSOR_H].slope; 388 break; 389 case IIO_TEMP: 390 data = hw->sensors[HTS221_SENSOR_T].b_gen; 391 div = hw->sensors[HTS221_SENSOR_T].slope; 392 break; 393 default: 394 return -EINVAL; 395 } 396 397 tmp = div_s64(data * 1000000000LL, div); 398 tmp = div_s64_rem(tmp, 1000000000LL, &rem); 399 400 *val = tmp; 401 *val2 = rem; 402 403 return IIO_VAL_INT_PLUS_NANO; 404 } 405 406 static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val) 407 { 408 __le16 data; 409 int err; 410 411 err = hts221_set_enable(hw, true); 412 if (err < 0) 413 return err; 414 415 msleep(50); 416 417 err = regmap_bulk_read(hw->regmap, addr, &data, sizeof(data)); 418 if (err < 0) 419 return err; 420 421 hts221_set_enable(hw, false); 422 423 *val = (s16)le16_to_cpu(data); 424 425 return IIO_VAL_INT; 426 } 427 428 static int hts221_read_raw(struct iio_dev *iio_dev, 429 struct iio_chan_spec const *ch, 430 int *val, int *val2, long mask) 431 { 432 struct hts221_hw *hw = iio_priv(iio_dev); 433 int ret; 434 435 ret = iio_device_claim_direct_mode(iio_dev); 436 if (ret) 437 return ret; 438 439 switch (mask) { 440 case IIO_CHAN_INFO_RAW: 441 ret = hts221_read_oneshot(hw, ch->address, val); 442 break; 443 case IIO_CHAN_INFO_SCALE: 444 ret = hts221_get_sensor_scale(hw, ch->type, val, val2); 445 break; 446 case IIO_CHAN_INFO_OFFSET: 447 ret = hts221_get_sensor_offset(hw, ch->type, val, val2); 448 break; 449 case IIO_CHAN_INFO_SAMP_FREQ: 450 *val = hw->odr; 451 ret = IIO_VAL_INT; 452 break; 453 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: { 454 u8 idx; 455 const struct hts221_avg *avg; 456 457 switch (ch->type) { 458 case IIO_HUMIDITYRELATIVE: 459 avg = &hts221_avg_list[HTS221_SENSOR_H]; 460 idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx; 461 *val = avg->avg_avl[idx]; 462 ret = IIO_VAL_INT; 463 break; 464 case IIO_TEMP: 465 avg = &hts221_avg_list[HTS221_SENSOR_T]; 466 idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx; 467 *val = avg->avg_avl[idx]; 468 ret = IIO_VAL_INT; 469 break; 470 default: 471 ret = -EINVAL; 472 break; 473 } 474 break; 475 } 476 default: 477 ret = -EINVAL; 478 break; 479 } 480 481 iio_device_release_direct_mode(iio_dev); 482 483 return ret; 484 } 485 486 static int hts221_write_raw(struct iio_dev *iio_dev, 487 struct iio_chan_spec const *chan, 488 int val, int val2, long mask) 489 { 490 struct hts221_hw *hw = iio_priv(iio_dev); 491 int ret; 492 493 ret = iio_device_claim_direct_mode(iio_dev); 494 if (ret) 495 return ret; 496 497 switch (mask) { 498 case IIO_CHAN_INFO_SAMP_FREQ: 499 ret = hts221_update_odr(hw, val); 500 break; 501 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 502 switch (chan->type) { 503 case IIO_HUMIDITYRELATIVE: 504 ret = hts221_update_avg(hw, HTS221_SENSOR_H, val); 505 break; 506 case IIO_TEMP: 507 ret = hts221_update_avg(hw, HTS221_SENSOR_T, val); 508 break; 509 default: 510 ret = -EINVAL; 511 break; 512 } 513 break; 514 default: 515 ret = -EINVAL; 516 break; 517 } 518 519 iio_device_release_direct_mode(iio_dev); 520 521 return ret; 522 } 523 524 static int hts221_validate_trigger(struct iio_dev *iio_dev, 525 struct iio_trigger *trig) 526 { 527 struct hts221_hw *hw = iio_priv(iio_dev); 528 529 return hw->trig == trig ? 0 : -EINVAL; 530 } 531 532 static IIO_DEVICE_ATTR(in_humidity_oversampling_ratio_available, S_IRUGO, 533 hts221_sysfs_rh_oversampling_avail, NULL, 0); 534 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available, S_IRUGO, 535 hts221_sysfs_temp_oversampling_avail, NULL, 0); 536 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hts221_sysfs_sampling_freq); 537 538 static struct attribute *hts221_attributes[] = { 539 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 540 &iio_dev_attr_in_humidity_oversampling_ratio_available.dev_attr.attr, 541 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr, 542 NULL, 543 }; 544 545 static const struct attribute_group hts221_attribute_group = { 546 .attrs = hts221_attributes, 547 }; 548 549 static const struct iio_info hts221_info = { 550 .attrs = &hts221_attribute_group, 551 .read_raw = hts221_read_raw, 552 .write_raw = hts221_write_raw, 553 .validate_trigger = hts221_validate_trigger, 554 }; 555 556 static const unsigned long hts221_scan_masks[] = {0x3, 0x0}; 557 558 int hts221_probe(struct device *dev, int irq, const char *name, 559 struct regmap *regmap) 560 { 561 struct iio_dev *iio_dev; 562 struct hts221_hw *hw; 563 int err; 564 u8 data; 565 566 iio_dev = devm_iio_device_alloc(dev, sizeof(*hw)); 567 if (!iio_dev) 568 return -ENOMEM; 569 570 dev_set_drvdata(dev, (void *)iio_dev); 571 572 hw = iio_priv(iio_dev); 573 hw->name = name; 574 hw->dev = dev; 575 hw->irq = irq; 576 hw->regmap = regmap; 577 578 err = hts221_check_whoami(hw); 579 if (err < 0) 580 return err; 581 582 iio_dev->modes = INDIO_DIRECT_MODE; 583 iio_dev->dev.parent = hw->dev; 584 iio_dev->available_scan_masks = hts221_scan_masks; 585 iio_dev->channels = hts221_channels; 586 iio_dev->num_channels = ARRAY_SIZE(hts221_channels); 587 iio_dev->name = HTS221_DEV_NAME; 588 iio_dev->info = &hts221_info; 589 590 /* enable Block Data Update */ 591 err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR, 592 HTS221_BDU_MASK, 593 FIELD_PREP(HTS221_BDU_MASK, 1)); 594 if (err < 0) 595 return err; 596 597 err = hts221_update_odr(hw, hts221_odr_table[0].hz); 598 if (err < 0) 599 return err; 600 601 /* configure humidity sensor */ 602 err = hts221_parse_rh_caldata(hw); 603 if (err < 0) { 604 dev_err(hw->dev, "failed to get rh calibration data\n"); 605 return err; 606 } 607 608 data = hts221_avg_list[HTS221_SENSOR_H].avg_avl[3]; 609 err = hts221_update_avg(hw, HTS221_SENSOR_H, data); 610 if (err < 0) { 611 dev_err(hw->dev, "failed to set rh oversampling ratio\n"); 612 return err; 613 } 614 615 /* configure temperature sensor */ 616 err = hts221_parse_temp_caldata(hw); 617 if (err < 0) { 618 dev_err(hw->dev, 619 "failed to get temperature calibration data\n"); 620 return err; 621 } 622 623 data = hts221_avg_list[HTS221_SENSOR_T].avg_avl[3]; 624 err = hts221_update_avg(hw, HTS221_SENSOR_T, data); 625 if (err < 0) { 626 dev_err(hw->dev, 627 "failed to set temperature oversampling ratio\n"); 628 return err; 629 } 630 631 if (hw->irq > 0) { 632 err = hts221_allocate_buffers(hw); 633 if (err < 0) 634 return err; 635 636 err = hts221_allocate_trigger(hw); 637 if (err) 638 return err; 639 } 640 641 return devm_iio_device_register(hw->dev, iio_dev); 642 } 643 EXPORT_SYMBOL(hts221_probe); 644 645 static int __maybe_unused hts221_suspend(struct device *dev) 646 { 647 struct iio_dev *iio_dev = dev_get_drvdata(dev); 648 struct hts221_hw *hw = iio_priv(iio_dev); 649 650 return regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR, 651 HTS221_ENABLE_MASK, 652 FIELD_PREP(HTS221_ENABLE_MASK, false)); 653 } 654 655 static int __maybe_unused hts221_resume(struct device *dev) 656 { 657 struct iio_dev *iio_dev = dev_get_drvdata(dev); 658 struct hts221_hw *hw = iio_priv(iio_dev); 659 int err = 0; 660 661 if (hw->enabled) 662 err = regmap_update_bits(hw->regmap, HTS221_REG_CNTRL1_ADDR, 663 HTS221_ENABLE_MASK, 664 FIELD_PREP(HTS221_ENABLE_MASK, 665 true)); 666 return err; 667 } 668 669 const struct dev_pm_ops hts221_pm_ops = { 670 SET_SYSTEM_SLEEP_PM_OPS(hts221_suspend, hts221_resume) 671 }; 672 EXPORT_SYMBOL(hts221_pm_ops); 673 674 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>"); 675 MODULE_DESCRIPTION("STMicroelectronics hts221 sensor driver"); 676 MODULE_LICENSE("GPL v2"); 677