Hi, Currently the DT only provides support for following node types for virtio-mmio nodes: virtio_mmio at a000000 { dma-coherent; interrupts = <0x00 0x10 0x01>; reg = <0x00 0xa000000 0x00 0x200>; compatible = "virtio,mmio"; }; And each virtio-mmio corresponds to a virtio-device. But there is no way for other users in the DT to show their dependency on virtio devices. This patchset provides that support. The first patch update virtio,mmio bindings to allow for device subnodes to be present and the second patch updates the virtio-mmio driver to update the of_node. Other patches add bindings for i2c and gpio virtio devices (they have some dependencies, mentioned in the patches). Tested on x86 with qemu for arm64. -- Viresh Viresh Kumar (5): dt-bindings: virtio: mmio: Add support for device subnode virtio_mmio: Bind virtio device to device-tree node dt-bindings: i2c: Add bindings for i2c-virtio i2c: virtio: Update i2c-adapter's of_node dt-bindings: gpio: Add bindings for gpio-virtio .../devicetree/bindings/gpio/gpio-virtio.yaml | 67 +++++++++++++++++++ .../devicetree/bindings/i2c/i2c-virtio.yaml | 59 ++++++++++++++++ .../devicetree/bindings/virtio/mmio.yaml | 41 ++++++++++++ drivers/i2c/busses/i2c-virtio.c | 1 + drivers/virtio/virtio_mmio.c | 44 ++++++++++++ include/dt-bindings/virtio/virtio_ids.h | 1 + 6 files changed, 213 insertions(+) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-virtio.yaml create mode 100644 Documentation/devicetree/bindings/i2c/i2c-virtio.yaml create mode 120000 include/dt-bindings/virtio/virtio_ids.h -- 2.31.1.272.g89b43f80a514
Viresh Kumar
2021-Jul-13 10:50 UTC
[PATCH 1/5] dt-bindings: virtio: mmio: Add support for device subnode
Allow virtio,mmio nodes to contain device specific subnodes. Since each virtio,mmio node can represent a single virtio device, each virtio node is allowed to contain a maximum of one device specific subnode. The device subnode must have the "reg" property, and its value must match the virtio device ID used by the virtio mmio node. A phandle to this device subnode can then be used by the users of the virtio device. Also add a symbolic link to uapi/linux/virtio_ids.h in order to use the definitions here. Signed-off-by: Viresh Kumar <viresh.kumar at linaro.org> --- .../devicetree/bindings/virtio/mmio.yaml | 41 +++++++++++++++++++ include/dt-bindings/virtio/virtio_ids.h | 1 + 2 files changed, 42 insertions(+) create mode 120000 include/dt-bindings/virtio/virtio_ids.h diff --git a/Documentation/devicetree/bindings/virtio/mmio.yaml b/Documentation/devicetree/bindings/virtio/mmio.yaml index d46597028cf1..e5f9fe6ecb5e 100644 --- a/Documentation/devicetree/bindings/virtio/mmio.yaml +++ b/Documentation/devicetree/bindings/virtio/mmio.yaml @@ -31,6 +31,31 @@ title: virtio memory mapped devices description: Required for devices making accesses thru an IOMMU. maxItems: 1 + "#address-cells": + const: 1 + description: + The cell is the device ID if a device subnode is used. + + "#size-cells": + const: 0 + +patternProperties: + '^[a-z0-9]+-virtio@[0-9]+$': + type: object + description: | + Exactly one node describing the virtio device. The name of the node isn't + significant but its phandle can be used to by an user of the virtio + device. + + properties: + reg: + description: + Must contain the Virtio ID of the device. + $ref: /schemas/types.yaml#/definitions/uint32 + + required: + - reg + required: - compatible - reg @@ -57,4 +82,20 @@ additionalProperties: false #iommu-cells = <1>; }; + - | + #include <dt-bindings/virtio/virtio_ids.h> + + virtio at 3200 { + compatible = "virtio,mmio"; + reg = <0x3200 0x100>; + interrupts = <43>; + + #address-cells = <1>; + #size-cells = <0>; + + i2c-virtio at 0 { + reg = <VIRTIO_ID_I2C_ADAPTER>; + }; + }; + ... diff --git a/include/dt-bindings/virtio/virtio_ids.h b/include/dt-bindings/virtio/virtio_ids.h new file mode 120000 index 000000000000..6e59ba332216 --- /dev/null +++ b/include/dt-bindings/virtio/virtio_ids.h @@ -0,0 +1 @@ +../../uapi/linux/virtio_ids.h \ No newline at end of file -- 2.31.1.272.g89b43f80a514
Viresh Kumar
2021-Jul-13 10:50 UTC
[PATCH 2/5] virtio_mmio: Bind virtio device to device-tree node
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 at linaro.org> --- 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", ®); + 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
Viresh Kumar
2021-Jul-13 10:50 UTC
[PATCH 3/5] dt-bindings: i2c: Add bindings for i2c-virtio
i2c-virtio represents a virtio I2C device and this patch adds binding for the same. The i2c-virtio subnode can be part of a virtio,mmio node and is based on its binding. Cc: Wolfram Sang <wsa at kernel.org> Signed-off-by: Viresh Kumar <viresh.kumar at linaro.org> --- .../devicetree/bindings/i2c/i2c-virtio.yaml | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/i2c-virtio.yaml diff --git a/Documentation/devicetree/bindings/i2c/i2c-virtio.yaml b/Documentation/devicetree/bindings/i2c/i2c-virtio.yaml new file mode 100644 index 000000000000..6b6538f9a3d8 --- /dev/null +++ b/Documentation/devicetree/bindings/i2c/i2c-virtio.yaml @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/i2c/i2c-virtio.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Virtio memory mapped I2C Adapter + +maintainers: + - Viresh Kumar <viresh.kumar at linaro.org> + +description: + Virtio I2C device, see /schemas/virtio/mmio.yaml for more details. + +allOf: + - $ref: /schemas/i2c/i2c-controller.yaml# + +properties: + $nodename: + pattern: '^i2c-virtio@[0-9]+$' + + reg: + description: + The cell is the device ID of the I2C device (VIRTIO_ID_I2C_ADAPTER) as per + dt-bindings/virtio/virtio_ids.h. + const: 34 + $ref: /schemas/virtio/mmio.yaml#/properties/reg + +required: + - reg + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/virtio/virtio_ids.h> + + virtio at 3000 { + compatible = "virtio,mmio"; + reg = <0x3000 0x100>; + interrupts = <41>; + + #address-cells = <1>; + #size-cells = <0>; + + i2c-virtio at 0 { + reg = <VIRTIO_ID_I2C_ADAPTER>; + + #address-cells = <1>; + #size-cells = <0>; + + light-sensor at 1c { + compatible = "dynaimage,al3320a"; + reg = <0x20>; + }; + }; + }; + +... -- 2.31.1.272.g89b43f80a514
Set of_node of the adapter from the virtio device to enable automatic parsing the of the I2C devices present in DT. Cc: Wolfram Sang <wsa at kernel.org> Signed-off-by: Viresh Kumar <viresh.kumar at linaro.org> --- Depends on: https://lore.kernel.org/linux-i2c/984ebecaf697058eb73389ed14ead9dd6d38fb53.1625796246.git.jie.deng at intel.com/ --- drivers/i2c/busses/i2c-virtio.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/i2c/busses/i2c-virtio.c b/drivers/i2c/busses/i2c-virtio.c index 0139cdc33cae..cb8cfae2748f 100644 --- a/drivers/i2c/busses/i2c-virtio.c +++ b/drivers/i2c/busses/i2c-virtio.c @@ -223,6 +223,7 @@ static int virtio_i2c_probe(struct virtio_device *vdev) "i2c_virtio at virtio bus %d", vdev->index); vi->adap.algo = &virtio_algorithm; vi->adap.dev.parent = &vdev->dev; + vi->adap.dev.of_node = vdev->dev.of_node; i2c_set_adapdata(&vi->adap, vi); /* -- 2.31.1.272.g89b43f80a514
Viresh Kumar
2021-Jul-13 10:50 UTC
[PATCH 5/5] dt-bindings: gpio: Add bindings for gpio-virtio
gpio-virtio represents a virtio GPIO controller and this patch adds binding for the same. The gpio-virtio subnode can be part of a virtio,mmio node and is based on its binding. Signed-off-by: Viresh Kumar <viresh.kumar at linaro.org> --- Depends on: https://lore.kernel.org/lkml/7c716c2eb7ace5b5a560d8502af93101dbb53d24.1626170146.git.viresh.kumar at linaro.org/ --- .../devicetree/bindings/gpio/gpio-virtio.yaml | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-virtio.yaml diff --git a/Documentation/devicetree/bindings/gpio/gpio-virtio.yaml b/Documentation/devicetree/bindings/gpio/gpio-virtio.yaml new file mode 100644 index 000000000000..c813cdfd60fd --- /dev/null +++ b/Documentation/devicetree/bindings/gpio/gpio-virtio.yaml @@ -0,0 +1,67 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/gpio/gpio-virtio.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Virtio memory mapped GPIO controller + +maintainers: + - Viresh Kumar <viresh.kumar at linaro.org> + +description: + Virtio GPIO controller, see /schemas/virtio/mmio.yaml for more details. + +allOf: + - $ref: /schemas/gpio/gpio.yaml# + +properties: + $nodename: + pattern: '^gpio-virtio@[0-9]+$' + + reg: + description: + The cell is the device ID of the GPIO device (VIRTIO_ID_GPIO) as per + dt-bindings/virtio/virtio_ids.h. + const: 41 + $ref: /schemas/virtio/mmio.yaml#/properties/reg + + gpio-controller: true + + "#gpio-cells": + const: 2 + + interrupt-controller: true + + "#interrupt-cells": + const: 2 + +required: + - reg + - gpio-controller + - "#gpio-cells" + +additionalProperties: false + +examples: + - | + #include <dt-bindings/virtio/virtio_ids.h> + + virtio at 3000 { + compatible = "virtio,mmio"; + reg = <0x3000 0x100>; + interrupts = <41>; + + #address-cells = <1>; + #size-cells = <0>; + + gpio-virtio at 0 { + reg = <VIRTIO_ID_GPIO>; + gpio-controller; + #gpio-cells = <2>; + interrupt-controller; + #interrupt-cells = <2>; + }; + }; + +... -- 2.31.1.272.g89b43f80a514