Skip to content

Commit b2a4a36

Browse files
ananglcarlescufi
authored andcommitted
drivers: i2c: nrfx: Add support for pinctrl
Add support for the new pinctrl API to the I2C drivers that handle the nRF TWI and TWIM peripherals. Update code of the drivers and related devicetree bindings. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 32b9e65 commit b2a4a36

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed

drivers/i2c/i2c_nrfx_twi.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <drivers/i2c.h>
99
#include <dt-bindings/i2c/i2c.h>
1010
#include <pm/device.h>
11+
#include <drivers/pinctrl.h>
12+
#include <soc.h>
1113
#include <nrfx_twi.h>
1214

1315
#include <logging/log.h>
@@ -25,6 +27,9 @@ struct i2c_nrfx_twi_data {
2527
struct i2c_nrfx_twi_config {
2628
nrfx_twi_t twi;
2729
nrfx_twi_config_t config;
30+
#ifdef CONFIG_PINCTRL
31+
const struct pinctrl_dev_config *pcfg;
32+
#endif
2833
};
2934

3035
static int i2c_nrfx_twi_transfer(const struct device *dev,
@@ -228,6 +233,13 @@ static int twi_nrfx_pm_action(const struct device *dev,
228233

229234
switch (action) {
230235
case PM_DEVICE_ACTION_RESUME:
236+
#ifdef CONFIG_PINCTRL
237+
ret = pinctrl_apply_state(config->pcfg,
238+
PINCTRL_STATE_DEFAULT);
239+
if (ret < 0) {
240+
return ret;
241+
}
242+
#endif
231243
init_twi(dev);
232244
if (data->dev_config) {
233245
i2c_nrfx_twi_configure(dev, data->dev_config);
@@ -236,6 +248,14 @@ static int twi_nrfx_pm_action(const struct device *dev,
236248

237249
case PM_DEVICE_ACTION_SUSPEND:
238250
nrfx_twi_uninit(&config->twi);
251+
252+
#ifdef CONFIG_PINCTRL
253+
ret = pinctrl_apply_state(config->pcfg,
254+
PINCTRL_STATE_SLEEP);
255+
if (ret < 0) {
256+
return ret;
257+
}
258+
#endif
239259
break;
240260

241261
default:
@@ -256,14 +276,30 @@ static int twi_nrfx_pm_action(const struct device *dev,
256276
#define I2C_FREQUENCY(idx) \
257277
I2C_NRFX_TWI_FREQUENCY(DT_PROP(I2C(idx), clock_frequency))
258278

279+
#define I2C_NRFX_TWI_PIN_CFG(idx) \
280+
COND_CODE_1(CONFIG_PINCTRL, \
281+
(.skip_gpio_cfg = true, \
282+
.skip_psel_cfg = true,), \
283+
(.scl = DT_PROP(I2C(idx), scl_pin), \
284+
.sda = DT_PROP(I2C(idx), sda_pin),))
285+
259286
#define I2C_NRFX_TWI_DEVICE(idx) \
287+
NRF_DT_ENSURE_PINS_ASSIGNED(I2C(idx), scl_pin); \
260288
BUILD_ASSERT(I2C_FREQUENCY(idx) != \
261289
I2C_NRFX_TWI_INVALID_FREQUENCY, \
262290
"Wrong I2C " #idx " frequency setting in dts"); \
263-
static int twi_##idx##_init(const struct device *dev) \
291+
static int twi_##idx##_init(const struct device *dev) \
264292
{ \
265293
IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), \
266294
nrfx_isr, nrfx_twi_##idx##_irq_handler, 0); \
295+
IF_ENABLED(CONFIG_PINCTRL, ( \
296+
const struct i2c_nrfx_twi_config *config = dev->config;\
297+
int err = pinctrl_apply_state(config->pcfg, \
298+
PINCTRL_STATE_DEFAULT); \
299+
if (err < 0) { \
300+
return err; \
301+
} \
302+
)) \
267303
return init_twi(dev); \
268304
} \
269305
static struct i2c_nrfx_twi_data twi_##idx##_data = { \
@@ -272,13 +308,15 @@ static int twi_nrfx_pm_action(const struct device *dev,
272308
.completion_sync = Z_SEM_INITIALIZER( \
273309
twi_##idx##_data.completion_sync, 0, 1) \
274310
}; \
311+
IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(I2C(idx)))); \
275312
static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
276313
.twi = NRFX_TWI_INSTANCE(idx), \
277314
.config = { \
278-
.scl = DT_PROP(I2C(idx), scl_pin), \
279-
.sda = DT_PROP(I2C(idx), sda_pin), \
315+
I2C_NRFX_TWI_PIN_CFG(idx) \
280316
.frequency = I2C_FREQUENCY(idx), \
281-
} \
317+
}, \
318+
IF_ENABLED(CONFIG_PINCTRL, \
319+
(.pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)),)) \
282320
}; \
283321
PM_DEVICE_DT_DEFINE(I2C(idx), twi_nrfx_pm_action); \
284322
I2C_DEVICE_DT_DEFINE(I2C(idx), \

drivers/i2c/i2c_nrfx_twim.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <drivers/i2c.h>
99
#include <dt-bindings/i2c/i2c.h>
1010
#include <pm/device.h>
11+
#include <drivers/pinctrl.h>
12+
#include <soc.h>
1113
#include <nrfx_twim.h>
1214
#include <sys/util.h>
1315

@@ -29,6 +31,9 @@ struct i2c_nrfx_twim_config {
2931
nrfx_twim_t twim;
3032
uint16_t concat_buf_size;
3133
uint16_t flash_buf_max_size;
34+
#ifdef CONFIG_PINCTRL
35+
const struct pinctrl_dev_config *pcfg;
36+
#endif
3237
};
3338

3439
static int init_twim(const struct device *dev);
@@ -317,15 +322,33 @@ static const struct i2c_driver_api i2c_nrfx_twim_driver_api = {
317322
static int twim_nrfx_pm_action(const struct device *dev,
318323
enum pm_device_action action)
319324
{
325+
#ifdef CONFIG_PINCTRL
326+
const struct i2c_nrfx_twim_config *dev_config = dev->config;
327+
#endif
320328
int ret = 0;
321329

322330
switch (action) {
323331
case PM_DEVICE_ACTION_RESUME:
332+
#ifdef CONFIG_PINCTRL
333+
ret = pinctrl_apply_state(dev_config->pcfg,
334+
PINCTRL_STATE_DEFAULT);
335+
if (ret < 0) {
336+
return ret;
337+
}
338+
#endif
324339
ret = init_twim(dev);
325340
break;
326341

327342
case PM_DEVICE_ACTION_SUSPEND:
328343
deinit_twim(dev);
344+
345+
#ifdef CONFIG_PINCTRL
346+
ret = pinctrl_apply_state(dev_config->pcfg,
347+
PINCTRL_STATE_SLEEP);
348+
if (ret < 0) {
349+
return ret;
350+
}
351+
#endif
329352
break;
330353

331354
default:
@@ -362,22 +385,38 @@ static int twim_nrfx_pm_action(const struct device *dev,
362385
(1))
363386
#define MSG_BUF_SIZE(idx) MAX(CONCAT_BUF_SIZE(idx), FLASH_BUF_MAX_SIZE(idx))
364387

388+
#define I2C_NRFX_TWIM_PIN_CFG(idx) \
389+
COND_CODE_1(CONFIG_PINCTRL, \
390+
(.skip_gpio_cfg = true, \
391+
.skip_psel_cfg = true,), \
392+
(.scl = DT_PROP(I2C(idx), scl_pin), \
393+
.sda = DT_PROP(I2C(idx), sda_pin),))
394+
365395
#define I2C_NRFX_TWIM_DEVICE(idx) \
396+
NRF_DT_ENSURE_PINS_ASSIGNED(I2C(idx), scl_pin); \
366397
BUILD_ASSERT(I2C_FREQUENCY(idx) != \
367398
I2C_NRFX_TWIM_INVALID_FREQUENCY, \
368399
"Wrong I2C " #idx " frequency setting in dts"); \
369400
static int twim_##idx##_init(const struct device *dev) \
370401
{ \
371402
IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), \
372403
nrfx_isr, nrfx_twim_##idx##_irq_handler, 0); \
404+
IF_ENABLED(CONFIG_PINCTRL, ( \
405+
const struct i2c_nrfx_twim_config *dev_config = \
406+
dev->config; \
407+
int err = pinctrl_apply_state(dev_config->pcfg, \
408+
PINCTRL_STATE_DEFAULT); \
409+
if (err < 0) { \
410+
return err; \
411+
} \
412+
)) \
373413
return init_twim(dev); \
374414
} \
375415
IF_ENABLED(USES_MSG_BUF(idx), \
376416
(static uint8_t twim_##idx##_msg_buf[MSG_BUF_SIZE(idx)];)) \
377417
static struct i2c_nrfx_twim_data twim_##idx##_data = { \
378418
.twim_config = { \
379-
.scl = DT_PROP(I2C(idx), scl_pin), \
380-
.sda = DT_PROP(I2C(idx), sda_pin), \
419+
I2C_NRFX_TWIM_PIN_CFG(idx) \
381420
.frequency = I2C_FREQUENCY(idx), \
382421
}, \
383422
.transfer_sync = Z_SEM_INITIALIZER( \
@@ -387,10 +426,13 @@ static int twim_nrfx_pm_action(const struct device *dev,
387426
IF_ENABLED(USES_MSG_BUF(idx), \
388427
(.msg_buf = twim_##idx##_msg_buf,)) \
389428
}; \
429+
IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(I2C(idx)))); \
390430
static const struct i2c_nrfx_twim_config twim_##idx##z_config = { \
391431
.twim = NRFX_TWIM_INSTANCE(idx), \
392432
.concat_buf_size = CONCAT_BUF_SIZE(idx), \
393433
.flash_buf_max_size = FLASH_BUF_MAX_SIZE(idx), \
434+
IF_ENABLED(CONFIG_PINCTRL, \
435+
(.pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)),)) \
394436
}; \
395437
PM_DEVICE_DT_DEFINE(I2C(idx), twim_nrfx_pm_action); \
396438
I2C_DEVICE_DT_DEFINE(I2C(idx), \

dts/bindings/i2c/nordic,nrf-twi-common.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Common fields for Nordic nRF family TWI peripherals
66

7-
include: i2c-controller.yaml
7+
include: [i2c-controller.yaml, pinctrl-device.yaml]
88

99
properties:
1010
reg:
@@ -15,8 +15,11 @@ properties:
1515

1616
sda-pin:
1717
type: int
18-
required: true
18+
required: false
1919
description: |
20+
IMPORTANT: This option will only be used if the new pin control driver
21+
is not enabled. It will be deprecated in the future.
22+
2023
The SDA pin to use.
2124
2225
For pins P0.0 through P0.31, use the pin number. For example,
@@ -31,7 +34,10 @@ properties:
3134
3235
scl-pin:
3336
type: int
34-
required: true
37+
required: false
3538
description: |
39+
IMPORTANT: This option will only be used if the new pin control driver
40+
is not enabled. It will be deprecated in the future.
41+
3642
The SCL pin to use. The pin numbering scheme is the same as
3743
the sda-pin property's.

0 commit comments

Comments
 (0)