forked from kofal.net/zmk
refactor: Move away from deprecated label usages.
* Move away from DT_LABEL. * Move to DEVICE_DT_GET for non-behavior device access. * Move various drivers to `gpio_spec_dt` and `DT` related macros. * Remove mcp23017 while at it, since better upstream driver is available.
This commit is contained in:
committed by
Pete Johanson
parent
062f94d014
commit
09ed79a867
@@ -8,9 +8,9 @@
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <drivers/gpio.h>
|
||||
#include <drivers/adc.h>
|
||||
#include <drivers/sensor.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/adc.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "battery_common.h"
|
||||
@@ -21,22 +21,15 @@ struct io_channel_config {
|
||||
uint8_t channel;
|
||||
};
|
||||
|
||||
struct gpio_channel_config {
|
||||
const char *label;
|
||||
uint8_t pin;
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
struct bvd_config {
|
||||
struct io_channel_config io_channel;
|
||||
struct gpio_channel_config power_gpios;
|
||||
struct gpio_dt_spec power;
|
||||
uint32_t output_ohm;
|
||||
uint32_t full_ohm;
|
||||
};
|
||||
|
||||
struct bvd_data {
|
||||
const struct device *adc;
|
||||
const struct device *gpio;
|
||||
struct adc_channel_cfg acc;
|
||||
struct adc_sequence as;
|
||||
struct battery_value value;
|
||||
@@ -56,19 +49,19 @@ static int bvd_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||
|
||||
int rc = 0;
|
||||
|
||||
// Enable power GPIO if present
|
||||
if (drv_data->gpio) {
|
||||
rc = gpio_pin_set(drv_data->gpio, drv_cfg->power_gpios.pin, 1);
|
||||
#if DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||
// Enable power before sampling
|
||||
rc = gpio_pin_set_dt(&drv_cfg->power, 1);
|
||||
|
||||
if (rc != 0) {
|
||||
LOG_DBG("Failed to enable ADC power GPIO: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
// wait for any capacitance to charge up
|
||||
k_sleep(K_MSEC(10));
|
||||
if (rc != 0) {
|
||||
LOG_DBG("Failed to enable ADC power GPIO: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
// wait for any capacitance to charge up
|
||||
k_sleep(K_MSEC(10));
|
||||
#endif // DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||
|
||||
// Read ADC
|
||||
rc = adc_read(drv_data->adc, as);
|
||||
as->calibrate = false;
|
||||
@@ -90,15 +83,15 @@ static int bvd_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||
LOG_DBG("Failed to read ADC: %d", rc);
|
||||
}
|
||||
|
||||
#if DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||
// Disable power GPIO if present
|
||||
if (drv_data->gpio) {
|
||||
int rc2 = gpio_pin_set(drv_data->gpio, drv_cfg->power_gpios.pin, 0);
|
||||
int rc2 = gpio_pin_set_dt(&drv_cfg->power, 0);
|
||||
|
||||
if (rc2 != 0) {
|
||||
LOG_DBG("Failed to disable ADC power GPIO: %d", rc2);
|
||||
return rc2;
|
||||
}
|
||||
if (rc2 != 0) {
|
||||
LOG_DBG("Failed to disable ADC power GPIO: %d", rc2);
|
||||
return rc2;
|
||||
}
|
||||
#endif // DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||
|
||||
return rc;
|
||||
}
|
||||
@@ -125,20 +118,17 @@ static int bvd_init(const struct device *dev) {
|
||||
|
||||
int rc = 0;
|
||||
|
||||
if (drv_cfg->power_gpios.label) {
|
||||
drv_data->gpio = device_get_binding(drv_cfg->power_gpios.label);
|
||||
if (drv_data->gpio == NULL) {
|
||||
LOG_ERR("Failed to get GPIO %s", drv_cfg->power_gpios.label);
|
||||
return -ENODEV;
|
||||
}
|
||||
rc = gpio_pin_configure(drv_data->gpio, drv_cfg->power_gpios.pin,
|
||||
GPIO_OUTPUT_INACTIVE | drv_cfg->power_gpios.flags);
|
||||
if (rc != 0) {
|
||||
LOG_ERR("Failed to control feed %s.%u: %d", drv_cfg->power_gpios.label,
|
||||
drv_cfg->power_gpios.pin, rc);
|
||||
return rc;
|
||||
}
|
||||
#if DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||
if (!device_is_ready(drv_cfg->power.port)) {
|
||||
LOG_ERR("GPIO port for power control is not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
rc = gpio_pin_configure_dt(&drv_cfg->power, GPIO_OUTPUT_INACTIVE);
|
||||
if (rc != 0) {
|
||||
LOG_ERR("Failed to control feed %u: %d", drv_cfg->power.pin, rc);
|
||||
return rc;
|
||||
}
|
||||
#endif // DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||
|
||||
drv_data->as = (struct adc_sequence){
|
||||
.channels = BIT(0),
|
||||
@@ -175,12 +165,7 @@ static const struct bvd_config bvd_cfg = {
|
||||
DT_IO_CHANNELS_INPUT(DT_DRV_INST(0)),
|
||||
},
|
||||
#if DT_INST_NODE_HAS_PROP(0, power_gpios)
|
||||
.power_gpios =
|
||||
{
|
||||
DT_INST_GPIO_LABEL(0, power_gpios),
|
||||
DT_INST_GPIO_PIN(0, power_gpios),
|
||||
DT_INST_GPIO_FLAGS(0, power_gpios),
|
||||
},
|
||||
.power = GPIO_DT_SPEC_INST_GET(0, power_gpios),
|
||||
#endif
|
||||
.output_ohm = DT_INST_PROP(0, output_ohms),
|
||||
.full_ohm = DT_INST_PROP(0, full_ohms),
|
||||
|
||||
@@ -19,11 +19,9 @@
|
||||
LOG_MODULE_REGISTER(EC11, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static int ec11_get_ab_state(const struct device *dev) {
|
||||
struct ec11_data *drv_data = dev->data;
|
||||
const struct ec11_config *drv_cfg = dev->config;
|
||||
|
||||
return (gpio_pin_get(drv_data->a, drv_cfg->a_pin) << 1) |
|
||||
gpio_pin_get(drv_data->b, drv_cfg->b_pin);
|
||||
return (gpio_pin_get_dt(&drv_cfg->a) << 1) | gpio_pin_get_dt(&drv_cfg->b);
|
||||
}
|
||||
|
||||
static int ec11_sample_fetch(const struct device *dev, enum sensor_channel chan) {
|
||||
@@ -94,27 +92,25 @@ int ec11_init(const struct device *dev) {
|
||||
struct ec11_data *drv_data = dev->data;
|
||||
const struct ec11_config *drv_cfg = dev->config;
|
||||
|
||||
LOG_DBG("A: %s %d B: %s %d resolution %d", drv_cfg->a_label, drv_cfg->a_pin, drv_cfg->b_label,
|
||||
drv_cfg->b_pin, drv_cfg->resolution);
|
||||
LOG_DBG("A: %s %d B: %s %d resolution %d", drv_cfg->a.port->name, drv_cfg->a.pin,
|
||||
drv_cfg->b.port->name, drv_cfg->b.pin, drv_cfg->resolution);
|
||||
|
||||
drv_data->a = device_get_binding(drv_cfg->a_label);
|
||||
if (drv_data->a == NULL) {
|
||||
LOG_ERR("Failed to get pointer to A GPIO device");
|
||||
if (!device_is_ready(drv_cfg->a.port)) {
|
||||
LOG_ERR("A GPIO device is not ready");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
drv_data->b = device_get_binding(drv_cfg->b_label);
|
||||
if (drv_data->b == NULL) {
|
||||
LOG_ERR("Failed to get pointer to B GPIO device");
|
||||
if (!device_is_ready(drv_cfg->b.port)) {
|
||||
LOG_ERR("B GPIO device is not ready");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (gpio_pin_configure(drv_data->a, drv_cfg->a_pin, drv_cfg->a_flags | GPIO_INPUT)) {
|
||||
if (gpio_pin_configure_dt(&drv_cfg->a, GPIO_INPUT)) {
|
||||
LOG_DBG("Failed to configure A pin");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (gpio_pin_configure(drv_data->b, drv_cfg->b_pin, drv_cfg->b_flags | GPIO_INPUT)) {
|
||||
if (gpio_pin_configure_dt(&drv_cfg->b, GPIO_INPUT)) {
|
||||
LOG_DBG("Failed to configure B pin");
|
||||
return -EIO;
|
||||
}
|
||||
@@ -134,12 +130,8 @@ int ec11_init(const struct device *dev) {
|
||||
#define EC11_INST(n) \
|
||||
struct ec11_data ec11_data_##n; \
|
||||
const struct ec11_config ec11_cfg_##n = { \
|
||||
.a_label = DT_INST_GPIO_LABEL(n, a_gpios), \
|
||||
.a_pin = DT_INST_GPIO_PIN(n, a_gpios), \
|
||||
.a_flags = DT_INST_GPIO_FLAGS(n, a_gpios), \
|
||||
.b_label = DT_INST_GPIO_LABEL(n, b_gpios), \
|
||||
.b_pin = DT_INST_GPIO_PIN(n, b_gpios), \
|
||||
.b_flags = DT_INST_GPIO_FLAGS(n, b_gpios), \
|
||||
.a = GPIO_DT_SPEC_INST_GET(n, a_gpios), \
|
||||
.b = GPIO_DT_SPEC_INST_GET(n, b_gpios), \
|
||||
COND_CODE_0(DT_INST_NODE_HAS_PROP(n, resolution), (1), (DT_INST_PROP(n, resolution))), \
|
||||
}; \
|
||||
DEVICE_DT_INST_DEFINE(n, ec11_init, NULL, &ec11_data_##n, &ec11_cfg_##n, POST_KERNEL, \
|
||||
|
||||
@@ -11,20 +11,13 @@
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
struct ec11_config {
|
||||
const char *a_label;
|
||||
const uint8_t a_pin;
|
||||
const uint8_t a_flags;
|
||||
|
||||
const char *b_label;
|
||||
const uint8_t b_pin;
|
||||
const uint8_t b_flags;
|
||||
const struct gpio_dt_spec a;
|
||||
const struct gpio_dt_spec b;
|
||||
|
||||
const uint8_t resolution;
|
||||
};
|
||||
|
||||
struct ec11_data {
|
||||
const struct device *a;
|
||||
const struct device *b;
|
||||
uint8_t ab_state;
|
||||
int8_t pulses;
|
||||
int8_t ticks;
|
||||
|
||||
@@ -20,18 +20,15 @@ extern struct ec11_data ec11_driver;
|
||||
LOG_MODULE_DECLARE(EC11, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static inline void setup_int(const struct device *dev, bool enable) {
|
||||
struct ec11_data *data = dev->data;
|
||||
const struct ec11_config *cfg = dev->config;
|
||||
|
||||
LOG_DBG("enabled %s", (enable ? "true" : "false"));
|
||||
|
||||
if (gpio_pin_interrupt_configure(data->a, cfg->a_pin,
|
||||
enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) {
|
||||
if (gpio_pin_interrupt_configure_dt(&cfg->a, enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) {
|
||||
LOG_WRN("Unable to set A pin GPIO interrupt");
|
||||
}
|
||||
|
||||
if (gpio_pin_interrupt_configure(data->b, cfg->b_pin,
|
||||
enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) {
|
||||
if (gpio_pin_interrupt_configure_dt(&cfg->b, enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) {
|
||||
LOG_WRN("Unable to set A pin GPIO interrupt");
|
||||
}
|
||||
}
|
||||
@@ -121,16 +118,16 @@ int ec11_init_interrupt(const struct device *dev) {
|
||||
drv_data->dev = dev;
|
||||
/* setup gpio interrupt */
|
||||
|
||||
gpio_init_callback(&drv_data->a_gpio_cb, ec11_a_gpio_callback, BIT(drv_cfg->a_pin));
|
||||
gpio_init_callback(&drv_data->a_gpio_cb, ec11_a_gpio_callback, BIT(drv_cfg->a.pin));
|
||||
|
||||
if (gpio_add_callback(drv_data->a, &drv_data->a_gpio_cb) < 0) {
|
||||
if (gpio_add_callback(drv_cfg->a.port, &drv_data->a_gpio_cb) < 0) {
|
||||
LOG_DBG("Failed to set A callback!");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
gpio_init_callback(&drv_data->b_gpio_cb, ec11_b_gpio_callback, BIT(drv_cfg->b_pin));
|
||||
gpio_init_callback(&drv_data->b_gpio_cb, ec11_b_gpio_callback, BIT(drv_cfg->b.pin));
|
||||
|
||||
if (gpio_add_callback(drv_data->b, &drv_data->b_gpio_cb) < 0) {
|
||||
if (gpio_add_callback(drv_cfg->b.port, &drv_data->b_gpio_cb) < 0) {
|
||||
LOG_DBG("Failed to set B callback!");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user