1 .. SPDX-License-Identifier: GPL-2.0
7 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
9 This document explains the NVMEM Framework along with the APIs provided,
14 *NVMEM* is the abbreviation for Non Volatile Memory layer. It is used to
15 retrieve configuration of SOC or Device specific data from non volatile
16 memories like eeprom, efuses and so on.
18 Before this framework existed, NVMEM drivers like eeprom were stored in
19 drivers/misc, where they all had to duplicate pretty much the same code to
20 register a sysfs file, allow in-kernel users to access the content of the
21 devices they were driving, etc.
23 This was also a problem as far as other in-kernel users were involved, since
24 the solutions used were pretty much different from one driver to another, there
25 was a rather big abstraction leak.
27 This framework aims at solve these problems. It also introduces DT
28 representation for consumer devices to go get the data they require (MAC
29 Addresses, SoC/Revision ID, part numbers, and so on) from the NVMEMs.
34 NVMEM provider refers to an entity that implements methods to initialize, read
35 and write the non-volatile memory.
37 2. Registering/Unregistering the NVMEM provider
38 ===============================================
40 A NVMEM provider can register with NVMEM core by supplying relevant
41 nvmem configuration to nvmem_register(), on success core would return a valid
44 nvmem_unregister(nvmem) is used to unregister a previously registered provider.
46 For example, a simple nvram case::
48 static int brcm_nvram_probe(struct platform_device *pdev)
50 struct nvmem_config config = {
52 .reg_read = brcm_nvram_read,
55 config.dev = &pdev->dev;
57 config.size = resource_size(res);
59 devm_nvmem_register(&config);
62 Users of board files can define and register nvmem cells using the
63 nvmem_cell_table struct::
65 static struct nvmem_cell_info foo_nvmem_cells[] = {
73 static struct nvmem_cell_table foo_nvmem_cell_table = {
74 .nvmem_name = "i2c-eeprom",
75 .cells = foo_nvmem_cells,
76 .ncells = ARRAY_SIZE(foo_nvmem_cells),
79 nvmem_add_cell_table(&foo_nvmem_cell_table);
81 Additionally it is possible to create nvmem cell lookup entries and register
82 them with the nvmem framework from machine code as shown in the example below::
84 static struct nvmem_cell_lookup foo_nvmem_lookup = {
85 .nvmem_name = "i2c-eeprom",
86 .cell_name = "macaddr",
87 .dev_id = "foo_mac.0",
88 .con_id = "mac-address",
91 nvmem_add_cell_lookups(&foo_nvmem_lookup, 1);
96 NVMEM consumers are the entities which make use of the NVMEM provider to
97 read from and to NVMEM.
99 3. NVMEM cell based consumer APIs
100 =================================
102 NVMEM cells are the data entries/fields in the NVMEM.
103 The NVMEM framework provides 3 APIs to read/write NVMEM cells::
105 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
106 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name);
108 void nvmem_cell_put(struct nvmem_cell *cell);
109 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
111 void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len);
112 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len);
114 `*nvmem_cell_get()` apis will get a reference to nvmem cell for a given id,
115 and nvmem_cell_read/write() can then read or write to the cell.
116 Once the usage of the cell is finished the consumer should call
117 `*nvmem_cell_put()` to free all the allocation memory for the cell.
119 4. Direct NVMEM device based consumer APIs
120 ==========================================
122 In some instances it is necessary to directly read/write the NVMEM.
123 To facilitate such consumers NVMEM framework provides below apis::
125 struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
126 struct nvmem_device *devm_nvmem_device_get(struct device *dev,
128 struct nvmem_device *nvmem_device_find(void *data,
129 int (*match)(struct device *dev, const void *data));
130 void nvmem_device_put(struct nvmem_device *nvmem);
131 int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
132 size_t bytes, void *buf);
133 int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
134 size_t bytes, void *buf);
135 int nvmem_device_cell_read(struct nvmem_device *nvmem,
136 struct nvmem_cell_info *info, void *buf);
137 int nvmem_device_cell_write(struct nvmem_device *nvmem,
138 struct nvmem_cell_info *info, void *buf);
140 Before the consumers can read/write NVMEM directly, it should get hold
141 of nvmem_controller from one of the `*nvmem_device_get()` api.
143 The difference between these apis and cell based apis is that these apis always
144 take nvmem_device as parameter.
146 5. Releasing a reference to the NVMEM
147 =====================================
149 When a consumer no longer needs the NVMEM, it has to release the reference
150 to the NVMEM it has obtained using the APIs mentioned in the above section.
151 The NVMEM framework provides 2 APIs to release a reference to the NVMEM::
153 void nvmem_cell_put(struct nvmem_cell *cell);
154 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
155 void nvmem_device_put(struct nvmem_device *nvmem);
156 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
158 Both these APIs are used to release a reference to the NVMEM and
159 devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated
165 6. Userspace binary interface
166 ==============================
168 Userspace can read/write the raw NVMEM file located at::
170 /sys/bus/nvmem/devices/*/nvmem
174 hexdump /sys/bus/nvmem/devices/qfprom0/nvmem
176 0000000 0000 0000 0000 0000 0000 0000 0000 0000
178 00000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00
179 0000000 0000 0000 0000 0000 0000 0000 0000 0000
184 7. DeviceTree Binding
185 =====================
187 See Documentation/devicetree/bindings/nvmem/nvmem.txt