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: 41424 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id BE4A325E18 for ; Mon, 24 Nov 2014 22:34:51 +0000 (UTC) Received: by mail-lb0-f200.google.com with SMTP id f15sf4862407lbj.3 for ; Mon, 24 Nov 2014 14:34:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=h6giDBhVvj6+M3TfINTf7MB64L6612sdZquO5dQlvcA=; b=lhRg6hfVDKgyA2FKdSWJbHVb/tlFKlEWkJl1I1fnVOulCbKL888hQvHRDWc/tK6S70 Ln3/7gk4Nwc9qCxnx+YpfYEA8OgWRSxoF58nzWpTlylwr8TGgAQwoqZ/N25naBdQd+Bb lCEU9FXrpEa0o6BaRFIgv2fQ7BNHUD0mAglzUTevcEzrvhDYxPF0dGV0Z3TSGLdF3JtG KdvcNmNpFQUPwRbGl8Rbrj2QS3A+R0eixINuGWoaS8v5B1BLwe8PxrKaPXX9D7dxFyH1 Ll3+NqZgCfKJrZHm0D6G/nE+kG+hAz89J0RBV//tNQ+Q09BX14fXNPjP7gdbVADUgOYZ rC6Q== X-Gm-Message-State: ALoCoQmRS2dHQ1PHylZEMvtRJnBhZsp4Pp9RS/FwYF3KDB6YJr/irYSYOejvHEcTHsKwpaDzkB4L X-Received: by 10.152.8.67 with SMTP id p3mr6749784laa.4.1416868490730; Mon, 24 Nov 2014 14:34:50 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.5.39 with SMTP id p7ls499873lap.5.gmail; Mon, 24 Nov 2014 14:34:50 -0800 (PST) X-Received: by 10.152.5.6 with SMTP id o6mr23140768lao.8.1416868490592; Mon, 24 Nov 2014 14:34:50 -0800 (PST) Received: from mail-la0-f54.google.com (mail-la0-f54.google.com. [209.85.215.54]) by mx.google.com with ESMTPS id wm2si16561487lbb.49.2014.11.24.14.34.50 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 24 Nov 2014 14:34:50 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.54 as permitted sender) client-ip=209.85.215.54; Received: by mail-la0-f54.google.com with SMTP id gf13so8575826lab.13 for ; Mon, 24 Nov 2014 14:34:50 -0800 (PST) X-Received: by 10.112.52.37 with SMTP id q5mr22573451lbo.32.1416868490167; Mon, 24 Nov 2014 14:34:50 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.184.201 with SMTP id ew9csp347746lbc; Mon, 24 Nov 2014 14:34:49 -0800 (PST) X-Received: by 10.66.66.135 with SMTP id f7mr36601134pat.81.1416868488029; Mon, 24 Nov 2014 14:34:48 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y8si23843756pdl.5.2014.11.24.14.34.47 for ; Mon, 24 Nov 2014 14:34:48 -0800 (PST) Received-SPF: none (google.com: linux-spi-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751002AbaKXWeq (ORCPT + 2 others); 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-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: list List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: grant.likely@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.54 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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: