Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Intel Corporation |
| 3 | * |
| 4 | * Driver for Bosch Sensortec BMP280 digital pressure sensor. |
| 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 | * |
| 10 | */ |
| 11 | |
| 12 | #define pr_fmt(fmt) "bmp280: " fmt |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/acpi.h> |
| 17 | #include <linux/regmap.h> |
| 18 | #include <linux/iio/iio.h> |
| 19 | #include <linux/iio/sysfs.h> |
| 20 | |
| 21 | #define BMP280_REG_TEMP_XLSB 0xFC |
| 22 | #define BMP280_REG_TEMP_LSB 0xFB |
| 23 | #define BMP280_REG_TEMP_MSB 0xFA |
| 24 | #define BMP280_REG_PRESS_XLSB 0xF9 |
| 25 | #define BMP280_REG_PRESS_LSB 0xF8 |
| 26 | #define BMP280_REG_PRESS_MSB 0xF7 |
| 27 | |
| 28 | #define BMP280_REG_CONFIG 0xF5 |
| 29 | #define BMP280_REG_CTRL_MEAS 0xF4 |
| 30 | #define BMP280_REG_STATUS 0xF3 |
| 31 | #define BMP280_REG_RESET 0xE0 |
| 32 | #define BMP280_REG_ID 0xD0 |
| 33 | |
| 34 | #define BMP280_REG_COMP_TEMP_START 0x88 |
| 35 | #define BMP280_COMP_TEMP_REG_COUNT 6 |
| 36 | |
| 37 | #define BMP280_REG_COMP_PRESS_START 0x8E |
| 38 | #define BMP280_COMP_PRESS_REG_COUNT 18 |
| 39 | |
| 40 | #define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2)) |
| 41 | #define BMP280_FILTER_OFF 0 |
| 42 | #define BMP280_FILTER_2X BIT(2) |
| 43 | #define BMP280_FILTER_4X BIT(3) |
| 44 | #define BMP280_FILTER_8X (BIT(3) | BIT(2)) |
| 45 | #define BMP280_FILTER_16X BIT(4) |
| 46 | |
| 47 | #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5)) |
| 48 | #define BMP280_OSRS_TEMP_SKIP 0 |
| 49 | #define BMP280_OSRS_TEMP_1X BIT(5) |
| 50 | #define BMP280_OSRS_TEMP_2X BIT(6) |
| 51 | #define BMP280_OSRS_TEMP_4X (BIT(6) | BIT(5)) |
| 52 | #define BMP280_OSRS_TEMP_8X BIT(7) |
| 53 | #define BMP280_OSRS_TEMP_16X (BIT(7) | BIT(5)) |
| 54 | |
| 55 | #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2)) |
| 56 | #define BMP280_OSRS_PRESS_SKIP 0 |
| 57 | #define BMP280_OSRS_PRESS_1X BIT(2) |
| 58 | #define BMP280_OSRS_PRESS_2X BIT(3) |
| 59 | #define BMP280_OSRS_PRESS_4X (BIT(3) | BIT(2)) |
| 60 | #define BMP280_OSRS_PRESS_8X BIT(4) |
| 61 | #define BMP280_OSRS_PRESS_16X (BIT(4) | BIT(2)) |
| 62 | |
| 63 | #define BMP280_MODE_MASK (BIT(1) | BIT(0)) |
| 64 | #define BMP280_MODE_SLEEP 0 |
| 65 | #define BMP280_MODE_FORCED BIT(0) |
| 66 | #define BMP280_MODE_NORMAL (BIT(1) | BIT(0)) |
| 67 | |
| 68 | #define BMP280_CHIP_ID 0x58 |
| 69 | #define BMP280_SOFT_RESET_VAL 0xB6 |
| 70 | |
| 71 | struct bmp280_data { |
| 72 | struct i2c_client *client; |
| 73 | struct mutex lock; |
| 74 | struct regmap *regmap; |
| 75 | |
| 76 | /* |
| 77 | * Carryover value from temperature conversion, used in pressure |
| 78 | * calculation. |
| 79 | */ |
| 80 | s32 t_fine; |
| 81 | }; |
| 82 | |
| 83 | /* Compensation parameters. */ |
| 84 | struct bmp280_comp_temp { |
| 85 | u16 dig_t1; |
| 86 | s16 dig_t2, dig_t3; |
| 87 | }; |
| 88 | |
| 89 | struct bmp280_comp_press { |
| 90 | u16 dig_p1; |
| 91 | s16 dig_p2, dig_p3, dig_p4, dig_p5, dig_p6, dig_p7, dig_p8, dig_p9; |
| 92 | }; |
| 93 | |
| 94 | static const struct iio_chan_spec bmp280_channels[] = { |
| 95 | { |
| 96 | .type = IIO_PRESSURE, |
| 97 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
| 98 | }, |
| 99 | { |
| 100 | .type = IIO_TEMP, |
| 101 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
| 102 | }, |
| 103 | }; |
| 104 | |
| 105 | static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg) |
| 106 | { |
| 107 | switch (reg) { |
| 108 | case BMP280_REG_CONFIG: |
| 109 | case BMP280_REG_CTRL_MEAS: |
| 110 | case BMP280_REG_RESET: |
| 111 | return true; |
| 112 | default: |
| 113 | return false; |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg) |
| 118 | { |
| 119 | switch (reg) { |
| 120 | case BMP280_REG_TEMP_XLSB: |
| 121 | case BMP280_REG_TEMP_LSB: |
| 122 | case BMP280_REG_TEMP_MSB: |
| 123 | case BMP280_REG_PRESS_XLSB: |
| 124 | case BMP280_REG_PRESS_LSB: |
| 125 | case BMP280_REG_PRESS_MSB: |
| 126 | case BMP280_REG_STATUS: |
| 127 | return true; |
| 128 | default: |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static const struct regmap_config bmp280_regmap_config = { |
| 134 | .reg_bits = 8, |
| 135 | .val_bits = 8, |
| 136 | |
| 137 | .max_register = BMP280_REG_TEMP_XLSB, |
| 138 | .cache_type = REGCACHE_RBTREE, |
| 139 | |
| 140 | .writeable_reg = bmp280_is_writeable_reg, |
| 141 | .volatile_reg = bmp280_is_volatile_reg, |
| 142 | }; |
| 143 | |
| 144 | static int bmp280_read_compensation_temp(struct bmp280_data *data, |
| 145 | struct bmp280_comp_temp *comp) |
| 146 | { |
| 147 | int ret; |
| 148 | __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2]; |
| 149 | |
| 150 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START, |
| 151 | buf, BMP280_COMP_TEMP_REG_COUNT); |
| 152 | if (ret < 0) { |
| 153 | dev_err(&data->client->dev, |
| 154 | "failed to read temperature calibration parameters\n"); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | comp->dig_t1 = (u16) le16_to_cpu(buf[0]); |
| 159 | comp->dig_t2 = (s16) le16_to_cpu(buf[1]); |
| 160 | comp->dig_t3 = (s16) le16_to_cpu(buf[2]); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int bmp280_read_compensation_press(struct bmp280_data *data, |
| 166 | struct bmp280_comp_press *comp) |
| 167 | { |
| 168 | int ret; |
| 169 | __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2]; |
| 170 | |
| 171 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START, |
| 172 | buf, BMP280_COMP_PRESS_REG_COUNT); |
| 173 | if (ret < 0) { |
| 174 | dev_err(&data->client->dev, |
| 175 | "failed to read pressure calibration parameters\n"); |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | comp->dig_p1 = (s16) le16_to_cpu(buf[0]); |
| 180 | comp->dig_p2 = (u16) le16_to_cpu(buf[1]); |
| 181 | comp->dig_p3 = (u16) le16_to_cpu(buf[2]); |
| 182 | comp->dig_p4 = (u16) le16_to_cpu(buf[3]); |
| 183 | comp->dig_p5 = (u16) le16_to_cpu(buf[4]); |
| 184 | comp->dig_p6 = (u16) le16_to_cpu(buf[5]); |
| 185 | comp->dig_p7 = (u16) le16_to_cpu(buf[6]); |
| 186 | comp->dig_p8 = (u16) le16_to_cpu(buf[7]); |
| 187 | comp->dig_p9 = (u16) le16_to_cpu(buf[8]); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Returns temperature in DegC, resolution is 0.01 DegC. Output value of |
| 194 | * "5123" equals 51.23 DegC. t_fine carries fine temperature as global |
| 195 | * value. |
| 196 | * |
| 197 | * Taken from datasheet, Section 3.11.3, "Compensation formula". |
| 198 | */ |
| 199 | static s32 bmp280_compensate_temp(struct bmp280_data *data, |
| 200 | struct bmp280_comp_temp *comp, |
| 201 | s32 adc_temp) |
| 202 | { |
| 203 | s32 var1, var2, t; |
| 204 | |
| 205 | var1 = (((adc_temp >> 3) - ((s32) comp->dig_t1 << 1)) * |
| 206 | ((s32) comp->dig_t2)) >> 11; |
| 207 | var2 = (((((adc_temp >> 4) - ((s32) comp->dig_t1)) * |
| 208 | ((adc_temp >> 4) - ((s32) comp->dig_t1))) >> 12) * |
| 209 | ((s32) comp->dig_t3)) >> 14; |
| 210 | |
| 211 | data->t_fine = var1 + var2; |
| 212 | t = (data->t_fine * 5 + 128) >> 8; |
| 213 | |
| 214 | return t; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 |
| 219 | * integer bits and 8 fractional bits). Output value of "24674867" |
| 220 | * represents 24674867/256 = 96386.2 Pa = 963.862 hPa |
| 221 | * |
| 222 | * Taken from datasheet, Section 3.11.3, "Compensation formula". |
| 223 | */ |
| 224 | static u32 bmp280_compensate_press(struct bmp280_data *data, |
| 225 | struct bmp280_comp_press *comp, |
| 226 | s32 adc_press) |
| 227 | { |
| 228 | s64 var1, var2, p; |
| 229 | |
| 230 | var1 = ((s64) data->t_fine) - 128000; |
| 231 | var2 = var1 * var1 * (s64) comp->dig_p6; |
| 232 | var2 = var2 + ((var1 * (s64) comp->dig_p5) << 17); |
| 233 | var2 = var2 + (((s64) comp->dig_p4) << 35); |
| 234 | var1 = ((var1 * var1 * (s64) comp->dig_p3) >> 8) + |
| 235 | ((var1 * (s64) comp->dig_p2) << 12); |
| 236 | var1 = (((((s64) 1) << 47) + var1)) * ((s64) comp->dig_p1) >> 33; |
| 237 | |
| 238 | if (var1 == 0) |
| 239 | return 0; |
| 240 | |
| 241 | p = ((((s64) 1048576 - adc_press) << 31) - var2) * 3125; |
Vlad Dogaru | 46ee98a | 2014-10-23 15:52:00 +0100 | [diff] [blame^] | 242 | p = div64_s64(p, var1); |
Vlad Dogaru | d5c9456 | 2014-10-21 11:09:58 +0300 | [diff] [blame] | 243 | var1 = (((s64) comp->dig_p9) * (p >> 13) * (p >> 13)) >> 25; |
| 244 | var2 = (((s64) comp->dig_p8) * p) >> 19; |
| 245 | p = ((p + var1 + var2) >> 8) + (((s64) comp->dig_p7) << 4); |
| 246 | |
| 247 | return (u32) p; |
| 248 | } |
| 249 | |
| 250 | static int bmp280_read_temp(struct bmp280_data *data, |
| 251 | int *val) |
| 252 | { |
| 253 | int ret; |
| 254 | __be32 tmp = 0; |
| 255 | s32 adc_temp, comp_temp; |
| 256 | struct bmp280_comp_temp comp; |
| 257 | |
| 258 | ret = bmp280_read_compensation_temp(data, &comp); |
| 259 | if (ret < 0) |
| 260 | return ret; |
| 261 | |
| 262 | ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, |
| 263 | (u8 *) &tmp, 3); |
| 264 | if (ret < 0) { |
| 265 | dev_err(&data->client->dev, "failed to read temperature\n"); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | adc_temp = be32_to_cpu(tmp) >> 12; |
| 270 | comp_temp = bmp280_compensate_temp(data, &comp, adc_temp); |
| 271 | |
| 272 | /* |
| 273 | * val might be NULL if we're called by the read_press routine, |
| 274 | * who only cares about the carry over t_fine value. |
| 275 | */ |
| 276 | if (val) { |
| 277 | *val = comp_temp * 10; |
| 278 | return IIO_VAL_INT; |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static int bmp280_read_press(struct bmp280_data *data, |
| 285 | int *val, int *val2) |
| 286 | { |
| 287 | int ret; |
| 288 | __be32 tmp = 0; |
| 289 | s32 adc_press; |
| 290 | u32 comp_press; |
| 291 | struct bmp280_comp_press comp; |
| 292 | |
| 293 | ret = bmp280_read_compensation_press(data, &comp); |
| 294 | if (ret < 0) |
| 295 | return ret; |
| 296 | |
| 297 | /* Read and compensate temperature so we get a reading of t_fine. */ |
| 298 | ret = bmp280_read_temp(data, NULL); |
| 299 | if (ret < 0) |
| 300 | return ret; |
| 301 | |
| 302 | ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, |
| 303 | (u8 *) &tmp, 3); |
| 304 | if (ret < 0) { |
| 305 | dev_err(&data->client->dev, "failed to read pressure\n"); |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | adc_press = be32_to_cpu(tmp) >> 12; |
| 310 | comp_press = bmp280_compensate_press(data, &comp, adc_press); |
| 311 | |
| 312 | *val = comp_press / 256000; |
| 313 | *val2 = comp_press * 1000000 / 256000; |
| 314 | |
| 315 | return IIO_VAL_INT_PLUS_MICRO; |
| 316 | } |
| 317 | |
| 318 | static int bmp280_read_raw(struct iio_dev *indio_dev, |
| 319 | struct iio_chan_spec const *chan, |
| 320 | int *val, int *val2, long mask) |
| 321 | { |
| 322 | int ret; |
| 323 | struct bmp280_data *data = iio_priv(indio_dev); |
| 324 | |
| 325 | mutex_lock(&data->lock); |
| 326 | |
| 327 | switch (mask) { |
| 328 | case IIO_CHAN_INFO_PROCESSED: |
| 329 | switch (chan->type) { |
| 330 | case IIO_PRESSURE: |
| 331 | ret = bmp280_read_press(data, val, val2); |
| 332 | break; |
| 333 | case IIO_TEMP: |
| 334 | ret = bmp280_read_temp(data, val); |
| 335 | break; |
| 336 | default: |
| 337 | ret = -EINVAL; |
| 338 | break; |
| 339 | } |
| 340 | break; |
| 341 | default: |
| 342 | ret = -EINVAL; |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | mutex_unlock(&data->lock); |
| 347 | |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | static const struct iio_info bmp280_info = { |
| 352 | .driver_module = THIS_MODULE, |
| 353 | .read_raw = &bmp280_read_raw, |
| 354 | }; |
| 355 | |
| 356 | static int bmp280_chip_init(struct bmp280_data *data) |
| 357 | { |
| 358 | int ret; |
| 359 | |
| 360 | ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS, |
| 361 | BMP280_OSRS_TEMP_MASK | |
| 362 | BMP280_OSRS_PRESS_MASK | |
| 363 | BMP280_MODE_MASK, |
| 364 | BMP280_OSRS_TEMP_2X | |
| 365 | BMP280_OSRS_PRESS_16X | |
| 366 | BMP280_MODE_NORMAL); |
| 367 | if (ret < 0) { |
| 368 | dev_err(&data->client->dev, |
| 369 | "failed to write config register\n"); |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG, |
| 374 | BMP280_FILTER_MASK, |
| 375 | BMP280_FILTER_4X); |
| 376 | if (ret < 0) { |
| 377 | dev_err(&data->client->dev, |
| 378 | "failed to write config register\n"); |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | static int bmp280_probe(struct i2c_client *client, |
| 386 | const struct i2c_device_id *id) |
| 387 | { |
| 388 | int ret; |
| 389 | struct iio_dev *indio_dev; |
| 390 | struct bmp280_data *data; |
| 391 | unsigned int chip_id; |
| 392 | |
| 393 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 394 | if (!indio_dev) |
| 395 | return -ENOMEM; |
| 396 | |
| 397 | i2c_set_clientdata(client, indio_dev); |
| 398 | data = iio_priv(indio_dev); |
| 399 | mutex_init(&data->lock); |
| 400 | data->client = client; |
| 401 | |
| 402 | indio_dev->dev.parent = &client->dev; |
| 403 | indio_dev->name = id->name; |
| 404 | indio_dev->channels = bmp280_channels; |
| 405 | indio_dev->num_channels = ARRAY_SIZE(bmp280_channels); |
| 406 | indio_dev->info = &bmp280_info; |
| 407 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 408 | |
| 409 | data->regmap = devm_regmap_init_i2c(client, &bmp280_regmap_config); |
| 410 | if (IS_ERR(data->regmap)) { |
| 411 | dev_err(&client->dev, "failed to allocate register map\n"); |
| 412 | return PTR_ERR(data->regmap); |
| 413 | } |
| 414 | |
| 415 | ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id); |
| 416 | if (ret < 0) |
| 417 | return ret; |
| 418 | if (chip_id != BMP280_CHIP_ID) { |
| 419 | dev_err(&client->dev, "bad chip id. expected %x got %x\n", |
| 420 | BMP280_CHIP_ID, chip_id); |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | |
| 424 | ret = bmp280_chip_init(data); |
| 425 | if (ret < 0) |
| 426 | return ret; |
| 427 | |
| 428 | return devm_iio_device_register(&client->dev, indio_dev); |
| 429 | } |
| 430 | |
| 431 | static const struct acpi_device_id bmp280_acpi_match[] = { |
| 432 | {"BMP0280", 0}, |
| 433 | { }, |
| 434 | }; |
| 435 | MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match); |
| 436 | |
| 437 | static const struct i2c_device_id bmp280_id[] = { |
| 438 | {"bmp280", 0}, |
| 439 | { }, |
| 440 | }; |
| 441 | MODULE_DEVICE_TABLE(i2c, bmp280_id); |
| 442 | |
| 443 | static struct i2c_driver bmp280_driver = { |
| 444 | .driver = { |
| 445 | .name = "bmp280", |
| 446 | .acpi_match_table = ACPI_PTR(bmp280_acpi_match), |
| 447 | }, |
| 448 | .probe = bmp280_probe, |
| 449 | .id_table = bmp280_id, |
| 450 | }; |
| 451 | module_i2c_driver(bmp280_driver); |
| 452 | |
| 453 | MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); |
| 454 | MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor"); |
| 455 | MODULE_LICENSE("GPL v2"); |