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 <asm/unaligned.h> 18 19 #include "hts221.h" 20 21 #define HTS221_REG_WHOAMI_ADDR 0x0f 22 #define HTS221_REG_WHOAMI_VAL 0xbc 23 24 #define HTS221_REG_CNTRL1_ADDR 0x20 25 #define HTS221_REG_CNTRL2_ADDR 0x21 26 27 #define HTS221_REG_AVG_ADDR 0x10 28 #define HTS221_REG_H_OUT_L 0x28 29 #define HTS221_REG_T_OUT_L 0x2a 30 31 #define HTS221_HUMIDITY_AVG_MASK 0x07 32 #define HTS221_TEMP_AVG_MASK 0x38 33 34 #define HTS221_ODR_MASK 0x03 35 #define HTS221_BDU_MASK BIT(2) 36 #define HTS221_ENABLE_MASK BIT(7) 37 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 48 49 struct hts221_odr { 50 u8 hz; 51 u8 val; 52 }; 53 54 #define HTS221_AVG_DEPTH 8 55 struct hts221_avg { 56 u8 addr; 57 u8 mask; 58 u16 avg_avl[HTS221_AVG_DEPTH]; 59 }; 60 61 static const struct hts221_odr hts221_odr_table[] = { 62 { 1, 0x01 }, /* 1Hz */ 63 { 7, 0x02 }, /* 7Hz */ 64 { 13, 0x03 }, /* 12.5Hz */ 65 }; 66 67 static const struct hts221_avg hts221_avg_list[] = { 68 { 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 */ 80 }, 81 }, 82 { 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 */ 94 }, 95 }, 96 }; 97 98 static const struct iio_chan_spec hts221_channels[] = { 99 { 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, 113 }, 114 }, 115 { 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, 129 }, 130 }, 131 IIO_CHAN_SOFT_TIMESTAMP(2), 132 }; 133 134 int hts221_write_with_mask(struct hts221_hw *hw, u8 addr, u8 mask, u8 val) 135 { 136 u8 data; 137 int err; 138 139 mutex_lock(&hw->lock); 140 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; 145 } 146 147 data = (data & ~mask) | ((val << __ffs(mask)) & mask); 148 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); 152 153 unlock: 154 mutex_unlock(&hw->lock); 155 156 return err; 157 } 158 159 static int hts221_check_whoami(struct hts221_hw *hw) 160 { 161 u8 data; 162 int err; 163 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; 169 } 170 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; 175 } 176 177 return 0; 178 } 179 180 static int hts221_update_odr(struct hts221_hw *hw, u8 odr) 181 { 182 int i, err; 183 184 for (i = 0; i < ARRAY_SIZE(hts221_odr_table); i++) 185 if (hts221_odr_table[i].hz == odr) 186 break; 187 188 if (i == ARRAY_SIZE(hts221_odr_table)) 189 return -EINVAL; 190 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; 195 196 hw->odr = odr; 197 198 return 0; 199 } 200 201 static int hts221_update_avg(struct hts221_hw *hw, 202 enum hts221_sensor_type type, 203 u16 val) 204 { 205 int i, err; 206 const struct hts221_avg *avg = &hts221_avg_list[type]; 207 208 for (i = 0; i < HTS221_AVG_DEPTH; i++) 209 if (avg->avg_avl[i] == val) 210 break; 211 212 if (i == HTS221_AVG_DEPTH) 213 return -EINVAL; 214 215 err = hts221_write_with_mask(hw, avg->addr, avg->mask, i); 216 if (err < 0) 217 return err; 218 219 hw->sensors[type].cur_avg_idx = i; 220 221 return 0; 222 } 223 224 static ssize_t hts221_sysfs_sampling_freq(struct device *dev, 225 struct device_attribute *attr, 226 char *buf) 227 { 228 int i; 229 ssize_t len = 0; 230 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'; 235 236 return len; 237 } 238 239 static ssize_t 240 hts221_sysfs_rh_oversampling_avail(struct device *dev, 241 struct device_attribute *attr, 242 char *buf) 243 { 244 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_H]; 245 ssize_t len = 0; 246 int i; 247 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'; 252 253 return len; 254 } 255 256 static ssize_t 257 hts221_sysfs_temp_oversampling_avail(struct device *dev, 258 struct device_attribute *attr, 259 char *buf) 260 { 261 const struct hts221_avg *avg = &hts221_avg_list[HTS221_SENSOR_T]; 262 ssize_t len = 0; 263 int i; 264 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'; 269 270 return len; 271 } 272 273 int hts221_set_enable(struct hts221_hw *hw, bool enable) 274 { 275 int err; 276 277 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR, 278 HTS221_ENABLE_MASK, enable); 279 if (err < 0) 280 return err; 281 282 hw->enabled = enable; 283 284 return 0; 285 } 286 287 static int hts221_parse_temp_caldata(struct hts221_hw *hw) 288 { 289 int err, *slope, *b_gen; 290 s16 cal_x0, cal_x1, cal_y0, cal_y1; 291 u8 cal0, cal1; 292 293 err = hw->tf->read(hw->dev, HTS221_REG_0T_CAL_Y_H, 294 sizeof(cal0), &cal0); 295 if (err < 0) 296 return err; 297 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; 303 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; 309 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); 315 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); 321 322 slope = &hw->sensors[HTS221_SENSOR_T].slope; 323 b_gen = &hw->sensors[HTS221_SENSOR_T].b_gen; 324 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; 329 330 return 0; 331 } 332 333 static int hts221_parse_rh_caldata(struct hts221_hw *hw) 334 { 335 int err, *slope, *b_gen; 336 s16 cal_x0, cal_x1, cal_y0, cal_y1; 337 u8 data; 338 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; 344 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; 350 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); 356 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); 362 363 slope = &hw->sensors[HTS221_SENSOR_H].slope; 364 b_gen = &hw->sensors[HTS221_SENSOR_H].b_gen; 365 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; 370 371 return 0; 372 } 373 374 static int hts221_get_sensor_scale(struct hts221_hw *hw, 375 enum iio_chan_type ch_type, 376 int *val, int *val2) 377 { 378 s64 tmp; 379 s32 rem, div, data; 380 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; 392 } 393 394 tmp = div_s64(data * 1000000000LL, div); 395 tmp = div_s64_rem(tmp, 1000000000LL, &rem); 396 397 *val = tmp; 398 *val2 = rem; 399 400 return IIO_VAL_INT_PLUS_NANO; 401 } 402 403 static int hts221_get_sensor_offset(struct hts221_hw *hw, 404 enum iio_chan_type ch_type, 405 int *val, int *val2) 406 { 407 s64 tmp; 408 s32 rem, div, data; 409 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; 421 } 422 423 tmp = div_s64(data * 1000000000LL, div); 424 tmp = div_s64_rem(tmp, 1000000000LL, &rem); 425 426 *val = tmp; 427 *val2 = rem; 428 429 return IIO_VAL_INT_PLUS_NANO; 430 } 431 432 static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val) 433 { 434 u8 data[HTS221_DATA_SIZE]; 435 int err; 436 437 err = hts221_set_enable(hw, true); 438 if (err < 0) 439 return err; 440 441 msleep(50); 442 443 err = hw->tf->read(hw->dev, addr, sizeof(data), data); 444 if (err < 0) 445 return err; 446 447 hts221_set_enable(hw, false); 448 449 *val = (s16)get_unaligned_le16(data); 450 451 return IIO_VAL_INT; 452 } 453 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) 457 { 458 struct hts221_hw *hw = iio_priv(iio_dev); 459 int ret; 460 461 ret = iio_device_claim_direct_mode(iio_dev); 462 if (ret) 463 return ret; 464 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; 482 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; 499 } 500 break; 501 } 502 default: 503 ret = -EINVAL; 504 break; 505 } 506 507 iio_device_release_direct_mode(iio_dev); 508 509 return ret; 510 } 511 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) 515 { 516 struct hts221_hw *hw = iio_priv(iio_dev); 517 int ret; 518 519 ret = iio_device_claim_direct_mode(iio_dev); 520 if (ret) 521 return ret; 522 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; 538 } 539 break; 540 default: 541 ret = -EINVAL; 542 break; 543 } 544 545 iio_device_release_direct_mode(iio_dev); 546 547 return ret; 548 } 549 550 static int hts221_validate_trigger(struct iio_dev *iio_dev, 551 struct iio_trigger *trig) 552 { 553 struct hts221_hw *hw = iio_priv(iio_dev); 554 555 return hw->trig == trig ? 0 : -EINVAL; 556 } 557 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); 563 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, 569 }; 570 571 static const struct attribute_group hts221_attribute_group = { 572 .attrs = hts221_attributes, 573 }; 574 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, 580 }; 581 582 static const unsigned long hts221_scan_masks[] = {0x3, 0x0}; 583 584 int hts221_probe(struct device *dev, int irq, const char *name, 585 const struct hts221_transfer_function *tf_ops) 586 { 587 struct iio_dev *iio_dev; 588 struct hts221_hw *hw; 589 int err; 590 u8 data; 591 592 iio_dev = devm_iio_device_alloc(dev, sizeof(*hw)); 593 if (!iio_dev) 594 return -ENOMEM; 595 596 dev_set_drvdata(dev, (void *)iio_dev); 597 598 hw = iio_priv(iio_dev); 599 hw->name = name; 600 hw->dev = dev; 601 hw->irq = irq; 602 hw->tf = tf_ops; 603 604 mutex_init(&hw->lock); 605 606 err = hts221_check_whoami(hw); 607 if (err < 0) 608 return err; 609 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; 617 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; 623 624 err = hts221_update_odr(hw, hts221_odr_table[0].hz); 625 if (err < 0) 626 return err; 627 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; 633 } 634 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; 640 } 641 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; 648 } 649 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; 656 } 657 658 if (hw->irq > 0) { 659 err = hts221_allocate_buffers(hw); 660 if (err < 0) 661 return err; 662 663 err = hts221_allocate_trigger(hw); 664 if (err) 665 return err; 666 } 667 668 return devm_iio_device_register(hw->dev, iio_dev); 669 } 670 EXPORT_SYMBOL(hts221_probe); 671 672 static int __maybe_unused hts221_suspend(struct device *dev) 673 { 674 struct iio_dev *iio_dev = dev_get_drvdata(dev); 675 struct hts221_hw *hw = iio_priv(iio_dev); 676 int err; 677 678 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR, 679 HTS221_ENABLE_MASK, false); 680 681 return err < 0 ? err : 0; 682 } 683 684 static int __maybe_unused hts221_resume(struct device *dev) 685 { 686 struct iio_dev *iio_dev = dev_get_drvdata(dev); 687 struct hts221_hw *hw = iio_priv(iio_dev); 688 int err = 0; 689 690 if (hw->enabled) 691 err = hts221_write_with_mask(hw, HTS221_REG_CNTRL1_ADDR, 692 HTS221_ENABLE_MASK, true); 693 694 return err; 695 } 696 697 const struct dev_pm_ops hts221_pm_ops = { 698 SET_SYSTEM_SLEEP_PM_OPS(hts221_suspend, hts221_resume) 699 }; 700 EXPORT_SYMBOL(hts221_pm_ops); 701 702 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>"); 703 MODULE_DESCRIPTION("STMicroelectronics hts221 sensor driver"); 704 MODULE_LICENSE("GPL v2"); 705