Skip to content

Commit

Permalink
iio: pressure: bmp280: Allow multiple chips id per family of devices
Browse files Browse the repository at this point in the history
Improve device detection in certain chip families known to have various
chip ids.

Signed-off-by: Angel Iglesias <[email protected]>
  • Loading branch information
angiglesias committed Aug 18, 2023
1 parent 7900a3a commit 9ca98fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
51 changes: 41 additions & 10 deletions drivers/iio/pressure/bmp280-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,12 @@ static int bmp280_chip_config(struct bmp280_data *data)
}

static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
static const int bmp280_chip_ids[] = { BMP280_CHIP_ID };

const struct bmp280_chip_info bmp280_chip_info = {
.id_reg = BMP280_REG_ID,
.chip_id = BMP280_CHIP_ID,
.chip_id = bmp280_chip_ids,
.num_chip_id = ARRAY_SIZE(bmp280_chip_ids),
.regmap_config = &bmp280_regmap_config,
.start_up_time = 2000,
.channels = bmp280_channels,
Expand Down Expand Up @@ -846,9 +848,12 @@ static int bme280_chip_config(struct bmp280_data *data)
return bmp280_chip_config(data);
}

static const int bme280_chip_ids[] = { BME280_CHIP_ID };

const struct bmp280_chip_info bme280_chip_info = {
.id_reg = BMP280_REG_ID,
.chip_id = BME280_CHIP_ID,
.chip_id = bme280_chip_ids,
.num_chip_id = ARRAY_SIZE(bme280_chip_ids),
.regmap_config = &bmp280_regmap_config,
.start_up_time = 2000,
.channels = bmp280_channels,
Expand Down Expand Up @@ -1220,10 +1225,12 @@ static int bmp380_chip_config(struct bmp280_data *data)

static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 };
static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128};
static const int bmp380_chip_ids[] = { BMP380_CHIP_ID };

const struct bmp280_chip_info bmp380_chip_info = {
.id_reg = BMP380_REG_ID,
.chip_id = BMP380_CHIP_ID,
.chip_id = bmp380_chip_ids,
.num_chip_id = ARRAY_SIZE(bmp380_chip_ids),
.regmap_config = &bmp380_regmap_config,
.start_up_time = 2000,
.channels = bmp380_channels,
Expand Down Expand Up @@ -1720,10 +1727,12 @@ static int bmp580_chip_config(struct bmp280_data *data)
}

static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
static const int bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT };

const struct bmp280_chip_info bmp580_chip_info = {
.id_reg = BMP580_REG_CHIP_ID,
.chip_id = BMP580_CHIP_ID,
.chip_id = bmp580_chip_ids,
.num_chip_id = ARRAY_SIZE(bmp580_chip_ids),
.regmap_config = &bmp580_regmap_config,
.start_up_time = 2000,
.channels = bmp380_channels,
Expand Down Expand Up @@ -1983,10 +1992,12 @@ static int bmp180_chip_config(struct bmp280_data *data)

static const int bmp180_oversampling_temp_avail[] = { 1 };
static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
static const int bmp180_chip_ids[] = { BMP180_CHIP_ID };

const struct bmp280_chip_info bmp180_chip_info = {
.id_reg = BMP280_REG_ID,
.chip_id = BMP180_CHIP_ID,
.chip_id = bmp180_chip_ids,
.num_chip_id = ARRAY_SIZE(bmp180_chip_ids),
.regmap_config = &bmp180_regmap_config,
.start_up_time = 2000,
.channels = bmp280_channels,
Expand Down Expand Up @@ -2077,7 +2088,7 @@ int bmp280_common_probe(struct device *dev,
struct bmp280_data *data;
struct gpio_desc *gpiod;
unsigned int chip_id;
int ret;
int ret, i;

indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
if (!indio_dev)
Expand Down Expand Up @@ -2142,10 +2153,30 @@ int bmp280_common_probe(struct device *dev,
ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
if (ret < 0)
return ret;
if (chip_id != data->chip_info->chip_id) {
dev_err(dev, "bad chip id: expected %x got %x\n",
data->chip_info->chip_id, chip_id);
return -EINVAL;

ret = -EINVAL;
for (i = 0; i < data->chip_info->num_chip_id; i++) {
if (chip_id == data->chip_info->chip_id[i]) {
ret = 0;
break;
}
}

if (ret) {
// 0x<id>, so four chars per number plus one space + ENDL
size_t nbuf = 5*data->chip_info->num_chip_id*sizeof(char);
char *buf = kmalloc(nbuf, GFP_KERNEL);

if (!buf)
return ret;

for (i = 0; i < data->chip_info->num_chip_id; i++)
snprintf(&buf[i*5], nbuf, "0x%x ", data->chip_info->chip_id[i]);
buf[nbuf-1] = '\0';

dev_err(dev, "bad chip id: expected [ %s ] got 0x%x\n", buf, chip_id);
kfree(buf);
return ret;
}

if (data->chip_info->preinit) {
Expand Down
3 changes: 2 additions & 1 deletion drivers/iio/pressure/bmp280.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ struct bmp280_data {

struct bmp280_chip_info {
unsigned int id_reg;
const unsigned int chip_id;
const unsigned int *chip_id;
int num_chip_id;

const struct regmap_config *regmap_config;

Expand Down

0 comments on commit 9ca98fd

Please sign in to comment.