diff mbox series

[v1,07/14] nvmem: core: add per-cell post processing

Message ID 20220825214423.903672-8-michael@walle.cc (mailing list archive)
State Not Applicable
Headers show
Series nvmem: core: introduce NVMEM layouts | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Michael Walle Aug. 25, 2022, 9:44 p.m. UTC
Instead of relying on the name the consumer is using for the cell, like
it is done for the nvmem .cell_post_process configuration parameter,
provide a per-cell post processing hook. This can then be populated by
the NVMEM provider (or the NVMEM layout) when adding the cell.

Signed-off-by: Michael Walle <michael@walle.cc>
---
 drivers/nvmem/core.c           | 16 ++++++++++++++++
 include/linux/nvmem-consumer.h |  5 +++++
 2 files changed, 21 insertions(+)

Comments

Srinivas Kandagatla Aug. 30, 2022, 1:37 p.m. UTC | #1
On 25/08/2022 22:44, Michael Walle wrote:
> Instead of relying on the name the consumer is using for the cell, like
> it is done for the nvmem .cell_post_process configuration parameter,
> provide a per-cell post processing hook. This can then be populated by
> the NVMEM provider (or the NVMEM layout) when adding the cell.
> 
> Signed-off-by: Michael Walle <michael@walle.cc>
> ---
>   drivers/nvmem/core.c           | 16 ++++++++++++++++
>   include/linux/nvmem-consumer.h |  5 +++++
>   2 files changed, 21 insertions(+)
> 
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 5357fc378700..cbfbe6264e6c 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -52,6 +52,7 @@ struct nvmem_cell_entry {
>   	int			bytes;
>   	int			bit_offset;
>   	int			nbits;
> +	nvmem_cell_post_process_t post_process;


two post_processing callbacks for cells is confusing tbh, we could 
totally move to use of cell->post_process.

one idea is to point cell->post_process to nvmem->cell_post_process 
during cell creation time which should clean this up a bit.

Other option is to move to using layouts for every thing.

prefixing post_process with read should also make it explicit that this 
callback is very specific to reads only.


>   	struct device_node	*np;
>   	struct nvmem_device	*nvmem;
>   	struct list_head	node;
> @@ -468,6 +469,7 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
>   	cell->offset = info->offset;
>   	cell->bytes = info->bytes;
>   	cell->name = info->name;
> +	cell->post_process = info->post_process;
>   
>   	cell->bit_offset = info->bit_offset;
>   	cell->nbits = info->nbits;
> @@ -1500,6 +1502,13 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
>   	if (cell->bit_offset || cell->nbits)
>   		nvmem_shift_read_buffer_in_place(cell, buf);
>   
> +	if (cell->post_process) {
> +		rc = cell->post_process(nvmem->priv, id, index,
> +					cell->offset, buf, cell->bytes);
> +		if (rc)
> +			return rc;
> +	}
> +
>   	if (nvmem->cell_post_process) {
>   		rc = nvmem->cell_post_process(nvmem->priv, id, index,
>   					      cell->offset, buf, cell->bytes);
> @@ -1608,6 +1617,13 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
>   	    (cell->bit_offset == 0 && len != cell->bytes))
>   		return -EINVAL;
>   
> +	/*
> +	 * Any cells which have a post_process hook are read-only because we
> +	 * cannot reverse the operation and it might affect other cells, too.
> +	 */
> +	if (cell->post_process)
> +		return -EINVAL;

Post process was always implicitly for reads only, this check should 
also tie the loose ends of cell_post_processing callback.


--srini
> +
>   	if (cell->bit_offset || cell->nbits) {
>   		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
>   		if (IS_ERR(buf))
> diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
> index 980f9c9ac0bc..761b8ef78adc 100644
> --- a/include/linux/nvmem-consumer.h
> +++ b/include/linux/nvmem-consumer.h
> @@ -19,6 +19,10 @@ struct device_node;
>   struct nvmem_cell;
>   struct nvmem_device;
>   
> +/* duplicated from nvmem-provider.h */
> +typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
> +					 unsigned int offset, void *buf, size_t bytes);
> +
>   struct nvmem_cell_info {
>   	const char		*name;
>   	unsigned int		offset;
> @@ -26,6 +30,7 @@ struct nvmem_cell_info {
>   	unsigned int		bit_offset;
>   	unsigned int		nbits;
>   	struct device_node	*np;
> +	nvmem_cell_post_process_t post_process;
>   };
>   
>   /**
Michael Walle Aug. 30, 2022, 2:20 p.m. UTC | #2
Hi,

Am 2022-08-30 15:37, schrieb Srinivas Kandagatla:
> On 25/08/2022 22:44, Michael Walle wrote:
>> Instead of relying on the name the consumer is using for the cell, 
>> like
>> it is done for the nvmem .cell_post_process configuration parameter,
>> provide a per-cell post processing hook. This can then be populated by
>> the NVMEM provider (or the NVMEM layout) when adding the cell.
>> 
>> Signed-off-by: Michael Walle <michael@walle.cc>
>> ---
>>   drivers/nvmem/core.c           | 16 ++++++++++++++++
>>   include/linux/nvmem-consumer.h |  5 +++++
>>   2 files changed, 21 insertions(+)
>> 
>> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
>> index 5357fc378700..cbfbe6264e6c 100644
>> --- a/drivers/nvmem/core.c
>> +++ b/drivers/nvmem/core.c
>> @@ -52,6 +52,7 @@ struct nvmem_cell_entry {
>>   	int			bytes;
>>   	int			bit_offset;
>>   	int			nbits;
>> +	nvmem_cell_post_process_t post_process;
> 
> 
> two post_processing callbacks for cells is confusing tbh, we could
> totally move to use of cell->post_process.
> 
> one idea is to point cell->post_process to nvmem->cell_post_process
> during cell creation time which should clean this up a bit.

You'll then trigger the read-only check below for all the cells
if nvmem->cell_post_process is set.

> Other option is to move to using layouts for every thing.

As mentioned in a previous reply, I can't see how it could be
achieved. The problem here is that:
  (1) the layout isn't creating the cells, the OF parser is
  (2) even if we would create the cells, we wouldn't know
      which cell needs the post_process. So we are back to
      the situation above, were we have to add it to all
      the cells, making them read-only. [We depend on the
      name of the nvmem-consumer to apply the hook.

> prefixing post_process with read should also make it explicit that
> this callback is very specific to reads only.

good idea.

-michael

>>   	struct device_node	*np;
>>   	struct nvmem_device	*nvmem;
>>   	struct list_head	node;
>> @@ -468,6 +469,7 @@ static int 
>> nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
>>   	cell->offset = info->offset;
>>   	cell->bytes = info->bytes;
>>   	cell->name = info->name;
>> +	cell->post_process = info->post_process;
>>     	cell->bit_offset = info->bit_offset;
>>   	cell->nbits = info->nbits;
>> @@ -1500,6 +1502,13 @@ static int __nvmem_cell_read(struct 
>> nvmem_device *nvmem,
>>   	if (cell->bit_offset || cell->nbits)
>>   		nvmem_shift_read_buffer_in_place(cell, buf);
>>   +	if (cell->post_process) {
>> +		rc = cell->post_process(nvmem->priv, id, index,
>> +					cell->offset, buf, cell->bytes);
>> +		if (rc)
>> +			return rc;
>> +	}
>> +
>>   	if (nvmem->cell_post_process) {
>>   		rc = nvmem->cell_post_process(nvmem->priv, id, index,
>>   					      cell->offset, buf, cell->bytes);
>> @@ -1608,6 +1617,13 @@ static int __nvmem_cell_entry_write(struct 
>> nvmem_cell_entry *cell, void *buf, si
>>   	    (cell->bit_offset == 0 && len != cell->bytes))
>>   		return -EINVAL;
>>   +	/*
>> +	 * Any cells which have a post_process hook are read-only because we
>> +	 * cannot reverse the operation and it might affect other cells, 
>> too.
>> +	 */
>> +	if (cell->post_process)
>> +		return -EINVAL;
> 
> Post process was always implicitly for reads only, this check should
> also tie the loose ends of cell_post_processing callback.
> 
> 
> --srini
>> +
>>   	if (cell->bit_offset || cell->nbits) {
>>   		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
>>   		if (IS_ERR(buf))
>> diff --git a/include/linux/nvmem-consumer.h 
>> b/include/linux/nvmem-consumer.h
>> index 980f9c9ac0bc..761b8ef78adc 100644
>> --- a/include/linux/nvmem-consumer.h
>> +++ b/include/linux/nvmem-consumer.h
>> @@ -19,6 +19,10 @@ struct device_node;
>>   struct nvmem_cell;
>>   struct nvmem_device;
>>   +/* duplicated from nvmem-provider.h */
>> +typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, 
>> int index,
>> +					 unsigned int offset, void *buf, size_t bytes);
>> +
>>   struct nvmem_cell_info {
>>   	const char		*name;
>>   	unsigned int		offset;
>> @@ -26,6 +30,7 @@ struct nvmem_cell_info {
>>   	unsigned int		bit_offset;
>>   	unsigned int		nbits;
>>   	struct device_node	*np;
>> +	nvmem_cell_post_process_t post_process;
>>   };
>>     /**
diff mbox series

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 5357fc378700..cbfbe6264e6c 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -52,6 +52,7 @@  struct nvmem_cell_entry {
 	int			bytes;
 	int			bit_offset;
 	int			nbits;
+	nvmem_cell_post_process_t post_process;
 	struct device_node	*np;
 	struct nvmem_device	*nvmem;
 	struct list_head	node;
@@ -468,6 +469,7 @@  static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
 	cell->offset = info->offset;
 	cell->bytes = info->bytes;
 	cell->name = info->name;
+	cell->post_process = info->post_process;
 
 	cell->bit_offset = info->bit_offset;
 	cell->nbits = info->nbits;
@@ -1500,6 +1502,13 @@  static int __nvmem_cell_read(struct nvmem_device *nvmem,
 	if (cell->bit_offset || cell->nbits)
 		nvmem_shift_read_buffer_in_place(cell, buf);
 
+	if (cell->post_process) {
+		rc = cell->post_process(nvmem->priv, id, index,
+					cell->offset, buf, cell->bytes);
+		if (rc)
+			return rc;
+	}
+
 	if (nvmem->cell_post_process) {
 		rc = nvmem->cell_post_process(nvmem->priv, id, index,
 					      cell->offset, buf, cell->bytes);
@@ -1608,6 +1617,13 @@  static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
 	    (cell->bit_offset == 0 && len != cell->bytes))
 		return -EINVAL;
 
+	/*
+	 * Any cells which have a post_process hook are read-only because we
+	 * cannot reverse the operation and it might affect other cells, too.
+	 */
+	if (cell->post_process)
+		return -EINVAL;
+
 	if (cell->bit_offset || cell->nbits) {
 		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
 		if (IS_ERR(buf))
diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
index 980f9c9ac0bc..761b8ef78adc 100644
--- a/include/linux/nvmem-consumer.h
+++ b/include/linux/nvmem-consumer.h
@@ -19,6 +19,10 @@  struct device_node;
 struct nvmem_cell;
 struct nvmem_device;
 
+/* duplicated from nvmem-provider.h */
+typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
+					 unsigned int offset, void *buf, size_t bytes);
+
 struct nvmem_cell_info {
 	const char		*name;
 	unsigned int		offset;
@@ -26,6 +30,7 @@  struct nvmem_cell_info {
 	unsigned int		bit_offset;
 	unsigned int		nbits;
 	struct device_node	*np;
+	nvmem_cell_post_process_t post_process;
 };
 
 /**