Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 1 | /* |
Linus Walleij | b26b4e9 | 2016-06-30 03:48:51 +0200 | [diff] [blame] | 2 | * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com> |
| 3 | * Copyright (c) 2012 Bosch Sensortec GmbH |
| 4 | * Copyright (c) 2012 Unixphere AB |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 5 | * Copyright (c) 2014 Intel Corporation |
Linus Walleij | b26b4e9 | 2016-06-30 03:48:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org> |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 7 | * |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 8 | * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor. |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 14 | * Datasheet: |
| 15 | * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf |
| 16 | * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 17 | * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #define pr_fmt(fmt) "bmp280: " fmt |
| 21 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 22 | #include <linux/device.h> |
Linus Walleij | 1711884 | 2016-06-30 03:48:50 +0200 | [diff] [blame] | 23 | #include <linux/module.h> |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 24 | #include <linux/regmap.h> |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 25 | #include <linux/delay.h> |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 26 | #include <linux/iio/iio.h> |
| 27 | #include <linux/iio/sysfs.h> |
Linus Walleij | c5842b4 | 2016-06-30 03:48:47 +0200 | [diff] [blame] | 28 | #include <linux/gpio/consumer.h> |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 29 | #include <linux/regulator/consumer.h> |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/irq.h> /* For irq_get_irq_data() */ |
| 32 | #include <linux/completion.h> |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 33 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 34 | #include "bmp280.h" |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 35 | |
| 36 | struct bmp280_data { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 37 | struct device *dev; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 38 | struct mutex lock; |
| 39 | struct regmap *regmap; |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 40 | struct completion done; |
| 41 | bool use_eoc; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 42 | const struct bmp280_chip_info *chip_info; |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 43 | struct regulator *vddd; |
| 44 | struct regulator *vdda; |
| 45 | unsigned int start_up_time; /* in milliseconds */ |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 46 | |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 47 | /* log of base 2 of oversampling rate */ |
| 48 | u8 oversampling_press; |
| 49 | u8 oversampling_temp; |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 50 | u8 oversampling_humid; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 51 | |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 52 | /* |
| 53 | * Carryover value from temperature conversion, used in pressure |
| 54 | * calculation. |
| 55 | */ |
| 56 | s32 t_fine; |
| 57 | }; |
| 58 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 59 | struct bmp280_chip_info { |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 60 | const int *oversampling_temp_avail; |
| 61 | int num_oversampling_temp_avail; |
| 62 | |
| 63 | const int *oversampling_press_avail; |
| 64 | int num_oversampling_press_avail; |
| 65 | |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 66 | const int *oversampling_humid_avail; |
| 67 | int num_oversampling_humid_avail; |
| 68 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 69 | int (*chip_config)(struct bmp280_data *); |
| 70 | int (*read_temp)(struct bmp280_data *, int *); |
| 71 | int (*read_press)(struct bmp280_data *, int *, int *); |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 72 | int (*read_humid)(struct bmp280_data *, int *, int *); |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 73 | }; |
| 74 | |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 75 | /* |
| 76 | * These enums are used for indexing into the array of compensation |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 77 | * parameters for BMP280. |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 78 | */ |
| 79 | enum { T1, T2, T3 }; |
| 80 | enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 81 | |
| 82 | static const struct iio_chan_spec bmp280_channels[] = { |
| 83 | { |
| 84 | .type = IIO_PRESSURE, |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 85 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
| 86 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 87 | }, |
| 88 | { |
| 89 | .type = IIO_TEMP, |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 90 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
| 91 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 92 | }, |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 93 | { |
| 94 | .type = IIO_HUMIDITYRELATIVE, |
| 95 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
| 96 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), |
| 97 | }, |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 98 | }; |
| 99 | |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 100 | /* |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 101 | * Returns humidity in percent, resolution is 0.01 percent. Output value of |
| 102 | * "47445" represents 47445/1024 = 46.333 %RH. |
| 103 | * |
| 104 | * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula". |
| 105 | */ |
| 106 | |
| 107 | static u32 bmp280_compensate_humidity(struct bmp280_data *data, |
| 108 | s32 adc_humidity) |
| 109 | { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 110 | struct device *dev = data->dev; |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 111 | unsigned int H1, H3, tmp; |
| 112 | int H2, H4, H5, H6, ret, var; |
| 113 | |
| 114 | ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1); |
| 115 | if (ret < 0) { |
| 116 | dev_err(dev, "failed to read H1 comp value\n"); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2); |
| 121 | if (ret < 0) { |
| 122 | dev_err(dev, "failed to read H2 comp value\n"); |
| 123 | return ret; |
| 124 | } |
| 125 | H2 = sign_extend32(le16_to_cpu(tmp), 15); |
| 126 | |
| 127 | ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3); |
| 128 | if (ret < 0) { |
| 129 | dev_err(dev, "failed to read H3 comp value\n"); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2); |
| 134 | if (ret < 0) { |
| 135 | dev_err(dev, "failed to read H4 comp value\n"); |
| 136 | return ret; |
| 137 | } |
| 138 | H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) | |
| 139 | (be16_to_cpu(tmp) & 0xf), 11); |
| 140 | |
| 141 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2); |
| 142 | if (ret < 0) { |
| 143 | dev_err(dev, "failed to read H5 comp value\n"); |
| 144 | return ret; |
| 145 | } |
| 146 | H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11); |
| 147 | |
| 148 | ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp); |
| 149 | if (ret < 0) { |
| 150 | dev_err(dev, "failed to read H6 comp value\n"); |
| 151 | return ret; |
| 152 | } |
| 153 | H6 = sign_extend32(tmp, 7); |
| 154 | |
| 155 | var = ((s32)data->t_fine) - 76800; |
| 156 | var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var)) + 16384) >> 15) |
| 157 | * (((((((var * H6) >> 10) * (((var * H3) >> 11) + 32768)) >> 10) |
| 158 | + 2097152) * H2 + 8192) >> 14); |
| 159 | var -= ((((var >> 15) * (var >> 15)) >> 7) * H1) >> 4; |
| 160 | |
| 161 | return var >> 12; |
| 162 | }; |
| 163 | |
| 164 | /* |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 165 | * Returns temperature in DegC, resolution is 0.01 DegC. Output value of |
| 166 | * "5123" equals 51.23 DegC. t_fine carries fine temperature as global |
| 167 | * value. |
| 168 | * |
| 169 | * Taken from datasheet, Section 3.11.3, "Compensation formula". |
| 170 | */ |
| 171 | static s32 bmp280_compensate_temp(struct bmp280_data *data, |
| 172 | s32 adc_temp) |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 173 | { |
| 174 | int ret; |
Hartmut Knaack | 44cf379 | 2014-12-19 23:59:25 +0100 | [diff] [blame] | 175 | s32 var1, var2; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 176 | __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2]; |
| 177 | |
| 178 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START, |
| 179 | buf, BMP280_COMP_TEMP_REG_COUNT); |
| 180 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 181 | dev_err(data->dev, |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 182 | "failed to read temperature calibration parameters\n"); |
| 183 | return ret; |
| 184 | } |
| 185 | |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 186 | /* |
| 187 | * The double casts are necessary because le16_to_cpu returns an |
| 188 | * unsigned 16-bit value. Casting that value directly to a |
| 189 | * signed 32-bit will not do proper sign extension. |
| 190 | * |
| 191 | * Conversely, T1 and P1 are unsigned values, so they can be |
| 192 | * cast straight to the larger type. |
| 193 | */ |
| 194 | var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) * |
| 195 | ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11; |
| 196 | var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) * |
| 197 | ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) * |
| 198 | ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14; |
Irina Tirdea | abad398 | 2015-04-08 18:26:12 +0300 | [diff] [blame] | 199 | data->t_fine = var1 + var2; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 200 | |
Hartmut Knaack | 44cf379 | 2014-12-19 23:59:25 +0100 | [diff] [blame] | 201 | return (data->t_fine * 5 + 128) >> 8; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 |
| 206 | * integer bits and 8 fractional bits). Output value of "24674867" |
| 207 | * represents 24674867/256 = 96386.2 Pa = 963.862 hPa |
| 208 | * |
| 209 | * Taken from datasheet, Section 3.11.3, "Compensation formula". |
| 210 | */ |
| 211 | static u32 bmp280_compensate_press(struct bmp280_data *data, |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 212 | s32 adc_press) |
| 213 | { |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 214 | int ret; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 215 | s64 var1, var2, p; |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 216 | __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2]; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 217 | |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 218 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START, |
| 219 | buf, BMP280_COMP_PRESS_REG_COUNT); |
| 220 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 221 | dev_err(data->dev, |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 222 | "failed to read pressure calibration parameters\n"); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | var1 = ((s64)data->t_fine) - 128000; |
| 227 | var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]); |
Hartmut Knaack | 44cf379 | 2014-12-19 23:59:25 +0100 | [diff] [blame] | 228 | var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17; |
| 229 | var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35; |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 230 | var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) + |
| 231 | ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12); |
| 232 | var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 233 | |
| 234 | if (var1 == 0) |
| 235 | return 0; |
| 236 | |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 237 | p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125; |
Vlad Dogaru | 46ee98a | 2014-10-23 15:52:00 +0100 | [diff] [blame] | 238 | p = div64_s64(p, var1); |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 239 | var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25; |
| 240 | var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19; |
| 241 | p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 242 | |
Hartmut Knaack | 44cf379 | 2014-12-19 23:59:25 +0100 | [diff] [blame] | 243 | return (u32)p; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int bmp280_read_temp(struct bmp280_data *data, |
| 247 | int *val) |
| 248 | { |
| 249 | int ret; |
| 250 | __be32 tmp = 0; |
| 251 | s32 adc_temp, comp_temp; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 252 | |
| 253 | ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, |
| 254 | (u8 *) &tmp, 3); |
| 255 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 256 | dev_err(data->dev, "failed to read temperature\n"); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | adc_temp = be32_to_cpu(tmp) >> 12; |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 261 | comp_temp = bmp280_compensate_temp(data, adc_temp); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * val might be NULL if we're called by the read_press routine, |
| 265 | * who only cares about the carry over t_fine value. |
| 266 | */ |
| 267 | if (val) { |
| 268 | *val = comp_temp * 10; |
| 269 | return IIO_VAL_INT; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int bmp280_read_press(struct bmp280_data *data, |
| 276 | int *val, int *val2) |
| 277 | { |
| 278 | int ret; |
| 279 | __be32 tmp = 0; |
| 280 | s32 adc_press; |
| 281 | u32 comp_press; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 282 | |
| 283 | /* Read and compensate temperature so we get a reading of t_fine. */ |
| 284 | ret = bmp280_read_temp(data, NULL); |
| 285 | if (ret < 0) |
| 286 | return ret; |
| 287 | |
| 288 | ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, |
| 289 | (u8 *) &tmp, 3); |
| 290 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 291 | dev_err(data->dev, "failed to read pressure\n"); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | adc_press = be32_to_cpu(tmp) >> 12; |
Vlad Dogaru | 0f8994b | 2014-11-20 14:00:48 +0200 | [diff] [blame] | 296 | comp_press = bmp280_compensate_press(data, adc_press); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 297 | |
Hartmut Knaack | 81ebe85 | 2014-10-31 01:22:00 +0000 | [diff] [blame] | 298 | *val = comp_press; |
| 299 | *val2 = 256000; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 300 | |
Hartmut Knaack | 81ebe85 | 2014-10-31 01:22:00 +0000 | [diff] [blame] | 301 | return IIO_VAL_FRACTIONAL; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 302 | } |
| 303 | |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 304 | static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2) |
| 305 | { |
| 306 | int ret; |
| 307 | __be16 tmp = 0; |
| 308 | s32 adc_humidity; |
| 309 | u32 comp_humidity; |
| 310 | |
| 311 | /* Read and compensate temperature so we get a reading of t_fine. */ |
| 312 | ret = bmp280_read_temp(data, NULL); |
| 313 | if (ret < 0) |
| 314 | return ret; |
| 315 | |
| 316 | ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB, |
| 317 | (u8 *) &tmp, 2); |
| 318 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 319 | dev_err(data->dev, "failed to read humidity\n"); |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | adc_humidity = be16_to_cpu(tmp); |
| 324 | comp_humidity = bmp280_compensate_humidity(data, adc_humidity); |
| 325 | |
| 326 | *val = comp_humidity; |
| 327 | *val2 = 1024; |
| 328 | |
| 329 | return IIO_VAL_FRACTIONAL; |
| 330 | } |
| 331 | |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 332 | static int bmp280_read_raw(struct iio_dev *indio_dev, |
| 333 | struct iio_chan_spec const *chan, |
| 334 | int *val, int *val2, long mask) |
| 335 | { |
| 336 | int ret; |
| 337 | struct bmp280_data *data = iio_priv(indio_dev); |
| 338 | |
| 339 | mutex_lock(&data->lock); |
| 340 | |
| 341 | switch (mask) { |
| 342 | case IIO_CHAN_INFO_PROCESSED: |
| 343 | switch (chan->type) { |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 344 | case IIO_HUMIDITYRELATIVE: |
| 345 | ret = data->chip_info->read_humid(data, val, val2); |
| 346 | break; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 347 | case IIO_PRESSURE: |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 348 | ret = data->chip_info->read_press(data, val, val2); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 349 | break; |
| 350 | case IIO_TEMP: |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 351 | ret = data->chip_info->read_temp(data, val); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 352 | break; |
| 353 | default: |
| 354 | ret = -EINVAL; |
| 355 | break; |
| 356 | } |
| 357 | break; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 358 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 359 | switch (chan->type) { |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 360 | case IIO_HUMIDITYRELATIVE: |
| 361 | *val = 1 << data->oversampling_humid; |
| 362 | ret = IIO_VAL_INT; |
| 363 | break; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 364 | case IIO_PRESSURE: |
| 365 | *val = 1 << data->oversampling_press; |
| 366 | ret = IIO_VAL_INT; |
| 367 | break; |
| 368 | case IIO_TEMP: |
| 369 | *val = 1 << data->oversampling_temp; |
| 370 | ret = IIO_VAL_INT; |
| 371 | break; |
| 372 | default: |
| 373 | ret = -EINVAL; |
| 374 | break; |
| 375 | } |
| 376 | break; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 377 | default: |
| 378 | ret = -EINVAL; |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | mutex_unlock(&data->lock); |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 387 | static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data, |
| 388 | int val) |
| 389 | { |
| 390 | int i; |
| 391 | const int *avail = data->chip_info->oversampling_humid_avail; |
| 392 | const int n = data->chip_info->num_oversampling_humid_avail; |
| 393 | |
| 394 | for (i = 0; i < n; i++) { |
| 395 | if (avail[i] == val) { |
| 396 | data->oversampling_humid = ilog2(val); |
| 397 | |
| 398 | return data->chip_info->chip_config(data); |
| 399 | } |
| 400 | } |
| 401 | return -EINVAL; |
| 402 | } |
| 403 | |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 404 | static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data, |
| 405 | int val) |
| 406 | { |
| 407 | int i; |
| 408 | const int *avail = data->chip_info->oversampling_temp_avail; |
| 409 | const int n = data->chip_info->num_oversampling_temp_avail; |
| 410 | |
| 411 | for (i = 0; i < n; i++) { |
| 412 | if (avail[i] == val) { |
| 413 | data->oversampling_temp = ilog2(val); |
| 414 | |
| 415 | return data->chip_info->chip_config(data); |
| 416 | } |
| 417 | } |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | |
| 421 | static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data, |
| 422 | int val) |
| 423 | { |
| 424 | int i; |
| 425 | const int *avail = data->chip_info->oversampling_press_avail; |
| 426 | const int n = data->chip_info->num_oversampling_press_avail; |
| 427 | |
| 428 | for (i = 0; i < n; i++) { |
| 429 | if (avail[i] == val) { |
| 430 | data->oversampling_press = ilog2(val); |
| 431 | |
| 432 | return data->chip_info->chip_config(data); |
| 433 | } |
| 434 | } |
| 435 | return -EINVAL; |
| 436 | } |
| 437 | |
| 438 | static int bmp280_write_raw(struct iio_dev *indio_dev, |
| 439 | struct iio_chan_spec const *chan, |
| 440 | int val, int val2, long mask) |
| 441 | { |
| 442 | int ret = 0; |
| 443 | struct bmp280_data *data = iio_priv(indio_dev); |
| 444 | |
| 445 | switch (mask) { |
| 446 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 447 | mutex_lock(&data->lock); |
| 448 | switch (chan->type) { |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 449 | case IIO_HUMIDITYRELATIVE: |
| 450 | ret = bmp280_write_oversampling_ratio_humid(data, val); |
| 451 | break; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 452 | case IIO_PRESSURE: |
| 453 | ret = bmp280_write_oversampling_ratio_press(data, val); |
| 454 | break; |
| 455 | case IIO_TEMP: |
| 456 | ret = bmp280_write_oversampling_ratio_temp(data, val); |
| 457 | break; |
| 458 | default: |
| 459 | ret = -EINVAL; |
| 460 | break; |
| 461 | } |
| 462 | mutex_unlock(&data->lock); |
| 463 | break; |
| 464 | default: |
| 465 | return -EINVAL; |
| 466 | } |
| 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n) |
| 472 | { |
| 473 | size_t len = 0; |
| 474 | int i; |
| 475 | |
| 476 | for (i = 0; i < n; i++) |
| 477 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]); |
| 478 | |
| 479 | buf[len - 1] = '\n'; |
| 480 | |
| 481 | return len; |
| 482 | } |
| 483 | |
| 484 | static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev, |
| 485 | struct device_attribute *attr, char *buf) |
| 486 | { |
| 487 | struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev)); |
| 488 | |
| 489 | return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail, |
| 490 | data->chip_info->num_oversampling_temp_avail); |
| 491 | } |
| 492 | |
| 493 | static ssize_t bmp280_show_press_oversampling_avail(struct device *dev, |
| 494 | struct device_attribute *attr, char *buf) |
| 495 | { |
| 496 | struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev)); |
| 497 | |
| 498 | return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail, |
| 499 | data->chip_info->num_oversampling_press_avail); |
| 500 | } |
| 501 | |
| 502 | static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available, |
| 503 | S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0); |
| 504 | |
| 505 | static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available, |
| 506 | S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0); |
| 507 | |
| 508 | static struct attribute *bmp280_attributes[] = { |
| 509 | &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr, |
| 510 | &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr, |
| 511 | NULL, |
| 512 | }; |
| 513 | |
| 514 | static const struct attribute_group bmp280_attrs_group = { |
| 515 | .attrs = bmp280_attributes, |
| 516 | }; |
| 517 | |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 518 | static const struct iio_info bmp280_info = { |
| 519 | .driver_module = THIS_MODULE, |
| 520 | .read_raw = &bmp280_read_raw, |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 521 | .write_raw = &bmp280_write_raw, |
| 522 | .attrs = &bmp280_attrs_group, |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 523 | }; |
| 524 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 525 | static int bmp280_chip_config(struct bmp280_data *data) |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 526 | { |
| 527 | int ret; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 528 | u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) | |
| 529 | BMP280_OSRS_PRESS_X(data->oversampling_press + 1); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 530 | |
| 531 | ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS, |
| 532 | BMP280_OSRS_TEMP_MASK | |
| 533 | BMP280_OSRS_PRESS_MASK | |
| 534 | BMP280_MODE_MASK, |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 535 | osrs | BMP280_MODE_NORMAL); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 536 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 537 | dev_err(data->dev, |
Hartmut Knaack | 44cf379 | 2014-12-19 23:59:25 +0100 | [diff] [blame] | 538 | "failed to write ctrl_meas register\n"); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG, |
| 543 | BMP280_FILTER_MASK, |
| 544 | BMP280_FILTER_4X); |
| 545 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 546 | dev_err(data->dev, |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 547 | "failed to write config register\n"); |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | return ret; |
| 552 | } |
| 553 | |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 554 | static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; |
| 555 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 556 | static const struct bmp280_chip_info bmp280_chip_info = { |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 557 | .oversampling_temp_avail = bmp280_oversampling_avail, |
| 558 | .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), |
| 559 | |
| 560 | .oversampling_press_avail = bmp280_oversampling_avail, |
| 561 | .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), |
| 562 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 563 | .chip_config = bmp280_chip_config, |
| 564 | .read_temp = bmp280_read_temp, |
| 565 | .read_press = bmp280_read_press, |
| 566 | }; |
| 567 | |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 568 | static int bme280_chip_config(struct bmp280_data *data) |
| 569 | { |
| 570 | int ret = bmp280_chip_config(data); |
| 571 | u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1); |
| 572 | |
| 573 | if (ret < 0) |
| 574 | return ret; |
| 575 | |
| 576 | return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY, |
| 577 | BMP280_OSRS_HUMIDITY_MASK, osrs); |
| 578 | } |
| 579 | |
| 580 | static const struct bmp280_chip_info bme280_chip_info = { |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 581 | .oversampling_temp_avail = bmp280_oversampling_avail, |
| 582 | .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), |
| 583 | |
| 584 | .oversampling_press_avail = bmp280_oversampling_avail, |
| 585 | .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), |
| 586 | |
| 587 | .oversampling_humid_avail = bmp280_oversampling_avail, |
| 588 | .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), |
| 589 | |
| 590 | .chip_config = bme280_chip_config, |
| 591 | .read_temp = bmp280_read_temp, |
| 592 | .read_press = bmp280_read_press, |
| 593 | .read_humid = bmp280_read_humid, |
| 594 | }; |
| 595 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 596 | static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas) |
| 597 | { |
| 598 | int ret; |
| 599 | const int conversion_time_max[] = { 4500, 7500, 13500, 25500 }; |
| 600 | unsigned int delay_us; |
| 601 | unsigned int ctrl; |
| 602 | |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 603 | if (data->use_eoc) |
| 604 | init_completion(&data->done); |
| 605 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 606 | ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas); |
| 607 | if (ret) |
| 608 | return ret; |
| 609 | |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 610 | if (data->use_eoc) { |
| 611 | /* |
| 612 | * If we have a completion interrupt, use it, wait up to |
| 613 | * 100ms. The longest conversion time listed is 76.5 ms for |
| 614 | * advanced resolution mode. |
| 615 | */ |
| 616 | ret = wait_for_completion_timeout(&data->done, |
| 617 | 1 + msecs_to_jiffies(100)); |
| 618 | if (!ret) |
| 619 | dev_err(data->dev, "timeout waiting for completion\n"); |
| 620 | } else { |
| 621 | if (ctrl_meas == BMP180_MEAS_TEMP) |
| 622 | delay_us = 4500; |
| 623 | else |
| 624 | delay_us = |
| 625 | conversion_time_max[data->oversampling_press]; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 626 | |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 627 | usleep_range(delay_us, delay_us + 1000); |
| 628 | } |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 629 | |
| 630 | ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl); |
| 631 | if (ret) |
| 632 | return ret; |
| 633 | |
| 634 | /* The value of this bit reset to "0" after conversion is complete */ |
| 635 | if (ctrl & BMP180_MEAS_SCO) |
| 636 | return -EIO; |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static int bmp180_read_adc_temp(struct bmp280_data *data, int *val) |
| 642 | { |
| 643 | int ret; |
| 644 | __be16 tmp = 0; |
| 645 | |
| 646 | ret = bmp180_measure(data, BMP180_MEAS_TEMP); |
| 647 | if (ret) |
| 648 | return ret; |
| 649 | |
| 650 | ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2); |
| 651 | if (ret) |
| 652 | return ret; |
| 653 | |
| 654 | *val = be16_to_cpu(tmp); |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * These enums are used for indexing into the array of calibration |
| 661 | * coefficients for BMP180. |
| 662 | */ |
| 663 | enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD }; |
| 664 | |
| 665 | struct bmp180_calib { |
| 666 | s16 AC1; |
| 667 | s16 AC2; |
| 668 | s16 AC3; |
| 669 | u16 AC4; |
| 670 | u16 AC5; |
| 671 | u16 AC6; |
| 672 | s16 B1; |
| 673 | s16 B2; |
| 674 | s16 MB; |
| 675 | s16 MC; |
| 676 | s16 MD; |
| 677 | }; |
| 678 | |
| 679 | static int bmp180_read_calib(struct bmp280_data *data, |
| 680 | struct bmp180_calib *calib) |
| 681 | { |
| 682 | int ret; |
| 683 | int i; |
| 684 | __be16 buf[BMP180_REG_CALIB_COUNT / 2]; |
| 685 | |
| 686 | ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf, |
| 687 | sizeof(buf)); |
| 688 | |
| 689 | if (ret < 0) |
| 690 | return ret; |
| 691 | |
| 692 | /* None of the words has the value 0 or 0xFFFF */ |
| 693 | for (i = 0; i < ARRAY_SIZE(buf); i++) { |
| 694 | if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff)) |
| 695 | return -EIO; |
| 696 | } |
| 697 | |
| 698 | calib->AC1 = be16_to_cpu(buf[AC1]); |
| 699 | calib->AC2 = be16_to_cpu(buf[AC2]); |
| 700 | calib->AC3 = be16_to_cpu(buf[AC3]); |
| 701 | calib->AC4 = be16_to_cpu(buf[AC4]); |
| 702 | calib->AC5 = be16_to_cpu(buf[AC5]); |
| 703 | calib->AC6 = be16_to_cpu(buf[AC6]); |
| 704 | calib->B1 = be16_to_cpu(buf[B1]); |
| 705 | calib->B2 = be16_to_cpu(buf[B2]); |
| 706 | calib->MB = be16_to_cpu(buf[MB]); |
| 707 | calib->MC = be16_to_cpu(buf[MC]); |
| 708 | calib->MD = be16_to_cpu(buf[MD]); |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Returns temperature in DegC, resolution is 0.1 DegC. |
| 715 | * t_fine carries fine temperature as global value. |
| 716 | * |
| 717 | * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". |
| 718 | */ |
| 719 | static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp) |
| 720 | { |
| 721 | int ret; |
| 722 | s32 x1, x2; |
| 723 | struct bmp180_calib calib; |
| 724 | |
| 725 | ret = bmp180_read_calib(data, &calib); |
| 726 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 727 | dev_err(data->dev, |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 728 | "failed to read calibration coefficients\n"); |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15; |
| 733 | x2 = (calib.MC << 11) / (x1 + calib.MD); |
| 734 | data->t_fine = x1 + x2; |
| 735 | |
| 736 | return (data->t_fine + 8) >> 4; |
| 737 | } |
| 738 | |
| 739 | static int bmp180_read_temp(struct bmp280_data *data, int *val) |
| 740 | { |
| 741 | int ret; |
| 742 | s32 adc_temp, comp_temp; |
| 743 | |
| 744 | ret = bmp180_read_adc_temp(data, &adc_temp); |
| 745 | if (ret) |
| 746 | return ret; |
| 747 | |
| 748 | comp_temp = bmp180_compensate_temp(data, adc_temp); |
| 749 | |
| 750 | /* |
| 751 | * val might be NULL if we're called by the read_press routine, |
| 752 | * who only cares about the carry over t_fine value. |
| 753 | */ |
| 754 | if (val) { |
| 755 | *val = comp_temp * 100; |
| 756 | return IIO_VAL_INT; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int bmp180_read_adc_press(struct bmp280_data *data, int *val) |
| 763 | { |
| 764 | int ret; |
| 765 | __be32 tmp = 0; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 766 | u8 oss = data->oversampling_press; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 767 | |
| 768 | ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss)); |
| 769 | if (ret) |
| 770 | return ret; |
| 771 | |
| 772 | ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | |
| 776 | *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Returns pressure in Pa, resolution is 1 Pa. |
| 783 | * |
| 784 | * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". |
| 785 | */ |
| 786 | static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press) |
| 787 | { |
| 788 | int ret; |
| 789 | s32 x1, x2, x3, p; |
| 790 | s32 b3, b6; |
| 791 | u32 b4, b7; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 792 | s32 oss = data->oversampling_press; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 793 | struct bmp180_calib calib; |
| 794 | |
| 795 | ret = bmp180_read_calib(data, &calib); |
| 796 | if (ret < 0) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 797 | dev_err(data->dev, |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 798 | "failed to read calibration coefficients\n"); |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | b6 = data->t_fine - 4000; |
| 803 | x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11; |
| 804 | x2 = calib.AC2 * b6 >> 11; |
| 805 | x3 = x1 + x2; |
| 806 | b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4; |
| 807 | x1 = calib.AC3 * b6 >> 13; |
| 808 | x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16; |
| 809 | x3 = (x1 + x2 + 2) >> 2; |
| 810 | b4 = calib.AC4 * (u32)(x3 + 32768) >> 15; |
| 811 | b7 = ((u32)adc_press - b3) * (50000 >> oss); |
| 812 | if (b7 < 0x80000000) |
| 813 | p = (b7 * 2) / b4; |
| 814 | else |
| 815 | p = (b7 / b4) * 2; |
| 816 | |
| 817 | x1 = (p >> 8) * (p >> 8); |
| 818 | x1 = (x1 * 3038) >> 16; |
| 819 | x2 = (-7357 * p) >> 16; |
| 820 | |
| 821 | return p + ((x1 + x2 + 3791) >> 4); |
| 822 | } |
| 823 | |
| 824 | static int bmp180_read_press(struct bmp280_data *data, |
| 825 | int *val, int *val2) |
| 826 | { |
| 827 | int ret; |
| 828 | s32 adc_press; |
| 829 | u32 comp_press; |
| 830 | |
| 831 | /* Read and compensate temperature so we get a reading of t_fine. */ |
| 832 | ret = bmp180_read_temp(data, NULL); |
| 833 | if (ret) |
| 834 | return ret; |
| 835 | |
| 836 | ret = bmp180_read_adc_press(data, &adc_press); |
| 837 | if (ret) |
| 838 | return ret; |
| 839 | |
| 840 | comp_press = bmp180_compensate_press(data, adc_press); |
| 841 | |
| 842 | *val = comp_press; |
| 843 | *val2 = 1000; |
| 844 | |
| 845 | return IIO_VAL_FRACTIONAL; |
| 846 | } |
| 847 | |
| 848 | static int bmp180_chip_config(struct bmp280_data *data) |
| 849 | { |
| 850 | return 0; |
| 851 | } |
| 852 | |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 853 | static const int bmp180_oversampling_temp_avail[] = { 1 }; |
| 854 | static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; |
| 855 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 856 | static const struct bmp280_chip_info bmp180_chip_info = { |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 857 | .oversampling_temp_avail = bmp180_oversampling_temp_avail, |
| 858 | .num_oversampling_temp_avail = |
| 859 | ARRAY_SIZE(bmp180_oversampling_temp_avail), |
| 860 | |
| 861 | .oversampling_press_avail = bmp180_oversampling_press_avail, |
| 862 | .num_oversampling_press_avail = |
| 863 | ARRAY_SIZE(bmp180_oversampling_press_avail), |
| 864 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 865 | .chip_config = bmp180_chip_config, |
| 866 | .read_temp = bmp180_read_temp, |
| 867 | .read_press = bmp180_read_press, |
| 868 | }; |
| 869 | |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 870 | static irqreturn_t bmp085_eoc_irq(int irq, void *d) |
| 871 | { |
| 872 | struct bmp280_data *data = d; |
| 873 | |
| 874 | complete(&data->done); |
| 875 | |
| 876 | return IRQ_HANDLED; |
| 877 | } |
| 878 | |
| 879 | static int bmp085_fetch_eoc_irq(struct device *dev, |
| 880 | const char *name, |
| 881 | int irq, |
| 882 | struct bmp280_data *data) |
| 883 | { |
| 884 | unsigned long irq_trig; |
| 885 | int ret; |
| 886 | |
| 887 | irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq)); |
| 888 | if (irq_trig != IRQF_TRIGGER_RISING) { |
| 889 | dev_err(dev, "non-rising trigger given for EOC interrupt, " |
| 890 | "trying to enforce it\n"); |
| 891 | irq_trig = IRQF_TRIGGER_RISING; |
| 892 | } |
| 893 | ret = devm_request_threaded_irq(dev, |
| 894 | irq, |
| 895 | bmp085_eoc_irq, |
| 896 | NULL, |
| 897 | irq_trig, |
| 898 | name, |
| 899 | data); |
| 900 | if (ret) { |
| 901 | /* Bail out without IRQ but keep the driver in place */ |
| 902 | dev_err(dev, "unable to request DRDY IRQ\n"); |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | data->use_eoc = true; |
| 907 | return 0; |
| 908 | } |
| 909 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 910 | int bmp280_common_probe(struct device *dev, |
| 911 | struct regmap *regmap, |
| 912 | unsigned int chip, |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 913 | const char *name, |
| 914 | int irq) |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 915 | { |
| 916 | int ret; |
| 917 | struct iio_dev *indio_dev; |
| 918 | struct bmp280_data *data; |
| 919 | unsigned int chip_id; |
Linus Walleij | c5842b4 | 2016-06-30 03:48:47 +0200 | [diff] [blame] | 920 | struct gpio_desc *gpiod; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 921 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 922 | indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 923 | if (!indio_dev) |
| 924 | return -ENOMEM; |
| 925 | |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 926 | data = iio_priv(indio_dev); |
| 927 | mutex_init(&data->lock); |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 928 | data->dev = dev; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 929 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 930 | indio_dev->dev.parent = dev; |
| 931 | indio_dev->name = name; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 932 | indio_dev->channels = bmp280_channels; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 933 | indio_dev->info = &bmp280_info; |
| 934 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 935 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 936 | switch (chip) { |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 937 | case BMP180_CHIP_ID: |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 938 | indio_dev->num_channels = 2; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 939 | data->chip_info = &bmp180_chip_info; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 940 | data->oversampling_press = ilog2(8); |
| 941 | data->oversampling_temp = ilog2(1); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 942 | data->start_up_time = 10; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 943 | break; |
| 944 | case BMP280_CHIP_ID: |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 945 | indio_dev->num_channels = 2; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 946 | data->chip_info = &bmp280_chip_info; |
Akinobu Mita | 6297990 | 2016-04-24 22:52:11 +0900 | [diff] [blame] | 947 | data->oversampling_press = ilog2(16); |
| 948 | data->oversampling_temp = ilog2(2); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 949 | data->start_up_time = 2; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 950 | break; |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 951 | case BME280_CHIP_ID: |
| 952 | indio_dev->num_channels = 3; |
| 953 | data->chip_info = &bme280_chip_info; |
| 954 | data->oversampling_press = ilog2(16); |
| 955 | data->oversampling_humid = ilog2(16); |
| 956 | data->oversampling_temp = ilog2(2); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 957 | data->start_up_time = 2; |
Matt Ranostay | 14beaa8 | 2016-05-04 22:57:30 -0700 | [diff] [blame] | 958 | break; |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 959 | default: |
| 960 | return -EINVAL; |
| 961 | } |
| 962 | |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 963 | /* Bring up regulators */ |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 964 | data->vddd = devm_regulator_get(dev, "vddd"); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 965 | if (IS_ERR(data->vddd)) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 966 | dev_err(dev, "failed to get VDDD regulator\n"); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 967 | return PTR_ERR(data->vddd); |
| 968 | } |
| 969 | ret = regulator_enable(data->vddd); |
| 970 | if (ret) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 971 | dev_err(dev, "failed to enable VDDD regulator\n"); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 972 | return ret; |
| 973 | } |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 974 | data->vdda = devm_regulator_get(dev, "vdda"); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 975 | if (IS_ERR(data->vdda)) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 976 | dev_err(dev, "failed to get VDDA regulator\n"); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 977 | ret = PTR_ERR(data->vddd); |
| 978 | goto out_disable_vddd; |
| 979 | } |
| 980 | ret = regulator_enable(data->vdda); |
| 981 | if (ret) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 982 | dev_err(dev, "failed to enable VDDA regulator\n"); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 983 | goto out_disable_vddd; |
| 984 | } |
| 985 | /* Wait to make sure we started up properly */ |
| 986 | mdelay(data->start_up_time); |
| 987 | |
Linus Walleij | c5842b4 | 2016-06-30 03:48:47 +0200 | [diff] [blame] | 988 | /* Bring chip out of reset if there is an assigned GPIO line */ |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 989 | gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); |
Linus Walleij | c5842b4 | 2016-06-30 03:48:47 +0200 | [diff] [blame] | 990 | /* Deassert the signal */ |
| 991 | if (!IS_ERR(gpiod)) { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 992 | dev_info(dev, "release reset\n"); |
Linus Walleij | c5842b4 | 2016-06-30 03:48:47 +0200 | [diff] [blame] | 993 | gpiod_set_value(gpiod, 0); |
| 994 | } |
| 995 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 996 | data->regmap = regmap; |
| 997 | ret = regmap_read(regmap, BMP280_REG_ID, &chip_id); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 998 | if (ret < 0) |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 999 | goto out_disable_vdda; |
| 1000 | if (chip_id != chip) { |
| 1001 | dev_err(dev, "bad chip id: expected %x got %x\n", |
| 1002 | chip, chip_id); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 1003 | ret = -EINVAL; |
| 1004 | goto out_disable_vdda; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 1005 | } |
| 1006 | |
Akinobu Mita | 6dba72e | 2016-04-24 22:52:10 +0900 | [diff] [blame] | 1007 | ret = data->chip_info->chip_config(data); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 1008 | if (ret < 0) |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 1009 | goto out_disable_vdda; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 1010 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 1011 | dev_set_drvdata(dev, indio_dev); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 1012 | |
Linus Walleij | aae9539 | 2016-06-30 03:48:52 +0200 | [diff] [blame^] | 1013 | /* |
| 1014 | * Attempt to grab an optional EOC IRQ - only the BMP085 has this |
| 1015 | * however as it happens, the BMP085 shares the chip ID of BMP180 |
| 1016 | * so we look for an IRQ if we have that. |
| 1017 | */ |
| 1018 | if (irq > 0 || (chip_id == BMP180_CHIP_ID)) { |
| 1019 | ret = bmp085_fetch_eoc_irq(dev, name, irq, data); |
| 1020 | if (ret) |
| 1021 | goto out_disable_vdda; |
| 1022 | } |
| 1023 | |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 1024 | ret = iio_device_register(indio_dev); |
| 1025 | if (ret) |
| 1026 | goto out_disable_vdda; |
| 1027 | |
| 1028 | return 0; |
| 1029 | |
| 1030 | out_disable_vdda: |
| 1031 | regulator_disable(data->vdda); |
| 1032 | out_disable_vddd: |
| 1033 | regulator_disable(data->vddd); |
| 1034 | return ret; |
| 1035 | } |
Linus Walleij | 1711884 | 2016-06-30 03:48:50 +0200 | [diff] [blame] | 1036 | EXPORT_SYMBOL(bmp280_common_probe); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 1037 | |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 1038 | int bmp280_common_remove(struct device *dev) |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 1039 | { |
Linus Walleij | 14e8015 | 2016-06-30 03:48:49 +0200 | [diff] [blame] | 1040 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Linus Walleij | bd525e6 | 2016-06-30 03:48:48 +0200 | [diff] [blame] | 1041 | struct bmp280_data *data = iio_priv(indio_dev); |
| 1042 | |
| 1043 | iio_device_unregister(indio_dev); |
| 1044 | regulator_disable(data->vdda); |
| 1045 | regulator_disable(data->vddd); |
| 1046 | return 0; |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 1047 | } |
Linus Walleij | 1711884 | 2016-06-30 03:48:50 +0200 | [diff] [blame] | 1048 | EXPORT_SYMBOL(bmp280_common_remove); |
| 1049 | |
| 1050 | MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); |
| 1051 | MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor"); |
| 1052 | MODULE_LICENSE("GPL v2"); |