From c634a793444e3d7b19d257d07fb7552745c29953 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Wed, 4 Aug 2021 22:24:43 +1000 Subject: [PATCH] eeprom: at2x: convert to `_dt_spec` Convert the at2x eeprom driver to `spi_dt_spec` and `i2c_dt_spec`. I2C functions are not fully converted due to the non-standard addressing scheme. Signed-off-by: Jordan Yates --- drivers/eeprom/eeprom_at2x.c | 53 +++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/drivers/eeprom/eeprom_at2x.c b/drivers/eeprom/eeprom_at2x.c index cb1aca0941..839a456c75 100644 --- a/drivers/eeprom/eeprom_at2x.c +++ b/drivers/eeprom/eeprom_at2x.c @@ -35,13 +35,12 @@ LOG_MODULE_REGISTER(eeprom_at2x); #define EEPROM_AT25_STATUS_BP1 BIT(3) /* Block Protection 1 (RW) */ struct eeprom_at2x_config { - const struct device *bus_dev; union { #ifdef CONFIG_EEPROM_AT24 - uint16_t i2c_addr; + struct i2c_dt_spec i2c; #endif /* CONFIG_EEPROM_AT24 */ #ifdef CONFIG_EEPROM_AT25 - struct spi_config spi_cfg; + struct spi_dt_spec spi; #endif /* CONFIG_EEPROM_AT25 */ } bus; struct gpio_dt_spec wp_gpio; @@ -50,6 +49,7 @@ struct eeprom_at2x_config { uint8_t addr_width; bool readonly; uint16_t timeout; + bool (*bus_is_ready)(const struct device *dev); eeprom_api_read read_fn; eeprom_api_write write_fn; }; @@ -66,7 +66,7 @@ static inline int eeprom_at2x_write_protect(const struct device *dev) return 0; } - return gpio_pin_set(config->wp_gpio.port, config->wp_gpio.pin, 1); + return gpio_pin_set_dt(&config->wp_gpio, 1); } static inline int eeprom_at2x_write_enable(const struct device *dev) @@ -77,7 +77,7 @@ static inline int eeprom_at2x_write_enable(const struct device *dev) return 0; } - return gpio_pin_set(config->wp_gpio.port, config->wp_gpio.pin, 0); + return gpio_pin_set_dt(&config->wp_gpio, 0); } static int eeprom_at2x_read(const struct device *dev, off_t offset, void *buf, @@ -203,6 +203,13 @@ static size_t eeprom_at2x_size(const struct device *dev) #ifdef CONFIG_EEPROM_AT24 +static bool eeprom_at24_bus_is_ready(const struct device *dev) +{ + const struct eeprom_at2x_config *config = dev->config; + + return device_is_ready(config->bus.i2c.bus); +} + /** * @brief translate an offset to a device address / offset pair * @@ -218,7 +225,7 @@ static uint16_t eeprom_at24_translate_offset(const struct device *dev, const uint16_t addr_incr = *offset >> config->addr_width; *offset &= BIT_MASK(config->addr_width); - return config->bus.i2c_addr + addr_incr; + return config->bus.i2c.addr + addr_incr; } static size_t eeprom_at24_adjust_read_count(const struct device *dev, @@ -260,7 +267,7 @@ static int eeprom_at24_read(const struct device *dev, off_t offset, void *buf, timeout = k_uptime_get() + config->timeout; while (1) { int64_t now = k_uptime_get(); - err = i2c_write_read(config->bus_dev, bus_addr, + err = i2c_write_read(config->bus.i2c.bus, bus_addr, addr, config->addr_width / 8, buf, len); if (!err || now > timeout) { @@ -308,7 +315,7 @@ static int eeprom_at24_write(const struct device *dev, off_t offset, timeout = k_uptime_get() + config->timeout; while (1) { int64_t now = k_uptime_get(); - err = i2c_write(config->bus_dev, block, sizeof(block), + err = i2c_write(config->bus.i2c.bus, block, sizeof(block), bus_addr); if (!err || now > timeout) { break; @@ -325,6 +332,14 @@ static int eeprom_at24_write(const struct device *dev, off_t offset, #endif /* CONFIG_EEPROM_AT24 */ #ifdef CONFIG_EEPROM_AT25 + +static bool eeprom_at25_bus_is_ready(const struct device *dev) +{ + const struct eeprom_at2x_config *config = dev->config; + + return spi_is_ready(&config->bus.spi); +} + static int eeprom_at25_rdsr(const struct device *dev, uint8_t *status) { const struct eeprom_at2x_config *config = dev->config; @@ -348,7 +363,7 @@ static int eeprom_at25_rdsr(const struct device *dev, uint8_t *status) .count = 1, }; - err = spi_transceive(config->bus_dev, &config->bus.spi_cfg, &tx, &rx); + err = spi_transceive_dt(&config->bus.spi, &tx, &rx); if (!err) { *status = sr[1]; } @@ -447,7 +462,7 @@ static int eeprom_at25_read(const struct device *dev, off_t offset, void *buf, return err; } - err = spi_transceive(config->bus_dev, &config->bus.spi_cfg, &tx, &rx); + err = spi_transceive_dt(&config->bus.spi, &tx, &rx); if (err < 0) { return err; } @@ -468,7 +483,7 @@ static int eeprom_at25_wren(const struct device *dev) .count = 1, }; - return spi_write(config->bus_dev, &config->bus.spi_cfg, &tx); + return spi_write_dt(&config->bus.spi, &tx); } static int eeprom_at25_write(const struct device *dev, off_t offset, @@ -522,7 +537,7 @@ static int eeprom_at25_write(const struct device *dev, off_t offset, return err; } - err = spi_transceive(config->bus_dev, &config->bus.spi_cfg, &tx, NULL); + err = spi_transceive_dt(&config->bus.spi, &tx, NULL); if (err) { return err; } @@ -539,7 +554,7 @@ static int eeprom_at2x_init(const struct device *dev) k_mutex_init(&data->lock); - if (!device_is_ready(config->bus_dev)) { + if (!config->bus_is_ready(dev)) { LOG_ERR("parent bus device not ready"); return -EINVAL; } @@ -585,12 +600,12 @@ static const struct eeprom_driver_api eeprom_at2x_api = { #define INST_DT_AT2X(inst, t) DT_INST(inst, atmel_at##t) #define EEPROM_AT24_BUS(n, t) \ - { .i2c_addr = DT_REG_ADDR(INST_DT_AT2X(n, t)) } + { .i2c = I2C_DT_SPEC_GET(INST_DT_AT2X(n, t)) } -#define EEPROM_AT25_BUS(n, t) \ - { .spi_cfg = SPI_CONFIG_DT(INST_DT_AT2X(n, t), \ - SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | \ - SPI_WORD_SET(8), 0) } +#define EEPROM_AT25_BUS(n, t) \ + { .spi = SPI_DT_SPEC_GET(INST_DT_AT2X(n, t), \ + SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | \ + SPI_WORD_SET(8), 0) } #define EEPROM_AT2X_DEVICE(n, t) \ ASSERT_PAGESIZE_IS_POWER_OF_2(DT_PROP(INST_DT_AT2X(n, t), pagesize)); \ @@ -599,7 +614,6 @@ static const struct eeprom_driver_api eeprom_at2x_api = { ASSERT_AT##t##_ADDR_W_VALID(DT_PROP(INST_DT_AT2X(n, t), \ address_width)); \ static const struct eeprom_at2x_config eeprom_at##t##_config_##n = { \ - .bus_dev = DEVICE_DT_GET(DT_BUS(INST_DT_AT2X(n, t))), \ .bus = EEPROM_AT##t##_BUS(n, t), \ .wp_gpio = GPIO_DT_SPEC_GET_OR(INST_DT_AT2X(n, t), wp_gpios, {0}), \ .size = DT_PROP(INST_DT_AT2X(n, t), size), \ @@ -607,6 +621,7 @@ static const struct eeprom_driver_api eeprom_at2x_api = { .addr_width = DT_PROP(INST_DT_AT2X(n, t), address_width), \ .readonly = DT_PROP(INST_DT_AT2X(n, t), read_only), \ .timeout = DT_PROP(INST_DT_AT2X(n, t), timeout), \ + .bus_is_ready = eeprom_at##t##_bus_is_ready, \ .read_fn = eeprom_at##t##_read, \ .write_fn = eeprom_at##t##_write, \ }; \