kernel: backport NVMEM patches queued for the v6.4
[openwrt/staging/hauke.git] / target / linux / generic / backport-5.10 / 814-v6.4-0005-nvmem-core-add-per-cell-post-processing.patch
1 From 345ec382cd4b736c36e01f155d08c913b225b736 Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Tue, 4 Apr 2023 18:21:24 +0100
4 Subject: [PATCH] nvmem: core: add per-cell post processing
5
6 Instead of relying on the name the consumer is using for the cell, like
7 it is done for the nvmem .cell_post_process configuration parameter,
8 provide a per-cell post processing hook. This can then be populated by
9 the NVMEM provider (or the NVMEM layout) when adding the cell.
10
11 Signed-off-by: Michael Walle <michael@walle.cc>
12 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
13 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
14 Link: https://lore.kernel.org/r/20230404172148.82422-17-srinivas.kandagatla@linaro.org
15 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
16 ---
17 drivers/nvmem/core.c | 17 +++++++++++++++++
18 include/linux/nvmem-provider.h | 3 +++
19 2 files changed, 20 insertions(+)
20
21 --- a/drivers/nvmem/core.c
22 +++ b/drivers/nvmem/core.c
23 @@ -54,6 +54,7 @@ struct nvmem_cell_entry {
24 int bytes;
25 int bit_offset;
26 int nbits;
27 + nvmem_cell_post_process_t read_post_process;
28 struct device_node *np;
29 struct nvmem_device *nvmem;
30 struct list_head node;
31 @@ -470,6 +471,7 @@ static int nvmem_cell_info_to_nvmem_cell
32 cell->offset = info->offset;
33 cell->bytes = info->bytes;
34 cell->name = info->name;
35 + cell->read_post_process = info->read_post_process;
36
37 cell->bit_offset = info->bit_offset;
38 cell->nbits = info->nbits;
39 @@ -1563,6 +1565,13 @@ static int __nvmem_cell_read(struct nvme
40 if (cell->bit_offset || cell->nbits)
41 nvmem_shift_read_buffer_in_place(cell, buf);
42
43 + if (cell->read_post_process) {
44 + rc = cell->read_post_process(nvmem->priv, id, index,
45 + cell->offset, buf, cell->bytes);
46 + if (rc)
47 + return rc;
48 + }
49 +
50 if (nvmem->cell_post_process) {
51 rc = nvmem->cell_post_process(nvmem->priv, id, index,
52 cell->offset, buf, cell->bytes);
53 @@ -1671,6 +1680,14 @@ static int __nvmem_cell_entry_write(stru
54 (cell->bit_offset == 0 && len != cell->bytes))
55 return -EINVAL;
56
57 + /*
58 + * Any cells which have a read_post_process hook are read-only because
59 + * we cannot reverse the operation and it might affect other cells,
60 + * too.
61 + */
62 + if (cell->read_post_process)
63 + return -EINVAL;
64 +
65 if (cell->bit_offset || cell->nbits) {
66 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
67 if (IS_ERR(buf))
68 --- a/include/linux/nvmem-provider.h
69 +++ b/include/linux/nvmem-provider.h
70 @@ -54,6 +54,8 @@ struct nvmem_keepout {
71 * @bit_offset: Bit offset if cell is smaller than a byte.
72 * @nbits: Number of bits.
73 * @np: Optional device_node pointer.
74 + * @read_post_process: Callback for optional post processing of cell data
75 + * on reads.
76 */
77 struct nvmem_cell_info {
78 const char *name;
79 @@ -62,6 +64,7 @@ struct nvmem_cell_info {
80 unsigned int bit_offset;
81 unsigned int nbits;
82 struct device_node *np;
83 + nvmem_cell_post_process_t read_post_process;
84 };
85
86 /**