blob: fc2fe4b186552448113759a87de993e4d174cddb [file] [log] [blame]
Srinivas Kandagatla354ebb52015-07-27 12:14:14 +01001 NVMEM SUBSYSTEM
2 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
3
4This document explains the NVMEM Framework along with the APIs provided,
5and how to use it.
6
71. Introduction
8===============
9*NVMEM* is the abbreviation for Non Volatile Memory layer. It is used to
10retrieve configuration of SOC or Device specific data from non volatile
11memories like eeprom, efuses and so on.
12
13Before this framework existed, NVMEM drivers like eeprom were stored in
14drivers/misc, where they all had to duplicate pretty much the same code to
15register a sysfs file, allow in-kernel users to access the content of the
16devices they were driving, etc.
17
18This was also a problem as far as other in-kernel users were involved, since
19the solutions used were pretty much different from one driver to another, there
20was a rather big abstraction leak.
21
22This framework aims at solve these problems. It also introduces DT
23representation for consumer devices to go get the data they require (MAC
24Addresses, SoC/Revision ID, part numbers, and so on) from the NVMEMs. This
25framework is based on regmap, so that most of the abstraction available in
26regmap can be reused, across multiple types of buses.
27
28NVMEM Providers
29+++++++++++++++
30
31NVMEM provider refers to an entity that implements methods to initialize, read
32and write the non-volatile memory.
33
342. Registering/Unregistering the NVMEM provider
35===============================================
36
37A NVMEM provider can register with NVMEM core by supplying relevant
38nvmem configuration to nvmem_register(), on success core would return a valid
39nvmem_device pointer.
40
41nvmem_unregister(nvmem) is used to unregister a previously registered provider.
42
43For example, a simple qfprom case:
44
45static struct nvmem_config econfig = {
46 .name = "qfprom",
47 .owner = THIS_MODULE,
48};
49
50static int qfprom_probe(struct platform_device *pdev)
51{
52 ...
53 econfig.dev = &pdev->dev;
54 nvmem = nvmem_register(&econfig);
55 ...
56}
57
58It is mandatory that the NVMEM provider has a regmap associated with its
59struct device. Failure to do would return error code from nvmem_register().
60
Bartosz Golaszewski4903d192018-09-21 06:40:18 -070061Users of board files can define and register nvmem cells using the
62nvmem_cell_table struct:
63
64static struct nvmem_cell_info foo_nvmem_cells[] = {
65 {
66 .name = "macaddr",
67 .offset = 0x7f00,
68 .bytes = ETH_ALEN,
69 }
70};
71
72static struct nvmem_cell_table foo_nvmem_cell_table = {
73 .nvmem_name = "i2c-eeprom",
74 .cells = foo_nvmem_cells,
75 .ncells = ARRAY_SIZE(foo_nvmem_cells),
76};
77
78nvmem_add_cell_table(&foo_nvmem_cell_table);
79
80Additionally it is possible to create nvmem cell lookup entries and register
81them with the nvmem framework from machine code as shown in the example below:
82
83static struct nvmem_cell_lookup foo_nvmem_lookup = {
84 .nvmem_name = "i2c-eeprom",
85 .cell_name = "macaddr",
86 .dev_id = "foo_mac.0",
87 .con_id = "mac-address",
88};
89
90nvmem_add_cell_lookups(&foo_nvmem_lookup, 1);
91
Srinivas Kandagatla354ebb52015-07-27 12:14:14 +010092NVMEM Consumers
93+++++++++++++++
94
95NVMEM consumers are the entities which make use of the NVMEM provider to
96read from and to NVMEM.
97
983. NVMEM cell based consumer APIs
99=================================
100
101NVMEM cells are the data entries/fields in the NVMEM.
102The NVMEM framework provides 3 APIs to read/write NVMEM cells.
103
104struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
105struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name);
106
107void nvmem_cell_put(struct nvmem_cell *cell);
108void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
109
110void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len);
111int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len);
112
113*nvmem_cell_get() apis will get a reference to nvmem cell for a given id,
114and nvmem_cell_read/write() can then read or write to the cell.
115Once the usage of the cell is finished the consumer should call *nvmem_cell_put()
116to free all the allocation memory for the cell.
117
1184. Direct NVMEM device based consumer APIs
119==========================================
120
121In some instances it is necessary to directly read/write the NVMEM.
122To facilitate such consumers NVMEM framework provides below apis.
123
124struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
125struct nvmem_device *devm_nvmem_device_get(struct device *dev,
126 const char *name);
127void nvmem_device_put(struct nvmem_device *nvmem);
128int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
129 size_t bytes, void *buf);
130int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
131 size_t bytes, void *buf);
132int nvmem_device_cell_read(struct nvmem_device *nvmem,
133 struct nvmem_cell_info *info, void *buf);
134int nvmem_device_cell_write(struct nvmem_device *nvmem,
135 struct nvmem_cell_info *info, void *buf);
136
137Before the consumers can read/write NVMEM directly, it should get hold
138of nvmem_controller from one of the *nvmem_device_get() api.
139
140The difference between these apis and cell based apis is that these apis always
141take nvmem_device as parameter.
142
1435. Releasing a reference to the NVMEM
144=====================================
145
Narenbe629b42017-08-16 15:45:57 -0700146When a consumer no longer needs the NVMEM, it has to release the reference
Srinivas Kandagatla354ebb52015-07-27 12:14:14 +0100147to the NVMEM it has obtained using the APIs mentioned in the above section.
148The NVMEM framework provides 2 APIs to release a reference to the NVMEM.
149
150void nvmem_cell_put(struct nvmem_cell *cell);
151void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
152void nvmem_device_put(struct nvmem_device *nvmem);
153void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
154
155Both these APIs are used to release a reference to the NVMEM and
156devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated
157with this NVMEM.
158
159Userspace
160+++++++++
161
1626. Userspace binary interface
163==============================
164
165Userspace can read/write the raw NVMEM file located at
166/sys/bus/nvmem/devices/*/nvmem
167
168ex:
169
170hexdump /sys/bus/nvmem/devices/qfprom0/nvmem
171
1720000000 0000 0000 0000 0000 0000 0000 0000 0000
173*
17400000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00
1750000000 0000 0000 0000 0000 0000 0000 0000 0000
176...
177*
1780001000
179
1807. DeviceTree Binding
181=====================
182
183See Documentation/devicetree/bindings/nvmem/nvmem.txt