BigW Consortium Gitlab

Commit 81fa09e2 by David Frey

import of bmp280 code from mainline kernel

parent d39bc84a
#
# Pressure drivers
#
# When adding new entries keep the list in alphabetical order
menu "BMP 280 Pressure sensor"
config BMP280
tristate "Bosch Sensortec BMP180/BMP280 pressure sensor I2C driver"
depends on (I2C || SPI_MASTER)
select REGMAP
select BMP280_I2C if (I2C)
select BMP280_SPI if (SPI_MASTER)
help
Say yes here to build support for Bosch Sensortec BMP180 and BMP280
pressure and temperature sensors. Also supports the BE280 with
an additional humidity sensor channel.
To compile this driver as a module, choose M here: the core module
will be called bmp280 and you will also get bmp280-i2c for I2C
and/or bmp280-spi for SPI support.
config BMP280_I2C
tristate
depends on BMP280
depends on I2C
select REGMAP_I2C
config BMP280_SPI
tristate
depends on BMP280
depends on SPI_MASTER
select REGMAP
endmenu
#
# Makefile for industrial I/O pressure drivers
#
# When adding new entries keep the list in alphabetical order
obj-$(CONFIG_BMP280) += bmp280.o
bmp280-objs := bmp280-core.o bmp280-regmap.o
obj-$(CONFIG_BMP280_I2C) += bmp280-i2c.o
obj-$(CONFIG_BMP280_SPI) += bmp280-spi.o
BMP280 Linux Kernel Driver
==========================
This version of the BMP280 Linux kernel driver has been extracted from the mainline kernel at
revision e6925852d5b862bac749fab9c8d26491cda99e4e. The files were taken from `drivers/iio/pressure`.
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/acpi.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include "bmp280.h"
static int bmp280_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct regmap *regmap;
const struct regmap_config *regmap_config;
switch (id->driver_data) {
case BMP180_CHIP_ID:
regmap_config = &bmp180_regmap_config;
break;
case BMP280_CHIP_ID:
case BME280_CHIP_ID:
regmap_config = &bmp280_regmap_config;
break;
default:
return -EINVAL;
}
regmap = devm_regmap_init_i2c(client, regmap_config);
if (IS_ERR(regmap)) {
dev_err(&client->dev, "failed to allocate register map\n");
return PTR_ERR(regmap);
}
return bmp280_common_probe(&client->dev,
regmap,
id->driver_data,
id->name,
client->irq);
}
static int bmp280_i2c_remove(struct i2c_client *client)
{
return bmp280_common_remove(&client->dev);
}
static const struct acpi_device_id bmp280_acpi_i2c_match[] = {
{"BMP0280", BMP280_CHIP_ID },
{"BMP0180", BMP180_CHIP_ID },
{"BMP0085", BMP180_CHIP_ID },
{"BME0280", BME280_CHIP_ID },
{ },
};
MODULE_DEVICE_TABLE(acpi, bmp280_acpi_i2c_match);
#ifdef CONFIG_OF
static const struct of_device_id bmp280_of_i2c_match[] = {
{ .compatible = "bosch,bme280", .data = (void *)BME280_CHIP_ID },
{ .compatible = "bosch,bmp280", .data = (void *)BMP280_CHIP_ID },
{ .compatible = "bosch,bmp180", .data = (void *)BMP180_CHIP_ID },
{ .compatible = "bosch,bmp085", .data = (void *)BMP180_CHIP_ID },
{ },
};
MODULE_DEVICE_TABLE(of, bmp280_of_i2c_match);
#else
#define bmp280_of_i2c_match NULL
#endif
static const struct i2c_device_id bmp280_i2c_id[] = {
{"bmp280", BMP280_CHIP_ID },
{"bmp180", BMP180_CHIP_ID },
{"bmp085", BMP180_CHIP_ID },
{"bme280", BME280_CHIP_ID },
{ },
};
MODULE_DEVICE_TABLE(i2c, bmp280_i2c_id);
static struct i2c_driver bmp280_i2c_driver = {
.driver = {
.name = "bmp280",
.acpi_match_table = ACPI_PTR(bmp280_acpi_i2c_match),
.of_match_table = of_match_ptr(bmp280_of_i2c_match),
.pm = &bmp280_dev_pm_ops,
},
.probe = bmp280_i2c_probe,
.remove = bmp280_i2c_remove,
.id_table = bmp280_i2c_id,
};
module_i2c_driver(bmp280_i2c_driver);
MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
MODULE_LICENSE("GPL v2");
#include <linux/device.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include "bmp280.h"
static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
case BMP280_REG_CTRL_MEAS:
case BMP280_REG_RESET:
return true;
default:
return false;
};
}
static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
case BMP180_REG_OUT_XLSB:
case BMP180_REG_OUT_LSB:
case BMP180_REG_OUT_MSB:
case BMP280_REG_CTRL_MEAS:
return true;
default:
return false;
}
}
const struct regmap_config bmp180_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = BMP180_REG_OUT_XLSB,
.cache_type = REGCACHE_RBTREE,
.writeable_reg = bmp180_is_writeable_reg,
.volatile_reg = bmp180_is_volatile_reg,
};
EXPORT_SYMBOL(bmp180_regmap_config);
static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
case BMP280_REG_CONFIG:
case BMP280_REG_CTRL_HUMIDITY:
case BMP280_REG_CTRL_MEAS:
case BMP280_REG_RESET:
return true;
default:
return false;
};
}
static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
case BMP280_REG_HUMIDITY_LSB:
case BMP280_REG_HUMIDITY_MSB:
case BMP280_REG_TEMP_XLSB:
case BMP280_REG_TEMP_LSB:
case BMP280_REG_TEMP_MSB:
case BMP280_REG_PRESS_XLSB:
case BMP280_REG_PRESS_LSB:
case BMP280_REG_PRESS_MSB:
case BMP280_REG_STATUS:
return true;
default:
return false;
}
}
const struct regmap_config bmp280_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = BMP280_REG_HUMIDITY_LSB,
.cache_type = REGCACHE_RBTREE,
.writeable_reg = bmp280_is_writeable_reg,
.volatile_reg = bmp280_is_volatile_reg,
};
EXPORT_SYMBOL(bmp280_regmap_config);
/*
* SPI interface for the BMP280 driver
*
* Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
*/
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include "bmp280.h"
static int bmp280_regmap_spi_write(void *context, const void *data,
size_t count)
{
struct device *dev = context;
struct spi_device *spi = to_spi_device(dev);
u8 buf[2];
memcpy(buf, data, 2);
/*
* The SPI register address (= full register address without bit 7) and
* the write command (bit7 = RW = '0')
*/
buf[0] &= ~0x80;
return spi_write_then_read(spi, buf, 2, NULL, 0);
}
static int bmp280_regmap_spi_read(void *context, const void *reg,
size_t reg_size, void *val, size_t val_size)
{
struct device *dev = context;
struct spi_device *spi = to_spi_device(dev);
return spi_write_then_read(spi, reg, reg_size, val, val_size);
}
static struct regmap_bus bmp280_regmap_bus = {
.write = bmp280_regmap_spi_write,
.read = bmp280_regmap_spi_read,
.reg_format_endian_default = REGMAP_ENDIAN_BIG,
.val_format_endian_default = REGMAP_ENDIAN_BIG,
};
static int bmp280_spi_probe(struct spi_device *spi)
{
const struct spi_device_id *id = spi_get_device_id(spi);
struct regmap *regmap;
const struct regmap_config *regmap_config;
int ret;
spi->bits_per_word = 8;
ret = spi_setup(spi);
if (ret < 0) {
dev_err(&spi->dev, "spi_setup failed!\n");
return ret;
}
switch (id->driver_data) {
case BMP180_CHIP_ID:
regmap_config = &bmp180_regmap_config;
break;
case BMP280_CHIP_ID:
case BME280_CHIP_ID:
regmap_config = &bmp280_regmap_config;
break;
default:
return -EINVAL;
}
regmap = devm_regmap_init(&spi->dev,
&bmp280_regmap_bus,
&spi->dev,
regmap_config);
if (IS_ERR(regmap)) {
dev_err(&spi->dev, "failed to allocate register map\n");
return PTR_ERR(regmap);
}
return bmp280_common_probe(&spi->dev,
regmap,
id->driver_data,
id->name,
spi->irq);
}
static int bmp280_spi_remove(struct spi_device *spi)
{
return bmp280_common_remove(&spi->dev);
}
static const struct of_device_id bmp280_of_spi_match[] = {
{ .compatible = "bosch,bmp085", },
{ .compatible = "bosch,bmp180", },
{ .compatible = "bosch,bmp181", },
{ .compatible = "bosch,bmp280", },
{ .compatible = "bosch,bme280", },
{ },
};
MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
static const struct spi_device_id bmp280_spi_id[] = {
{ "bmp180", BMP180_CHIP_ID },
{ "bmp181", BMP180_CHIP_ID },
{ "bmp280", BMP280_CHIP_ID },
{ "bme280", BME280_CHIP_ID },
{ }
};
MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
static struct spi_driver bmp280_spi_driver = {
.driver = {
.name = "bmp280",
.of_match_table = bmp280_of_spi_match,
.pm = &bmp280_dev_pm_ops,
},
.id_table = bmp280_spi_id,
.probe = bmp280_spi_probe,
.remove = bmp280_spi_remove,
};
module_spi_driver(bmp280_spi_driver);
MODULE_DESCRIPTION("BMP280 SPI bus driver");
MODULE_LICENSE("GPL");
#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/regmap.h>
/* BMP280 specific registers */
#define BMP280_REG_HUMIDITY_LSB 0xFE
#define BMP280_REG_HUMIDITY_MSB 0xFD
#define BMP280_REG_TEMP_XLSB 0xFC
#define BMP280_REG_TEMP_LSB 0xFB
#define BMP280_REG_TEMP_MSB 0xFA
#define BMP280_REG_PRESS_XLSB 0xF9
#define BMP280_REG_PRESS_LSB 0xF8
#define BMP280_REG_PRESS_MSB 0xF7
#define BMP280_REG_CONFIG 0xF5
#define BMP280_REG_CTRL_MEAS 0xF4
#define BMP280_REG_STATUS 0xF3
#define BMP280_REG_CTRL_HUMIDITY 0xF2
/* Due to non linear mapping, and data sizes we can't do a bulk read */
#define BMP280_REG_COMP_H1 0xA1
#define BMP280_REG_COMP_H2 0xE1
#define BMP280_REG_COMP_H3 0xE3
#define BMP280_REG_COMP_H4 0xE4
#define BMP280_REG_COMP_H5 0xE5
#define BMP280_REG_COMP_H6 0xE7
#define BMP280_REG_COMP_TEMP_START 0x88
#define BMP280_COMP_TEMP_REG_COUNT 6
#define BMP280_REG_COMP_PRESS_START 0x8E
#define BMP280_COMP_PRESS_REG_COUNT 18
#define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
#define BMP280_FILTER_OFF 0
#define BMP280_FILTER_2X BIT(2)
#define BMP280_FILTER_4X BIT(3)
#define BMP280_FILTER_8X (BIT(3) | BIT(2))
#define BMP280_FILTER_16X BIT(4)
#define BMP280_OSRS_HUMIDITY_MASK (BIT(2) | BIT(1) | BIT(0))
#define BMP280_OSRS_HUMIDITIY_X(osrs_h) ((osrs_h) << 0)
#define BMP280_OSRS_HUMIDITY_SKIP 0
#define BMP280_OSRS_HUMIDITY_1X BMP280_OSRS_HUMIDITIY_X(1)
#define BMP280_OSRS_HUMIDITY_2X BMP280_OSRS_HUMIDITIY_X(2)
#define BMP280_OSRS_HUMIDITY_4X BMP280_OSRS_HUMIDITIY_X(3)
#define BMP280_OSRS_HUMIDITY_8X BMP280_OSRS_HUMIDITIY_X(4)
#define BMP280_OSRS_HUMIDITY_16X BMP280_OSRS_HUMIDITIY_X(5)
#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
#define BMP280_OSRS_TEMP_SKIP 0
#define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
#define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
#define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
#define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
#define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
#define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
#define BMP280_OSRS_PRESS_SKIP 0
#define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
#define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
#define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
#define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
#define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
#define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
#define BMP280_MODE_MASK (BIT(1) | BIT(0))
#define BMP280_MODE_SLEEP 0
#define BMP280_MODE_FORCED BIT(0)
#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
/* BMP180 specific registers */
#define BMP180_REG_OUT_XLSB 0xF8
#define BMP180_REG_OUT_LSB 0xF7
#define BMP180_REG_OUT_MSB 0xF6
#define BMP180_REG_CALIB_START 0xAA
#define BMP180_REG_CALIB_COUNT 22
#define BMP180_MEAS_SCO BIT(5)
#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
/* BMP180 and BMP280 common registers */
#define BMP280_REG_CTRL_MEAS 0xF4
#define BMP280_REG_RESET 0xE0
#define BMP280_REG_ID 0xD0
#define BMP180_CHIP_ID 0x55
#define BMP280_CHIP_ID 0x58
#define BME280_CHIP_ID 0x60
#define BMP280_SOFT_RESET_VAL 0xB6
/* Regmap configurations */
extern const struct regmap_config bmp180_regmap_config;
extern const struct regmap_config bmp280_regmap_config;
/* Probe called from different transports */
int bmp280_common_probe(struct device *dev,
struct regmap *regmap,
unsigned int chip,
const char *name,
int irq);
int bmp280_common_remove(struct device *dev);
/* PM ops */
extern const struct dev_pm_ops bmp280_dev_pm_ops;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment