Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: swnode: Add ability to specify native chip selects for SPI

SPI devices can specify a cs-gpios property to enumerate their
chip selects. Under device tree, a zero entry in this property can
be used to specify that a particular chip select is using the SPI
controllers native chip select, for example:

cs-gpios = <&gpio1 0 0>, <0>;

Here, the second chip select is native. However, when using swnodes
there is currently no way to specify a native chip select. The
proposal here is to register a swnode_gpio_undefined software node,
that can be specified to allow the indication of a native chip
select. For example:

static const struct software_node_ref_args device_cs_refs[] = {
{
.node = &device_gpiochip_swnode,
.nargs = 2,
.args = { 0, GPIO_ACTIVE_LOW },
},
{
.node = &swnode_gpio_undefined,
.nargs = 0,
},
};

Register the swnode as the gpiolib is initialised and check in
swnode_get_gpio_device() if the returned node matches
swnode_gpio_undefined and return -ENOENT, which matches the
behaviour of the device tree system when it encounters a 0 phandle.

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
Link: https://lore.kernel.org/r/20240416100904.3738093-2-ckeepax@opensource.cirrus.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Charles Keepax and committed by
Mark Brown
9d50f95b fec50db7

+57
+9
drivers/gpio/Kconfig
··· 103 103 select REGMAP 104 104 tristate 105 105 106 + config GPIO_SWNODE_UNDEFINED 107 + bool 108 + help 109 + This adds a special place holder for software nodes to contain an 110 + undefined GPIO reference, this is primarily used by SPI to allow a 111 + list of GPIO chip selects to mark a certain chip select as being 112 + controlled the SPI device's internal chip select mechanism and not 113 + a GPIO. 114 + 106 115 # put drivers in the right section, in alphabetical order 107 116 108 117 # This symbol is selected by both I2C and SPI expanders
+44
drivers/gpio/gpiolib-swnode.c
··· 4 4 * 5 5 * Copyright 2022 Google LLC 6 6 */ 7 + 8 + #define pr_fmt(fmt) "gpiolib: swnode: " fmt 9 + 7 10 #include <linux/err.h> 8 11 #include <linux/errno.h> 12 + #include <linux/export.h> 13 + #include <linux/init.h> 9 14 #include <linux/kernel.h> 10 15 #include <linux/printk.h> 11 16 #include <linux/property.h> ··· 21 16 22 17 #include "gpiolib.h" 23 18 #include "gpiolib-swnode.h" 19 + 20 + #define GPIOLIB_SWNODE_UNDEFINED_NAME "swnode-gpio-undefined" 24 21 25 22 static void swnode_format_propname(const char *con_id, char *propname, 26 23 size_t max_size) ··· 46 39 gdev_node = to_software_node(fwnode); 47 40 if (!gdev_node || !gdev_node->name) 48 41 return ERR_PTR(-EINVAL); 42 + 43 + /* 44 + * Check for a special node that identifies undefined GPIOs, this is 45 + * primarily used as a key for internal chip selects in SPI bindings. 46 + */ 47 + if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) && 48 + !strcmp(gdev_node->name, GPIOLIB_SWNODE_UNDEFINED_NAME)) 49 + return ERR_PTR(-ENOENT); 49 50 50 51 gdev = gpio_device_find_by_label(gdev_node->name); 51 52 return gdev ?: ERR_PTR(-EPROBE_DEFER); ··· 136 121 137 122 return count ?: -ENOENT; 138 123 } 124 + 125 + #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) 126 + /* 127 + * A special node that identifies undefined GPIOs, this is primarily used as 128 + * a key for internal chip selects in SPI bindings. 129 + */ 130 + const struct software_node swnode_gpio_undefined = { 131 + .name = GPIOLIB_SWNODE_UNDEFINED_NAME, 132 + }; 133 + EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, GPIO_SWNODE); 134 + 135 + static int __init swnode_gpio_init(void) 136 + { 137 + int ret; 138 + 139 + ret = software_node_register(&swnode_gpio_undefined); 140 + if (ret < 0) 141 + pr_err("failed to register swnode: %d\n", ret); 142 + 143 + return ret; 144 + } 145 + subsys_initcall(swnode_gpio_init); 146 + 147 + static void __exit swnode_gpio_cleanup(void) 148 + { 149 + software_node_unregister(&swnode_gpio_undefined); 150 + } 151 + __exitcall(swnode_gpio_cleanup); 152 + #endif
+4
include/linux/gpio/property.h
··· 5 5 #include <dt-bindings/gpio/gpio.h> /* for GPIO_* flags */ 6 6 #include <linux/property.h> 7 7 8 + struct software_node; 9 + 8 10 #define PROPERTY_ENTRY_GPIO(_name_, _chip_node_, _idx_, _flags_) \ 9 11 PROPERTY_ENTRY_REF(_name_, _chip_node_, _idx_, _flags_) 12 + 13 + extern const struct software_node swnode_gpio_undefined; 10 14 11 15 #endif /* __LINUX_GPIO_PROPERTY_H */