Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2939088ybl; Mon, 19 Aug 2019 09:36:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqxh1qtnjIMLWU7n/yH+Yw3ypqAkjqYNegGYK7c+uNe2gjHvqmcjT+cAXGgi3vj6tG6MAaYA X-Received: by 2002:a17:902:8a93:: with SMTP id p19mr23788152plo.106.1566232605852; Mon, 19 Aug 2019 09:36:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566232605; cv=none; d=google.com; s=arc-20160816; b=jBV5eGrSRUi8XtDA5U/86ubyBAP5W3EbCjLz2X6mic8Bj1YP64yYpsWlPHXN9RIH1k vrWHssIjyz1eP8tr9dv+U7oWzI9wfG/Ypo2ZLXFKZJJxG7VzLL/LWBiHUfzO6tHQH4ED 1loPewdJAY9ViLLez7j6AjwS8x8FgSgD+104eu2a3nEg3FDWOzh9lOGgnxvr0rmpHawn 0SvlMFm97PWv0dV/pUu80KXqb3jacSg/1N680ucxOINrV7ZOrlXNaL4Utkt7iiGtxqMg E7brPHsHqjFweYHCm1iETLxZj6cKQyZ7KXZ7oueVyzhbcijaSyJZUbWbI45OWdmEh9bi oRTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:to:from; bh=lBk1XmoZEZr/hnUOZyDBYHW+zxsDp8U4c+XX4IQZ5jk=; b=IJ7mxWIseOrNPiikDNGWbnx4i7FJ6zey8JAEjPTc15tzPtcdEUR7B+bDsvSQKIvvXs B2cC+N/uWmTWvvVYymoRhucRS7MVnbM/meh+3nSkFeb6EVjhWJTK07T4ERRAqvFq40qG QAbt5LRpJeAThb4HFGlMIZbCwQEMkvsDAkj5DWkJbfpbinJ8ZlwZTGLWoWUbS1k1lWtR gUQiaso+wdpy5H5859dQWcPfL2GK7wdqLrcWwDEOE/LH3QnbvLLXNXP7EnoWwsK7v08/ drgm1c/RwvxidpqJYW3rhpSW1PI9Bt7SGDBGLVYqXlPZwxyNi8+trQ6Yf7GzaM2Q+Pze pMVQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j1si10238456plk.55.2019.08.19.09.36.28; Mon, 19 Aug 2019 09:36:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728147AbfHSQeK (ORCPT + 99 others); Mon, 19 Aug 2019 12:34:10 -0400 Received: from mx2.suse.de ([195.135.220.15]:55310 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727525AbfHSQcE (ORCPT ); Mon, 19 Aug 2019 12:32:04 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id EEAB3B0E2; Mon, 19 Aug 2019 16:32:01 +0000 (UTC) From: Thomas Bogendoerfer To: Jonathan Corbet , Ralf Baechle , Paul Burton , James Hogan , Dmitry Torokhov , Lee Jones , "David S. Miller" , Srinivas Kandagatla , Alessandro Zummo , Alexandre Belloni , Greg Kroah-Hartman , Jiri Slaby , Evgeniy Polyakov , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-input@vger.kernel.org, netdev@vger.kernel.org, linux-rtc@vger.kernel.org, linux-serial@vger.kernel.org Subject: [PATCH v5 03/17] nvmem: core: add nvmem_device_find Date: Mon, 19 Aug 2019 18:31:26 +0200 Message-Id: <20190819163144.3478-4-tbogendoerfer@suse.de> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20190819163144.3478-1-tbogendoerfer@suse.de> References: <20190819163144.3478-1-tbogendoerfer@suse.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org nvmem_device_find provides a way to search for nvmem devices with the help of a match function simlair to bus_find_device. Signed-off-by: Thomas Bogendoerfer --- Documentation/driver-api/nvmem.rst | 2 ++ drivers/nvmem/core.c | 62 ++++++++++++++++++++------------------ include/linux/nvmem-consumer.h | 9 ++++++ 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Documentation/driver-api/nvmem.rst b/Documentation/driver-api/nvmem.rst index d9d958d5c824..287e86819640 100644 --- a/Documentation/driver-api/nvmem.rst +++ b/Documentation/driver-api/nvmem.rst @@ -129,6 +129,8 @@ To facilitate such consumers NVMEM framework provides below apis:: struct nvmem_device *nvmem_device_get(struct device *dev, const char *name); struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *name); + struct nvmem_device *nvmem_device_find(void *data, + int (*match)(struct device *dev, const void *data)); void nvmem_device_put(struct nvmem_device *nvmem); int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset, size_t bytes, void *buf); diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index ac5d945be88a..e591ba54758f 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -76,36 +76,18 @@ static struct bus_type nvmem_bus_type = { .name = "nvmem", }; +#if IS_ENABLED(CONFIG_OF) static int of_nvmem_match(struct device *dev, const void *nvmem_np) { return dev->of_node == nvmem_np; } +#endif -static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np) +static int nvmem_match_name(struct device *dev, const void *data) { - struct device *d; - - if (!nvmem_np) - return NULL; - - d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match); - - if (!d) - return NULL; + const char *name = data; - 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); + return sysfs_streq(name, dev_name(dev)); } static void nvmem_cell_drop(struct nvmem_cell *cell) @@ -537,13 +519,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, - const char *nvmem_name) +static struct nvmem_device *__nvmem_device_get(void *data, + int (*match)(struct device *dev, const void *data)) { struct nvmem_device *nvmem = NULL; + struct device *dev; mutex_lock(&nvmem_mutex); - nvmem = np ? of_nvmem_find(np) : nvmem_find(nvmem_name); + dev = bus_find_device(&nvmem_bus_type, NULL, data, match); + if (dev) + nvmem = to_nvmem_device(dev); mutex_unlock(&nvmem_mutex); if (!nvmem) return ERR_PTR(-EPROBE_DEFER); @@ -592,7 +577,7 @@ struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id) if (!nvmem_np) return ERR_PTR(-ENOENT); - return __nvmem_device_get(nvmem_np, NULL); + return __nvmem_device_get(nvmem_np, of_nvmem_match); } EXPORT_SYMBOL_GPL(of_nvmem_device_get); #endif @@ -618,10 +603,26 @@ struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name) } - return __nvmem_device_get(NULL, dev_name); + return __nvmem_device_get((void *)dev_name, nvmem_match_name); } EXPORT_SYMBOL_GPL(nvmem_device_get); +/** + * nvmem_device_find() - Find nvmem device with matching function + * + * @data: Data to pass to match function + * @match: Callback function to check device + * + * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device + * on success. + */ +struct nvmem_device *nvmem_device_find(void *data, + int (*match)(struct device *dev, const void *data)) +{ + return __nvmem_device_get(data, match); +} +EXPORT_SYMBOL_GPL(nvmem_device_find); + static int devm_nvmem_device_match(struct device *dev, void *res, void *data) { struct nvmem_device **nvmem = res; @@ -715,7 +716,8 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) 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); + nvmem = __nvmem_device_get((void *)lookup->nvmem_name, + nvmem_match_name); if (IS_ERR(nvmem)) { /* Provider may not be registered yet. */ cell = ERR_CAST(nvmem); @@ -785,7 +787,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) if (!nvmem_np) return ERR_PTR(-EINVAL); - nvmem = __nvmem_device_get(nvmem_np, NULL); + nvmem = __nvmem_device_get(nvmem_np, of_nvmem_match); of_node_put(nvmem_np); if (IS_ERR(nvmem)) return ERR_CAST(nvmem); diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h index 8f8be5b00060..02dc4aa992b2 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -89,6 +89,9 @@ void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, int nvmem_register_notifier(struct notifier_block *nb); int nvmem_unregister_notifier(struct notifier_block *nb); +struct nvmem_device *nvmem_device_find(void *data, + int (*match)(struct device *dev, const void *data)); + #else static inline struct nvmem_cell *nvmem_cell_get(struct device *dev, @@ -204,6 +207,12 @@ static inline int nvmem_unregister_notifier(struct notifier_block *nb) return -EOPNOTSUPP; } +static inline struct nvmem_device *nvmem_device_find(void *data, + int (*match)(struct device *dev, const void *data)) +{ + return NULL; +} + #endif /* CONFIG_NVMEM */ #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF) -- 2.13.7