Add a devres-managed version of spi_new_ancillary_device() that
automatically unregisters the ancillary SPI device when the parent
device is removed.
This follows the same devm_add_action_or_reset() pattern used by the
other managed SPI functions (devm_spi_optimize_message,
devm_spi_register_controller, etc.) and eliminates the need for drivers
to open-code their own devm cleanup callbacks for ancillary devices.
Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
---
Changes in v3:
- New patch
drivers/spi/spi.c | 40 ++++++++++++++++++++++++++++++++++++++++
include/linux/spi/spi.h | 1 +
2 files changed, 41 insertions(+)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 693bdcc5a12a..1b48ec67b8e0 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -2612,6 +2612,46 @@ struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
}
EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
+static void devm_spi_unregister_device(void *spi)
+{
+ spi_unregister_device(spi);
+}
+
+/**
+ * devm_spi_new_ancillary_device() - Register managed ancillary SPI device
+ * @spi: Pointer to the main SPI device registering the ancillary device
+ * @chip_select: Chip Select of the ancillary device
+ *
+ * Register an ancillary SPI device; for example some chips have a chip-select
+ * for normal device usage and another one for setup/firmware upload.
+ *
+ * This is the managed version of spi_new_ancillary_device(). The ancillary
+ * device will be unregistered automatically when the parent SPI device is
+ * unregistered.
+ *
+ * This may only be called from main SPI device's probe routine.
+ *
+ * Return: Pointer to new ancillary device on success; ERR_PTR on failure
+ */
+struct spi_device *devm_spi_new_ancillary_device(struct spi_device *spi,
+ u8 chip_select)
+{
+ struct spi_device *ancillary;
+ int ret;
+
+ ancillary = spi_new_ancillary_device(spi, chip_select);
+ if (IS_ERR(ancillary))
+ return ancillary;
+
+ ret = devm_add_action_or_reset(&spi->dev, devm_spi_unregister_device,
+ ancillary);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return ancillary;
+}
+EXPORT_SYMBOL_GPL(devm_spi_new_ancillary_device);
+
#ifdef CONFIG_ACPI
struct acpi_spi_lookup {
struct spi_controller *ctlr;
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index cb2c2df31089..9aef1987b12f 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -374,6 +374,7 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
}
extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 chip_select);
+extern struct spi_device *devm_spi_new_ancillary_device(struct spi_device *spi, u8 chip_select);
/* Use a define to avoid include chaining to get THIS_MODULE */
#define spi_register_driver(driver) \
--
2.43.0
On Fri, 2026-02-13 at 16:47 +0200, Antoniu Miclaus wrote:
> Add a devres-managed version of spi_new_ancillary_device() that
> automatically unregisters the ancillary SPI device when the parent
> device is removed.
>
> This follows the same devm_add_action_or_reset() pattern used by the
> other managed SPI functions (devm_spi_optimize_message,
> devm_spi_register_controller, etc.) and eliminates the need for drivers
> to open-code their own devm cleanup callbacks for ancillary devices.
>
> Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
> ---
Acked-by: Nuno Sá <nuno.sa@analog.com>
> Changes in v3:
> - New patch
>
> drivers/spi/spi.c | 40 ++++++++++++++++++++++++++++++++++++++++
> include/linux/spi/spi.h | 1 +
> 2 files changed, 41 insertions(+)
>
> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
> index 693bdcc5a12a..1b48ec67b8e0 100644
> --- a/drivers/spi/spi.c
> +++ b/drivers/spi/spi.c
> @@ -2612,6 +2612,46 @@ struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
> }
> EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
>
> +static void devm_spi_unregister_device(void *spi)
> +{
> + spi_unregister_device(spi);
> +}
> +
> +/**
> + * devm_spi_new_ancillary_device() - Register managed ancillary SPI device
> + * @spi: Pointer to the main SPI device registering the ancillary device
> + * @chip_select: Chip Select of the ancillary device
> + *
> + * Register an ancillary SPI device; for example some chips have a chip-select
> + * for normal device usage and another one for setup/firmware upload.
> + *
> + * This is the managed version of spi_new_ancillary_device(). The ancillary
> + * device will be unregistered automatically when the parent SPI device is
> + * unregistered.
> + *
> + * This may only be called from main SPI device's probe routine.
> + *
> + * Return: Pointer to new ancillary device on success; ERR_PTR on failure
> + */
> +struct spi_device *devm_spi_new_ancillary_device(struct spi_device *spi,
> + u8 chip_select)
> +{
> + struct spi_device *ancillary;
> + int ret;
> +
> + ancillary = spi_new_ancillary_device(spi, chip_select);
> + if (IS_ERR(ancillary))
> + return ancillary;
> +
> + ret = devm_add_action_or_reset(&spi->dev, devm_spi_unregister_device,
> + ancillary);
> + if (ret)
> + return ERR_PTR(ret);
> +
> + return ancillary;
> +}
> +EXPORT_SYMBOL_GPL(devm_spi_new_ancillary_device);
> +
> #ifdef CONFIG_ACPI
> struct acpi_spi_lookup {
> struct spi_controller *ctlr;
> diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
> index cb2c2df31089..9aef1987b12f 100644
> --- a/include/linux/spi/spi.h
> +++ b/include/linux/spi/spi.h
> @@ -374,6 +374,7 @@ static inline void spi_unregister_driver(struct spi_driver *sdrv)
> }
>
> extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 chip_select);
> +extern struct spi_device *devm_spi_new_ancillary_device(struct spi_device *spi, u8 chip_select);
>
> /* Use a define to avoid include chaining to get THIS_MODULE */
> #define spi_register_driver(driver) \
© 2016 - 2026 Red Hat, Inc.