nvmem: core: allow to modify a cell before adding it
[platform/kernel/linux-starfive.git] / include / linux / nvmem-provider.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * nvmem framework provider.
4  *
5  * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6  * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7  */
8
9 #ifndef _LINUX_NVMEM_PROVIDER_H
10 #define _LINUX_NVMEM_PROVIDER_H
11
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/gpio/consumer.h>
15
16 struct nvmem_device;
17 typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
18                                 void *val, size_t bytes);
19 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
20                                  void *val, size_t bytes);
21 /* used for vendor specific post processing of cell data */
22 typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
23                                          unsigned int offset, void *buf, size_t bytes);
24
25 enum nvmem_type {
26         NVMEM_TYPE_UNKNOWN = 0,
27         NVMEM_TYPE_EEPROM,
28         NVMEM_TYPE_OTP,
29         NVMEM_TYPE_BATTERY_BACKED,
30         NVMEM_TYPE_FRAM,
31 };
32
33 #define NVMEM_DEVID_NONE        (-1)
34 #define NVMEM_DEVID_AUTO        (-2)
35
36 /**
37  * struct nvmem_keepout - NVMEM register keepout range.
38  *
39  * @start:      The first byte offset to avoid.
40  * @end:        One beyond the last byte offset to avoid.
41  * @value:      The byte to fill reads with for this region.
42  */
43 struct nvmem_keepout {
44         unsigned int start;
45         unsigned int end;
46         unsigned char value;
47 };
48
49 /**
50  * struct nvmem_cell_info - NVMEM cell description
51  * @name:       Name.
52  * @offset:     Offset within the NVMEM device.
53  * @bytes:      Length of the cell.
54  * @bit_offset: Bit offset if cell is smaller than a byte.
55  * @nbits:      Number of bits.
56  * @np:         Optional device_node pointer.
57  * @read_post_process:  Callback for optional post processing of cell data
58  *                      on reads.
59  */
60 struct nvmem_cell_info {
61         const char              *name;
62         unsigned int            offset;
63         unsigned int            bytes;
64         unsigned int            bit_offset;
65         unsigned int            nbits;
66         struct device_node      *np;
67         nvmem_cell_post_process_t read_post_process;
68 };
69
70 /**
71  * struct nvmem_config - NVMEM device configuration
72  *
73  * @dev:        Parent device.
74  * @name:       Optional name.
75  * @id:         Optional device ID used in full name. Ignored if name is NULL.
76  * @owner:      Pointer to exporter module. Used for refcounting.
77  * @cells:      Optional array of pre-defined NVMEM cells.
78  * @ncells:     Number of elements in cells.
79  * @keepout:    Optional array of keepout ranges (sorted ascending by start).
80  * @nkeepout:   Number of elements in the keepout array.
81  * @type:       Type of the nvmem storage
82  * @read_only:  Device is read-only.
83  * @root_only:  Device is accessibly to root only.
84  * @of_node:    If given, this will be used instead of the parent's of_node.
85  * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
86  * @reg_read:   Callback to read data.
87  * @reg_write:  Callback to write data.
88  * @cell_post_process:  Callback for vendor specific post processing of cell data
89  * @size:       Device size.
90  * @word_size:  Minimum read/write access granularity.
91  * @stride:     Minimum read/write access stride.
92  * @priv:       User context passed to read/write callbacks.
93  * @ignore_wp:  Write Protect pin is managed by the provider.
94  * @layout:     Fixed layout associated with this nvmem device.
95  *
96  * Note: A default "nvmem<id>" name will be assigned to the device if
97  * no name is specified in its configuration. In such case "<id>" is
98  * generated with ida_simple_get() and provided id field is ignored.
99  *
100  * Note: Specifying name and setting id to -1 implies a unique device
101  * whose name is provided as-is (kept unaltered).
102  */
103 struct nvmem_config {
104         struct device           *dev;
105         const char              *name;
106         int                     id;
107         struct module           *owner;
108         const struct nvmem_cell_info    *cells;
109         int                     ncells;
110         const struct nvmem_keepout *keepout;
111         unsigned int            nkeepout;
112         enum nvmem_type         type;
113         bool                    read_only;
114         bool                    root_only;
115         bool                    ignore_wp;
116         struct nvmem_layout     *layout;
117         struct device_node      *of_node;
118         bool                    no_of_node;
119         nvmem_reg_read_t        reg_read;
120         nvmem_reg_write_t       reg_write;
121         nvmem_cell_post_process_t cell_post_process;
122         int     size;
123         int     word_size;
124         int     stride;
125         void    *priv;
126         /* To be only used by old driver/misc/eeprom drivers */
127         bool                    compat;
128         struct device           *base_dev;
129 };
130
131 /**
132  * struct nvmem_cell_table - NVMEM cell definitions for given provider
133  *
134  * @nvmem_name:         Provider name.
135  * @cells:              Array of cell definitions.
136  * @ncells:             Number of cell definitions in the array.
137  * @node:               List node.
138  *
139  * This structure together with related helper functions is provided for users
140  * that don't can't access the nvmem provided structure but wish to register
141  * cell definitions for it e.g. board files registering an EEPROM device.
142  */
143 struct nvmem_cell_table {
144         const char              *nvmem_name;
145         const struct nvmem_cell_info    *cells;
146         size_t                  ncells;
147         struct list_head        node;
148 };
149
150 /**
151  * struct nvmem_layout - NVMEM layout definitions
152  *
153  * @name:               Layout name.
154  * @of_match_table:     Open firmware match table.
155  * @add_cells:          Will be called if a nvmem device is found which
156  *                      has this layout. The function will add layout
157  *                      specific cells with nvmem_add_one_cell().
158  * @fixup_cell_info:    Will be called before a cell is added. Can be
159  *                      used to modify the nvmem_cell_info.
160  * @owner:              Pointer to struct module.
161  * @node:               List node.
162  *
163  * A nvmem device can hold a well defined structure which can just be
164  * evaluated during runtime. For example a TLV list, or a list of "name=val"
165  * pairs. A nvmem layout can parse the nvmem device and add appropriate
166  * cells.
167  */
168 struct nvmem_layout {
169         const char *name;
170         const struct of_device_id *of_match_table;
171         int (*add_cells)(struct device *dev, struct nvmem_device *nvmem,
172                          struct nvmem_layout *layout);
173         void (*fixup_cell_info)(struct nvmem_device *nvmem,
174                                 struct nvmem_layout *layout,
175                                 struct nvmem_cell_info *cell);
176
177         /* private */
178         struct module *owner;
179         struct list_head node;
180 };
181
182 #if IS_ENABLED(CONFIG_NVMEM)
183
184 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
185 void nvmem_unregister(struct nvmem_device *nvmem);
186
187 struct nvmem_device *devm_nvmem_register(struct device *dev,
188                                          const struct nvmem_config *cfg);
189
190 void nvmem_add_cell_table(struct nvmem_cell_table *table);
191 void nvmem_del_cell_table(struct nvmem_cell_table *table);
192
193 int nvmem_add_one_cell(struct nvmem_device *nvmem,
194                        const struct nvmem_cell_info *info);
195
196 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner);
197 #define nvmem_layout_register(layout) \
198         __nvmem_layout_register(layout, THIS_MODULE)
199 void nvmem_layout_unregister(struct nvmem_layout *layout);
200
201 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
202                                         struct nvmem_layout *layout);
203
204 #else
205
206 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
207 {
208         return ERR_PTR(-EOPNOTSUPP);
209 }
210
211 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
212
213 static inline struct nvmem_device *
214 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
215 {
216         return nvmem_register(c);
217 }
218
219 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
220 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
221 static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
222                                      const struct nvmem_cell_info *info)
223 {
224         return -EOPNOTSUPP;
225 }
226
227 static inline int nvmem_layout_register(struct nvmem_layout *layout)
228 {
229         return -EOPNOTSUPP;
230 }
231
232 static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
233
234 static inline const void *
235 nvmem_layout_get_match_data(struct nvmem_device *nvmem,
236                             struct nvmem_layout *layout)
237 {
238         return NULL;
239 }
240
241 #endif /* CONFIG_NVMEM */
242 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */