drivers: i2c: i2c_gecko: Refactor driver to use pinctrl api

Deprecate the use of location_* properties in the i2c_gecko driver
and migrate to the pinctrl api for enhanced maintainability and
compliance with current standards.

Signed-off-by: Arunmani Alagarsamy <arunmani.a@capgemini.com>
This commit is contained in:
Arunmani Alagarsamy 2024-04-18 11:07:39 +05:30 committed by Anas Nashif
commit 0810180135
2 changed files with 28 additions and 75 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018 Diego Sueiro
* Copyright (c) 2024 Capgemini
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -8,6 +9,7 @@
#include <errno.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>
#include <em_cmu.h>
@ -24,17 +26,10 @@ LOG_MODULE_REGISTER(i2c_gecko);
#define DEV_BASE(dev) ((I2C_TypeDef *)((const struct i2c_gecko_config *const)(dev)->config)->base)
struct i2c_gecko_config {
const struct pinctrl_dev_config *pcfg;
I2C_TypeDef *base;
CMU_Clock_TypeDef clock;
uint32_t bitrate;
struct soc_gpio_pin pin_sda;
struct soc_gpio_pin pin_scl;
#ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
uint8_t loc_sda;
uint8_t loc_scl;
#else
uint8_t loc;
#endif
#if defined(CONFIG_I2C_TARGET)
void (*irq_config_func)(const struct device *dev);
#endif
@ -48,32 +43,6 @@ struct i2c_gecko_data {
#endif
};
void i2c_gecko_config_pins(const struct device *dev, const struct soc_gpio_pin *pin_sda,
const struct soc_gpio_pin *pin_scl)
{
I2C_TypeDef *base = DEV_BASE(dev);
const struct i2c_gecko_config *config = dev->config;
GPIO_PinModeSet(pin_scl->port, pin_scl->pin, pin_scl->mode, pin_scl->out);
GPIO_PinModeSet(pin_sda->port, pin_sda->pin, pin_sda->mode, pin_sda->out);
#ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
base->ROUTEPEN = I2C_ROUTEPEN_SDAPEN | I2C_ROUTEPEN_SCLPEN;
base->ROUTELOC0 = (config->loc_sda << _I2C_ROUTELOC0_SDALOC_SHIFT) |
(config->loc_scl << _I2C_ROUTELOC0_SCLLOC_SHIFT);
#elif defined(GPIO_I2C_ROUTEEN_SCLPEN) && defined(GPIO_I2C_ROUTEEN_SDAPEN)
GPIO->I2CROUTE[I2C_NUM(base)].ROUTEEN = GPIO_I2C_ROUTEEN_SCLPEN | GPIO_I2C_ROUTEEN_SDAPEN;
GPIO->I2CROUTE[I2C_NUM(base)].SCLROUTE =
(config->pin_scl.pin << _GPIO_I2C_SCLROUTE_PIN_SHIFT) |
(config->pin_scl.port << _GPIO_I2C_SCLROUTE_PORT_SHIFT);
GPIO->I2CROUTE[I2C_NUM(base)].SDAROUTE =
(config->pin_sda.pin << _GPIO_I2C_SDAROUTE_PIN_SHIFT) |
(config->pin_sda.port << _GPIO_I2C_SDAROUTE_PORT_SHIFT);
#else
base->ROUTE = I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | (config->loc << 8);
#endif
}
static int i2c_gecko_configure(const struct device *dev, uint32_t dev_config_raw)
{
I2C_TypeDef *base = DEV_BASE(dev);
@ -182,7 +151,11 @@ static int i2c_gecko_init(const struct device *dev)
CMU_ClockEnable(config->clock, true);
i2c_gecko_config_pins(dev, &config->pin_sda, &config->pin_scl);
error = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
if (error < 0) {
LOG_ERR("Failed to configure I2C pins err[%d]", error);
return error;
}
bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
@ -296,19 +269,6 @@ void i2c_gecko_isr(const struct device *dev)
}
#endif
#ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
#define I2C_LOC_DATA(idx) \
.loc_sda = DT_INST_PROP_BY_IDX(idx, location_sda, 0), \
.loc_scl = DT_INST_PROP_BY_IDX(idx, location_scl, 0)
#define I2C_VALIDATE_LOC(idx) BUILD_ASSERT(true, "")
#else
#define I2C_VALIDATE_LOC(idx) \
BUILD_ASSERT(DT_INST_PROP_BY_IDX(idx, location_sda, 0) == \
DT_INST_PROP_BY_IDX(idx, location_scl, 0), \
"DTS location-* properties must be equal")
#define I2C_LOC_DATA(idx) .loc = DT_INST_PROP_BY_IDX(idx, location_scl, 0)
#endif
#if defined(CONFIG_I2C_TARGET)
#define GECKO_I2C_IRQ_DEF(idx) static void i2c_gecko_config_func_##idx(const struct device *dev);
#define GECKO_I2C_IRQ_DATA(idx) .irq_config_func = i2c_gecko_config_func_##idx,
@ -325,23 +285,23 @@ void i2c_gecko_isr(const struct device *dev)
#define GECKO_I2C_IRQ_DATA(idx)
#endif
#define I2C_INIT(idx) \
I2C_VALIDATE_LOC(idx); \
GECKO_I2C_IRQ_DEF(idx) \
static const struct i2c_gecko_config i2c_gecko_config_##idx = { \
.base = (I2C_TypeDef *)DT_INST_REG_ADDR(idx), \
.clock = cmuClock_I2C##idx, \
.pin_sda = {DT_INST_PROP_BY_IDX(idx, location_sda, 1), \
DT_INST_PROP_BY_IDX(idx, location_sda, 2), gpioModeWiredAnd, 1}, \
.pin_scl = {DT_INST_PROP_BY_IDX(idx, location_scl, 1), \
DT_INST_PROP_BY_IDX(idx, location_scl, 2), gpioModeWiredAnd, 1}, \
I2C_LOC_DATA(idx), \
.bitrate = DT_INST_PROP(idx, clock_frequency), \
GECKO_I2C_IRQ_DATA(idx)}; \
static struct i2c_gecko_data i2c_gecko_data_##idx; \
I2C_DEVICE_DT_INST_DEFINE(idx, i2c_gecko_init, NULL, &i2c_gecko_data_##idx, \
&i2c_gecko_config_##idx, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
&i2c_gecko_driver_api); \
#define I2C_INIT(idx) \
PINCTRL_DT_INST_DEFINE(idx); \
GECKO_I2C_IRQ_DEF(idx); \
\
static const struct i2c_gecko_config i2c_gecko_config_##idx = { \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
.base = (I2C_TypeDef *)DT_INST_REG_ADDR(idx), \
.clock = cmuClock_I2C##idx, \
.bitrate = DT_INST_PROP(idx, clock_frequency), \
GECKO_I2C_IRQ_DATA(idx)}; \
\
static struct i2c_gecko_data i2c_gecko_data_##idx; \
\
I2C_DEVICE_DT_INST_DEFINE(idx, i2c_gecko_init, NULL, &i2c_gecko_data_##idx, \
&i2c_gecko_config_##idx, POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, &i2c_gecko_driver_api); \
\
GECKO_I2C_IRQ_HANDLER(idx)
DT_INST_FOREACH_STATUS_OKAY(I2C_INIT)

View file

@ -5,7 +5,7 @@ description: Silabs Gecko I2C node
compatible: "silabs,gecko-i2c"
include: i2c-controller.yaml
include: [i2c-controller.yaml, pinctrl-device.yaml]
properties:
reg:
@ -14,15 +14,8 @@ properties:
interrupts:
required: true
# Note: Not all SoC series support setting individual pin location. If this
# is a case all location-* properties need to have identical value.
location-sda:
type: array
pinctrl-0:
required: true
description: SDA pin configuration defined as <location port pin>
location-scl:
type: array
pinctrl-names:
required: true
description: SCL pin configuration defined as <location port pin>