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@xxxxxxx>
---
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;
+}