Skip to content

Commit 821c03a

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
i2c: nordic: switch to phandle arrays for pinmux
Deprecate the scl-pin and sda-pin properties in the devicetree. Provide new scl-gpios and sda-gpios properties instead. This lets the user specify SCL and SDA like this: &i2c0 { scl-gpios = <&gpio0 1 0>; sda-gpios = <&gpio1 4 0>; }; Instead of having to use: &i2c0 { scl-pin = <1>; sda-pin = <36>; }; Provide error checking and understandable error messages for invalid configurations. Signed-off-by: Martí Bolívar <[email protected]>
1 parent f7aeb2a commit 821c03a

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

drivers/i2c/i2c_nrfx_twi.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <drivers/i2c.h>
99
#include <dt-bindings/i2c/i2c.h>
1010
#include <nrfx_twi.h>
11+
#include <soc.h>
1112

1213
#include <logging/log.h>
1314
LOG_MODULE_REGISTER(i2c_nrfx_twi, CONFIG_I2C_LOG_LEVEL);
@@ -276,6 +277,14 @@ static int twi_nrfx_pm_control(const struct device *dev,
276277
BUILD_ASSERT(I2C_FREQUENCY(idx) != \
277278
I2C_NRFX_TWI_INVALID_FREQUENCY, \
278279
"Wrong I2C " #idx " frequency setting in dts"); \
280+
NRF_DT_PSEL_CHECK_EXACTLY_ONE(I2C(idx), \
281+
sda_pin, "sda-pin", \
282+
sda_gpios, "sda-gpios"); \
283+
NRF_DT_PSEL_CHECK_EXACTLY_ONE(I2C(idx), \
284+
scl_pin, "scl-pin", \
285+
scl_gpios, "scl-gpios"); \
286+
NRF_DT_CHECK_GPIO_CTLR_IS_SOC(I2C(idx), sda_gpios, "sda-gpios"); \
287+
NRF_DT_CHECK_GPIO_CTLR_IS_SOC(I2C(idx), scl_gpios, "scl-gpios"); \
279288
static int twi_##idx##_init(const struct device *dev) \
280289
{ \
281290
IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), \
@@ -291,8 +300,8 @@ static int twi_nrfx_pm_control(const struct device *dev,
291300
static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
292301
.twi = NRFX_TWI_INSTANCE(idx), \
293302
.config = { \
294-
.scl = DT_PROP(I2C(idx), scl_pin), \
295-
.sda = DT_PROP(I2C(idx), sda_pin), \
303+
.scl = NRF_DT_PSEL(I2C(idx), scl_pin, scl_gpios, 0), \
304+
.sda = NRF_DT_PSEL(I2C(idx), sda_pin, sda_gpios, 0), \
296305
.frequency = I2C_FREQUENCY(idx), \
297306
} \
298307
}; \

drivers/i2c/i2c_nrfx_twim.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <dt-bindings/i2c/i2c.h>
1010
#include <nrfx_twim.h>
1111
#include <sys/util.h>
12+
#include <soc.h>
1213

1314
#include <logging/log.h>
1415
LOG_MODULE_REGISTER(i2c_nrfx_twim, CONFIG_I2C_LOG_LEVEL);
@@ -324,6 +325,14 @@ static int twim_nrfx_pm_control(const struct device *dev,
324325
BUILD_ASSERT(I2C_FREQUENCY(idx) != \
325326
I2C_NRFX_TWIM_INVALID_FREQUENCY, \
326327
"Wrong I2C " #idx " frequency setting in dts"); \
328+
NRF_DT_PSEL_CHECK_EXACTLY_ONE(I2C(idx), \
329+
sda_pin, "sda-pin", \
330+
sda_gpios, "sda-gpios"); \
331+
NRF_DT_PSEL_CHECK_EXACTLY_ONE(I2C(idx), \
332+
scl_pin, "scl-pin", \
333+
scl_gpios, "scl-gpios"); \
334+
NRF_DT_CHECK_GPIO_CTLR_IS_SOC(I2C(idx), sda_gpios, "sda-gpios"); \
335+
NRF_DT_CHECK_GPIO_CTLR_IS_SOC(I2C(idx), scl_gpios, "scl-gpios"); \
327336
static int twim_##idx##_init(const struct device *dev) \
328337
{ \
329338
IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), \
@@ -344,8 +353,8 @@ static int twim_nrfx_pm_control(const struct device *dev,
344353
static const struct i2c_nrfx_twim_config twim_##idx##z_config = { \
345354
.twim = NRFX_TWIM_INSTANCE(idx), \
346355
.config = { \
347-
.scl = DT_PROP(I2C(idx), scl_pin), \
348-
.sda = DT_PROP(I2C(idx), sda_pin), \
356+
.scl = NRF_DT_PSEL(I2C(idx), scl_pin, scl_gpios, 0), \
357+
.sda = NRF_DT_PSEL(I2C(idx), sda_pin, sda_gpios, 0), \
349358
.frequency = I2C_FREQUENCY(idx), \
350359
} \
351360
}; \

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,36 @@ properties:
1313
interrupts:
1414
required: true
1515

16+
sda-gpios:
17+
type: phandle-array
18+
required: false
19+
description: |
20+
The SDA pin to use. The value is "<&gpioX Y flags>".
21+
Pin PX.Y will be used for SDA. The "flags" portion currently
22+
has no effect, but must be set to zero.
23+
24+
For example, to use P0.16 for SDA, set:
25+
26+
sda-gpios = <&gpio0 16 0>;
27+
28+
To use P1.2 for SDA, set:
29+
30+
sda-gpios = <&gpio1 2 0>;
31+
32+
scl-gpios:
33+
type: phandle-array
34+
required: false
35+
description: |
36+
The SCL pin to use. The value should be set in the same
37+
way as the sda-gpios property.
38+
1639
sda-pin:
1740
type: int
18-
required: true
41+
required: false
42+
deprecated: true
1943
description: |
44+
Deprecated; use sda-gpios instead.
45+
2046
The SDA pin to use.
2147
2248
For pins P0.0 through P0.31, use the pin number. For example,
@@ -31,7 +57,10 @@ properties:
3157
3258
scl-pin:
3359
type: int
34-
required: true
60+
required: false
61+
deprecated: true
3562
description: |
63+
Deprecated; use scl-gpios instead.
64+
3665
The SCL pin to use. The pin numbering scheme is the same as
3766
the sda-pin property's.

0 commit comments

Comments
 (0)