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