11/*
22 * Copyright (c) 2018 Diego Sueiro
3+ * Copyright (c) 2024 Capgemini
34 *
45 * SPDX-License-Identifier: Apache-2.0
56 */
89
910#include <errno.h>
1011#include <zephyr/drivers/i2c.h>
12+ #include <zephyr/drivers/pinctrl.h>
1113#include <zephyr/kernel.h>
1214#include <zephyr/sys/util.h>
1315#include <em_cmu.h>
@@ -24,17 +26,10 @@ LOG_MODULE_REGISTER(i2c_gecko);
2426#define DEV_BASE (dev ) ((I2C_TypeDef *)((const struct i2c_gecko_config *const)(dev)->config)->base)
2527
2628struct i2c_gecko_config {
29+ const struct pinctrl_dev_config * pcfg ;
2730 I2C_TypeDef * base ;
2831 CMU_Clock_TypeDef clock ;
2932 uint32_t bitrate ;
30- struct soc_gpio_pin pin_sda ;
31- struct soc_gpio_pin pin_scl ;
32- #ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
33- uint8_t loc_sda ;
34- uint8_t loc_scl ;
35- #else
36- uint8_t loc ;
37- #endif
3833#if defined(CONFIG_I2C_TARGET )
3934 void (* irq_config_func )(const struct device * dev );
4035#endif
@@ -48,32 +43,6 @@ struct i2c_gecko_data {
4843#endif
4944};
5045
51- void i2c_gecko_config_pins (const struct device * dev , const struct soc_gpio_pin * pin_sda ,
52- const struct soc_gpio_pin * pin_scl )
53- {
54- I2C_TypeDef * base = DEV_BASE (dev );
55- const struct i2c_gecko_config * config = dev -> config ;
56-
57- GPIO_PinModeSet (pin_scl -> port , pin_scl -> pin , pin_scl -> mode , pin_scl -> out );
58- GPIO_PinModeSet (pin_sda -> port , pin_sda -> pin , pin_sda -> mode , pin_sda -> out );
59-
60- #ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
61- base -> ROUTEPEN = I2C_ROUTEPEN_SDAPEN | I2C_ROUTEPEN_SCLPEN ;
62- base -> ROUTELOC0 = (config -> loc_sda << _I2C_ROUTELOC0_SDALOC_SHIFT ) |
63- (config -> loc_scl << _I2C_ROUTELOC0_SCLLOC_SHIFT );
64- #elif defined(GPIO_I2C_ROUTEEN_SCLPEN ) && defined(GPIO_I2C_ROUTEEN_SDAPEN )
65- GPIO -> I2CROUTE [I2C_NUM (base )].ROUTEEN = GPIO_I2C_ROUTEEN_SCLPEN | GPIO_I2C_ROUTEEN_SDAPEN ;
66- GPIO -> I2CROUTE [I2C_NUM (base )].SCLROUTE =
67- (config -> pin_scl .pin << _GPIO_I2C_SCLROUTE_PIN_SHIFT ) |
68- (config -> pin_scl .port << _GPIO_I2C_SCLROUTE_PORT_SHIFT );
69- GPIO -> I2CROUTE [I2C_NUM (base )].SDAROUTE =
70- (config -> pin_sda .pin << _GPIO_I2C_SDAROUTE_PIN_SHIFT ) |
71- (config -> pin_sda .port << _GPIO_I2C_SDAROUTE_PORT_SHIFT );
72- #else
73- base -> ROUTE = I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | (config -> loc << 8 );
74- #endif
75- }
76-
7746static int i2c_gecko_configure (const struct device * dev , uint32_t dev_config_raw )
7847{
7948 I2C_TypeDef * base = DEV_BASE (dev );
@@ -182,7 +151,11 @@ static int i2c_gecko_init(const struct device *dev)
182151
183152 CMU_ClockEnable (config -> clock , true);
184153
185- i2c_gecko_config_pins (dev , & config -> pin_sda , & config -> pin_scl );
154+ error = pinctrl_apply_state (config -> pcfg , PINCTRL_STATE_DEFAULT );
155+ if (error < 0 ) {
156+ LOG_ERR ("Failed to configure I2C pins err[%d]" , error );
157+ return error ;
158+ }
186159
187160 bitrate_cfg = i2c_map_dt_bitrate (config -> bitrate );
188161
@@ -296,19 +269,6 @@ void i2c_gecko_isr(const struct device *dev)
296269}
297270#endif
298271
299- #ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
300- #define I2C_LOC_DATA (idx ) \
301- .loc_sda = DT_INST_PROP_BY_IDX(idx, location_sda, 0), \
302- .loc_scl = DT_INST_PROP_BY_IDX(idx, location_scl, 0)
303- #define I2C_VALIDATE_LOC (idx ) BUILD_ASSERT(true, "")
304- #else
305- #define I2C_VALIDATE_LOC (idx ) \
306- BUILD_ASSERT(DT_INST_PROP_BY_IDX(idx, location_sda, 0) == \
307- DT_INST_PROP_BY_IDX(idx, location_scl, 0), \
308- "DTS location-* properties must be equal")
309- #define I2C_LOC_DATA (idx ) .loc = DT_INST_PROP_BY_IDX(idx, location_scl, 0)
310- #endif
311-
312272#if defined(CONFIG_I2C_TARGET )
313273#define GECKO_I2C_IRQ_DEF (idx ) static void i2c_gecko_config_func_##idx(const struct device *dev);
314274#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)
325285#define GECKO_I2C_IRQ_DATA (idx )
326286#endif
327287
328- #define I2C_INIT (idx ) \
329- I2C_VALIDATE_LOC (idx); \
330- GECKO_I2C_IRQ_DEF(idx) \
331- static const struct i2c_gecko_config i2c_gecko_config_##idx = { \
332- .base = (I2C_TypeDef *)DT_INST_REG_ADDR( idx), \
333- .clock = cmuClock_I2C## idx, \
334- .pin_sda = {DT_INST_PROP_BY_IDX(idx, location_sda, 1), \
335- DT_INST_PROP_BY_IDX( idx, location_sda, 2), gpioModeWiredAnd, 1}, \
336- .pin_scl = {DT_INST_PROP_BY_IDX (idx, location_scl, 1), \
337- DT_INST_PROP_BY_IDX (idx, location_scl, 2), gpioModeWiredAnd, 1}, \
338- I2C_LOC_DATA(idx), \
339- .bitrate = DT_INST_PROP(idx, clock_frequency), \
340- GECKO_I2C_IRQ_DATA(idx)}; \
341- static struct i2c_gecko_data i2c_gecko_data_##idx; \
342- I2C_DEVICE_DT_INST_DEFINE(idx, i2c_gecko_init, NULL, &i2c_gecko_data_ ##idx, \
343- &i2c_gecko_config_##idx, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
344- &i2c_gecko_driver_api); \
288+ #define I2C_INIT (idx ) \
289+ PINCTRL_DT_INST_DEFINE (idx); \
290+ GECKO_I2C_IRQ_DEF(idx); \
291+ \
292+ static const struct i2c_gecko_config i2c_gecko_config_## idx = { \
293+ .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET( idx), \
294+ .base = (I2C_TypeDef *)DT_INST_REG_ADDR(idx), \
295+ .clock = cmuClock_I2C## idx, \
296+ .bitrate = DT_INST_PROP (idx, clock_frequency), \
297+ GECKO_I2C_IRQ_DATA (idx)}; \
298+ \
299+ static struct i2c_gecko_data i2c_gecko_data_##idx; \
300+ \
301+ I2C_DEVICE_DT_INST_DEFINE(idx, i2c_gecko_init, NULL, & i2c_gecko_data_##idx, \
302+ &i2c_gecko_config_ ##idx, POST_KERNEL, \
303+ CONFIG_I2C_INIT_PRIORITY, &i2c_gecko_driver_api); \
304+ \
345305 GECKO_I2C_IRQ_HANDLER(idx)
346306
347307DT_INST_FOREACH_STATUS_OKAY (I2C_INIT )
0 commit comments