[PATCH 2/5] virtio_mmio: Bind virtio device to device-tree node

From: Viresh Kumar
Date: Tue Jul 13 2021 - 06:50:58 EST


Bind the virtio device with its device protocol's sub-node. This will
help users of the virtio device to mention their dependencies on the
device in the DT file itself. Like GPIO pin users can use the phandle of
the device node, or the node may contain more subnodes to add i2c or spi
eeproms and other users.

Signed-off-by: Viresh Kumar <viresh.kumar@xxxxxxxxxx>
---
drivers/virtio/virtio_mmio.c | 44 ++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)

diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c
index 56128b9c46eb..ae40546a66a3 100644
--- a/drivers/virtio/virtio_mmio.c
+++ b/drivers/virtio/virtio_mmio.c
@@ -551,11 +551,51 @@ static void virtio_mmio_release_dev(struct device *_d)
struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
struct platform_device *pdev = vm_dev->pdev;

+ of_node_put(vdev->dev.of_node);
devm_kfree(&pdev->dev, vm_dev);
}

/* Platform device */

+static int virtio_mmio_of_init(struct virtio_device *vdev)
+{
+ struct device_node *np, *pnode = vdev->dev.parent->of_node;
+ int ret, count;
+ u32 reg;
+
+ if (!pnode)
+ return 0;
+
+ count = of_get_available_child_count(pnode);
+ if (!count)
+ return 0;
+
+ /* There can be only 1 child node */
+ if (WARN_ON(count > 1))
+ return -EINVAL;
+
+ np = of_get_next_available_child(pnode, NULL);
+ if (WARN_ON(!np))
+ return -ENODEV;
+
+ ret = of_property_read_u32(np, "reg", &reg);
+ if (ret < 0)
+ goto out;
+
+ /* The reg field should match the device id */
+ if (WARN_ON(reg != vdev->id.device)) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ vdev->dev.of_node = np;
+ return 0;
+
+out:
+ of_node_put(np);
+ return ret;
+}
+
static int virtio_mmio_probe(struct platform_device *pdev)
{
struct virtio_mmio_device *vm_dev;
@@ -621,6 +661,10 @@ static int virtio_mmio_probe(struct platform_device *pdev)
if (rc)
dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");

+ rc = virtio_mmio_of_init(&vm_dev->vdev);
+ if (rc)
+ return rc;
+
platform_set_drvdata(pdev, vm_dev);

rc = register_virtio_device(&vm_dev->vdev);
--
2.31.1.272.g89b43f80a514