drivers: sensor: hmc5883l: Update driver to use i2c_dt_spec

Simplify driver by using i2c_dt_spec for bus access.

Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
This commit is contained in:
Benjamin Björnsson 2022-06-19 10:46:58 +02:00 committed by Maureen Helm
commit be1e512aad
2 changed files with 23 additions and 20 deletions

View file

@ -57,14 +57,14 @@ static int hmc5883l_sample_fetch(const struct device *dev,
enum sensor_channel chan) enum sensor_channel chan)
{ {
struct hmc5883l_data *drv_data = dev->data; struct hmc5883l_data *drv_data = dev->data;
const struct hmc5883l_config *config = dev->config;
int16_t buf[3]; int16_t buf[3];
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
/* fetch magnetometer sample */ /* fetch magnetometer sample */
if (i2c_burst_read(drv_data->i2c, if (i2c_burst_read_dt(&config->i2c, HMC5883L_REG_DATA_START,
DT_INST_REG_ADDR(0), (uint8_t *)buf, 6) < 0) {
HMC5883L_REG_DATA_START, (uint8_t *)buf, 6) < 0) {
LOG_ERR("Failed to fetch magnetometer sample."); LOG_ERR("Failed to fetch magnetometer sample.");
return -EIO; return -EIO;
} }
@ -87,20 +87,16 @@ static const struct sensor_driver_api hmc5883l_driver_api = {
int hmc5883l_init(const struct device *dev) int hmc5883l_init(const struct device *dev)
{ {
struct hmc5883l_data *drv_data = dev->data; struct hmc5883l_data *drv_data = dev->data;
const struct hmc5883l_config *config = dev->config;
uint8_t chip_cfg[3], id[3], idx; uint8_t chip_cfg[3], id[3], idx;
drv_data->i2c = device_get_binding( if (!device_is_ready(config->i2c.bus)) {
DT_INST_BUS_LABEL(0)); LOG_ERR("I2C bus device not ready");
if (drv_data->i2c == NULL) { return -ENODEV;
LOG_ERR("Failed to get pointer to %s device.",
DT_INST_BUS_LABEL(0));
return -EINVAL;
} }
/* check chip ID */ /* check chip ID */
if (i2c_burst_read(drv_data->i2c, if (i2c_burst_read_dt(&config->i2c, HMC5883L_REG_CHIP_ID, id, 3) < 0) {
DT_INST_REG_ADDR(0),
HMC5883L_REG_CHIP_ID, id, 3) < 0) {
LOG_ERR("Failed to read chip ID."); LOG_ERR("Failed to read chip ID.");
return -EIO; return -EIO;
} }
@ -142,9 +138,8 @@ int hmc5883l_init(const struct device *dev)
chip_cfg[1] = drv_data->gain_idx << HMC5883L_GAIN_SHIFT; chip_cfg[1] = drv_data->gain_idx << HMC5883L_GAIN_SHIFT;
chip_cfg[2] = HMC5883L_MODE_CONTINUOUS; chip_cfg[2] = HMC5883L_MODE_CONTINUOUS;
if (i2c_burst_write(drv_data->i2c, if (i2c_burst_write_dt(&config->i2c, HMC5883L_REG_CONFIG_A,
DT_INST_REG_ADDR(0), chip_cfg, 3) < 0) {
HMC5883L_REG_CONFIG_A, chip_cfg, 3) < 0) {
LOG_ERR("Failed to configure chip."); LOG_ERR("Failed to configure chip.");
return -EIO; return -EIO;
} }
@ -159,8 +154,12 @@ int hmc5883l_init(const struct device *dev)
return 0; return 0;
} }
struct hmc5883l_data hmc5883l_driver; static struct hmc5883l_data hmc5883l_data_inst;
DEVICE_DT_INST_DEFINE(0, hmc5883l_init, NULL, static const struct hmc5883l_config hmc5883l_config_inst = {
&hmc5883l_driver, NULL, POST_KERNEL, .i2c = I2C_DT_SPEC_INST_GET(0),
CONFIG_SENSOR_INIT_PRIORITY, &hmc5883l_driver_api); };
DEVICE_DT_INST_DEFINE(0, hmc5883l_init, NULL, &hmc5883l_data_inst,
&hmc5883l_config_inst, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &hmc5883l_driver_api);

View file

@ -10,6 +10,7 @@
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/sys/util.h> #include <zephyr/sys/util.h>
#include <zephyr/types.h> #include <zephyr/types.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio.h>
#define HMC5883L_REG_CONFIG_A 0x00 #define HMC5883L_REG_CONFIG_A 0x00
@ -41,7 +42,6 @@ static const uint16_t hmc5883l_gain[] = {
}; };
struct hmc5883l_data { struct hmc5883l_data {
const struct device *i2c;
int16_t x_sample; int16_t x_sample;
int16_t y_sample; int16_t y_sample;
int16_t z_sample; int16_t z_sample;
@ -66,6 +66,10 @@ struct hmc5883l_data {
#endif /* CONFIG_HMC5883L_TRIGGER */ #endif /* CONFIG_HMC5883L_TRIGGER */
}; };
struct hmc5883l_config {
struct i2c_dt_spec i2c;
};
#ifdef CONFIG_HMC5883L_TRIGGER #ifdef CONFIG_HMC5883L_TRIGGER
int hmc5883l_trigger_set(const struct device *dev, int hmc5883l_trigger_set(const struct device *dev,
const struct sensor_trigger *trig, const struct sensor_trigger *trig,