Skip to content

Commit 16c40b1

Browse files
talih0carlescufi
authored andcommitted
drivers: i2c: i2c_ifx_xmc4: Configure I2C and other small fixes
Currently the driver is not configured as controller during initialization. Any use of I2C in controller mode without an explicit i2c_configure() will not work. In this commit the driver is automatically configured. But, delay the configuraition until first use instead of during init because otherwise tests/drivers/i2c/i2c_target_api hangs without any errors on xmc47_relax_kit (when internal pulls are used). This issue needs to be investigated. There are a few other fixes/cleanups: - Change the default master_frequency from XMC4_I2C_SPEED_STANDARD to I2C_SPEED_STANDARD. - Use devicetree clock frequency for target configuration instead of I2C_SPEED_STANDARD. - Rename master_frequency to bitrate as it's also used by the target configuration now. - Remove several uneeded casts. - Forward backup config in get_config(). Signed-off-by: Andriy Gelman <[email protected]>
1 parent 24b5741 commit 16c40b1

File tree

1 file changed

+59
-47
lines changed

1 file changed

+59
-47
lines changed

drivers/i2c/i2c_ifx_xmc4.c

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111

1212
#define DT_DRV_COMPAT infineon_xmc4_i2c
1313

14+
#include <zephyr/logging/log.h>
15+
LOG_MODULE_REGISTER(i2c_infineon_xmc4, CONFIG_I2C_LOG_LEVEL);
16+
1417
#include <zephyr/drivers/i2c.h>
1518
#include <zephyr/drivers/pinctrl.h>
1619

20+
#include "i2c-priv.h"
21+
1722
#include <xmc_i2c.h>
1823
#include <xmc_usic.h>
1924

2025
#define USIC_IRQ_MIN 84
2126
#define IRQS_PER_USIC 6
2227

23-
#include <zephyr/logging/log.h>
24-
LOG_MODULE_REGISTER(i2c_infineon_xmc4, CONFIG_I2C_LOG_LEVEL);
25-
2628
#define I2C_XMC_EVENTS_MASK ( \
2729
XMC_I2C_CH_EVENT_RECEIVE_START | \
2830
XMC_I2C_CH_EVENT_DATA_LOST | \
@@ -61,6 +63,7 @@ struct ifx_xmc4_i2c_data {
6163
uint8_t target_wr_byte;
6264
uint8_t target_wr_buffer[CONFIG_I2C_INFINEON_XMC4_TARGET_BUF];
6365
bool ignore_slave_select;
66+
bool is_configured;
6467
};
6568

6669
/* Device config structure */
@@ -69,7 +72,7 @@ struct ifx_xmc4_i2c_config {
6972
const struct pinctrl_dev_config *pcfg;
7073
uint8_t scl_src;
7174
uint8_t sda_src;
72-
uint32_t master_frequency;
75+
uint32_t bitrate;
7376
void (*irq_config_func)(const struct device *dev);
7477
};
7578

@@ -78,24 +81,22 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
7881
struct ifx_xmc4_i2c_data *data = dev->data;
7982
const struct ifx_xmc4_i2c_config *config = dev->config;
8083

81-
if (dev_config != 0) {
82-
switch (I2C_SPEED_GET(dev_config)) {
83-
case I2C_SPEED_STANDARD:
84-
data->cfg.baudrate = XMC4_I2C_SPEED_STANDARD;
85-
break;
86-
case I2C_SPEED_FAST:
87-
data->cfg.baudrate = XMC4_I2C_SPEED_FAST;
88-
break;
89-
default:
90-
LOG_ERR("Unsupported speed");
91-
return -ERANGE;
92-
}
84+
/* This is deprecated and could be ignored in the future */
85+
if (dev_config & I2C_ADDR_10_BITS) {
86+
LOG_ERR("Use I2C_MSG_ADDR_10_BITS instead of I2C_ADDR_10_BITS");
87+
return -EIO;
88+
}
9389

94-
/* This is deprecated and could be ignored in the future */
95-
if (dev_config & I2C_ADDR_10_BITS) {
96-
LOG_ERR("Use I2C_MSG_ADDR_10_BITS instead of I2C_ADDR_10_BITS");
97-
return -EIO;
98-
}
90+
switch (I2C_SPEED_GET(dev_config)) {
91+
case I2C_SPEED_STANDARD:
92+
data->cfg.baudrate = XMC4_I2C_SPEED_STANDARD;
93+
break;
94+
case I2C_SPEED_FAST:
95+
data->cfg.baudrate = XMC4_I2C_SPEED_FAST;
96+
break;
97+
default:
98+
LOG_ERR("Unsupported speed");
99+
return -ERANGE;
99100
}
100101

101102
data->dev_config = dev_config;
@@ -105,6 +106,8 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
105106
return -EIO;
106107
}
107108

109+
XMC_I2C_CH_Stop(config->i2c);
110+
108111
/* Configure the I2C resource */
109112
data->cfg.normal_divider_mode = false;
110113
XMC_I2C_CH_Init(config->i2c, &data->cfg);
@@ -118,6 +121,7 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
118121
config->irq_config_func(dev);
119122
}
120123
XMC_I2C_CH_Start(config->i2c);
124+
data->is_configured = true;
121125

