linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alban Bedel <albeu@free.fr>
To: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Cc: linux-kernel@vger.kernel.org, Alban Bedel <albeu@free.fr>
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	[thread overview]
Message-ID: <20190106192820.12558-4-albeu@free.fr> (raw)
In-Reply-To: <20190106192820.12558-1-albeu@free.fr>

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 <albeu@free.fr>
---
 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


  parent reply	other threads:[~2019-01-06 19:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-06 19:28 [PATCH 0/8] nvmem: Various small fixes and improvements Alban Bedel
2019-01-06 19:28 ` [PATCH 1/8] nvmem: core: Set the provider read-only when no write callback is given Alban Bedel
2019-01-06 19:28 ` [PATCH 2/8] nvmem: core: Fix of_nvmem_cell_get() for optional cells Alban Bedel
2019-01-06 19:28 ` Alban Bedel [this message]
2019-01-15 12:40   ` [PATCH 3/8] nvmem: Add nvmem_cell_get_optional and devm_nvmem_cell_get_optional Srinivas Kandagatla
2019-01-16 18:26     ` Alban
2019-01-17 10:20       ` Srinivas Kandagatla
2019-01-06 19:28 ` [PATCH 4/8] nvmem: core: Fix cell lookup when no cell is found Alban Bedel
2019-01-06 19:28 ` [PATCH 5/8] nvmem: core: Properly handle connection ID in of_nvmem_device_get() Alban Bedel
2019-01-06 19:28 ` [PATCH 6/8] nvmem: core: Always reference the device returned by nvmem_device_get() Alban Bedel
2019-01-06 19:28 ` [PATCH 7/8] nvmem: core: Fix device reference leak Alban Bedel
2019-01-06 19:28 ` [PATCH 8/8] nvmem: core: Avoid useless iterations in nvmem_cell_get_from_lookup() Alban Bedel
2019-01-15 12:40 ` [PATCH 0/8] nvmem: Various small fixes and improvements Srinivas Kandagatla

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190106192820.12558-4-albeu@free.fr \
    --to=albeu@free.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=srinivas.kandagatla@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).