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