From patchwork Mon Nov 24 22:33:42 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Grant Likely X-Patchwork-Id: 5370351 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5197C9F39D for ; Mon, 24 Nov 2014 22:34:49 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7299520158 for ; Mon, 24 Nov 2014 22:34:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 75C9B2015A for ; Mon, 24 Nov 2014 22:34:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751002AbaKXWeq (ORCPT ); Mon, 24 Nov 2014 17:34:46 -0500 Received: from mail-wi0-f177.google.com ([209.85.212.177]:40193 "EHLO mail-wi0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751423AbaKXWeQ (ORCPT ); Mon, 24 Nov 2014 17:34:16 -0500 Received: by mail-wi0-f177.google.com with SMTP id l15so7480787wiw.4 for ; Mon, 24 Nov 2014 14:34:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=vkUZ0+XfsxU8hiSI9g8v2cVpHN9j7vks4ST+iWM30mk=; b=OGqSGyCw3Iwn53CFjocqrNnV5AB58gyGHJcrVi/b8x/A0NTO2KBhfJNDhrhB7YY4EY 2yK1AhvnCb5EVH8KH7OwuirbnmyrgQ/PetepO2RTU1J+aE0C1/YQ7EU/3jgxdwT88eS2 GnZ1Qq5ubwTGHWQRg7Rg4eomvElF4Lc52KwfKoJ+1ZYn2mPL/ag33YYLjLByoo7DO1h0 9hJzJDdSFcHmWzWwET/LY78pbLqCKOyMq3Yo5lay0cwIPqRw5Mq3t6LMaAd2cNYXdvsj vQOyzMAjekO3VTzYFvsBzqdodBJ9CL+4bKs/esiJbNAcaBKamfunpVBDjTBkuju2bk2V Ftng== X-Gm-Message-State: ALoCoQkIuml2DfF3hd2HCeOcbopFNOZ+YhNWaTdib20EARWLsNoTO2gPLssP/s1Z9x32KJdYt9lK X-Received: by 10.194.78.3 with SMTP id x3mr37206952wjw.127.1416868455092; Mon, 24 Nov 2014 14:34:15 -0800 (PST) Received: from trevor.secretlab.ca (host86-166-84-117.range86-166.btcentralplus.com. [86.166.84.117]) by mx.google.com with ESMTPSA id wa10sm22898766wjc.8.2014.11.24.14.34.11 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Nov 2014 14:34:13 -0800 (PST) Received: by trevor.secretlab.ca (Postfix, from userid 1000) id 10D3BC44218; Mon, 24 Nov 2014 22:33:46 +0000 (GMT) From: Grant Likely To: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Pantelis Antoniou Cc: Rob Herring , Mark Brown , Wolfram Sang , Grant Likely , Subject: [PATCH v2 14/14] spi/of: Add OF notifier handler Date: Mon, 24 Nov 2014 22:33:42 +0000 Message-Id: <1416868422-22103-15-git-send-email-grant.likely@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1416868422-22103-1-git-send-email-grant.likely@linaro.org> References: <1416868422-22103-1-git-send-email-grant.likely@linaro.org> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Pantelis Antoniou Add OF notifier handler needed for creating/destroying spi devices according to dynamic runtime changes in the DT live tree. This code is enabled when CONFIG_OF_DYNAMIC is selected. Signed-off-by: Pantelis Antoniou Signed-off-by: Grant Likely Cc: Mark Brown Cc: --- drivers/spi/spi.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index a3557c57d4f7..bf1cab0fad66 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2317,6 +2317,86 @@ EXPORT_SYMBOL_GPL(spi_write_then_read); /*-------------------------------------------------------------------------*/ +#if IS_ENABLED(CONFIG_OF_DYNAMIC) +static int __spi_of_device_match(struct device *dev, void *data) +{ + return dev->of_node == data; +} + +/* must call put_device() when done with returned spi_device device */ +static struct spi_device *of_find_spi_device_by_node(struct device_node *node) +{ + struct device *dev = bus_find_device(&spi_bus_type, NULL, node, + __spi_of_device_match); + return dev ? to_spi_device(dev) : NULL; +} + +static int __spi_of_master_match(struct device *dev, const void *data) +{ + return dev->of_node == data; +} + +/* the spi masters are not using spi_bus, so we find it with another way */ +static struct spi_master *of_find_spi_master_by_node(struct device_node *node) +{ + struct device *dev; + + dev = class_find_device(&spi_master_class, NULL, node, + __spi_of_master_match); + if (!dev) + return NULL; + + /* reference got in class_find_device */ + return container_of(dev, struct spi_master, dev); +} + +static int of_spi_notify(struct notifier_block *nb, unsigned long action, + void *arg) +{ + struct of_reconfig_data *rd = arg; + struct spi_master *master; + struct spi_device *spi; + + switch (of_reconfig_get_state_change(action, arg)) { + case OF_RECONFIG_CHANGE_ADD: + master = of_find_spi_master_by_node(rd->dn->parent); + if (master == NULL) + return NOTIFY_OK; /* not for us */ + + spi = of_register_spi_device(master, rd->dn); + put_device(&master->dev); + + if (IS_ERR(spi)) { + pr_err("%s: failed to create for '%s'\n", + __func__, rd->dn->full_name); + return notifier_from_errno(PTR_ERR(spi)); + } + break; + + case OF_RECONFIG_CHANGE_REMOVE: + /* find our device by node */ + spi = of_find_spi_device_by_node(rd->dn); + if (spi == NULL) + return NOTIFY_OK; /* no? not meant for us */ + + /* unregister takes one ref away */ + spi_unregister_device(spi); + + /* and put the reference of the find */ + put_device(&spi->dev); + break; + } + + return NOTIFY_OK; +} + +static struct notifier_block spi_of_notifier = { + .notifier_call = of_spi_notify, +}; +#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ +extern struct notifier_block spi_of_notifier; +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ + static int __init spi_init(void) { int status; @@ -2334,6 +2414,10 @@ static int __init spi_init(void) status = class_register(&spi_master_class); if (status < 0) goto err2; + + if (IS_ENABLED(CONFIG_OF)) + WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); + return 0; err2: