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 */
58 struct nvmem_cell_info {
59 const char *name;
60 unsigned int offset;
61 unsigned int bytes;
62 unsigned int bit_offset;
63 unsigned int nbits;
64 struct device_node *np;
65 };
66
67 /**
68 * struct nvmem_config - NVMEM device configuration
69 *
70 * @dev: Parent device.
71 * @name: Optional name.
72 * @id: Optional device ID used in full name. Ignored if name is NULL.
73 * @owner: Pointer to exporter module. Used for refcounting.
74 * @cells: Optional array of pre-defined NVMEM cells.
75 * @ncells: Number of elements in cells.
76 * @keepout: Optional array of keepout ranges (sorted ascending by start).
77 * @nkeepout: Number of elements in the keepout array.
78 * @type: Type of the nvmem storage
79 * @read_only: Device is read-only.
80 * @root_only: Device is accessibly to root only.
81 * @of_node: If given, this will be used instead of the parent's of_node.
82 * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
83 * @reg_read: Callback to read data.
84 * @reg_write: Callback to write data.
85 * @cell_post_process: Callback for vendor specific post processing of cell data
86 * @size: Device size.
87 * @word_size: Minimum read/write access granularity.
88 * @stride: Minimum read/write access stride.
89 * @priv: User context passed to read/write callbacks.
90 * @ignore_wp: Write Protect pin is managed by the provider.
91 *
92 * Note: A default "nvmem<id>" name will be assigned to the device if
93 * no name is specified in its configuration. In such case "<id>" is
94 * generated with ida_simple_get() and provided id field is ignored.
95 *
96 * Note: Specifying name and setting id to -1 implies a unique device
97 * whose name is provided as-is (kept unaltered).
98 */
99 struct nvmem_config {
100 struct device *dev;
101 const char *name;
102 int id;
103 struct module *owner;
104 const struct nvmem_cell_info *cells;
105 int ncells;
106 const struct nvmem_keepout *keepout;
107 unsigned int nkeepout;
108 enum nvmem_type type;
109 bool read_only;
110 bool root_only;
111 bool ignore_wp;
112 struct device_node *of_node;
113 bool no_of_node;
114 nvmem_reg_read_t reg_read;
115 nvmem_reg_write_t reg_write;
116 nvmem_cell_post_process_t cell_post_process;
117 int size;
118 int word_size;
119 int stride;
120 void *priv;
121 /* To be only used by old driver/misc/eeprom drivers */
122 bool compat;
123 struct device *base_dev;
124 };
125
126 /**
127 * struct nvmem_cell_table - NVMEM cell definitions for given provider
128 *
129 * @nvmem_name: Provider name.
130 * @cells: Array of cell definitions.
131 * @ncells: Number of cell definitions in the array.
132 * @node: List node.
133 *
134 * This structure together with related helper functions is provided for users
135 * that don't can't access the nvmem provided structure but wish to register
136 * cell definitions for it e.g. board files registering an EEPROM device.
137 */
138 struct nvmem_cell_table {
139 const char *nvmem_name;
140 const struct nvmem_cell_info *cells;
141 size_t ncells;
142 struct list_head node;
143 };
144
145 #if IS_ENABLED(CONFIG_NVMEM)
146
147 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
148 void nvmem_unregister(struct nvmem_device *nvmem);
149
150 struct nvmem_device *devm_nvmem_register(struct device *dev,
151 const struct nvmem_config *cfg);
152
153 void nvmem_add_cell_table(struct nvmem_cell_table *table);
154 void nvmem_del_cell_table(struct nvmem_cell_table *table);
155
156 int nvmem_add_one_cell(struct nvmem_device *nvmem,
157 const struct nvmem_cell_info *info);
158
159 #else
160
nvmem_register(const struct nvmem_config * c)161 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
162 {
163 return ERR_PTR(-EOPNOTSUPP);
164 }
165
nvmem_unregister(struct nvmem_device * nvmem)166 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
167
168 static inline struct nvmem_device *
devm_nvmem_register(struct device * dev,const struct nvmem_config * c)169 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
170 {
171 return nvmem_register(c);
172 }
173
nvmem_add_cell_table(struct nvmem_cell_table * table)174 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
nvmem_del_cell_table(struct nvmem_cell_table * table)175 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
nvmem_add_one_cell(struct nvmem_device * nvmem,const struct nvmem_cell_info * info)176 static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
177 const struct nvmem_cell_info *info)
178 {
179 return -EOPNOTSUPP;
180 }
181
182 #endif /* CONFIG_NVMEM */
183 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */
184