1 // SPDX-License-Identifier: GPL-2.0+
6 #include <i2c_eeprom.h>
7 #include <linker_lists.h>
11 #include <dm/device_compat.h>
12 #include <dm/ofnode.h>
14 #include <dm/uclass.h>
16 int nvmem_cell_read(struct nvmem_cell *cell, void *buf, size_t size)
18 dev_dbg(cell->nvmem, "%s: off=%u size=%zu\n", __func__, cell->offset, size);
19 if (size != cell->size)
22 switch (cell->nvmem->driver->id) {
23 case UCLASS_I2C_EEPROM:
24 return i2c_eeprom_read(cell->nvmem, cell->offset, buf, size);
26 int ret = misc_read(cell->nvmem, cell->offset, buf, size);
35 return dm_rtc_read(cell->nvmem, cell->offset, buf, size);
41 int nvmem_cell_write(struct nvmem_cell *cell, const void *buf, size_t size)
43 dev_dbg(cell->nvmem, "%s: off=%u size=%zu\n", __func__, cell->offset, size);
44 if (size != cell->size)
47 switch (cell->nvmem->driver->id) {
48 case UCLASS_I2C_EEPROM:
49 return i2c_eeprom_write(cell->nvmem, cell->offset, buf, size);
51 int ret = misc_write(cell->nvmem, cell->offset, buf, size);
60 return dm_rtc_write(cell->nvmem, cell->offset, buf, size);
67 * nvmem_get_device() - Get an nvmem device for a cell
68 * @node: ofnode of the nvmem device
69 * @cell: Cell to look up
71 * Try to find a nvmem-compatible device by going through the nvmem interfaces.
75 * * -ENODEV if we didn't find anything
76 * * A negative error if there was a problem looking up the device
78 static int nvmem_get_device(ofnode node, struct nvmem_cell *cell)
81 enum uclass_id ids[] = {
87 for (i = 0; i < ARRAY_SIZE(ids); i++) {
88 ret = uclass_get_device_by_ofnode(ids[i], node, &cell->nvmem);
91 if (ret != -ENODEV && ret != -EPFNOSUPPORT)
98 int nvmem_cell_get_by_index(struct udevice *dev, int index,
99 struct nvmem_cell *cell)
102 fdt_size_t size = FDT_SIZE_T_NONE;
104 struct ofnode_phandle_args args;
106 dev_dbg(dev, "%s: index=%d\n", __func__, index);
108 ret = dev_read_phandle_with_args(dev, "nvmem-cells", NULL, 0, index,
113 ret = nvmem_get_device(ofnode_get_parent(args.node), cell);
117 offset = ofnode_get_addr_size_index_notrans(args.node, 0, &size);
118 if (offset == FDT_ADDR_T_NONE || size == FDT_SIZE_T_NONE) {
119 dev_dbg(cell->nvmem, "missing address or size for %s\n",
120 ofnode_get_name(args.node));
124 cell->offset = offset;
129 int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
130 struct nvmem_cell *cell)
134 dev_dbg(dev, "%s, name=%s\n", __func__, name);
136 index = dev_read_stringlist_search(dev, "nvmem-cell-names", name);
140 return nvmem_cell_get_by_index(dev, index, cell);