Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions include/devicetree/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,94 @@ extern "C" {
#define DT_INST_GPIO_FLAGS(inst, gpio_pha) \
DT_INST_GPIO_FLAGS_BY_IDX(inst, gpio_pha, 0)

/**
* @brief Create a struct initializer for a "gpio" phandle-array element
*
* The struct initializer will be of the form:
*
* { label, pin, flags },
*
* Example devicetree fragment:
*
* gpio1: gpio@... {
* label = "GPIO_1";
* };
*
* gpio2: gpio@... {
* label = "GPIO_2";
* };
*
* n: node {
* gpios = <&gpio1 10 20>, <&gpio2 30 40>;
* };
*
* Example usage:
*
* DT_GPIO_ELEM(DT_NODELABEL(n), gpios, 1)
*
* Will produce the following struct initializer:
*
* { "GPIO_2", 30, 40 },
*
* The idx element is placed first so that the macro will be usable by
* UTIL_LISTIFY
*
* @param idx into the gpio_pha phandle-array
* @param node_id node identifier
* @param gpio_pha lowercase-and-underscores GPIO property with
* type "phandle-array"
* @return a struct initializer for the give index "idx" into the
* "phandle-array" gpio_pha
*/
#define DT_GPIO_ELEM(idx, node_id, gpio_pha) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about the name DT_GPIO_LPF_AT_IDX?

The other phandle array macros also take arguments in (node_id, gpio_pha, idx) order.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DT_GPIO_CELLS_AT_IDX would be more consistent with the underlying devicetree terminology and would be highlight the similarity with other phandle-valued aggregates. I really find _LPF_ to be obscure. (Pins, Flags, what is L?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think L is label

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, not obvious especially if label goes away. I'd rather find a name that covers all phandle+specifiers so we can use it consistently for DT_PWM_, DT_IOCHANNEL_, .... CELLS ignores the phandle, so maybe DT_GPIO_PHCELLS_? Ideally when the phandle reference changes from a label to an identifier we wouldn't have to change the name, just the definition of the macro and of the structure into which it should be stored.

But really this PR is jumping to a partial solution for #23691 without addressing the other part: eliminating the custom struct sx1276_dio. We do not need a global macro creating precedent and adding new names to convert lora/sx1276.c; that file can use a local define as I did in #23245 (review)

{ \
DT_GPIO_LABEL_BY_IDX(node_id, gpio_pha, idx), \
DT_GPIO_PIN_BY_IDX(node_id, gpio_pha, idx), \
DT_GPIO_FLAGS_BY_IDX(node_id, gpio_pha, idx), \
},

/**
* @brief Create an array initializer for a "gpio" phandle-array
*
* The array initializer will be of the form:
*
* {
* { label[0], pin[0], flags[0] }
* ...
* { label[n-1], pin[n-1], flags[n-1] },
* }
*
* Example devicetree fragment:
*
* gpio1: gpio@... {
* label = "GPIO_1";
* };
*
* gpio2: gpio@... {
* label = "GPIO_2";
* };
*
* n: node {
* gpios = <&gpio1 10 20>, <&gpio2 30 40>;
* };
*
* Example usage:
*
* DT_GPIO_LISTIFY(DT_NODELABEL(n), gpios)
*
* Will produce:
*
* { { "GPIO_1", 10, 20 }, { "GPIO_2", 30, 40 }, }
*
* @param node_id node identifier
* @param gpio_pha lowercase-and-underscores GPIO property with
* type "phandle-array"
* @return an array initializer for the given "phandle-array" gpio_pha
*/
#define DT_GPIO_LISTIFY(node_id, gpio_pha) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to let the user pass the macro here as an argument instead of assuming DT_GPIO_ELEM? Keeps the API generic in cases where it's not a label/pin/flags always.

{ UTIL_LISTIFY(DT_PROP_LEN(node_id, gpio_pha), DT_GPIO_ELEM, \
node_id, gpio_pha) }

/**
* @}
*/
Expand Down
11 changes: 0 additions & 11 deletions tests/lib/devicetree/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,6 @@ struct gpios_struct {
label) \
}

/* Helper macro that UTIL_LISTIFY can use and produces an element with comma */
#define DT_GPIO_ELEM(idx, node_id, prop) \
{ \
DT_PROP(DT_PHANDLE_BY_IDX(node_id, prop, idx), label), \
DT_PHA_BY_IDX(node_id, prop, idx, pin),\
DT_PHA_BY_IDX(node_id, prop, idx, flags),\
},
#define DT_GPIO_LISTIFY(node_id, prop) \
{ UTIL_LISTIFY(DT_PROP_LEN(node_id, prop), DT_GPIO_ELEM, \
node_id, prop) }

#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_phandle_holder
static void test_phandles(void)
Expand Down