122126
/* Release semaphore */
123127
k_sem_give(&data->operation_sem);
@@ -128,26 +132,18 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
128132
static int ifx_xmc4_i2c_get_config(const struct device *dev, uint32_t *dev_config)
129133
{
130134
struct ifx_xmc4_i2c_data *data = dev->data;
131-
uint32_t config;
135+
const struct ifx_xmc4_i2c_config *config = dev->config;
132136

133-
switch (data->cfg.baudrate) {
134-
case XMC4_I2C_SPEED_STANDARD:
135-
config = I2C_SPEED_SET(I2C_SPEED_STANDARD);
136-
break;
137-
case XMC4_I2C_SPEED_FAST:
138-
config = I2C_SPEED_SET(I2C_SPEED_FAST);
139-
break;
140-
default:
141-
LOG_ERR("Unsupported speed");
142-
return -ERANGE;
143-
}
137+
if (!data->is_configured) {
138+
/* if not yet configured return configuration that will be used */
139+
/* when transfer() is called */
140+
uint32_t bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
144141

145-
if (data->dev_config & I2C_MODE_CONTROLLER) {
146-
config |= I2C_MODE_CONTROLLER;
142+
*dev_config = I2C_MODE_CONTROLLER | bitrate_cfg;
143+
return 0;
147144
}
148145

149-
/* Return current configuration */
150-
*dev_config = config;
146+
*dev_config = data->dev_config;
151147

152148
return 0;
153149
}
@@ -173,6 +169,16 @@ static int ifx_xmc4_i2c_transfer(const struct device *dev, struct i2c_msg *msg,
173169
return 0;
174170
}
175171

172+
if (!data->is_configured) {
173+
int ret;
174+
uint32_t bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
175+
176+
ret = ifx_xmc4_i2c_configure(dev, I2C_MODE_CONTROLLER | bitrate_cfg);
177+
if (ret) {
178+
return ret;
179+
}
180+
}
181+
176182
/* Acquire semaphore (block I2C transfer for another thread) */
177183
if (k_sem_take(&data->operation_sem, K_FOREVER)) {
178184
return -EIO;
@@ -295,17 +301,14 @@ static int ifx_xmc4_i2c_init(const struct device *dev)
295301
}
296302

297303
/* Configure dt provided device signals when available */
298-
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
299-
if (ret < 0) {
300-
return ret;
301-
}
302-
303-
return 0;
304+
return pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
304305
}
305306

306307
static int ifx_xmc4_i2c_target_register(const struct device *dev, struct i2c_target_config *cfg)
307308
{
308-
struct ifx_xmc4_i2c_data *data = (struct ifx_xmc4_i2c_data *)dev->data;
309+
struct ifx_xmc4_i2c_data *data = dev->data;
310+
const struct ifx_xmc4_i2c_config *config = dev->config;
311+
uint32_t bitrate_cfg;
309312

310313
if (!cfg ||
311314
!cfg->callbacks->read_requested ||
@@ -328,7 +331,16 @@ static int ifx_xmc4_i2c_target_register(const struct device *dev, struct i2c_tar
328331
data->p_target_config = cfg;
329332
data->cfg.address = cfg->address << 1;
330333

331-
if (ifx_xmc4_i2c_configure(dev, I2C_SPEED_SET(I2C_SPEED_STANDARD)) != 0) {
334+
if (data->is_configured) {
335+
uint32_t bitrate;
336+
337+
bitrate = I2C_SPEED_GET(data->dev_config);
338+
bitrate_cfg = i2c_map_dt_bitrate(bitrate);
339+
} else {
340+
bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
341+
}
342+
343+
if (ifx_xmc4_i2c_configure(dev, bitrate_cfg) != 0) {
332344
/* Release semaphore */
333345
k_sem_give(&data->target_sem);
334346
return -EIO;
@@ -340,7 +352,7 @@ static int ifx_xmc4_i2c_target_register(const struct device *dev, struct i2c_tar
340352

341353
static int ifx_xmc4_i2c_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
342354
{
343-
struct ifx_xmc4_i2c_data *data = (struct ifx_xmc4_i2c_data *)dev->data;
355+
struct ifx_xmc4_i2c_data *data = dev->data;
344356
const struct ifx_xmc4_i2c_config *config = dev->config;
345357

346358
/* Acquire semaphore (block I2C operation for another thread) */
@@ -454,7 +466,7 @@ static const struct i2c_driver_api i2c_xmc4_driver_api = {
454466
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
455467
.scl_src = DT_INST_ENUM_IDX(n, scl_src), \
456468
.sda_src = DT_INST_ENUM_IDX(n, sda_src), \
457-
.master_frequency = DT_INST_PROP_OR(n, clock_frequency, XMC4_I2C_SPEED_STANDARD), \
469+
.bitrate = DT_INST_PROP_OR(n, clock_frequency, I2C_SPEED_STANDARD), \
458470
XMC4_IRQ_HANDLER_STRUCT_INIT(n) \
459471
}; \
460472
\

0 commit comments

Comments
 (0)