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