From patchwork Thu Jul 22 09:56:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 483875 Delivered-To: patch@linaro.org Received: by 2002:a02:c94a:0:0:0:0:0 with SMTP id u10csp203167jao; Thu, 22 Jul 2021 02:57:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4XIgxy8p08L1A5OwhINZ4twpe+K1mQSAFVXqqz8MkSnihOVUiqkr1gtdh+ZRmpdQUDlzc X-Received: by 2002:a05:6602:584:: with SMTP id v4mr22396925iox.181.1626947853035; Thu, 22 Jul 2021 02:57:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626947853; cv=none; d=google.com; s=arc-20160816; b=dcmPYBbE0PRsF/0aZibAFaWYojizIkDMV1bLSs76On8vD7xNBlPrXnHEk0JbYTyDHr XprkI+PfPhk1G1hpY29lThssh02XQ8jhaAZArGc8yAmnGWla40PfzwQRmHoV9SMLf3uS +UWlECV24L8+NHYWgyuD5BzCTNsqiya6iBtIeNKqGXLfMXphI1yEffnBxwvZOhODiTCL 9lE8pNVaj8b/wfRuE7ZRJXgDXG57yxhkcTWQxVqyrTUoOCZUlBuTEQ57DZ+rR46njKu9 lPBVsRgVRKv3XsI1AUCRf6PghPLkhcvMAs+2q4miNBWJCnp5PcrtFHRcz2d8A3rVI3Xa eBXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zqCgE34yKyEIxvuJmPDWwVVxXXN+22OEi5ENUvJ8OeA=; b=UsKeGLoCKTqicgO8/uw7gHPZ4cd2qNMivqfnNEa38lG/RoKWy2dM6VkNAiPvSj61MZ WmL+CJZCSUmZWLsuFF62vxQHYpUrPwrdcr4WDm6ry53UnqdX/b8/ioGcy2AMX8ZHMIOO qp7NQVx+ryo/OBx0Pbx/vjbunVmQ1RVIv8z5yyaSZ+WVbB+yWjOKh5FP+LkqIXoeEIA/ ZQryiBb7PWTgpIPLvo6lmXFAXsx/ttyI5kVc4F1fgmGoaAkHskezYhe5bxzt+TXUJMfd Ni/q4c2+E3AlLDV6dsFCnUYIZ+fmSi0afZNEaOUVSclQHm5VV/gfXSMyHB29SgKNGSo1 vvAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oNd9Myfv; spf=pass (google.com: domain of devicetree-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z25si18113964iod.45.2021.07.22.02.57.32; Thu, 22 Jul 2021 02:57:33 -0700 (PDT) Received-SPF: pass (google.com: domain of devicetree-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oNd9Myfv; spf=pass (google.com: domain of devicetree-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231567AbhGVJQq (ORCPT + 7 others); Thu, 22 Jul 2021 05:16:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231580AbhGVJQo (ORCPT ); Thu, 22 Jul 2021 05:16:44 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2E2FC0617A2 for ; Thu, 22 Jul 2021 02:57:03 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id n11so3860560plc.2 for ; Thu, 22 Jul 2021 02:57:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zqCgE34yKyEIxvuJmPDWwVVxXXN+22OEi5ENUvJ8OeA=; b=oNd9MyfvR0nYVTjrW1t93WrZmnXFXN7AAJDIlU2Ob6ZsJ6AU3uISvEzFldVwS/MN9y T+lAXUsG6wseU6YsEN069LVRTMg2F+luTvrzDcO6ABMJee/zl4OVlVUjIxOw3AKPXCVK 7aV8DcMDgsVAgTuP+HGVtzfa1qTbErhmDrgwIiELLIroBKiMtJlnjJYkcQBlQ3wbCB9x 9SCcjQZhUu+w+qSF1F5GNFSwX+ny4kwes2xQbcFfKHhotxKM/Nq9JMYodrgOaFYlb8Ik jERO1a1rAdxXN9yAyjOamz+lXVVqJkBTLTcEyh26w+pWL1N1SH/IbLS3ggA6HRd6N7WI fTHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zqCgE34yKyEIxvuJmPDWwVVxXXN+22OEi5ENUvJ8OeA=; b=Em2RUu57DIj63pNZhY+6eRQAyna64aqv4Wv0JZR93JUsIKwnX/zKZMfSrqe0eKiCKo WV48TE7bXbpf1faapnd5aeQ3RvOCCHefGWLBG42BCrSTe+0/b6BbeWpNlKA8dmBehks3 PPu4kp+ScK/PhA034o0VhVEVMoetFmrpti6sFP+tiH9+pxe8OZcYoQbyMnH0fJuqC6Zz FxkwImTz8TNvx/ZWd0ai3wISQSYfXy7kxANgCk7slbFyqHLnohqLL2Z/6dxjxUJB+shk HY1XZ2h2Gx3HVMFFmSy004DCNJ5KvcoVj+od63kGYr97dQt25JSDvw7C56QiIIm2JZdJ a+Zw== X-Gm-Message-State: AOAM532qbRAAYSmvq2AAt+eGR0TBS1IZ4ozU8x9zL/92l04pmlhA3But aXhcDl03f47x0Dx5jER8SE1/mQ== X-Received: by 2002:a17:90a:658c:: with SMTP id k12mr8021272pjj.167.1626947823491; Thu, 22 Jul 2021 02:57:03 -0700 (PDT) Received: from localhost ([106.201.108.2]) by smtp.gmail.com with ESMTPSA id ay2sm2400955pjb.14.2021.07.22.02.57.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jul 2021 02:57:03 -0700 (PDT) From: Viresh Kumar To: Jason Wang , "Michael S. Tsirkin" , Rob Herring , Arnd Bergmann , Jean-Philippe Brucker Cc: Viresh Kumar , Vincent Guittot , Bill Mills , =?utf-8?q?Alex_Benn=C3=A9e?= , "Enrico Weigelt, metux IT consult" , Jie Deng , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH V2 5/5] virtio: Bind virtio device to device-tree node Date: Thu, 22 Jul 2021 15:26:43 +0530 Message-Id: <026ad5f274d64d46590623f9f3a04b8abfbe62d7.1626947324.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Bind the virtio devices with their of_node. This will help users of the virtio devices to mention their dependencies on the device in the DT 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 --- drivers/virtio/virtio.c | 67 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) -- 2.31.1.272.g89b43f80a514 diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 4b15c00c0a0a..87bee5b966c3 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -4,6 +4,7 @@ #include #include #include +#include #include /* Unique numbering for virtio devices. */ @@ -292,6 +293,9 @@ static int virtio_dev_remove(struct device *_d) /* Acknowledge the device's existence again. */ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); + + of_node_put(dev->dev.of_node); + return 0; } @@ -319,6 +323,53 @@ void unregister_virtio_driver(struct virtio_driver *driver) } EXPORT_SYMBOL_GPL(unregister_virtio_driver); +/* Virtio device compatibles and IDs */ +static const struct of_device_id of_virtio_devices[] = { + { .compatible = "virtio,22", .data = (void *)VIRTIO_ID_I2C_ADAPTER }, + { .compatible = "virtio,29", .data = (void *)VIRTIO_ID_GPIO }, + { } +}; + +static int virtio_device_of_init(struct virtio_device *dev) +{ + struct device_node *np, *pnode = dev->dev.parent->of_node; + const struct of_device_id *match; + int ret, count; + + 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; + + match = of_match_node(of_virtio_devices, np); + if (!match) { + ret = -ENODEV; + goto out; + } + + if ((unsigned long)match->data != dev->id.device) { + ret = -EINVAL; + goto out; + } + + dev->dev.of_node = np; + return 0; + +out: + of_node_put(np); + return ret; +} + /** * register_virtio_device - register virtio device * @dev : virtio device to be registered @@ -343,6 +394,10 @@ int register_virtio_device(struct virtio_device *dev) dev->index = err; dev_set_name(&dev->dev, "virtio%u", dev->index); + err = virtio_device_of_init(dev); + if (err) + goto out_ida_remove; + spin_lock_init(&dev->config_lock); dev->config_enabled = false; dev->config_change_pending = false; @@ -362,10 +417,16 @@ int register_virtio_device(struct virtio_device *dev) */ err = device_add(&dev->dev); if (err) - ida_simple_remove(&virtio_index_ida, dev->index); + goto out_of_node_put; + + return 0; + +out_of_node_put: + of_node_put(dev->dev.of_node); +out_ida_remove: + ida_simple_remove(&virtio_index_ida, dev->index); out: - if (err) - virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); + virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); return err; } EXPORT_SYMBOL_GPL(register_virtio_device);