nvmem: core: add nvmem_cell_read_u16
authorFabrice Gasnier <fabrice.gasnier@st.com>
Sat, 13 Apr 2019 10:32:57 +0000 (11:32 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 25 Apr 2019 17:43:12 +0000 (19:43 +0200)
Add nvmem_cell_read_u16() helper to ease read of an u16 value on consumer
side. This is inspired by nvmem_cell_read_u32() function.
This helper is useful on stm32 that has 16 bits data cells stored in non
volatile memory.

Signed-off-by: Fabrice Gasnier <fabrice.gasnier@st.com>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/nvmem/core.c
include/linux/nvmem-consumer.h

index f24008b66826902f43b2abdaeb5703e9a6deb201..1d00f5671368e3b0644238ea84f4d3179ad192d9 100644 (file)
@@ -1334,6 +1334,43 @@ int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
 }
 EXPORT_SYMBOL_GPL(nvmem_cell_write);
 
+/**
+ * nvmem_cell_read_u16() - Read a cell value as an u16
+ *
+ * @dev: Device that requests the nvmem cell.
+ * @cell_id: Name of nvmem cell to read.
+ * @val: pointer to output value.
+ *
+ * Return: 0 on success or negative errno.
+ */
+int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
+{
+       struct nvmem_cell *cell;
+       void *buf;
+       size_t len;
+
+       cell = nvmem_cell_get(dev, cell_id);
+       if (IS_ERR(cell))
+               return PTR_ERR(cell);
+
+       buf = nvmem_cell_read(cell, &len);
+       if (IS_ERR(buf)) {
+               nvmem_cell_put(cell);
+               return PTR_ERR(buf);
+       }
+       if (len != sizeof(*val)) {
+               kfree(buf);
+               nvmem_cell_put(cell);
+               return -EINVAL;
+       }
+       memcpy(val, buf, sizeof(*val));
+       kfree(buf);
+       nvmem_cell_put(cell);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
+
 /**
  * nvmem_cell_read_u32() - Read a cell value as an u32
  *
index 312bfa5efd806d0fe3e30a553e3af48457f2a8df..8f8be5b00060285b61f57abe008026ede9303ff3 100644 (file)
@@ -61,6 +61,7 @@ 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);
 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len);
+int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val);
 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val);
 
 /* direct nvmem device read/write interface */
@@ -122,6 +123,12 @@ static inline int nvmem_cell_write(struct nvmem_cell *cell,
        return -EOPNOTSUPP;
 }
 
+static inline int nvmem_cell_read_u16(struct device *dev,
+                                     const char *cell_id, u16 *val)
+{
+       return -EOPNOTSUPP;
+}
+
 static inline int nvmem_cell_read_u32(struct device *dev,
                                      const char *cell_id, u32 *val)
 {