diff mbox series

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

Message ID 26ba6941fa01eee88c99ecdd611d235c22bd6e3c.1626173013.git.viresh.kumar@linaro.org
State New
Headers show
Series virtio: Parse virtio-device nodes from DT | expand

Commit Message

Viresh Kumar July 13, 2021, 10:50 a.m. UTC
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@linaro.org>

---
 drivers/virtio/virtio_mmio.c | 44 ++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

-- 
2.31.1.272.g89b43f80a514

Comments

Arnd Bergmann July 13, 2021, 12:26 p.m. UTC | #1
On Tue, Jul 13, 2021 at 12:51 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>

> 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@linaro.org>

> ---

>  drivers/virtio/virtio_mmio.c | 44 ++++++++++++++++++++++++++++++++++++

>  1 file changed, 44 insertions(+)


Hi Viresh,

I don't see anything in this patch that is specific to virtio-mmio, as
opposed to
virtio-pci. It would be better to move this into the virtio core code so it can
be called independently of the transport that is used for virtio.

The PCI code has similar code that will set vdev->dev.parent->of_node
for a virtio-pci device, as long as that node is present.

        Arnd
Viresh Kumar July 14, 2021, 3:11 a.m. UTC | #2
On 13-07-21, 14:26, Arnd Bergmann wrote:
> On Tue, Jul 13, 2021 at 12:51 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:

> >

> > 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@linaro.org>

> > ---

> >  drivers/virtio/virtio_mmio.c | 44 ++++++++++++++++++++++++++++++++++++

> >  1 file changed, 44 insertions(+)

> 

> Hi Viresh,

> 

> I don't see anything in this patch that is specific to virtio-mmio, as

> opposed to

> virtio-pci. It would be better to move this into the virtio core code so it can

> be called independently of the transport that is used for virtio.

> 

> The PCI code has similar code that will set vdev->dev.parent->of_node

> for a virtio-pci device, as long as that node is present.


Sure.
-- 
viresh
diff mbox series

Patch

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