[PATCH v12 06/12] spmi: Add helper to get a parent SPMI device
From: AngeloGioacchino Del Regno
Date: Tue Jul 07 2026 - 07:32:11 EST
Drivers that register a SPMI sub-device as a platform_driver are
not guaranteed to have a SPMI device as their parent: add a new
helper `spmi_get_parent_spmi_device()` that takes a struct device
and checks if there's a parent, and if that parent is actually a
SPMI device by checking if its device type matches with SPMI: if
so, returns a pointer to the relative struct spmi_device without
incrementing any refcount.
As a note, in the specific case of using this helper to retrieve
a SPMI subdevice's parent, the spmi_subdevice_alloc_and_add()
function does actually call device_add(), which will already take
care of raising the refcount of the associated parent device.
Signed-off-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@xxxxxxxxxxxxx>
---
drivers/spmi/spmi.c | 23 +++++++++++++++++++++++
include/linux/spmi.h | 1 +
2 files changed, 24 insertions(+)
diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
index e218c8140545..a0a6ea9b1d2d 100644
--- a/drivers/spmi/spmi.c
+++ b/drivers/spmi/spmi.c
@@ -433,6 +433,29 @@ struct spmi_device *spmi_find_device_by_of_node(struct device_node *np)
}
EXPORT_SYMBOL_GPL(spmi_find_device_by_of_node);
+/**
+ * spmi_get_parent_spmi_device() - get the parent SPMI device from current dev
+ * @dev: pointer to a subdevice on SPMI bus
+ *
+ * Checks if the passed device is a child of an SPMI device and returns a
+ * handle to the parent SPMI device without incrementing any refcount.
+ *
+ * Return: Handle to the parent SPMI device or NULL
+ */
+struct spmi_device *spmi_get_parent_spmi_device(struct device *dev)
+{
+ struct device *parent;
+
+ if (dev && dev->parent) {
+ parent = dev->parent;
+
+ if (parent->type == &spmi_dev_type)
+ return to_spmi_device(parent);
+ }
+ return NULL;
+}
+EXPORT_SYMBOL_NS_GPL(spmi_get_parent_spmi_device, "SPMI");
+
/**
* spmi_device_alloc() - Allocate a new SPMI device
* @ctrl: associated controller
diff --git a/include/linux/spmi.h b/include/linux/spmi.h
index a78a8924b2ac..4daebc980501 100644
--- a/include/linux/spmi.h
+++ b/include/linux/spmi.h
@@ -189,6 +189,7 @@ static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
struct device_node;
struct spmi_device *spmi_find_device_by_of_node(struct device_node *np);
+struct spmi_device *spmi_get_parent_spmi_device(struct device *dev);
int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
size_t len);
--
2.54.0