Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2850010imu; Sun, 6 Jan 2019 11:31:11 -0800 (PST) X-Google-Smtp-Source: ALg8bN4XlyXMYjtgv+ZVGHYEfVBCrrYFh/TOz9ygmPJtm4oKKnaj8Yp24rFaSI1DVBtX9g7rE/ko X-Received: by 2002:a63:5964:: with SMTP id j36mr20212708pgm.210.1546803071431; Sun, 06 Jan 2019 11:31:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546803071; cv=none; d=google.com; s=arc-20160816; b=JIgJv9S33v+AM6Lq6bYB8cevf3L/nzemQsoc18tveOp04WIAEIhrpbNm4FvWgGJxHI CLtjV8WN5mtkFRGZj0piRQPOTNhTpfGbCWCB6opak5Dza8VMMvsp4cZyabpDDjqo5WoZ Ouergj4Rlie6ZFoOV6XC+LHiImZTQG4TYvF11TUP7Su6YnuN13A4r9oyW7TAEizl8qmD OtYKqOY9CQzyxXdaWjk+Hxsi9uYiISzsI1Wr0P/uwv38ArPiV1PSlnFFvVtmtOgWg7b9 cPhd5Vo5TsyOuaJxdydJpxWq0d8cgf10NFRFhjgAnM8MTYXcRZQ5pZWUdyNwah7vu0Ix rVsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=6w0zGOyYKJDthYHhg+9SSqkgQm48mcliV+wVfee9a1E=; b=Sp5MKdoQpgbPp5MZVsUrM1Hkjz+H86PGl9bUt6v4tAhWzOMLE4le13AIJMLAgjO8eN qh4vQYFDvdyuuYl6yb7EKgdWy8e4RMhqP4V1HVvBwnHBAJJlr04FO1DU9zhW8so/DckA 81UH9uMVfdXSEyFdApC93gqT8wy6mrd6kce8798uUEZRHLe9hDT5Ba0Q201semoC6BZX /XQMo3NNm8PO9384Nx8AwdVIbDW6xj/WcOnQVX3rp2aiB0eM2HTnaQA46vILAix3uHI3 YLAdS4ideCpNU0Gp7avbuRIryXGW4tbjcKJE4boPm59U87PJG55esd5Vh5LZ5Dl/vJAs zPXQ== 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 d37si6452386plb.140.2019.01.06.11.30.56; Sun, 06 Jan 2019 11:31:11 -0800 (PST) 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 S1726374AbfAFT2w (ORCPT + 99 others); Sun, 6 Jan 2019 14:28:52 -0500 Received: from smtp3-g21.free.fr ([212.27.42.3]:31704 "EHLO smtp3-g21.free.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726050AbfAFT2v (ORCPT ); Sun, 6 Jan 2019 14:28:51 -0500 Received: from localhost.localdomain (unknown [IPv6:2a02:8108:4840:8f74:143e:fcd5:beb7:2140]) (Authenticated sender: albeu) by smtp3-g21.free.fr (Postfix) with ESMTPA id 0AFDD13F8B7; Sun, 6 Jan 2019 20:28:42 +0100 (CET) From: Alban Bedel To: Srinivas Kandagatla Cc: linux-kernel@vger.kernel.org, Alban Bedel Subject: [PATCH 3/8] nvmem: Add nvmem_cell_get_optional and devm_nvmem_cell_get_optional Date: Sun, 6 Jan 2019 20:28:15 +0100 Message-Id: <20190106192820.12558-4-albeu@free.fr> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190106192820.12558-1-albeu@free.fr> References: <20190106192820.12558-1-albeu@free.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add helper functions to make the driver code simpler when a cell is optional. Using these functions just return NULL when the cell doesn't exists or if nvmem is disabled. Signed-off-by: Alban Bedel --- drivers/nvmem/core.c | 48 ++++++++++++++++++++++++++++++++++ include/linux/nvmem-consumer.h | 16 ++++++++++++ 2 files changed, 64 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index f8c43da6f2ca..8e1b52559467 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -1083,6 +1083,30 @@ struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id) } EXPORT_SYMBOL_GPL(nvmem_cell_get); +/** + * nvmem_cell_get_optional() - Get an optional nvmem cell of device from + * a given id. + * + * @dev: Device that requests the nvmem cell. + * @cell_id: nvmem cell name to get. + * + * Return: Will be NULL if no cell with the given name is defined, + * an ERR_PTR() on error or a valid pointer to a struct nvmem_cell. + * The nvmem_cell will be freed by the nvmem_cell_put(). + */ +struct nvmem_cell *nvmem_cell_get_optional(struct device *dev, + const char *cell_id) +{ + struct nvmem_cell *cell; + + cell = nvmem_cell_get(dev, cell_id); + if (IS_ERR(cell) && PTR_ERR(cell) == -ENOENT) + return NULL; + + return cell; +} +EXPORT_SYMBOL_GPL(nvmem_cell_get_optional); + static void devm_nvmem_cell_release(struct device *dev, void *res) { nvmem_cell_put(*(struct nvmem_cell **)res); @@ -1118,6 +1142,30 @@ struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id) } EXPORT_SYMBOL_GPL(devm_nvmem_cell_get); +/** + * devm_nvmem_cell_get() - Get an optional nvmem cell of device from + * a given id. + * + * @dev: Device that requests the nvmem cell. + * @id: nvmem cell name id to get. + * + * Return: Will be NULL if the cell doesn't exists, an ERR_PTR() on + * error or a valid pointer to a struct nvmem_cell. The nvmem_cell + * will be freed by the automatically once the device is freed. + */ +struct nvmem_cell *devm_nvmem_cell_get_optional(struct device *dev, + const char *cell_id) +{ + struct nvmem_cell *cell; + + cell = devm_nvmem_cell_get(dev, cell_id); + if (IS_ERR(cell) && PTR_ERR(cell) == -ENOENT) + return NULL; + + return cell; +} +EXPORT_SYMBOL_GPL(devm_nvmem_cell_get_optional); + static int devm_nvmem_cell_match(struct device *dev, void *res, void *data) { struct nvmem_cell **c = res; diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h index 312bfa5efd80..8d7bf21a9adc 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -56,7 +56,11 @@ enum { /* Cell based interface */ struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id); +struct nvmem_cell *nvmem_cell_get_optional(struct device *dev, + const char *id); struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id); +struct nvmem_cell *devm_nvmem_cell_get_optional(struct device *dev, + const char *id); void nvmem_cell_put(struct nvmem_cell *cell); void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell); void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len); @@ -96,12 +100,24 @@ static inline struct nvmem_cell *nvmem_cell_get(struct device *dev, return ERR_PTR(-EOPNOTSUPP); } +static inline struct nvmem_cell *nvmem_cell_get_optional(struct device *dev, + const char *id) +{ + return NULL; +} + static inline struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id) { return ERR_PTR(-EOPNOTSUPP); } +static inline struct nvmem_cell * +devm_nvmem_cell_get_optional(struct device *dev, const char *id) +{ + return NULL; +} + static inline void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) { -- 2.19.1