From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41641C6377D for ; Thu, 22 Jul 2021 09:57:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2A36360D07 for ; Thu, 22 Jul 2021 09:57:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231529AbhGVJRC (ORCPT ); Thu, 22 Jul 2021 05:17:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231579AbhGVJQo (ORCPT ); Thu, 22 Jul 2021 05:16:44 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E09DBC0617A1 for ; Thu, 22 Jul 2021 02:57:03 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id x13-20020a17090a46cdb0290175cf22899cso4738493pjg.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=S1dDesGUI9a6hgRiZ6PNxEPe8+Ba5ANxR3rII/dd6Ds0nNn8yhg8wJaBsxGmJBBIPQ wlCXag83H/9e7QZqC/6f0sD+OYH+oSwXpUwEcJBsFBRuKrKrU7j9UkAgt3NK0kyZJGOt 8iy4L/4JT5Vd04DPsaifkaj5fqrJ4LJIlr40VFnSanz1D1PNysy0xMdhtk7uQyvzZJL5 F/zd+YU7Z263hHLjguWd96N6NWIvwwx6xbQHVoiFgv1HaL9+ZyZn+XDnQnBGRssLxUBf +/EMPNZgaLaEUlpir0a2s1MkHyLNJ1SdAqWg1GrPLESR2O7ViLDWLKvwJS0VN3m58Cse RkGA== X-Gm-Message-State: AOAM532RQMozu/kSTDq6XO+glecOh8T+ZGrlS689yURQN/2Xu8e5Cq9+ Zbh/CyCu0wDuonogLFjxT7Zxkg== X-Google-Smtp-Source: ABdhPJxt9prk84Rhz1qfl8PCP3CdBdMFHhzAR/AnhH9ZFn8x+XmZlhtH9HWAY7tkUHoyMCqWqiy6gw== 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=20Benn=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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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(-) 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); -- 2.31.1.272.g89b43f80a514