1 /* SPDX-License-Identifier: GPL-2.0 */
3 * nvmem framework provider.
5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
9 #ifndef _LINUX_NVMEM_PROVIDER_H
10 #define _LINUX_NVMEM_PROVIDER_H
12 #include <linux/device.h>
13 #include <linux/device/driver.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/gpio/consumer.h>
19 typedef int (*nvmem_reg_read_t
)(void *priv
, unsigned int offset
,
20 void *val
, size_t bytes
);
21 typedef int (*nvmem_reg_write_t
)(void *priv
, unsigned int offset
,
22 void *val
, size_t bytes
);
23 /* used for vendor specific post processing of cell data */
24 typedef int (*nvmem_cell_post_process_t
)(void *priv
, const char *id
, int index
,
25 unsigned int offset
, void *buf
,
29 NVMEM_TYPE_UNKNOWN
= 0,
32 NVMEM_TYPE_BATTERY_BACKED
,
36 #define NVMEM_DEVID_NONE (-1)
37 #define NVMEM_DEVID_AUTO (-2)
40 * struct nvmem_keepout - NVMEM register keepout range.
42 * @start: The first byte offset to avoid.
43 * @end: One beyond the last byte offset to avoid.
44 * @value: The byte to fill reads with for this region.
46 struct nvmem_keepout
{
53 * struct nvmem_cell_info - NVMEM cell description
55 * @offset: Offset within the NVMEM device.
56 * @raw_len: Length of raw data (without post processing).
57 * @bytes: Length of the cell.
58 * @bit_offset: Bit offset if cell is smaller than a byte.
59 * @nbits: Number of bits.
60 * @np: Optional device_node pointer.
61 * @read_post_process: Callback for optional post processing of cell data
63 * @priv: Opaque data passed to the read_post_process hook.
65 struct nvmem_cell_info
{
70 unsigned int bit_offset
;
72 struct device_node
*np
;
73 nvmem_cell_post_process_t read_post_process
;
78 * struct nvmem_config - NVMEM device configuration
80 * @dev: Parent device.
81 * @name: Optional name.
82 * @id: Optional device ID used in full name. Ignored if name is NULL.
83 * @owner: Pointer to exporter module. Used for refcounting.
84 * @cells: Optional array of pre-defined NVMEM cells.
85 * @ncells: Number of elements in cells.
86 * @add_legacy_fixed_of_cells: Read fixed NVMEM cells from old OF syntax.
87 * @fixup_dt_cell_info: Will be called before a cell is added. Can be
88 * used to modify the nvmem_cell_info.
89 * @keepout: Optional array of keepout ranges (sorted ascending by start).
90 * @nkeepout: Number of elements in the keepout array.
91 * @type: Type of the nvmem storage
92 * @read_only: Device is read-only.
93 * @root_only: Device is accessibly to root only.
94 * @of_node: If given, this will be used instead of the parent's of_node.
95 * @reg_read: Callback to read data.
96 * @reg_write: Callback to write data.
98 * @word_size: Minimum read/write access granularity.
99 * @stride: Minimum read/write access stride.
100 * @priv: User context passed to read/write callbacks.
101 * @ignore_wp: Write Protect pin is managed by the provider.
102 * @layout: Fixed layout associated with this nvmem device.
104 * Note: A default "nvmem<id>" name will be assigned to the device if
105 * no name is specified in its configuration. In such case "<id>" is
106 * generated with ida_simple_get() and provided id field is ignored.
108 * Note: Specifying name and setting id to -1 implies a unique device
109 * whose name is provided as-is (kept unaltered).
111 struct nvmem_config
{
115 struct module
*owner
;
116 const struct nvmem_cell_info
*cells
;
118 bool add_legacy_fixed_of_cells
;
119 void (*fixup_dt_cell_info
)(struct nvmem_device
*nvmem
,
120 struct nvmem_cell_info
*cell
);
121 const struct nvmem_keepout
*keepout
;
122 unsigned int nkeepout
;
123 enum nvmem_type type
;
127 struct nvmem_layout
*layout
;
128 struct device_node
*of_node
;
129 nvmem_reg_read_t reg_read
;
130 nvmem_reg_write_t reg_write
;
135 /* To be only used by old driver/misc/eeprom drivers */
137 struct device
*base_dev
;
141 * struct nvmem_cell_table - NVMEM cell definitions for given provider
143 * @nvmem_name: Provider name.
144 * @cells: Array of cell definitions.
145 * @ncells: Number of cell definitions in the array.
148 * This structure together with related helper functions is provided for users
149 * that don't can't access the nvmem provided structure but wish to register
150 * cell definitions for it e.g. board files registering an EEPROM device.
152 struct nvmem_cell_table
{
153 const char *nvmem_name
;
154 const struct nvmem_cell_info
*cells
;
156 struct list_head node
;
160 * struct nvmem_layout - NVMEM layout definitions
162 * @dev: Device-model layout device.
163 * @nvmem: The underlying NVMEM device
164 * @add_cells: Will be called if a nvmem device is found which
165 * has this layout. The function will add layout
166 * specific cells with nvmem_add_one_cell().
168 * A nvmem device can hold a well defined structure which can just be
169 * evaluated during runtime. For example a TLV list, or a list of "name=val"
170 * pairs. A nvmem layout can parse the nvmem device and add appropriate
173 struct nvmem_layout
{
175 struct nvmem_device
*nvmem
;
176 int (*add_cells
)(struct nvmem_layout
*layout
);
179 struct nvmem_layout_driver
{
180 struct device_driver driver
;
181 int (*probe
)(struct nvmem_layout
*layout
);
182 void (*remove
)(struct nvmem_layout
*layout
);
185 #if IS_ENABLED(CONFIG_NVMEM)
187 struct nvmem_device
*nvmem_register(const struct nvmem_config
*cfg
);
188 void nvmem_unregister(struct nvmem_device
*nvmem
);
190 struct nvmem_device
*devm_nvmem_register(struct device
*dev
,
191 const struct nvmem_config
*cfg
);
193 void nvmem_add_cell_table(struct nvmem_cell_table
*table
);
194 void nvmem_del_cell_table(struct nvmem_cell_table
*table
);
196 int nvmem_add_one_cell(struct nvmem_device
*nvmem
,
197 const struct nvmem_cell_info
*info
);
199 int nvmem_layout_register(struct nvmem_layout
*layout
);
200 void nvmem_layout_unregister(struct nvmem_layout
*layout
);
202 #define nvmem_layout_driver_register(drv) \
203 __nvmem_layout_driver_register(drv, THIS_MODULE)
204 int __nvmem_layout_driver_register(struct nvmem_layout_driver
*drv
,
205 struct module
*owner
);
206 void nvmem_layout_driver_unregister(struct nvmem_layout_driver
*drv
);
207 #define module_nvmem_layout_driver(__nvmem_layout_driver) \
208 module_driver(__nvmem_layout_driver, nvmem_layout_driver_register, \
209 nvmem_layout_driver_unregister)
213 static inline struct nvmem_device
*nvmem_register(const struct nvmem_config
*c
)
215 return ERR_PTR(-EOPNOTSUPP
);
218 static inline void nvmem_unregister(struct nvmem_device
*nvmem
) {}
220 static inline struct nvmem_device
*
221 devm_nvmem_register(struct device
*dev
, const struct nvmem_config
*c
)
223 return nvmem_register(c
);
226 static inline void nvmem_add_cell_table(struct nvmem_cell_table
*table
) {}
227 static inline void nvmem_del_cell_table(struct nvmem_cell_table
*table
) {}
228 static inline int nvmem_add_one_cell(struct nvmem_device
*nvmem
,
229 const struct nvmem_cell_info
*info
)
234 static inline int nvmem_layout_register(struct nvmem_layout
*layout
)
239 static inline void nvmem_layout_unregister(struct nvmem_layout
*layout
) {}
241 #endif /* CONFIG_NVMEM */
243 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
246 * of_nvmem_layout_get_container() - Get OF node of layout container
248 * @nvmem: nvmem device
250 * Return: a node pointer with refcount incremented or NULL if no
251 * container exists. Use of_node_put() on it when done.
253 struct device_node
*of_nvmem_layout_get_container(struct nvmem_device
*nvmem
);
255 #else /* CONFIG_NVMEM && CONFIG_OF */
257 static inline struct device_node
*of_nvmem_layout_get_container(struct nvmem_device
*nvmem
)
262 #endif /* CONFIG_NVMEM && CONFIG_OF */
264 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */