spmi: Add helper to get a parent SPMI device

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@collabora.com>
Reviewed-by: Abel Vesa <abel.vesa@oss.qualcomm.com>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
This commit is contained in:
AngeloGioacchino Del Regno 2026-07-21 11:36:20 +02:00 committed by Stephen Boyd
parent 6e19e5d699
commit 939e80cef9
No known key found for this signature in database
GPG Key ID: AD028897C6E49525
2 changed files with 24 additions and 0 deletions

View File

@ -431,6 +431,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

View File

@ -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);