From patchwork Fri Mar 9 14:46:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 10270779 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6101460236 for ; Fri, 9 Mar 2018 14:52:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 53A862898C for ; Fri, 9 Mar 2018 14:52:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4814F28BF6; Fri, 9 Mar 2018 14:52:34 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable 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 4326B28AC0 for ; Fri, 9 Mar 2018 14:52:33 +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=O9mj6N/r1T9QRS8rQbBGlnkgYeAX4+sFqdhQKFZGCvk=; b=IMb9iE1ZEHJ9SVdF8FqQkuJcYJ SXvPtBJfCWtAaisg3Ib2Tqh8Ems5nTnDmtiH3J4hdS864sbVmrQtBEIfFsXViK7yIl8fejhd6w4dW O+gnNpWvfRz4/vT1eKetD9Yc8j8pJEpBUhL0VH5AmkxSGALXUIxi5YyJ8BKCLNZ8FjHyMqOLrnPWe g61ZqH42PFd5CIuoKDrMkAyEAFZWd/+xLvoP7Adg+F3D2eevpF5pDtqC2C9Ampc05Ts4+rULCl7ia o7cBkKzNeHeR/bhh641cCy7RFZJYh7o6ayzRKXdD86TDiUcYZlm1gnVbIusbtHLDsfGD8t9+zTPNc oFfo9QAg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1euJNl-0001VP-0S; Fri, 09 Mar 2018 14:52:29 +0000 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1euJM0-0000Or-Rr for linux-amlogic@lists.infradead.org; Fri, 09 Mar 2018 14:51:11 +0000 Received: by mail-wm0-x242.google.com with SMTP id s206so3792967wme.0 for ; Fri, 09 Mar 2018 06:50:30 -0800 (PST) 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; bh=RT33r5HhOvcPJ59VnfxCyDIOhWrlyqcWmOJNk3eMS/4=; b=YEu3kEpg9ftU0mnIZUH6bihm5esPS6wbiM7nAa3I9mCjh5gFwyjahVOmGXhX2ktWZP g4uoFSzpdZZrgBjh9chDuBta8WxNKrAqSO4zX9JJ8+YnavbAig8AEJrBRxNILFJKaWpK d0HtH4hLTJTlTxt3rYCJ4K91vqhDCz1oFaGJ0= 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=RT33r5HhOvcPJ59VnfxCyDIOhWrlyqcWmOJNk3eMS/4=; b=kG9L6Y0cPQTc1apyaRb4LEsuf4i21uIFQTlJAgz1n88DtFui9bGBfJBbaIP8zWq3N0 eRdCwSeThhTfpjXGVVCeBXWwp7SFKkOQs74sjzPipnt2aw94Zrc1OwrFhy7nmYV9tPx+ aYTo9bzsCb8dL4H6PRhsajpCRrt5eA+IZqnl5s5zKcieQXjzbvsRIOFov8U4iKBQFolc xLPwSwQKiTCUhMYYzXvLuu9zDA4iuQhX0p2swU1P7sjr0G4bZNF7LpJu+kP15D3AzmVt TuNcYlNOiQLt7N11kxn044G4Sm6WVVeEywSM5arBXawachqIsuELVxaqhiB4PwSVIJ5z Zz0g== X-Gm-Message-State: AElRT7F0NFP7f14/SbXySH3Ssq+br0RrvKu1DG26tR/CzdpYSRi0zk18 uJ6kw0ibdYoV1rqqodVGiT8ijA== X-Google-Smtp-Source: AG47ELs5L475cgKxe12ntovScHWD4bMZ6RlX7XDuRxlbRC+H9dokXkpg4f28OuwR45JMo4XDNpfStA== X-Received: by 10.28.18.2 with SMTP id 2mr2164891wms.108.1520607029338; Fri, 09 Mar 2018 06:50:29 -0800 (PST) Received: from localhost.localdomain (cpc90716-aztw32-2-0-cust92.18-1.cable.virginm.net. [86.26.100.93]) by smtp.gmail.com with ESMTPSA id u48sm1191901wrb.85.2018.03.09.06.50.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Mar 2018 06:50:28 -0800 (PST) From: srinivas.kandagatla@linaro.org To: gregkh@linuxfoundation.org Subject: [PATCH 03/25] nvmem: Introduce devm_nvmem_(un)register() Date: Fri, 9 Mar 2018 14:46:57 +0000 Message-Id: <20180309144719.29904-4-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180309144719.29904-1-srinivas.kandagatla@linaro.org> References: <20180309144719.29904-1-srinivas.kandagatla@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180309_065041_011977_0850B380 X-CRM114-Status: GOOD ( 15.03 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Heiko Stuebner , linux-rockchip@lists.infradead.org, Andrey Smirnov , Kevin Hilman , linux-kernel@vger.kernel.org, Masahiro Yamada , srinivas.kandagatla@linaro.org, linux-arm-kernel@lists.infradead.org, linux-amlogic@lists.infradead.org, Carlo Caione , linux-mediatek@lists.infradead.org, cphealy@gmail.com, Matthias Brugger MIME-Version: 1.0 Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Andrey Smirnov Introduce devm_nvmem_register()/devm_nvmem_unregister() to make .remove() unnecessary in trivial drivers. Cc: Srinivas Kandagatla Cc: Heiko Stuebner Cc: Masahiro Yamada Cc: Carlo Caione Cc: Kevin Hilman Cc: Matthias Brugger Cc: cphealy@gmail.com Cc: linux-kernel@vger.kernel.org Cc: linux-mediatek@lists.infradead.org Cc: linux-rockchip@lists.infradead.org Cc: linux-amlogic@lists.infradead.org Cc: linux-arm-kernel@lists.infradead.org Signed-off-by: Andrey Smirnov Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 59 ++++++++++++++++++++++++++++++++++++++++++ include/linux/nvmem-provider.h | 17 ++++++++++++ 2 files changed, 76 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 99e04cfcc723..b05aa8e81303 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -549,6 +549,65 @@ int nvmem_unregister(struct nvmem_device *nvmem) } EXPORT_SYMBOL_GPL(nvmem_unregister); +static void devm_nvmem_release(struct device *dev, void *res) +{ + WARN_ON(nvmem_unregister(*(struct nvmem_device **)res)); +} + +/** + * devm_nvmem_register() - Register a managed nvmem device for given + * nvmem_config. + * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem + * + * @config: nvmem device configuration with which nvmem device is created. + * + * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device + * on success. + */ +struct nvmem_device *devm_nvmem_register(struct device *dev, + const struct nvmem_config *config) +{ + struct nvmem_device **ptr, *nvmem; + + ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + nvmem = nvmem_register(config); + + if (!IS_ERR(nvmem)) { + *ptr = nvmem; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } + + return nvmem; +} +EXPORT_SYMBOL_GPL(devm_nvmem_register); + +static int devm_nvmem_match(struct device *dev, void *res, void *data) +{ + struct nvmem_device **r = res; + + return *r == data; +} + +/** + * devm_nvmem_unregister() - Unregister previously registered managed nvmem + * device. + * + * @nvmem: Pointer to previously registered nvmem device. + * + * Return: Will be an negative on error or a zero on success. + */ +int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) +{ + return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); +} +EXPORT_SYMBOL(devm_nvmem_unregister); + + static struct nvmem_device *__nvmem_device_get(struct device_node *np, struct nvmem_cell **cellp, const char *cell_id) diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index b00567a07496..f89598bc4e1c 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -72,6 +72,11 @@ struct nvmem_config { struct nvmem_device *nvmem_register(const struct nvmem_config *cfg); int nvmem_unregister(struct nvmem_device *nvmem); +struct nvmem_device *devm_nvmem_register(struct device *dev, + const struct nvmem_config *cfg); + +int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem); + #else static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c) @@ -84,5 +89,17 @@ static inline int nvmem_unregister(struct nvmem_device *nvmem) return -ENOSYS; } +static inline struct nvmem_device * +devm_nvmem_register(struct device *dev, const struct nvmem_config *c) +{ + return nvmem_register(c); +} + +static inline int +devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) +{ + return nvmem_unregister(nvmem); +} + #endif /* CONFIG_NVMEM */ #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */