-
-
Save himanshujha199640/729ea33ce04329a13a1d03eeeca1090f to your computer and use it in GitHub Desktop.
bme680-testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* SPDX-License-Identifier: GPL-2.0 */ | |
#ifndef BME680_H_ | |
#define BME680_H_ | |
extern const struct regmap_config bme680_regmap_config; | |
int bme680_core_probe(struct device *dev, struct regmap *regmap, | |
const char *name, bool use_spi); | |
void bme680_core_remove(struct device *dev); | |
#endif /* BME680_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Bosch BME680 - Temperature, Pressure, Humidity & Gas Sensor | |
* | |
* IIO core driver - I2C & SPI bus support | |
*/ | |
#include <linux/iio/iio.h> | |
#include <linux/module.h> | |
#include <linux/regmap.h> | |
#define BME680_REG_CHIP_I2C_ID 0xD0 | |
#define BME680_REG_CHIP_SPI_ID 0x50 | |
#define BME680_CHIP_ID_VAL 0x61 | |
#define BME680_SOFT_RESET 0xE0 | |
#define BME680_CMD_SOFTRESET 0xB6 | |
struct bme680_data { | |
struct regmap *regmap; | |
}; | |
const struct regmap_config bme680_regmap_config = { | |
.reg_bits = 8, | |
.val_bits = 8, | |
}; | |
EXPORT_SYMBOL(bme680_regmap_config); | |
static const struct iio_info bme680_info = { | |
}; | |
static int bme680_chip_init(struct bme680_data *data, bool use_spi) | |
{ | |
struct device *dev = regmap_get_device(data->regmap); | |
unsigned int val; | |
int ret; | |
/* Power on Soft Reset */ | |
ret = regmap_write(data->regmap, BME680_SOFT_RESET, BME680_CMD_SOFTRESET); | |
if (ret < 0) | |
return ret; | |
if (!use_spi) { | |
ret = regmap_read(data->regmap, BME680_REG_CHIP_I2C_ID, &val); | |
} else { | |
ret = regmap_read(data->regmap, BME680_REG_CHIP_SPI_ID, &val); | |
} | |
if (ret < 0) { | |
dev_err(dev, "Error reading chip ID\n"); | |
return ret; | |
} | |
if (val != BME680_CHIP_ID_VAL) { | |
dev_err(dev, "Wrong chip ID, got %x expected %x\n", | |
val, BME680_CHIP_ID_VAL); | |
return -ENODEV; | |
} | |
return 0; | |
} | |
int bme680_core_probe(struct device *dev, struct regmap *regmap, | |
const char *name, bool use_spi) | |
{ | |
struct iio_dev *indio_dev; | |
struct bme680_data *data; | |
int ret; | |
indio_dev = devm_iio_device_alloc(dev, sizeof(*dev)); | |
if (!indio_dev) | |
return -ENOMEM; | |
data = iio_priv(indio_dev); | |
dev_set_drvdata(dev, indio_dev); | |
data->regmap = regmap; | |
ret = bme680_chip_init(data, use_spi); | |
if (ret < 0) | |
return ret; | |
indio_dev->dev.parent = dev; | |
indio_dev->name = name; | |
indio_dev->info = &bme680_info; | |
ret = iio_device_register(indio_dev); | |
if (ret < 0) | |
return ret; | |
return 0; | |
} | |
EXPORT_SYMBOL_GPL(bme680_core_probe); | |
void bme680_core_remove(struct device *dev) | |
{ | |
struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
struct bme680_data *data = iio_priv(indio_dev); | |
iio_device_unregister(indio_dev); | |
} | |
EXPORT_SYMBOL_GPL(bme680_core_remove); | |
MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>"); | |
MODULE_DESCRIPTION("Bosch BME680 Driver"); | |
MODULE_LICENSE("GPL v2"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 7-Bit I2C slave address is: | |
* - 0x76 if SDO is pulled to GND | |
* - 0x77 if SDO is pulled to VDDIO | |
*/ | |
#include <linux/i2c.h> | |
#include <linux/module.h> | |
#include <linux/regmap.h> | |
#include "bme680.h" | |
static int bme680_i2c_probe(struct i2c_client *client, | |
const struct i2c_device_id *id) | |
{ | |
struct regmap *regmap; | |
const char *name = NULL; | |
regmap = devm_regmap_init_i2c(client, &bme680_regmap_config); | |
if (IS_ERR(regmap)) { | |
dev_err(&client->dev, "Failed to register i2c regmap %d\n", | |
(int)PTR_ERR(regmap)); | |
return PTR_ERR(regmap); | |
} | |
if (id) | |
name = id->name; | |
return bme680_core_probe(&client->dev, regmap, name, false); | |
} | |
static int bme680_i2c_remove(struct i2c_client *client) | |
{ | |
bme680_core_remove(&client->dev); | |
return 0; | |
} | |
static const struct i2c_device_id bme680_i2c_id[] = { | |
{"bme680", 0}, | |
{}, | |
}; | |
MODULE_DEVICE_TABLE(i2c, bme680_i2c_id); | |
static struct i2c_driver bme680_i2c_driver = { | |
.driver = { | |
.name = "bme680_i2c", | |
}, | |
.probe = bme680_i2c_probe, | |
.remove = bme680_i2c_remove, | |
.id_table = bme680_i2c_id, | |
}; | |
module_i2c_driver(bme680_i2c_driver); | |
MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>"); | |
MODULE_DESCRIPTION("BME680 I2C driver"); | |
MODULE_LICENSE("GPL v2"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* BMI680 - SPI Driver | |
*/ | |
#include <linux/module.h> | |
#include <linux/spi/spi.h> | |
#include <linux/regmap.h> | |
#include "bme680.h" | |
static int bme680_spi_probe(struct spi_device *spi) | |
{ | |
struct regmap *regmap; | |
const struct spi_device_id *id = spi_get_device_id(spi); | |
regmap = devm_regmap_init_spi(spi, &bme680_regmap_config); | |
if (IS_ERR(regmap)) { | |
dev_err(&spi->dev, "Failed to register spi regmap %d\n", | |
(int)PTR_ERR(regmap)); | |
return PTR_ERR(regmap); | |
} | |
return bme680_core_probe(&spi->dev, regmap, id->name, true); | |
} | |
static int bme680_spi_remove(struct spi_device *spi) | |
{ | |
bme680_core_remove(&spi->dev); | |
return 0; | |
} | |
static const struct spi_device_id bme680_spi_id[] = { | |
{"bme680", 0}, | |
{} | |
}; | |
MODULE_DEVICE_TABLE(spi, bme680_spi_id); | |
static struct spi_driver bme680_spi_driver = { | |
.driver = { | |
.name = "bme680", | |
}, | |
.probe = bme680_spi_probe, | |
.remove = bme680_spi_remove, | |
.id_table = bme680_spi_id, | |
}; | |
module_spi_driver(bme680_spi_driver); | |
MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>"); | |
MODULE_DESCRIPTION("Bosch BME680 SPI driver"); | |
MODULE_LICENSE("GPL v2"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment