aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorAkinobu Mita <akinobu.mita@gmail.com>2016-04-24 09:52:10 -0400
committerJonathan Cameron <jic23@kernel.org>2016-04-25 14:03:14 -0400
commit6dba72eca7fb879bf2e0c8fdc784fb974cb4f9d5 (patch)
treef80000595f8ca8e709e6c58c1f4f8dc2d362611f /drivers/iio
parent06ad7ea10e2bcf70a602e504ea32ee6ef6d77aa9 (diff)
iio: pressure: bmp280: add support for BMP180
This adds support for the BMP180 to the bmp280 iio driver. The BMP180 has already been supported by misc/bmp085 driver but it doesn't use iio framework. This change adds the kconfig dependency not to be selected both of them in order to avoid any issues. Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com> Acked-by: Vlad Dogaru <vlad.dogaru@intel.com> Cc: Christoph Mair <christoph.mair@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/pressure/Kconfig7
-rw-r--r--drivers/iio/pressure/bmp280.c367
2 files changed, 357 insertions, 17 deletions
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index 54c616520512..cda9f128f3a4 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -6,12 +6,13 @@
6menu "Pressure sensors" 6menu "Pressure sensors"
7 7
8config BMP280 8config BMP280
9 tristate "Bosch Sensortec BMP280 pressure sensor driver" 9 tristate "Bosch Sensortec BMP180 and BMP280 pressure sensor driver"
10 depends on I2C 10 depends on I2C
11 depends on !(BMP085_I2C=y || BMP085_I2C=m)
11 select REGMAP_I2C 12 select REGMAP_I2C
12 help 13 help
13 Say yes here to build support for Bosch Sensortec BMP280 14 Say yes here to build support for Bosch Sensortec BMP180 and BMP280
14 pressure and temperature sensor. 15 pressure and temperature sensors.
15 16
16 To compile this driver as a module, choose M here: the module 17 To compile this driver as a module, choose M here: the module
17 will be called bmp280. 18 will be called bmp280.
diff --git a/drivers/iio/pressure/bmp280.c b/drivers/iio/pressure/bmp280.c
index a2602d8dd6d5..dcf3b0ae2b87 100644
--- a/drivers/iio/pressure/bmp280.c
+++ b/drivers/iio/pressure/bmp280.c
@@ -1,12 +1,15 @@
1/* 1/*
2 * Copyright (c) 2014 Intel Corporation 2 * Copyright (c) 2014 Intel Corporation
3 * 3 *
4 * Driver for Bosch Sensortec BMP280 digital pressure sensor. 4 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 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 7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 * 9 *
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
10 */ 13 */
11 14
12#define pr_fmt(fmt) "bmp280: " fmt 15#define pr_fmt(fmt) "bmp280: " fmt
@@ -15,9 +18,11 @@
15#include <linux/i2c.h> 18#include <linux/i2c.h>
16#include <linux/acpi.h> 19#include <linux/acpi.h>
17#include <linux/regmap.h> 20#include <linux/regmap.h>
21#include <linux/delay.h>
18#include <linux/iio/iio.h> 22#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h> 23#include <linux/iio/sysfs.h>
20 24
25/* BMP280 specific registers */
21#define BMP280_REG_TEMP_XLSB 0xFC 26#define BMP280_REG_TEMP_XLSB 0xFC
22#define BMP280_REG_TEMP_LSB 0xFB 27#define BMP280_REG_TEMP_LSB 0xFB
23#define BMP280_REG_TEMP_MSB 0xFA 28#define BMP280_REG_TEMP_MSB 0xFA
@@ -26,10 +31,7 @@
26#define BMP280_REG_PRESS_MSB 0xF7 31#define BMP280_REG_PRESS_MSB 0xF7
27 32
28#define BMP280_REG_CONFIG 0xF5 33#define BMP280_REG_CONFIG 0xF5
29#define BMP280_REG_CTRL_MEAS 0xF4
30#define BMP280_REG_STATUS 0xF3 34#define BMP280_REG_STATUS 0xF3
31#define BMP280_REG_RESET 0xE0
32#define BMP280_REG_ID 0xD0
33 35
34#define BMP280_REG_COMP_TEMP_START 0x88 36#define BMP280_REG_COMP_TEMP_START 0x88
35#define BMP280_COMP_TEMP_REG_COUNT 6 37#define BMP280_COMP_TEMP_REG_COUNT 6
@@ -65,6 +67,28 @@
65#define BMP280_MODE_FORCED BIT(0) 67#define BMP280_MODE_FORCED BIT(0)
66#define BMP280_MODE_NORMAL (BIT(1) | BIT(0)) 68#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
67 69
70/* BMP180 specific registers */
71#define BMP180_REG_OUT_XLSB 0xF8
72#define BMP180_REG_OUT_LSB 0xF7
73#define BMP180_REG_OUT_MSB 0xF6
74
75#define BMP180_REG_CALIB_START 0xAA
76#define BMP180_REG_CALIB_COUNT 22
77
78#define BMP180_MEAS_SCO BIT(5)
79#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
80#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
81#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
82#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
83#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
84#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
85
86/* BMP180 and BMP280 common registers */
87#define BMP280_REG_CTRL_MEAS 0xF4
88#define BMP280_REG_RESET 0xE0
89#define BMP280_REG_ID 0xD0
90
91#define BMP180_CHIP_ID 0x55
68#define BMP280_CHIP_ID 0x58 92#define BMP280_CHIP_ID 0x58
69#define BMP280_SOFT_RESET_VAL 0xB6 93#define BMP280_SOFT_RESET_VAL 0xB6
70 94
@@ -72,6 +96,7 @@ struct bmp280_data {
72 struct i2c_client *client; 96 struct i2c_client *client;
73 struct mutex lock; 97 struct mutex lock;
74 struct regmap *regmap; 98 struct regmap *regmap;
99 const struct bmp280_chip_info *chip_info;
75 100
76 /* 101 /*
77 * Carryover value from temperature conversion, used in pressure 102 * Carryover value from temperature conversion, used in pressure
@@ -80,9 +105,17 @@ struct bmp280_data {
80 s32 t_fine; 105 s32 t_fine;
81}; 106};
82 107
108struct bmp280_chip_info {
109 const struct regmap_config *regmap_config;
110
111 int (*chip_config)(struct bmp280_data *);
112 int (*read_temp)(struct bmp280_data *, int *);
113 int (*read_press)(struct bmp280_data *, int *, int *);
114};
115
83/* 116/*
84 * These enums are used for indexing into the array of compensation 117 * These enums are used for indexing into the array of compensation
85 * parameters. 118 * parameters for BMP280.
86 */ 119 */
87enum { T1, T2, T3 }; 120enum { T1, T2, T3 };
88enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; 121enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
@@ -290,10 +323,10 @@ static int bmp280_read_raw(struct iio_dev *indio_dev,
290 case IIO_CHAN_INFO_PROCESSED: 323 case IIO_CHAN_INFO_PROCESSED:
291 switch (chan->type) { 324 switch (chan->type) {
292 case IIO_PRESSURE: 325 case IIO_PRESSURE:
293 ret = bmp280_read_press(data, val, val2); 326 ret = data->chip_info->read_press(data, val, val2);
294 break; 327 break;
295 case IIO_TEMP: 328 case IIO_TEMP:
296 ret = bmp280_read_temp(data, val); 329 ret = data->chip_info->read_temp(data, val);
297 break; 330 break;
298 default: 331 default:
299 ret = -EINVAL; 332 ret = -EINVAL;
@@ -315,7 +348,7 @@ static const struct iio_info bmp280_info = {
315 .read_raw = &bmp280_read_raw, 348 .read_raw = &bmp280_read_raw,
316}; 349};
317 350
318static int bmp280_chip_init(struct bmp280_data *data) 351static int bmp280_chip_config(struct bmp280_data *data)
319{ 352{
320 int ret; 353 int ret;
321 354
@@ -344,6 +377,296 @@ static int bmp280_chip_init(struct bmp280_data *data)
344 return ret; 377 return ret;
345} 378}
346 379
380static const struct bmp280_chip_info bmp280_chip_info = {
381 .regmap_config = &bmp280_regmap_config,
382 .chip_config = bmp280_chip_config,
383 .read_temp = bmp280_read_temp,
384 .read_press = bmp280_read_press,
385};
386
387static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
388{
389 switch (reg) {
390 case BMP280_REG_CTRL_MEAS:
391 case BMP280_REG_RESET:
392 return true;
393 default:
394 return false;
395 };
396}
397
398static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
399{
400 switch (reg) {
401 case BMP180_REG_OUT_XLSB:
402 case BMP180_REG_OUT_LSB:
403 case BMP180_REG_OUT_MSB:
404 case BMP280_REG_CTRL_MEAS:
405 return true;
406 default:
407 return false;
408 }
409}
410
411static const struct regmap_config bmp180_regmap_config = {
412 .reg_bits = 8,
413 .val_bits = 8,
414
415 .max_register = BMP180_REG_OUT_XLSB,
416 .cache_type = REGCACHE_RBTREE,
417
418 .writeable_reg = bmp180_is_writeable_reg,
419 .volatile_reg = bmp180_is_volatile_reg,
420};
421
422static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
423{
424 int ret;
425 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
426 unsigned int delay_us;
427 unsigned int ctrl;
428
429 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
430 if (ret)
431 return ret;
432
433 if (ctrl_meas == BMP180_MEAS_TEMP)
434 delay_us = 4500;
435 else
436 delay_us = conversion_time_max[ilog2(8)];
437
438 usleep_range(delay_us, delay_us + 1000);
439
440 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
441 if (ret)
442 return ret;
443
444 /* The value of this bit reset to "0" after conversion is complete */
445 if (ctrl & BMP180_MEAS_SCO)
446 return -EIO;
447
448 return 0;
449}
450
451static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
452{
453 int ret;
454 __be16 tmp = 0;
455
456 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
457 if (ret)
458 return ret;
459
460 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
461 if (ret)
462 return ret;
463
464 *val = be16_to_cpu(tmp);
465
466 return 0;
467}
468
469/*
470 * These enums are used for indexing into the array of calibration
471 * coefficients for BMP180.
472 */
473enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
474
475struct bmp180_calib {
476 s16 AC1;
477 s16 AC2;
478 s16 AC3;
479 u16 AC4;
480 u16 AC5;
481 u16 AC6;
482 s16 B1;
483 s16 B2;
484 s16 MB;
485 s16 MC;
486 s16 MD;
487};
488
489static int bmp180_read_calib(struct bmp280_data *data,
490 struct bmp180_calib *calib)
491{
492 int ret;
493 int i;
494 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
495
496 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
497 sizeof(buf));
498
499 if (ret < 0)
500 return ret;
501
502 /* None of the words has the value 0 or 0xFFFF */
503 for (i = 0; i < ARRAY_SIZE(buf); i++) {
504 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
505 return -EIO;
506 }
507
508 calib->AC1 = be16_to_cpu(buf[AC1]);
509 calib->AC2 = be16_to_cpu(buf[AC2]);
510 calib->AC3 = be16_to_cpu(buf[AC3]);
511 calib->AC4 = be16_to_cpu(buf[AC4]);
512 calib->AC5 = be16_to_cpu(buf[AC5]);
513 calib->AC6 = be16_to_cpu(buf[AC6]);
514 calib->B1 = be16_to_cpu(buf[B1]);
515 calib->B2 = be16_to_cpu(buf[B2]);
516 calib->MB = be16_to_cpu(buf[MB]);
517 calib->MC = be16_to_cpu(buf[MC]);
518 calib->MD = be16_to_cpu(buf[MD]);
519
520 return 0;
521}
522
523/*
524 * Returns temperature in DegC, resolution is 0.1 DegC.
525 * t_fine carries fine temperature as global value.
526 *
527 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
528 */
529static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
530{
531 int ret;
532 s32 x1, x2;
533 struct bmp180_calib calib;
534
535 ret = bmp180_read_calib(data, &calib);
536 if (ret < 0) {
537 dev_err(&data->client->dev,
538 "failed to read calibration coefficients\n");
539 return ret;
540 }
541
542 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
543 x2 = (calib.MC << 11) / (x1 + calib.MD);
544 data->t_fine = x1 + x2;
545
546 return (data->t_fine + 8) >> 4;
547}
548
549static int bmp180_read_temp(struct bmp280_data *data, int *val)
550{
551 int ret;
552 s32 adc_temp, comp_temp;
553
554 ret = bmp180_read_adc_temp(data, &adc_temp);
555 if (ret)
556 return ret;
557
558 comp_temp = bmp180_compensate_temp(data, adc_temp);
559
560 /*
561 * val might be NULL if we're called by the read_press routine,
562 * who only cares about the carry over t_fine value.
563 */
564 if (val) {
565 *val = comp_temp * 100;
566 return IIO_VAL_INT;
567 }
568
569 return 0;
570}
571
572static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
573{
574 int ret;
575 __be32 tmp = 0;
576 u8 oss = ilog2(8);
577
578 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
579 if (ret)
580 return ret;
581
582 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
583 if (ret)
584 return ret;
585
586 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
587
588 return 0;
589}
590
591/*
592 * Returns pressure in Pa, resolution is 1 Pa.
593 *
594 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
595 */
596static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
597{
598 int ret;
599 s32 x1, x2, x3, p;
600 s32 b3, b6;
601 u32 b4, b7;
602 s32 oss = ilog2(8);
603 struct bmp180_calib calib;
604
605 ret = bmp180_read_calib(data, &calib);
606 if (ret < 0) {
607 dev_err(&data->client->dev,
608 "failed to read calibration coefficients\n");
609 return ret;
610 }
611
612 b6 = data->t_fine - 4000;
613 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
614 x2 = calib.AC2 * b6 >> 11;
615 x3 = x1 + x2;
616 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
617 x1 = calib.AC3 * b6 >> 13;
618 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
619 x3 = (x1 + x2 + 2) >> 2;
620 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
621 b7 = ((u32)adc_press - b3) * (50000 >> oss);
622 if (b7 < 0x80000000)
623 p = (b7 * 2) / b4;
624 else
625 p = (b7 / b4) * 2;
626
627 x1 = (p >> 8) * (p >> 8);
628 x1 = (x1 * 3038) >> 16;
629 x2 = (-7357 * p) >> 16;
630
631 return p + ((x1 + x2 + 3791) >> 4);
632}
633
634static int bmp180_read_press(struct bmp280_data *data,
635 int *val, int *val2)
636{
637 int ret;
638 s32 adc_press;
639 u32 comp_press;
640
641 /* Read and compensate temperature so we get a reading of t_fine. */
642 ret = bmp180_read_temp(data, NULL);
643 if (ret)
644 return ret;
645
646 ret = bmp180_read_adc_press(data, &adc_press);
647 if (ret)
648 return ret;
649
650 comp_press = bmp180_compensate_press(data, adc_press);
651
652 *val = comp_press;
653 *val2 = 1000;
654
655 return IIO_VAL_FRACTIONAL;
656}
657
658static int bmp180_chip_config(struct bmp280_data *data)
659{
660 return 0;
661}
662
663static const struct bmp280_chip_info bmp180_chip_info = {
664 .regmap_config = &bmp180_regmap_config,
665 .chip_config = bmp180_chip_config,
666 .read_temp = bmp180_read_temp,
667 .read_press = bmp180_read_press,
668};
669
347static int bmp280_probe(struct i2c_client *client, 670static int bmp280_probe(struct i2c_client *client,
348 const struct i2c_device_id *id) 671 const struct i2c_device_id *id)
349{ 672{
@@ -367,7 +690,19 @@ static int bmp280_probe(struct i2c_client *client,
367 indio_dev->info = &bmp280_info; 690 indio_dev->info = &bmp280_info;
368 indio_dev->modes = INDIO_DIRECT_MODE; 691 indio_dev->modes = INDIO_DIRECT_MODE;
369 692
370 data->regmap = devm_regmap_init_i2c(client, &bmp280_regmap_config); 693 switch (id->driver_data) {
694 case BMP180_CHIP_ID:
695 data->chip_info = &bmp180_chip_info;
696 break;
697 case BMP280_CHIP_ID:
698 data->chip_info = &bmp280_chip_info;
699 break;
700 default:
701 return -EINVAL;
702 }
703
704 data->regmap = devm_regmap_init_i2c(client,
705 data->chip_info->regmap_config);
371 if (IS_ERR(data->regmap)) { 706 if (IS_ERR(data->regmap)) {
372 dev_err(&client->dev, "failed to allocate register map\n"); 707 dev_err(&client->dev, "failed to allocate register map\n");
373 return PTR_ERR(data->regmap); 708 return PTR_ERR(data->regmap);
@@ -376,13 +711,13 @@ static int bmp280_probe(struct i2c_client *client,
376 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id); 711 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
377 if (ret < 0) 712 if (ret < 0)
378 return ret; 713 return ret;
379 if (chip_id != BMP280_CHIP_ID) { 714 if (chip_id != id->driver_data) {
380 dev_err(&client->dev, "bad chip id. expected %x got %x\n", 715 dev_err(&client->dev, "bad chip id. expected %x got %x\n",
381 BMP280_CHIP_ID, chip_id); 716 BMP280_CHIP_ID, chip_id);
382 return -EINVAL; 717 return -EINVAL;
383 } 718 }
384 719
385 ret = bmp280_chip_init(data); 720 ret = data->chip_info->chip_config(data);
386 if (ret < 0) 721 if (ret < 0)
387 return ret; 722 return ret;
388 723
@@ -390,13 +725,17 @@ static int bmp280_probe(struct i2c_client *client,
390} 725}
391 726
392static const struct acpi_device_id bmp280_acpi_match[] = { 727static const struct acpi_device_id bmp280_acpi_match[] = {
393 {"BMP0280", 0}, 728 {"BMP0280", BMP280_CHIP_ID },
729 {"BMP0180", BMP180_CHIP_ID },
730 {"BMP0085", BMP180_CHIP_ID },
394 { }, 731 { },
395}; 732};
396MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match); 733MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
397 734
398static const struct i2c_device_id bmp280_id[] = { 735static const struct i2c_device_id bmp280_id[] = {
399 {"bmp280", 0}, 736 {"bmp280", BMP280_CHIP_ID },
737 {"bmp180", BMP180_CHIP_ID },
738 {"bmp085", BMP180_CHIP_ID },
400 { }, 739 { },
401}; 740};
402MODULE_DEVICE_TABLE(i2c, bmp280_id); 741MODULE_DEVICE_TABLE(i2c, bmp280_id);
@@ -412,5 +751,5 @@ static struct i2c_driver bmp280_driver = {
412module_i2c_driver(bmp280_driver); 751module_i2c_driver(bmp280_driver);
413 752
414MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 753MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
415MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor"); 754MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
416MODULE_LICENSE("GPL v2"); 755MODULE_LICENSE("GPL v2");