drivers: gpio: gpio_pca953x: move 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:
parent
21cf889c87
commit
ff327c419d
1 changed files with 9 additions and 16 deletions
|
@ -64,10 +64,9 @@ struct pca953x_drv_data {
|
||||||
struct pca953x_config {
|
struct pca953x_config {
|
||||||
/* gpio_driver_config needs to be first */
|
/* gpio_driver_config needs to be first */
|
||||||
struct gpio_driver_config common;
|
struct gpio_driver_config common;
|
||||||
const struct device *i2c_dev;
|
struct i2c_dt_spec i2c;
|
||||||
const struct gpio_dt_spec gpio_int;
|
const struct gpio_dt_spec gpio_int;
|
||||||
bool interrupt_enabled;
|
bool interrupt_enabled;
|
||||||
uint8_t i2c_addr;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -86,8 +85,7 @@ static int update_input(const struct device *dev)
|
||||||
uint8_t input_states;
|
uint8_t input_states;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
rc = i2c_reg_read_byte(cfg->i2c_dev, cfg->i2c_addr,
|
rc = i2c_reg_read_byte_dt(&cfg->i2c, PCA953X_INPUT_PORT, &input_states);
|
||||||
PCA953X_INPUT_PORT, &input_states);
|
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
drv_data->pin_state.input = input_states;
|
drv_data->pin_state.input = input_states;
|
||||||
|
@ -228,14 +226,12 @@ static int gpio_pca953x_config(const struct device *dev, gpio_pin_t pin,
|
||||||
|
|
||||||
/* Set output values */
|
/* Set output values */
|
||||||
if (data_first) {
|
if (data_first) {
|
||||||
rc = i2c_reg_write_byte(cfg->i2c_dev, cfg->i2c_addr,
|
rc = i2c_reg_write_byte_dt(&cfg->i2c, PCA953X_OUTPUT_PORT, pins->output);
|
||||||
PCA953X_OUTPUT_PORT, pins->output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
/* Set pin directions */
|
/* Set pin directions */
|
||||||
rc = i2c_reg_write_byte(cfg->i2c_dev, cfg->i2c_addr,
|
rc = i2c_reg_write_byte_dt(&cfg->i2c, PCA953X_CONFIGURATION, pins->dir);
|
||||||
PCA953X_CONFIGURATION, pins->dir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
|
@ -264,8 +260,7 @@ static int gpio_pca953x_port_read(const struct device *dev,
|
||||||
k_sem_take(&drv_data->lock, K_FOREVER);
|
k_sem_take(&drv_data->lock, K_FOREVER);
|
||||||
|
|
||||||
/* Read Input Register */
|
/* Read Input Register */
|
||||||
rc = i2c_reg_read_byte(cfg->i2c_dev, cfg->i2c_addr,
|
rc = i2c_reg_read_byte_dt(&cfg->i2c, PCA953X_INPUT_PORT, &input_pin_data);
|
||||||
PCA953X_INPUT_PORT, &input_pin_data);
|
|
||||||
|
|
||||||
LOG_DBG("read %x got %d", input_pin_data, rc);
|
LOG_DBG("read %x got %d", input_pin_data, rc);
|
||||||
|
|
||||||
|
@ -300,8 +295,7 @@ static int gpio_pca953x_port_write(const struct device *dev,
|
||||||
orig_out = *outp;
|
orig_out = *outp;
|
||||||
out = ((orig_out & ~mask) | (value & mask)) ^ toggle;
|
out = ((orig_out & ~mask) | (value & mask)) ^ toggle;
|
||||||
|
|
||||||
rc = i2c_reg_write_byte(cfg->i2c_dev, cfg->i2c_addr,
|
rc = i2c_reg_write_byte_dt(&cfg->i2c, PCA953X_OUTPUT_PORT, out);
|
||||||
PCA953X_OUTPUT_PORT, out);
|
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
*outp = out;
|
*outp = out;
|
||||||
|
@ -404,8 +398,8 @@ static int gpio_pca953x_init(const struct device *dev)
|
||||||
struct pca953x_drv_data *drv_data = dev->data;
|
struct pca953x_drv_data *drv_data = dev->data;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
if (!device_is_ready(cfg->i2c_dev)) {
|
if (!device_is_ready(cfg->i2c.bus)) {
|
||||||
LOG_ERR("I2C device not found");
|
LOG_ERR("I2C bus device not found");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -468,13 +462,12 @@ static const struct gpio_driver_api api_table = {
|
||||||
|
|
||||||
#define GPIO_PCA953X_INIT(n) \
|
#define GPIO_PCA953X_INIT(n) \
|
||||||
static const struct pca953x_config pca953x_cfg_##n = { \
|
static const struct pca953x_config pca953x_cfg_##n = { \
|
||||||
.i2c_dev = DEVICE_DT_GET(DT_INST_BUS(n)), \
|
.i2c = I2C_DT_SPEC_INST_GET(n), \
|
||||||
.common = { \
|
.common = { \
|
||||||
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
|
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
|
||||||
}, \
|
}, \
|
||||||
.interrupt_enabled = DT_INST_NODE_HAS_PROP(n, nint_gpios), \
|
.interrupt_enabled = DT_INST_NODE_HAS_PROP(n, nint_gpios), \
|
||||||
.gpio_int = GPIO_DT_SPEC_INST_GET_OR(n, nint_gpios, {0}), \
|
.gpio_int = GPIO_DT_SPEC_INST_GET_OR(n, nint_gpios, {0}), \
|
||||||
.i2c_addr = DT_INST_REG_ADDR(n), \
|
|
||||||
}; \
|
}; \
|
||||||
\
|
\
|
||||||
static struct pca953x_drv_data pca953x_drvdata_##n = { \
|
static struct pca953x_drv_data pca953x_drvdata_##n = { \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue