From patchwork Fri Sep 7 10:07:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10591999 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6BF4013AC for ; Fri, 7 Sep 2018 10:19:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 57B0D2A9B9 for ; Fri, 7 Sep 2018 10:19:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4AC742A9D7; Fri, 7 Sep 2018 10:19:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 92AA32A9B9 for ; Fri, 7 Sep 2018 10:19:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=uBjPgjuY25P2B/5nBkqV46JqyYswOTWkSnferJPCFr8=; b=pD22aik+EZGrC5Bz0x8rntTJgN 0zaizkV8Vh7Z0RidrnouKX5vzgRNel9hLqL991Q84H+dFSjd9m0C/TqfD2y8tHjwfzEoqWA3hr1X+ vKEa8lnsHkgo1Aj11saIpTK3TZQB7SK6ELHPwjjflebLqw0qO96OGb+jbM20g/TV5MvcAKVnbPYqL IQJKwgFHgP6Mdz4T7SRSdRraFQYIuTyZ03mPaCigGUM7PsoIFPWKwVpCMKPOdtrLIiReMXCKWcSXR zxxkp4T3HconnP+BAJeVyU1WcdGWkPY5GoaKX94rUp1RN2Tmn6SnVGyef7P6DOCWJLavCrHAnN8N3 HSPZnXuA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fyDqc-0005ry-EB; Fri, 07 Sep 2018 10:18:42 +0000 Received: from mail-wr1-f68.google.com ([209.85.221.68]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fyDjn-0000BO-J5 for linux-arm-kernel@lists.infradead.org; Fri, 07 Sep 2018 10:11:44 +0000 Received: by mail-wr1-f68.google.com with SMTP id z96-v6so14361525wrb.8 for ; Fri, 07 Sep 2018 03:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=O6kW/MWMreMdqngfmMkd7IkhX/posN3NlXBEDLsqaVk=; b=B2LeJ69LnpVEnygPtrvKbCTfC3GKdCl3LpnW/Opnqk9O6dGsko4UrPLNyOeLwJC/tB uy7s5P4gJnDKT+yYQ7lIa+wqYl2P48aLuDjmegkx2UNLXWpmuPolU3Fs7TeZh8hFBvyr z6bFP0uKAc7MubH3xzLjb+ZZea6jgPuQQiC+cWfd+yiWrUA00PPRZWalQi8yfv5cfxgU udparDXdn0LP7S+jrSSefg5gFrc3w0NWdIcMUHXza9/O+TGoMUpZr3bZDemXUiUt6fa5 d6164NMy9tATVxf8Ikoeg+AnbsLA/tSaQlVEN0QzxYq5Ydn19/NQK2JXUpJtPDFtediv bYTw== 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; bh=O6kW/MWMreMdqngfmMkd7IkhX/posN3NlXBEDLsqaVk=; b=b6WNKreZD2gu6TBeq+0EnyxlC6TRSLeTetXvWRiq1bxnMRkazTU4s1BkzSikl0OT/Z CE/W+Ld9XKrp36zjib73h+bfGzGKo/9JizxvwWK7P3FtSSb7xZTInF4mY5FVBb5P2cGD hzLGnx3KrvjOnVOoTbA94KMt7bY/1glDRbzZO0NwdsHlguSXndMsBwBUslDfrWp8ccmH x/c/DCUbKNXS7akj06Wtp/tjRgaGNB8/5eIpLbYEBxhW07fLFj0ok34YGVx0I9sIV0c2 sN1E6hq40ycEMNft13/df+aw0sYKWDHlq7Cb8Buzs3JCico9TQ0E060W5ovSm8tWFdYb wGbg== X-Gm-Message-State: APzg51AQaD0ooSf0uAcievrlgipLvBi3HJHH5vPUyyMmzWc8qBJ06Lgv ZMlzVtzMhHe6tOBVefLX2QOgmA== X-Google-Smtp-Source: ANB0VdabosKZ0ZjzOPCNJjpWuJUjKC2RXOe8YGBWVe8H5ORsjjLitO9xqRtxg2Ml0RuKu28UZcyKHg== X-Received: by 2002:adf:ae5a:: with SMTP id u26-v6mr5780676wrd.246.1536314915443; Fri, 07 Sep 2018 03:08:35 -0700 (PDT) Received: from brgl-bgdev.baylibre.local (AStLambert-681-1-83-232.w90-86.abo.wanadoo.fr. [90.86.25.232]) by smtp.gmail.com with ESMTPSA id 72-v6sm6631619wrb.48.2018.09.07.03.08.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 03:08:34 -0700 (PDT) From: Bartosz Golaszewski To: Srinivas Kandagatla , "David S . Miller" , Mauro Carvalho Chehab , Greg Kroah-Hartman , Andrew Morton , Arnd Bergmann , Jonathan Corbet , Sekhar Nori , Kevin Hilman , David Lechner , Boris Brezillon , Andrew Lunn , Alban Bedel , Maxime Ripard , Chen-Yu Tsai Subject: [PATCH v2 13/16] nvmem: add support for cell lookups from machine code Date: Fri, 7 Sep 2018 12:07:47 +0200 Message-Id: <20180907100750.14564-14-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180907100750.14564-1-brgl@bgdev.pl> References: <20180907100750.14564-1-brgl@bgdev.pl> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180907_031139_668105_1B0D7F23 X-CRM114-Status: GOOD ( 20.02 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bartosz Golaszewski , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Bartosz Golaszewski Add a way for machine code users to associate devices with nvmem cells. Signed-off-by: Bartosz Golaszewski --- drivers/nvmem/core.c | 143 +++++++++++++++++++++++++++------- include/linux/nvmem-machine.h | 16 ++++ 2 files changed, 132 insertions(+), 27 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index da7a9d5beb33..9e2f9c993a07 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -62,6 +62,9 @@ static DEFINE_IDA(nvmem_ida); static DEFINE_MUTEX(nvmem_cell_mutex); static LIST_HEAD(nvmem_cell_tables); +static DEFINE_MUTEX(nvmem_lookup_mutex); +static LIST_HEAD(nvmem_lookup_list); + static BLOCKING_NOTIFIER_HEAD(nvmem_notifier); #ifdef CONFIG_DEBUG_LOCK_ALLOC @@ -285,6 +288,18 @@ static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) return to_nvmem_device(d); } +static struct nvmem_device *nvmem_find(const char *name) +{ + struct device *d; + + d = bus_find_device_by_name(&nvmem_bus_type, NULL, name); + + if (!d) + return NULL; + + return to_nvmem_device(d); +} + static void nvmem_cell_drop(struct nvmem_cell *cell) { mutex_lock(&nvmem_mutex); @@ -421,6 +436,21 @@ nvmem_find_cell_by_index(struct nvmem_device *nvmem, int index) return cell; } +static struct nvmem_cell * +nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) +{ + struct nvmem_cell *cell = NULL; + + mutex_lock(&nvmem_mutex); + list_for_each_entry(cell, &nvmem->cells, node) { + if (strcmp(cell_id, cell->name) == 0) + break; + } + mutex_unlock(&nvmem_mutex); + + return cell; +} + static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) { struct device_node *parent, *child; @@ -691,22 +721,16 @@ int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) } EXPORT_SYMBOL(devm_nvmem_unregister); -static struct nvmem_device *__nvmem_device_get(struct device_node *np) +static struct nvmem_device * +__nvmem_device_get(struct device_node *np, const char *name) { struct nvmem_device *nvmem = NULL; - if (!np) - return ERR_PTR(-EINVAL); - mutex_lock(&nvmem_mutex); - - nvmem = of_nvmem_find(np); - if (!nvmem) { - mutex_unlock(&nvmem_mutex); - return ERR_PTR(-EPROBE_DEFER); - } - + nvmem = np ? of_nvmem_find(np) : nvmem_find(name); mutex_unlock(&nvmem_mutex); + if (!nvmem) + return ERR_PTR(-EPROBE_DEFER); if (!try_module_get(nvmem->owner)) { dev_err(&nvmem->dev, @@ -726,18 +750,6 @@ static void __nvmem_device_put(struct nvmem_device *nvmem) kref_put(&nvmem->refcnt, nvmem_device_release); } -static struct nvmem_device *nvmem_find(const char *name) -{ - struct device *d; - - d = bus_find_device_by_name(&nvmem_bus_type, NULL, name); - - if (!d) - return NULL; - - return to_nvmem_device(d); -} - #if IS_ENABLED(CONFIG_OF) /** * of_nvmem_device_get() - Get nvmem device from a given id @@ -760,7 +772,7 @@ struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id) if (!nvmem_np) return ERR_PTR(-EINVAL); - return __nvmem_device_get(nvmem_np); + return __nvmem_device_get(nvmem_np, NULL); } EXPORT_SYMBOL_GPL(of_nvmem_device_get); #endif @@ -897,7 +909,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, if (!nvmem_np) return ERR_PTR(-EINVAL); - nvmem = __nvmem_device_get(nvmem_np); + nvmem = __nvmem_device_get(nvmem_np, NULL); of_node_put(nvmem_np); if (IS_ERR(nvmem)) return ERR_CAST(nvmem); @@ -913,6 +925,44 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, EXPORT_SYMBOL_GPL(of_nvmem_cell_get); #endif +static struct nvmem_cell * +nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) +{ + struct nvmem_cell *cell = ERR_PTR(-ENOENT); + struct nvmem_cell_lookup *lookup; + struct nvmem_device *nvmem; + const char *dev_id; + + if (!dev) + return ERR_PTR(-EINVAL); + + dev_id = dev_name(dev); + + mutex_lock(&nvmem_lookup_mutex); + + list_for_each_entry(lookup, &nvmem_lookup_list, node) { + if ((strcmp(lookup->dev_id, dev_id) == 0) && + (strcmp(lookup->con_id, con_id) == 0)) { + /* This is the right entry. */ + nvmem = __nvmem_device_get(NULL, lookup->nvmem_name); + if (!nvmem) { + /* Provider may not be registered yet. */ + cell = ERR_PTR(-EPROBE_DEFER); + goto out; + } + + cell = nvmem_find_cell_by_name(nvmem, + lookup->cell_name); + if (!cell) + goto out; + } + } + +out: + mutex_unlock(&nvmem_lookup_mutex); + return cell; +} + /** * nvmem_cell_get() - Get nvmem cell of device form a given cell name * @@ -925,10 +975,14 @@ EXPORT_SYMBOL_GPL(of_nvmem_cell_get); */ struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id) { - if (!dev->of_node) + if (dev->of_node) + return of_nvmem_cell_get(dev->of_node, cell_id); + + /* Only allow empty cell_id for DT systems. */ + if (!cell_id) return ERR_PTR(-EINVAL); - return of_nvmem_cell_get(dev->of_node, cell_id); + return nvmem_cell_get_from_lookup(dev, cell_id); } EXPORT_SYMBOL_GPL(nvmem_cell_get); @@ -1300,6 +1354,41 @@ void nvmem_del_cell_table(struct nvmem_cell_table *table) } EXPORT_SYMBOL_GPL(nvmem_del_cell_table); +/** + * nvmem_add_cell_lookups() - register a list of cell lookup entries + * + * @entries: array of cell lookup entries + * @nentries: number of cell lookup entries in the array + */ +void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) +{ + int i; + + mutex_lock(&nvmem_lookup_mutex); + for (i = 0; i < nentries; i++) + list_add_tail(&entries[i].node, &nvmem_lookup_list); + mutex_unlock(&nvmem_lookup_mutex); +} +EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups); + +/** + * nvmem_del_cell_lookups() - remove a list of previously added cell lookup + * entries + * + * @entries: array of cell lookup entries + * @nentries: number of cell lookup entries in the array + */ +void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) +{ + int i; + + mutex_lock(&nvmem_lookup_mutex); + for (i = 0; i < nentries; i++) + list_del(&entries[i].node); + mutex_unlock(&nvmem_lookup_mutex); +} +EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups); + /** * nvmem_dev_name() - Get the name of a given nvmem device. * diff --git a/include/linux/nvmem-machine.h b/include/linux/nvmem-machine.h index 1e199dfaacab..7859c08934d5 100644 --- a/include/linux/nvmem-machine.h +++ b/include/linux/nvmem-machine.h @@ -26,16 +26,32 @@ struct nvmem_cell_table { struct list_head node; }; +struct nvmem_cell_lookup { + const char *nvmem_name; + const char *cell_name; + const char *dev_id; + const char *con_id; + struct list_head node; +}; + #if IS_ENABLED(CONFIG_NVMEM) void nvmem_add_cell_table(struct nvmem_cell_table *table); void nvmem_del_cell_table(struct nvmem_cell_table *table); +void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries); +void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries); + #else /* CONFIG_NVMEM */ static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {} static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {} +static inline void +nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) {} +static inline void +nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) {} + #endif /* CONFIG_NVMEM */ #endif /* ifndef _LINUX_NVMEM_MACHINE_H */