0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #define pr_fmt(fmt) "bmp280: " fmt
0018
0019 #include <linux/device.h>
0020 #include <linux/module.h>
0021 #include <linux/regmap.h>
0022 #include <linux/delay.h>
0023 #include <linux/iio/iio.h>
0024 #include <linux/iio/sysfs.h>
0025 #include <linux/gpio/consumer.h>
0026 #include <linux/regulator/consumer.h>
0027 #include <linux/interrupt.h>
0028 #include <linux/irq.h> /* For irq_get_irq_data() */
0029 #include <linux/completion.h>
0030 #include <linux/pm_runtime.h>
0031 #include <linux/random.h>
0032
0033 #include "bmp280.h"
0034
0035
0036
0037
0038
0039 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
0040
0041 struct bmp180_calib {
0042 s16 AC1;
0043 s16 AC2;
0044 s16 AC3;
0045 u16 AC4;
0046 u16 AC5;
0047 u16 AC6;
0048 s16 B1;
0049 s16 B2;
0050 s16 MB;
0051 s16 MC;
0052 s16 MD;
0053 };
0054
0055
0056 struct bmp280_calib {
0057 u16 T1;
0058 s16 T2;
0059 s16 T3;
0060 u16 P1;
0061 s16 P2;
0062 s16 P3;
0063 s16 P4;
0064 s16 P5;
0065 s16 P6;
0066 s16 P7;
0067 s16 P8;
0068 s16 P9;
0069 u8 H1;
0070 s16 H2;
0071 u8 H3;
0072 s16 H4;
0073 s16 H5;
0074 s8 H6;
0075 };
0076
0077 static const char *const bmp280_supply_names[] = {
0078 "vddd", "vdda"
0079 };
0080
0081 #define BMP280_NUM_SUPPLIES ARRAY_SIZE(bmp280_supply_names)
0082
0083 struct bmp280_data {
0084 struct device *dev;
0085 struct mutex lock;
0086 struct regmap *regmap;
0087 struct completion done;
0088 bool use_eoc;
0089 const struct bmp280_chip_info *chip_info;
0090 union {
0091 struct bmp180_calib bmp180;
0092 struct bmp280_calib bmp280;
0093 } calib;
0094 struct regulator_bulk_data supplies[BMP280_NUM_SUPPLIES];
0095 unsigned int start_up_time;
0096
0097
0098 u8 oversampling_press;
0099 u8 oversampling_temp;
0100 u8 oversampling_humid;
0101
0102
0103
0104
0105
0106 s32 t_fine;
0107 };
0108
0109 struct bmp280_chip_info {
0110 const int *oversampling_temp_avail;
0111 int num_oversampling_temp_avail;
0112
0113 const int *oversampling_press_avail;
0114 int num_oversampling_press_avail;
0115
0116 const int *oversampling_humid_avail;
0117 int num_oversampling_humid_avail;
0118
0119 int (*chip_config)(struct bmp280_data *);
0120 int (*read_temp)(struct bmp280_data *, int *);
0121 int (*read_press)(struct bmp280_data *, int *, int *);
0122 int (*read_humid)(struct bmp280_data *, int *, int *);
0123 };
0124
0125
0126
0127
0128
0129 enum { T1, T2, T3 };
0130 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
0131
0132 static const struct iio_chan_spec bmp280_channels[] = {
0133 {
0134 .type = IIO_PRESSURE,
0135 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
0136 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
0137 },
0138 {
0139 .type = IIO_TEMP,
0140 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
0141 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
0142 },
0143 {
0144 .type = IIO_HUMIDITYRELATIVE,
0145 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
0146 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
0147 },
0148 };
0149
0150 static int bmp280_read_calib(struct bmp280_data *data,
0151 struct bmp280_calib *calib,
0152 unsigned int chip)
0153 {
0154 int ret;
0155 unsigned int tmp;
0156 __le16 l16;
0157 __be16 b16;
0158 struct device *dev = data->dev;
0159 __le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2];
0160 __le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2];
0161
0162
0163 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
0164 t_buf, BMP280_COMP_TEMP_REG_COUNT);
0165 if (ret < 0) {
0166 dev_err(data->dev,
0167 "failed to read temperature calibration parameters\n");
0168 return ret;
0169 }
0170
0171
0172 add_device_randomness(t_buf, sizeof(t_buf));
0173
0174 calib->T1 = le16_to_cpu(t_buf[T1]);
0175 calib->T2 = le16_to_cpu(t_buf[T2]);
0176 calib->T3 = le16_to_cpu(t_buf[T3]);
0177
0178
0179 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
0180 p_buf, BMP280_COMP_PRESS_REG_COUNT);
0181 if (ret < 0) {
0182 dev_err(data->dev,
0183 "failed to read pressure calibration parameters\n");
0184 return ret;
0185 }
0186
0187
0188 add_device_randomness(p_buf, sizeof(p_buf));
0189
0190 calib->P1 = le16_to_cpu(p_buf[P1]);
0191 calib->P2 = le16_to_cpu(p_buf[P2]);
0192 calib->P3 = le16_to_cpu(p_buf[P3]);
0193 calib->P4 = le16_to_cpu(p_buf[P4]);
0194 calib->P5 = le16_to_cpu(p_buf[P5]);
0195 calib->P6 = le16_to_cpu(p_buf[P6]);
0196 calib->P7 = le16_to_cpu(p_buf[P7]);
0197 calib->P8 = le16_to_cpu(p_buf[P8]);
0198 calib->P9 = le16_to_cpu(p_buf[P9]);
0199
0200
0201
0202
0203
0204
0205
0206
0207 if (chip != BME280_CHIP_ID)
0208 return 0;
0209
0210 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
0211 if (ret < 0) {
0212 dev_err(dev, "failed to read H1 comp value\n");
0213 return ret;
0214 }
0215 calib->H1 = tmp;
0216
0217 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &l16, 2);
0218 if (ret < 0) {
0219 dev_err(dev, "failed to read H2 comp value\n");
0220 return ret;
0221 }
0222 calib->H2 = sign_extend32(le16_to_cpu(l16), 15);
0223
0224 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
0225 if (ret < 0) {
0226 dev_err(dev, "failed to read H3 comp value\n");
0227 return ret;
0228 }
0229 calib->H3 = tmp;
0230
0231 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &b16, 2);
0232 if (ret < 0) {
0233 dev_err(dev, "failed to read H4 comp value\n");
0234 return ret;
0235 }
0236 calib->H4 = sign_extend32(((be16_to_cpu(b16) >> 4) & 0xff0) |
0237 (be16_to_cpu(b16) & 0xf), 11);
0238
0239 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &l16, 2);
0240 if (ret < 0) {
0241 dev_err(dev, "failed to read H5 comp value\n");
0242 return ret;
0243 }
0244 calib->H5 = sign_extend32(((le16_to_cpu(l16) >> 4) & 0xfff), 11);
0245
0246 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
0247 if (ret < 0) {
0248 dev_err(dev, "failed to read H6 comp value\n");
0249 return ret;
0250 }
0251 calib->H6 = sign_extend32(tmp, 7);
0252
0253 return 0;
0254 }
0255
0256
0257
0258
0259
0260
0261 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
0262 s32 adc_humidity)
0263 {
0264 s32 var;
0265 struct bmp280_calib *calib = &data->calib.bmp280;
0266
0267 var = ((s32)data->t_fine) - (s32)76800;
0268 var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
0269 + (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
0270 * (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
0271 + (s32)2097152) * calib->H2 + 8192) >> 14);
0272 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
0273
0274 var = clamp_val(var, 0, 419430400);
0275
0276 return var >> 12;
0277 };
0278
0279
0280
0281
0282
0283
0284
0285
0286 static s32 bmp280_compensate_temp(struct bmp280_data *data,
0287 s32 adc_temp)
0288 {
0289 s32 var1, var2;
0290 struct bmp280_calib *calib = &data->calib.bmp280;
0291
0292 var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
0293 ((s32)calib->T2)) >> 11;
0294 var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
0295 ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
0296 ((s32)calib->T3)) >> 14;
0297 data->t_fine = var1 + var2;
0298
0299 return (data->t_fine * 5 + 128) >> 8;
0300 }
0301
0302
0303
0304
0305
0306
0307
0308
0309 static u32 bmp280_compensate_press(struct bmp280_data *data,
0310 s32 adc_press)
0311 {
0312 s64 var1, var2, p;
0313 struct bmp280_calib *calib = &data->calib.bmp280;
0314
0315 var1 = ((s64)data->t_fine) - 128000;
0316 var2 = var1 * var1 * (s64)calib->P6;
0317 var2 += (var1 * (s64)calib->P5) << 17;
0318 var2 += ((s64)calib->P4) << 35;
0319 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
0320 ((var1 * (s64)calib->P2) << 12);
0321 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
0322
0323 if (var1 == 0)
0324 return 0;
0325
0326 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
0327 p = div64_s64(p, var1);
0328 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
0329 var2 = ((s64)(calib->P8) * p) >> 19;
0330 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
0331
0332 return (u32)p;
0333 }
0334
0335 static int bmp280_read_temp(struct bmp280_data *data,
0336 int *val)
0337 {
0338 int ret;
0339 __be32 tmp = 0;
0340 s32 adc_temp, comp_temp;
0341
0342 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, &tmp, 3);
0343 if (ret < 0) {
0344 dev_err(data->dev, "failed to read temperature\n");
0345 return ret;
0346 }
0347
0348 adc_temp = be32_to_cpu(tmp) >> 12;
0349 if (adc_temp == BMP280_TEMP_SKIPPED) {
0350
0351 dev_err(data->dev, "reading temperature skipped\n");
0352 return -EIO;
0353 }
0354 comp_temp = bmp280_compensate_temp(data, adc_temp);
0355
0356
0357
0358
0359
0360 if (val) {
0361 *val = comp_temp * 10;
0362 return IIO_VAL_INT;
0363 }
0364
0365 return 0;
0366 }
0367
0368 static int bmp280_read_press(struct bmp280_data *data,
0369 int *val, int *val2)
0370 {
0371 int ret;
0372 __be32 tmp = 0;
0373 s32 adc_press;
0374 u32 comp_press;
0375
0376
0377 ret = bmp280_read_temp(data, NULL);
0378 if (ret < 0)
0379 return ret;
0380
0381 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, &tmp, 3);
0382 if (ret < 0) {
0383 dev_err(data->dev, "failed to read pressure\n");
0384 return ret;
0385 }
0386
0387 adc_press = be32_to_cpu(tmp) >> 12;
0388 if (adc_press == BMP280_PRESS_SKIPPED) {
0389
0390 dev_err(data->dev, "reading pressure skipped\n");
0391 return -EIO;
0392 }
0393 comp_press = bmp280_compensate_press(data, adc_press);
0394
0395 *val = comp_press;
0396 *val2 = 256000;
0397
0398 return IIO_VAL_FRACTIONAL;
0399 }
0400
0401 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
0402 {
0403 __be16 tmp;
0404 int ret;
0405 s32 adc_humidity;
0406 u32 comp_humidity;
0407
0408
0409 ret = bmp280_read_temp(data, NULL);
0410 if (ret < 0)
0411 return ret;
0412
0413 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB, &tmp, 2);
0414 if (ret < 0) {
0415 dev_err(data->dev, "failed to read humidity\n");
0416 return ret;
0417 }
0418
0419 adc_humidity = be16_to_cpu(tmp);
0420 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
0421
0422 dev_err(data->dev, "reading humidity skipped\n");
0423 return -EIO;
0424 }
0425 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
0426
0427 *val = comp_humidity * 1000 / 1024;
0428
0429 return IIO_VAL_INT;
0430 }
0431
0432 static int bmp280_read_raw(struct iio_dev *indio_dev,
0433 struct iio_chan_spec const *chan,
0434 int *val, int *val2, long mask)
0435 {
0436 int ret;
0437 struct bmp280_data *data = iio_priv(indio_dev);
0438
0439 pm_runtime_get_sync(data->dev);
0440 mutex_lock(&data->lock);
0441
0442 switch (mask) {
0443 case IIO_CHAN_INFO_PROCESSED:
0444 switch (chan->type) {
0445 case IIO_HUMIDITYRELATIVE:
0446 ret = data->chip_info->read_humid(data, val, val2);
0447 break;
0448 case IIO_PRESSURE:
0449 ret = data->chip_info->read_press(data, val, val2);
0450 break;
0451 case IIO_TEMP:
0452 ret = data->chip_info->read_temp(data, val);
0453 break;
0454 default:
0455 ret = -EINVAL;
0456 break;
0457 }
0458 break;
0459 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
0460 switch (chan->type) {
0461 case IIO_HUMIDITYRELATIVE:
0462 *val = 1 << data->oversampling_humid;
0463 ret = IIO_VAL_INT;
0464 break;
0465 case IIO_PRESSURE:
0466 *val = 1 << data->oversampling_press;
0467 ret = IIO_VAL_INT;
0468 break;
0469 case IIO_TEMP:
0470 *val = 1 << data->oversampling_temp;
0471 ret = IIO_VAL_INT;
0472 break;
0473 default:
0474 ret = -EINVAL;
0475 break;
0476 }
0477 break;
0478 default:
0479 ret = -EINVAL;
0480 break;
0481 }
0482
0483 mutex_unlock(&data->lock);
0484 pm_runtime_mark_last_busy(data->dev);
0485 pm_runtime_put_autosuspend(data->dev);
0486
0487 return ret;
0488 }
0489
0490 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
0491 int val)
0492 {
0493 int i;
0494 const int *avail = data->chip_info->oversampling_humid_avail;
0495 const int n = data->chip_info->num_oversampling_humid_avail;
0496
0497 for (i = 0; i < n; i++) {
0498 if (avail[i] == val) {
0499 data->oversampling_humid = ilog2(val);
0500
0501 return data->chip_info->chip_config(data);
0502 }
0503 }
0504 return -EINVAL;
0505 }
0506
0507 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
0508 int val)
0509 {
0510 int i;
0511 const int *avail = data->chip_info->oversampling_temp_avail;
0512 const int n = data->chip_info->num_oversampling_temp_avail;
0513
0514 for (i = 0; i < n; i++) {
0515 if (avail[i] == val) {
0516 data->oversampling_temp = ilog2(val);
0517
0518 return data->chip_info->chip_config(data);
0519 }
0520 }
0521 return -EINVAL;
0522 }
0523
0524 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
0525 int val)
0526 {
0527 int i;
0528 const int *avail = data->chip_info->oversampling_press_avail;
0529 const int n = data->chip_info->num_oversampling_press_avail;
0530
0531 for (i = 0; i < n; i++) {
0532 if (avail[i] == val) {
0533 data->oversampling_press = ilog2(val);
0534
0535 return data->chip_info->chip_config(data);
0536 }
0537 }
0538 return -EINVAL;
0539 }
0540
0541 static int bmp280_write_raw(struct iio_dev *indio_dev,
0542 struct iio_chan_spec const *chan,
0543 int val, int val2, long mask)
0544 {
0545 int ret = 0;
0546 struct bmp280_data *data = iio_priv(indio_dev);
0547
0548 switch (mask) {
0549 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
0550 pm_runtime_get_sync(data->dev);
0551 mutex_lock(&data->lock);
0552 switch (chan->type) {
0553 case IIO_HUMIDITYRELATIVE:
0554 ret = bmp280_write_oversampling_ratio_humid(data, val);
0555 break;
0556 case IIO_PRESSURE:
0557 ret = bmp280_write_oversampling_ratio_press(data, val);
0558 break;
0559 case IIO_TEMP:
0560 ret = bmp280_write_oversampling_ratio_temp(data, val);
0561 break;
0562 default:
0563 ret = -EINVAL;
0564 break;
0565 }
0566 mutex_unlock(&data->lock);
0567 pm_runtime_mark_last_busy(data->dev);
0568 pm_runtime_put_autosuspend(data->dev);
0569 break;
0570 default:
0571 return -EINVAL;
0572 }
0573
0574 return ret;
0575 }
0576
0577 static int bmp280_read_avail(struct iio_dev *indio_dev,
0578 struct iio_chan_spec const *chan,
0579 const int **vals, int *type, int *length,
0580 long mask)
0581 {
0582 struct bmp280_data *data = iio_priv(indio_dev);
0583
0584 switch (mask) {
0585 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
0586 switch (chan->type) {
0587 case IIO_PRESSURE:
0588 *vals = data->chip_info->oversampling_press_avail;
0589 *length = data->chip_info->num_oversampling_press_avail;
0590 break;
0591 case IIO_TEMP:
0592 *vals = data->chip_info->oversampling_temp_avail;
0593 *length = data->chip_info->num_oversampling_temp_avail;
0594 break;
0595 default:
0596 return -EINVAL;
0597 }
0598 *type = IIO_VAL_INT;
0599 return IIO_AVAIL_LIST;
0600 default:
0601 return -EINVAL;
0602 }
0603 }
0604
0605 static const struct iio_info bmp280_info = {
0606 .read_raw = &bmp280_read_raw,
0607 .read_avail = &bmp280_read_avail,
0608 .write_raw = &bmp280_write_raw,
0609 };
0610
0611 static int bmp280_chip_config(struct bmp280_data *data)
0612 {
0613 int ret;
0614 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
0615 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
0616
0617 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
0618 BMP280_OSRS_TEMP_MASK |
0619 BMP280_OSRS_PRESS_MASK |
0620 BMP280_MODE_MASK,
0621 osrs | BMP280_MODE_NORMAL);
0622 if (ret < 0) {
0623 dev_err(data->dev,
0624 "failed to write ctrl_meas register\n");
0625 return ret;
0626 }
0627
0628 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
0629 BMP280_FILTER_MASK,
0630 BMP280_FILTER_4X);
0631 if (ret < 0) {
0632 dev_err(data->dev,
0633 "failed to write config register\n");
0634 return ret;
0635 }
0636
0637 return ret;
0638 }
0639
0640 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
0641
0642 static const struct bmp280_chip_info bmp280_chip_info = {
0643 .oversampling_temp_avail = bmp280_oversampling_avail,
0644 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
0645
0646 .oversampling_press_avail = bmp280_oversampling_avail,
0647 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
0648
0649 .chip_config = bmp280_chip_config,
0650 .read_temp = bmp280_read_temp,
0651 .read_press = bmp280_read_press,
0652 };
0653
0654 static int bme280_chip_config(struct bmp280_data *data)
0655 {
0656 int ret;
0657 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
0658
0659
0660
0661
0662
0663 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
0664 BMP280_OSRS_HUMIDITY_MASK, osrs);
0665
0666 if (ret < 0)
0667 return ret;
0668
0669 return bmp280_chip_config(data);
0670 }
0671
0672 static const struct bmp280_chip_info bme280_chip_info = {
0673 .oversampling_temp_avail = bmp280_oversampling_avail,
0674 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
0675
0676 .oversampling_press_avail = bmp280_oversampling_avail,
0677 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
0678
0679 .oversampling_humid_avail = bmp280_oversampling_avail,
0680 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
0681
0682 .chip_config = bme280_chip_config,
0683 .read_temp = bmp280_read_temp,
0684 .read_press = bmp280_read_press,
0685 .read_humid = bmp280_read_humid,
0686 };
0687
0688 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
0689 {
0690 int ret;
0691 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
0692 unsigned int delay_us;
0693 unsigned int ctrl;
0694
0695 if (data->use_eoc)
0696 reinit_completion(&data->done);
0697
0698 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
0699 if (ret)
0700 return ret;
0701
0702 if (data->use_eoc) {
0703
0704
0705
0706
0707
0708 ret = wait_for_completion_timeout(&data->done,
0709 1 + msecs_to_jiffies(100));
0710 if (!ret)
0711 dev_err(data->dev, "timeout waiting for completion\n");
0712 } else {
0713 if (ctrl_meas == BMP180_MEAS_TEMP)
0714 delay_us = 4500;
0715 else
0716 delay_us =
0717 conversion_time_max[data->oversampling_press];
0718
0719 usleep_range(delay_us, delay_us + 1000);
0720 }
0721
0722 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
0723 if (ret)
0724 return ret;
0725
0726
0727 if (ctrl & BMP180_MEAS_SCO)
0728 return -EIO;
0729
0730 return 0;
0731 }
0732
0733 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
0734 {
0735 __be16 tmp;
0736 int ret;
0737
0738 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
0739 if (ret)
0740 return ret;
0741
0742 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, &tmp, 2);
0743 if (ret)
0744 return ret;
0745
0746 *val = be16_to_cpu(tmp);
0747
0748 return 0;
0749 }
0750
0751 static int bmp180_read_calib(struct bmp280_data *data,
0752 struct bmp180_calib *calib)
0753 {
0754 int ret;
0755 int i;
0756 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
0757
0758 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
0759 sizeof(buf));
0760
0761 if (ret < 0)
0762 return ret;
0763
0764
0765 for (i = 0; i < ARRAY_SIZE(buf); i++) {
0766 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
0767 return -EIO;
0768 }
0769
0770
0771 add_device_randomness(buf, sizeof(buf));
0772
0773 calib->AC1 = be16_to_cpu(buf[AC1]);
0774 calib->AC2 = be16_to_cpu(buf[AC2]);
0775 calib->AC3 = be16_to_cpu(buf[AC3]);
0776 calib->AC4 = be16_to_cpu(buf[AC4]);
0777 calib->AC5 = be16_to_cpu(buf[AC5]);
0778 calib->AC6 = be16_to_cpu(buf[AC6]);
0779 calib->B1 = be16_to_cpu(buf[B1]);
0780 calib->B2 = be16_to_cpu(buf[B2]);
0781 calib->MB = be16_to_cpu(buf[MB]);
0782 calib->MC = be16_to_cpu(buf[MC]);
0783 calib->MD = be16_to_cpu(buf[MD]);
0784
0785 return 0;
0786 }
0787
0788
0789
0790
0791
0792
0793
0794 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
0795 {
0796 s32 x1, x2;
0797 struct bmp180_calib *calib = &data->calib.bmp180;
0798
0799 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
0800 x2 = (calib->MC << 11) / (x1 + calib->MD);
0801 data->t_fine = x1 + x2;
0802
0803 return (data->t_fine + 8) >> 4;
0804 }
0805
0806 static int bmp180_read_temp(struct bmp280_data *data, int *val)
0807 {
0808 int ret;
0809 s32 adc_temp, comp_temp;
0810
0811 ret = bmp180_read_adc_temp(data, &adc_temp);
0812 if (ret)
0813 return ret;
0814
0815 comp_temp = bmp180_compensate_temp(data, adc_temp);
0816
0817
0818
0819
0820
0821 if (val) {
0822 *val = comp_temp * 100;
0823 return IIO_VAL_INT;
0824 }
0825
0826 return 0;
0827 }
0828
0829 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
0830 {
0831 int ret;
0832 __be32 tmp = 0;
0833 u8 oss = data->oversampling_press;
0834
0835 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
0836 if (ret)
0837 return ret;
0838
0839 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, &tmp, 3);
0840 if (ret)
0841 return ret;
0842
0843 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
0844
0845 return 0;
0846 }
0847
0848
0849
0850
0851
0852
0853 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
0854 {
0855 s32 x1, x2, x3, p;
0856 s32 b3, b6;
0857 u32 b4, b7;
0858 s32 oss = data->oversampling_press;
0859 struct bmp180_calib *calib = &data->calib.bmp180;
0860
0861 b6 = data->t_fine - 4000;
0862 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
0863 x2 = calib->AC2 * b6 >> 11;
0864 x3 = x1 + x2;
0865 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
0866 x1 = calib->AC3 * b6 >> 13;
0867 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
0868 x3 = (x1 + x2 + 2) >> 2;
0869 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
0870 b7 = ((u32)adc_press - b3) * (50000 >> oss);
0871 if (b7 < 0x80000000)
0872 p = (b7 * 2) / b4;
0873 else
0874 p = (b7 / b4) * 2;
0875
0876 x1 = (p >> 8) * (p >> 8);
0877 x1 = (x1 * 3038) >> 16;
0878 x2 = (-7357 * p) >> 16;
0879
0880 return p + ((x1 + x2 + 3791) >> 4);
0881 }
0882
0883 static int bmp180_read_press(struct bmp280_data *data,
0884 int *val, int *val2)
0885 {
0886 int ret;
0887 s32 adc_press;
0888 u32 comp_press;
0889
0890
0891 ret = bmp180_read_temp(data, NULL);
0892 if (ret)
0893 return ret;
0894
0895 ret = bmp180_read_adc_press(data, &adc_press);
0896 if (ret)
0897 return ret;
0898
0899 comp_press = bmp180_compensate_press(data, adc_press);
0900
0901 *val = comp_press;
0902 *val2 = 1000;
0903
0904 return IIO_VAL_FRACTIONAL;
0905 }
0906
0907 static int bmp180_chip_config(struct bmp280_data *data)
0908 {
0909 return 0;
0910 }
0911
0912 static const int bmp180_oversampling_temp_avail[] = { 1 };
0913 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
0914
0915 static const struct bmp280_chip_info bmp180_chip_info = {
0916 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
0917 .num_oversampling_temp_avail =
0918 ARRAY_SIZE(bmp180_oversampling_temp_avail),
0919
0920 .oversampling_press_avail = bmp180_oversampling_press_avail,
0921 .num_oversampling_press_avail =
0922 ARRAY_SIZE(bmp180_oversampling_press_avail),
0923
0924 .chip_config = bmp180_chip_config,
0925 .read_temp = bmp180_read_temp,
0926 .read_press = bmp180_read_press,
0927 };
0928
0929 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
0930 {
0931 struct bmp280_data *data = d;
0932
0933 complete(&data->done);
0934
0935 return IRQ_HANDLED;
0936 }
0937
0938 static int bmp085_fetch_eoc_irq(struct device *dev,
0939 const char *name,
0940 int irq,
0941 struct bmp280_data *data)
0942 {
0943 unsigned long irq_trig;
0944 int ret;
0945
0946 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
0947 if (irq_trig != IRQF_TRIGGER_RISING) {
0948 dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
0949 irq_trig = IRQF_TRIGGER_RISING;
0950 }
0951
0952 init_completion(&data->done);
0953
0954 ret = devm_request_threaded_irq(dev,
0955 irq,
0956 bmp085_eoc_irq,
0957 NULL,
0958 irq_trig,
0959 name,
0960 data);
0961 if (ret) {
0962
0963 dev_err(dev, "unable to request DRDY IRQ\n");
0964 return 0;
0965 }
0966
0967 data->use_eoc = true;
0968 return 0;
0969 }
0970
0971 static void bmp280_pm_disable(void *data)
0972 {
0973 struct device *dev = data;
0974
0975 pm_runtime_get_sync(dev);
0976 pm_runtime_put_noidle(dev);
0977 pm_runtime_disable(dev);
0978 }
0979
0980 static void bmp280_regulators_disable(void *data)
0981 {
0982 struct regulator_bulk_data *supplies = data;
0983
0984 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
0985 }
0986
0987 int bmp280_common_probe(struct device *dev,
0988 struct regmap *regmap,
0989 unsigned int chip,
0990 const char *name,
0991 int irq)
0992 {
0993 int ret;
0994 struct iio_dev *indio_dev;
0995 struct bmp280_data *data;
0996 unsigned int chip_id;
0997 struct gpio_desc *gpiod;
0998
0999 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1000 if (!indio_dev)
1001 return -ENOMEM;
1002
1003 data = iio_priv(indio_dev);
1004 mutex_init(&data->lock);
1005 data->dev = dev;
1006
1007 indio_dev->name = name;
1008 indio_dev->channels = bmp280_channels;
1009 indio_dev->info = &bmp280_info;
1010 indio_dev->modes = INDIO_DIRECT_MODE;
1011
1012 switch (chip) {
1013 case BMP180_CHIP_ID:
1014 indio_dev->num_channels = 2;
1015 data->chip_info = &bmp180_chip_info;
1016 data->oversampling_press = ilog2(8);
1017 data->oversampling_temp = ilog2(1);
1018 data->start_up_time = 10000;
1019 break;
1020 case BMP280_CHIP_ID:
1021 indio_dev->num_channels = 2;
1022 data->chip_info = &bmp280_chip_info;
1023 data->oversampling_press = ilog2(16);
1024 data->oversampling_temp = ilog2(2);
1025 data->start_up_time = 2000;
1026 break;
1027 case BME280_CHIP_ID:
1028 indio_dev->num_channels = 3;
1029 data->chip_info = &bme280_chip_info;
1030 data->oversampling_press = ilog2(16);
1031 data->oversampling_humid = ilog2(16);
1032 data->oversampling_temp = ilog2(2);
1033 data->start_up_time = 2000;
1034 break;
1035 default:
1036 return -EINVAL;
1037 }
1038
1039
1040 regulator_bulk_set_supply_names(data->supplies,
1041 bmp280_supply_names,
1042 BMP280_NUM_SUPPLIES);
1043
1044 ret = devm_regulator_bulk_get(dev,
1045 BMP280_NUM_SUPPLIES, data->supplies);
1046 if (ret) {
1047 dev_err(dev, "failed to get regulators\n");
1048 return ret;
1049 }
1050
1051 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1052 if (ret) {
1053 dev_err(dev, "failed to enable regulators\n");
1054 return ret;
1055 }
1056
1057 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
1058 data->supplies);
1059 if (ret)
1060 return ret;
1061
1062
1063 usleep_range(data->start_up_time, data->start_up_time + 100);
1064
1065
1066 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1067
1068 if (gpiod) {
1069 dev_info(dev, "release reset\n");
1070 gpiod_set_value(gpiod, 0);
1071 }
1072
1073 data->regmap = regmap;
1074 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1075 if (ret < 0)
1076 return ret;
1077 if (chip_id != chip) {
1078 dev_err(dev, "bad chip id: expected %x got %x\n",
1079 chip, chip_id);
1080 return -EINVAL;
1081 }
1082
1083 ret = data->chip_info->chip_config(data);
1084 if (ret < 0)
1085 return ret;
1086
1087 dev_set_drvdata(dev, indio_dev);
1088
1089
1090
1091
1092
1093
1094 if (chip_id == BMP180_CHIP_ID) {
1095 ret = bmp180_read_calib(data, &data->calib.bmp180);
1096 if (ret < 0) {
1097 dev_err(data->dev,
1098 "failed to read calibration coefficients\n");
1099 return ret;
1100 }
1101 } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
1102 ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
1103 if (ret < 0) {
1104 dev_err(data->dev,
1105 "failed to read calibration coefficients\n");
1106 return ret;
1107 }
1108 }
1109
1110
1111
1112
1113
1114
1115 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1116 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1117 if (ret)
1118 return ret;
1119 }
1120
1121
1122 pm_runtime_get_noresume(dev);
1123 pm_runtime_set_active(dev);
1124 pm_runtime_enable(dev);
1125
1126
1127
1128
1129 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
1130 pm_runtime_use_autosuspend(dev);
1131 pm_runtime_put(dev);
1132
1133 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
1134 if (ret)
1135 return ret;
1136
1137 return devm_iio_device_register(dev, indio_dev);
1138 }
1139 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
1140
1141 static int bmp280_runtime_suspend(struct device *dev)
1142 {
1143 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1144 struct bmp280_data *data = iio_priv(indio_dev);
1145
1146 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
1147 }
1148
1149 static int bmp280_runtime_resume(struct device *dev)
1150 {
1151 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1152 struct bmp280_data *data = iio_priv(indio_dev);
1153 int ret;
1154
1155 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1156 if (ret)
1157 return ret;
1158 usleep_range(data->start_up_time, data->start_up_time + 100);
1159 return data->chip_info->chip_config(data);
1160 }
1161
1162 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
1163 bmp280_runtime_resume, NULL);
1164
1165 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1166 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1167 MODULE_LICENSE("GPL v2");