drivers: i2c: gpio: use gpio_dt_spec
Simplify the implementation by using gpio_dt_spec. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
parent
2c5de11a8b
commit
be63d6addc
1 changed files with 22 additions and 40 deletions
|
@ -39,42 +39,34 @@ LOG_MODULE_REGISTER(i2c_gpio);
|
|||
|
||||
/* Driver config */
|
||||
struct i2c_gpio_config {
|
||||
const char *scl_gpio_name;
|
||||
const char *sda_gpio_name;
|
||||
gpio_pin_t scl_pin;
|
||||
gpio_pin_t sda_pin;
|
||||
gpio_dt_flags_t scl_flags;
|
||||
gpio_dt_flags_t sda_flags;
|
||||
struct gpio_dt_spec scl_gpio;
|
||||
struct gpio_dt_spec sda_gpio;
|
||||
uint32_t bitrate;
|
||||
};
|
||||
|
||||
/* Driver instance data */
|
||||
struct i2c_gpio_context {
|
||||
struct i2c_bitbang bitbang; /* Bit-bang library data */
|
||||
const struct device *scl_gpio; /* GPIO used for I2C SCL line */
|
||||
const struct device *sda_gpio; /* GPIO used for I2C SDA line */
|
||||
gpio_pin_t scl_pin; /* Pin on gpio used for SCL line */
|
||||
gpio_pin_t sda_pin; /* Pin on gpio used for SDA line */
|
||||
};
|
||||
|
||||
static void i2c_gpio_set_scl(void *io_context, int state)
|
||||
{
|
||||
struct i2c_gpio_context *context = io_context;
|
||||
const struct i2c_gpio_config *config = io_context;
|
||||
|
||||
gpio_pin_set(context->scl_gpio, context->scl_pin, state);
|
||||
gpio_pin_set_dt(&config->scl_gpio, state);
|
||||
}
|
||||
|
||||
static void i2c_gpio_set_sda(void *io_context, int state)
|
||||
{
|
||||
struct i2c_gpio_context *context = io_context;
|
||||
const struct i2c_gpio_config *config = io_context;
|
||||
|
||||
gpio_pin_set(context->sda_gpio, context->sda_pin, state);
|
||||
gpio_pin_set_dt(&config->sda_gpio, state);
|
||||
}
|
||||
|
||||
static int i2c_gpio_get_sda(void *io_context)
|
||||
{
|
||||
struct i2c_gpio_context *context = io_context;
|
||||
int rc = gpio_pin_get(context->sda_gpio, context->sda_pin);
|
||||
const struct i2c_gpio_config *config = io_context;
|
||||
int rc = gpio_pin_get_dt(&config->sda_gpio);
|
||||
|
||||
/* Default high as that would be a NACK */
|
||||
return rc != 0;
|
||||
|
@ -122,40 +114,34 @@ static int i2c_gpio_init(const struct device *dev)
|
|||
uint32_t bitrate_cfg;
|
||||
int err;
|
||||
|
||||
context->scl_gpio = device_get_binding(config->scl_gpio_name);
|
||||
if (!context->scl_gpio) {
|
||||
LOG_ERR("failed to get SCL GPIO device");
|
||||
return -EINVAL;
|
||||
if (!device_is_ready(config->scl_gpio.port)) {
|
||||
LOG_ERR("SCL GPIO device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = gpio_pin_configure(context->scl_gpio, config->scl_pin,
|
||||
config->scl_flags | GPIO_OUTPUT_HIGH);
|
||||
err = gpio_pin_configure_dt(&config->scl_gpio, GPIO_OUTPUT_HIGH);
|
||||
if (err) {
|
||||
LOG_ERR("failed to configure SCL GPIO pin (err %d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
context->sda_gpio = device_get_binding(config->sda_gpio_name);
|
||||
if (!context->sda_gpio) {
|
||||
LOG_ERR("failed to get SDA GPIO device");
|
||||
return -EINVAL;
|
||||
if (!device_is_ready(config->sda_gpio.port)) {
|
||||
LOG_ERR("SDA GPIO device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = gpio_pin_configure(context->sda_gpio, config->sda_pin,
|
||||
config->sda_flags | GPIO_INPUT | GPIO_OUTPUT_HIGH);
|
||||
err = gpio_pin_configure_dt(&config->sda_gpio,
|
||||
GPIO_INPUT | GPIO_OUTPUT_HIGH);
|
||||
if (err == -ENOTSUP) {
|
||||
err = gpio_pin_configure(context->sda_gpio, config->sda_pin,
|
||||
config->sda_flags | GPIO_OUTPUT_HIGH);
|
||||
err = gpio_pin_configure_dt(&config->sda_gpio,
|
||||
GPIO_OUTPUT_HIGH);
|
||||
}
|
||||
if (err) {
|
||||
LOG_ERR("failed to configure SDA GPIO pin (err %d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
context->sda_pin = config->sda_pin;
|
||||
context->scl_pin = config->scl_pin;
|
||||
|
||||
i2c_bitbang_init(&context->bitbang, &io_fns, context);
|
||||
i2c_bitbang_init(&context->bitbang, &io_fns, config);
|
||||
|
||||
bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
|
||||
err = i2c_bitbang_configure(&context->bitbang,
|
||||
|
@ -173,12 +159,8 @@ static int i2c_gpio_init(const struct device *dev)
|
|||
static struct i2c_gpio_context i2c_gpio_dev_data_##_num; \
|
||||
\
|
||||
static const struct i2c_gpio_config i2c_gpio_dev_cfg_##_num = { \
|
||||
.scl_gpio_name = DT_INST_GPIO_LABEL(_num, scl_gpios), \
|
||||
.sda_gpio_name = DT_INST_GPIO_LABEL(_num, sda_gpios), \
|
||||
.scl_pin = DT_INST_GPIO_PIN(_num, scl_gpios), \
|
||||
.sda_pin = DT_INST_GPIO_PIN(_num, sda_gpios), \
|
||||
.scl_flags = DT_INST_GPIO_FLAGS(_num, scl_gpios), \
|
||||
.sda_flags = DT_INST_GPIO_FLAGS(_num, sda_gpios), \
|
||||
.scl_gpio = GPIO_DT_SPEC_INST_GET(_num, scl_gpios), \
|
||||
.sda_gpio = GPIO_DT_SPEC_INST_GET(_num, sda_gpios), \
|
||||
.bitrate = DT_INST_PROP(_num, clock_frequency), \
|
||||
}; \
|
||||
\
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue