linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] nvmem: core: add nvmem_cell_read_common
@ 2020-01-26 19:56 Yangtao Li
  2020-01-26 19:56 ` [PATCH 2/2] nvmem: core: add nvmem_cell_read_u64 Yangtao Li
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Yangtao Li @ 2020-01-26 19:56 UTC (permalink / raw)
  To: srinivas.kandagatla, linux-kernel; +Cc: Yangtao Li

Now there are nvmem_cell_read_u16 and nvmem_cell_read_u32.
They are very similar, let's strip out a common part.

And use nvmem_cell_read_common to simplify their implementation.

Signed-off-by: Yangtao Li <tiny.windzz@gmail.com>
---
 drivers/nvmem/core.c | 54 ++++++++++++++++----------------------------
 1 file changed, 19 insertions(+), 35 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 9f1ee9c766ec..f4226546e49a 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1071,16 +1071,8 @@ 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)
+static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
+				  void *val, size_t count)
 {
 	struct nvmem_cell *cell;
 	void *buf;
@@ -1095,17 +1087,31 @@ int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
 		nvmem_cell_put(cell);
 		return PTR_ERR(buf);
 	}
-	if (len != sizeof(*val)) {
+	if (len != count) {
 		kfree(buf);
 		nvmem_cell_put(cell);
 		return -EINVAL;
 	}
-	memcpy(val, buf, sizeof(*val));
+	memcpy(val, buf, count);
 	kfree(buf);
 	nvmem_cell_put(cell);
 
 	return 0;
 }
+
+/**
+ * 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)
+{
+	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
+}
 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
 
 /**
@@ -1119,29 +1125,7 @@ EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
  */
 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *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;
+	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
 }
 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] nvmem: core: add nvmem_cell_read_u64
  2020-01-26 19:56 [PATCH 1/2] nvmem: core: add nvmem_cell_read_common Yangtao Li
@ 2020-01-26 19:56 ` Yangtao Li
  2020-02-05 11:50 ` [PATCH 1/2] nvmem: core: add nvmem_cell_read_common Frank Lee
  2020-02-17 10:47 ` Srinivas Kandagatla
  2 siblings, 0 replies; 4+ messages in thread
From: Yangtao Li @ 2020-01-26 19:56 UTC (permalink / raw)
  To: srinivas.kandagatla, linux-kernel; +Cc: Yangtao Li

Add nvmem_cell_read_u64() helper to ease read of an u64 value on consumer
side. This helper is useful on some sunxi platform that has 64 bits data
cells stored in no volatile memory.

Signed-off-by: Yangtao Li <tiny.windzz@gmail.com>
---
 drivers/nvmem/core.c           | 15 +++++++++++++++
 include/linux/nvmem-consumer.h |  7 +++++++
 2 files changed, 22 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index f4226546e49a..6c9ad0f75847 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1129,6 +1129,21 @@ int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
 }
 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
 
+/**
+ * nvmem_cell_read_u64() - Read a cell value as an u64
+ *
+ * @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_u64(struct device *dev, const char *cell_id, u64 *val)
+{
+	return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
+}
+EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
+
 /**
  * nvmem_device_cell_read() - Read a given nvmem device and cell
  *
diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
index d3776be48c53..1b311d27c9b8 100644
--- a/include/linux/nvmem-consumer.h
+++ b/include/linux/nvmem-consumer.h
@@ -63,6 +63,7 @@ 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);
+int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val);
 
 /* direct nvmem device read/write interface */
 struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
@@ -138,6 +139,12 @@ static inline int nvmem_cell_read_u32(struct device *dev,
 	return -EOPNOTSUPP;
 }
 
+static inline int nvmem_cell_read_u64(struct device *dev,
+				      const char *cell_id, u64 *val)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline struct nvmem_device *nvmem_device_get(struct device *dev,
 						    const char *name)
 {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] nvmem: core: add nvmem_cell_read_common
  2020-01-26 19:56 [PATCH 1/2] nvmem: core: add nvmem_cell_read_common Yangtao Li
  2020-01-26 19:56 ` [PATCH 2/2] nvmem: core: add nvmem_cell_read_u64 Yangtao Li
@ 2020-02-05 11:50 ` Frank Lee
  2020-02-17 10:47 ` Srinivas Kandagatla
  2 siblings, 0 replies; 4+ messages in thread
From: Frank Lee @ 2020-02-05 11:50 UTC (permalink / raw)
  To: Srini Kandagatla, Linux Kernel Mailing List

ping...

On Mon, Jan 27, 2020 at 3:56 AM Yangtao Li <tiny.windzz@gmail.com> wrote:
>
> Now there are nvmem_cell_read_u16 and nvmem_cell_read_u32.
> They are very similar, let's strip out a common part.
>
> And use nvmem_cell_read_common to simplify their implementation.
>
> Signed-off-by: Yangtao Li <tiny.windzz@gmail.com>
> ---
>  drivers/nvmem/core.c | 54 ++++++++++++++++----------------------------
>  1 file changed, 19 insertions(+), 35 deletions(-)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 9f1ee9c766ec..f4226546e49a 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -1071,16 +1071,8 @@ 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)
> +static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
> +                                 void *val, size_t count)
>  {
>         struct nvmem_cell *cell;
>         void *buf;
> @@ -1095,17 +1087,31 @@ int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
>                 nvmem_cell_put(cell);
>                 return PTR_ERR(buf);
>         }
> -       if (len != sizeof(*val)) {
> +       if (len != count) {
>                 kfree(buf);
>                 nvmem_cell_put(cell);
>                 return -EINVAL;
>         }
> -       memcpy(val, buf, sizeof(*val));
> +       memcpy(val, buf, count);
>         kfree(buf);
>         nvmem_cell_put(cell);
>
>         return 0;
>  }
> +
> +/**
> + * 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)
> +{
> +       return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
> +}
>  EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
>
>  /**
> @@ -1119,29 +1125,7 @@ EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
>   */
>  int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *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;
> +       return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
>  }
>  EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
>
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] nvmem: core: add nvmem_cell_read_common
  2020-01-26 19:56 [PATCH 1/2] nvmem: core: add nvmem_cell_read_common Yangtao Li
  2020-01-26 19:56 ` [PATCH 2/2] nvmem: core: add nvmem_cell_read_u64 Yangtao Li
  2020-02-05 11:50 ` [PATCH 1/2] nvmem: core: add nvmem_cell_read_common Frank Lee
@ 2020-02-17 10:47 ` Srinivas Kandagatla
  2 siblings, 0 replies; 4+ messages in thread
From: Srinivas Kandagatla @ 2020-02-17 10:47 UTC (permalink / raw)
  To: Yangtao Li, linux-kernel



On 26/01/2020 19:56, Yangtao Li wrote:
> Now there are nvmem_cell_read_u16 and nvmem_cell_read_u32.
> They are very similar, let's strip out a common part.
> 
> And use nvmem_cell_read_common to simplify their implementation.
> 
> Signed-off-by: Yangtao Li <tiny.windzz@gmail.com>
> ---
>   drivers/nvmem/core.c | 54 ++++++++++++++++----------------------------
>   1 file changed, 19 insertions(+), 35 deletions(-)
> 
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c

Applied both,

thanks,
srini

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-02-17 10:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-26 19:56 [PATCH 1/2] nvmem: core: add nvmem_cell_read_common Yangtao Li
2020-01-26 19:56 ` [PATCH 2/2] nvmem: core: add nvmem_cell_read_u64 Yangtao Li
2020-02-05 11:50 ` [PATCH 1/2] nvmem: core: add nvmem_cell_read_common Frank Lee
2020-02-17 10:47 ` Srinivas Kandagatla

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).