Lines Matching refs:nvmem

3  * nvmem framework core.
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
59 struct nvmem_device *nvmem;
83 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
86 if (nvmem->reg_read)
87 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
92 static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
97 if (nvmem->reg_write) {
98 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
99 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
100 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
107 static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
114 const struct nvmem_keepout *keepout = nvmem->keepout;
115 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
131 rc = __nvmem_reg_write(nvmem, offset, val, ksize);
133 rc = __nvmem_reg_read(nvmem, offset, val, ksize);
163 return __nvmem_reg_write(nvmem, offset, val, ksize);
165 return __nvmem_reg_read(nvmem, offset, val, ksize);
171 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
174 if (!nvmem->nkeepout)
175 return __nvmem_reg_read(nvmem, offset, val, bytes);
177 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
180 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
183 if (!nvmem->nkeepout)
184 return __nvmem_reg_write(nvmem, offset, val, bytes);
186 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
205 struct nvmem_device *nvmem = to_nvmem_device(dev);
207 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
222 struct nvmem_device *nvmem;
229 nvmem = to_nvmem_device(dev);
232 if (pos >= nvmem->size)
235 if (!IS_ALIGNED(pos, nvmem->stride))
238 if (count < nvmem->word_size)
241 if (pos + count > nvmem->size)
242 count = nvmem->size - pos;
244 count = round_down(count, nvmem->word_size);
246 if (!nvmem->reg_read)
249 rc = nvmem_reg_read(nvmem, pos, buf, count);
262 struct nvmem_device *nvmem;
269 nvmem = to_nvmem_device(dev);
272 if (pos >= nvmem->size)
275 if (!IS_ALIGNED(pos, nvmem->stride))
278 if (count < nvmem->word_size)
281 if (pos + count > nvmem->size)
282 count = nvmem->size - pos;
284 count = round_down(count, nvmem->word_size);
286 if (!nvmem->reg_write)
289 rc = nvmem_reg_write(nvmem, pos, buf, count);
297 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
301 if (!nvmem->root_only)
304 if (!nvmem->read_only)
307 if (!nvmem->reg_write)
310 if (!nvmem->reg_read)
320 struct nvmem_device *nvmem = to_nvmem_device(dev);
322 attr->size = nvmem->size;
324 return nvmem_bin_attr_get_umode(nvmem);
330 .name = "nvmem",
366 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
380 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
381 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
382 nvmem->eeprom.size = nvmem->size;
384 nvmem->eeprom.attr.key = &eeprom_lock_key;
386 nvmem->eeprom.private = &nvmem->dev;
387 nvmem->base_dev = config->base_dev;
389 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
391 dev_err(&nvmem->dev,
396 nvmem->flags |= FLAG_COMPAT;
401 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
405 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
410 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
415 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
424 struct nvmem_device *nvmem = to_nvmem_device(dev);
426 ida_free(&nvmem_ida, nvmem->id);
427 gpiod_put(nvmem->wp_gpio);
428 kfree(nvmem);
436 .name = "nvmem",
450 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
454 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
461 list_add_tail(&cell->node, &cell->nvmem->cells);
466 static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
470 cell->nvmem = nvmem;
486 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
487 dev_err(&nvmem->dev,
488 "cell %s unaligned to nvmem stride %d\n",
489 cell->name ?: "<unknown>", nvmem->stride);
496 static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
502 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
514 * nvmem_add_one_cell() - Add one cell information to an nvmem device
516 * @nvmem: nvmem device to add cells to.
517 * @info: nvmem cell info to add to the device
521 int nvmem_add_one_cell(struct nvmem_device *nvmem,
531 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
544 * nvmem_add_cells() - Add cell information to an nvmem device
546 * @nvmem: nvmem device to add cells to.
547 * @info: nvmem cell info to add to the device
552 static int nvmem_add_cells(struct nvmem_device *nvmem,
559 rval = nvmem_add_one_cell(nvmem, &info[i]);
568 * nvmem_register_notifier() - Register a notifier block for nvmem events.
570 * @nb: notifier block to be called on nvmem events.
581 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
593 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
602 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
612 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
629 nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
634 list_for_each_entry(iter, &nvmem->cells, node) {
645 static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
648 const struct nvmem_keepout *keepout = nvmem->keepout;
649 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
654 dev_err(&nvmem->dev,
661 dev_err(&nvmem->dev,
671 if ((keepout->end - keepout->start < nvmem->word_size) ||
673 (keepout->start - cur < nvmem->word_size))) {
675 dev_err(&nvmem->dev,
682 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
683 !IS_ALIGNED(keepout->end, nvmem->stride)) {
685 dev_err(&nvmem->dev,
698 static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
700 struct nvmem_layout *layout = nvmem->layout;
701 struct device *dev = &nvmem->dev;
713 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
731 layout->fixup_cell_info(nvmem, layout, &info);
733 ret = nvmem_add_one_cell(nvmem, &info);
744 static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
746 return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
749 static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
754 layout_np = of_nvmem_layout_get_container(nvmem);
759 err = nvmem_add_cells_from_dt(nvmem, layout_np);
790 static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem)
795 layout_np = of_nvmem_layout_get_container(nvmem);
806 * In case the nvmem device was built-in while the layout was built as a
835 static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem)
837 struct nvmem_layout *layout = nvmem->layout;
841 ret = layout->add_cells(&nvmem->dev, nvmem, layout);
853 * @nvmem: nvmem device.
858 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
860 return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
865 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
871 layout_np = of_nvmem_layout_get_container(nvmem);
879 * nvmem_register() - Register a nvmem device for given nvmem_config.
880 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
882 * @config: nvmem device configuration with which nvmem device is created.
890 struct nvmem_device *nvmem;
899 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
900 if (!nvmem)
905 kfree(nvmem);
909 nvmem->id = rval;
911 nvmem->dev.type = &nvmem_provider_type;
912 nvmem->dev.bus = &nvmem_bus_type;
913 nvmem->dev.parent = config->dev;
915 device_initialize(&nvmem->dev);
918 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
920 if (IS_ERR(nvmem->wp_gpio)) {
921 rval = PTR_ERR(nvmem->wp_gpio);
922 nvmem->wp_gpio = NULL;
926 kref_init(&nvmem->refcnt);
927 INIT_LIST_HEAD(&nvmem->cells);
929 nvmem->owner = config->owner;
930 if (!nvmem->owner && config->dev->driver)
931 nvmem->owner = config->dev->driver->owner;
932 nvmem->stride = config->stride ?: 1;
933 nvmem->word_size = config->word_size ?: 1;
934 nvmem->size = config->size;
935 nvmem->root_only = config->root_only;
936 nvmem->priv = config->priv;
937 nvmem->type = config->type;
938 nvmem->reg_read = config->reg_read;
939 nvmem->reg_write = config->reg_write;
940 nvmem->keepout = config->keepout;
941 nvmem->nkeepout = config->nkeepout;
943 nvmem->dev.of_node = config->of_node;
945 nvmem->dev.of_node = config->dev->of_node;
949 rval = dev_set_name(&nvmem->dev, "%s", config->name);
952 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
955 rval = dev_set_name(&nvmem->dev, "%s%d",
956 config->name ? : "nvmem",
957 config->name ? config->id : nvmem->id);
964 nvmem->read_only = device_property_present(config->dev, "read-only") ||
965 config->read_only || !nvmem->reg_write;
968 nvmem->dev.groups = nvmem_dev_groups;
971 if (nvmem->nkeepout) {
972 rval = nvmem_validate_keepouts(nvmem);
978 rval = nvmem_sysfs_setup_compat(nvmem, config);
987 nvmem->layout = config->layout ?: nvmem_layout_get(nvmem);
988 if (IS_ERR(nvmem->layout)) {
989 rval = PTR_ERR(nvmem->layout);
990 nvmem->layout = NULL;
997 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
1002 rval = nvmem_add_cells_from_table(nvmem);
1006 rval = nvmem_add_cells_from_legacy_of(nvmem);
1010 rval = nvmem_add_cells_from_fixed_layout(nvmem);
1014 rval = nvmem_add_cells_from_layout(nvmem);
1018 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
1020 rval = device_add(&nvmem->dev);
1024 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
1026 return nvmem;
1029 nvmem_device_remove_all_cells(nvmem);
1030 nvmem_layout_put(nvmem->layout);
1033 nvmem_sysfs_remove_compat(nvmem, config);
1035 put_device(&nvmem->dev);
1043 struct nvmem_device *nvmem;
1045 nvmem = container_of(kref, struct nvmem_device, refcnt);
1047 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1049 if (nvmem->flags & FLAG_COMPAT)
1050 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1052 nvmem_device_remove_all_cells(nvmem);
1053 nvmem_layout_put(nvmem->layout);
1054 device_unregister(&nvmem->dev);
1058 * nvmem_unregister() - Unregister previously registered nvmem device
1060 * @nvmem: Pointer to previously registered nvmem device.
1062 void nvmem_unregister(struct nvmem_device *nvmem)
1064 if (nvmem)
1065 kref_put(&nvmem->refcnt, nvmem_device_release);
1069 static void devm_nvmem_unregister(void *nvmem)
1071 nvmem_unregister(nvmem);
1075 * devm_nvmem_register() - Register a managed nvmem device for given
1077 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1079 * @dev: Device that uses the nvmem device.
1080 * @config: nvmem device configuration with which nvmem device is created.
1088 struct nvmem_device *nvmem;
1091 nvmem = nvmem_register(config);
1092 if (IS_ERR(nvmem))
1093 return nvmem;
1095 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1099 return nvmem;
1106 struct nvmem_device *nvmem = NULL;
1112 nvmem = to_nvmem_device(dev);
1114 if (!nvmem)
1117 if (!try_module_get(nvmem->owner)) {
1118 dev_err(&nvmem->dev,
1120 nvmem_dev_name(nvmem));
1122 put_device(&nvmem->dev);
1126 kref_get(&nvmem->refcnt);
1128 return nvmem;
1131 static void __nvmem_device_put(struct nvmem_device *nvmem)
1133 put_device(&nvmem->dev);
1134 module_put(nvmem->owner);
1135 kref_put(&nvmem->refcnt, nvmem_device_release);
1140 * of_nvmem_device_get() - Get nvmem device from a given id
1142 * @np: Device tree node that uses the nvmem device.
1143 * @id: nvmem name from nvmem-names property.
1152 struct nvmem_device *nvmem;
1156 index = of_property_match_string(np, "nvmem-names", id);
1158 nvmem_np = of_parse_phandle(np, "nvmem", index);
1162 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1164 return nvmem;
1170 * nvmem_device_get() - Get nvmem device from a given id
1172 * @dev: Device that uses the nvmem device.
1173 * @dev_name: name of the requested nvmem device.
1181 struct nvmem_device *nvmem;
1183 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1185 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1186 return nvmem;
1195 * nvmem_device_find() - Find nvmem device with matching function
1212 struct nvmem_device **nvmem = res;
1214 if (WARN_ON(!nvmem || !*nvmem))
1217 return *nvmem == data;
1226 * devm_nvmem_device_put() - put alredy got nvmem device
1228 * @dev: Device that uses the nvmem device.
1229 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1232 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1237 devm_nvmem_device_match, nvmem);
1244 * nvmem_device_put() - put alredy got nvmem device
1246 * @nvmem: pointer to nvmem device that needs to be released.
1248 void nvmem_device_put(struct nvmem_device *nvmem)
1250 __nvmem_device_put(nvmem);
1255 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1257 * @dev: Device that requests the nvmem device.
1258 * @id: name id for the requested nvmem device.
1266 struct nvmem_device **ptr, *nvmem;
1272 nvmem = nvmem_device_get(dev, id);
1273 if (!IS_ERR(nvmem)) {
1274 *ptr = nvmem;
1280 return nvmem;
1315 struct nvmem_device *nvmem;
1329 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1331 if (IS_ERR(nvmem)) {
1333 cell = ERR_CAST(nvmem);
1337 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1340 __nvmem_device_put(nvmem);
1345 __nvmem_device_put(nvmem);
1357 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1362 list_for_each_entry(iter, &nvmem->cells, node) {
1374 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1376 * @np: Device tree node that uses the nvmem cell.
1377 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1379 * nvmem-cell-names property).
1388 struct nvmem_device *nvmem;
1398 index = of_property_match_string(np, "nvmem-cell-names", id);
1400 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1401 "#nvmem-cell-cells",
1419 /* nvmem layouts produce cells within the nvmem-layout container */
1420 if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1428 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1430 if (IS_ERR(nvmem)) {
1432 return ERR_CAST(nvmem);
1435 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1438 __nvmem_device_put(nvmem);
1444 __nvmem_device_put(nvmem);
1452 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1454 * @dev: Device that requests the nvmem cell.
1455 * @id: nvmem cell name to get (this corresponds with the name from the
1456 * nvmem-cell-names property for DT systems and with the con_id from
1487 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1489 * @dev: Device that requests the nvmem cell.
1490 * @id: nvmem cell name id to get.
1527 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1530 * @dev: Device that requests the nvmem cell.
1531 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1545 * nvmem_cell_put() - Release previously allocated nvmem cell.
1547 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1551 struct nvmem_device *nvmem = cell->entry->nvmem;
1557 __nvmem_device_put(nvmem);
1594 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1600 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
1623 * nvmem_cell_read() - Read a given nvmem cell
1625 * @cell: nvmem cell to be read.
1635 struct nvmem_device *nvmem = entry->nvmem;
1639 if (!nvmem)
1646 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1659 struct nvmem_device *nvmem = cell->nvmem;
1675 /* setup the first byte with lsb bits from nvmem */
1676 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1694 /* setup the last byte with msb bits from nvmem */
1695 rc = nvmem_reg_read(nvmem,
1711 struct nvmem_device *nvmem = cell->nvmem;
1714 if (!nvmem || nvmem->read_only ||
1732 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1745 * nvmem_cell_write() - Write to a given nvmem cell
1747 * @cell: nvmem cell to be written.
1749 * @len: length of buffer to be written to nvmem cell.
1791 * @dev: Device that requests the nvmem cell.
1792 * @cell_id: Name of nvmem cell to read.
1806 * @dev: Device that requests the nvmem cell.
1807 * @cell_id: Name of nvmem cell to read.
1821 * @dev: Device that requests the nvmem cell.
1822 * @cell_id: Name of nvmem cell to read.
1836 * @dev: Device that requests the nvmem cell.
1837 * @cell_id: Name of nvmem cell to read.
1884 * @dev: Device that requests the nvmem cell.
1885 * @cell_id: Name of nvmem cell to read.
1915 * @dev: Device that requests the nvmem cell.
1916 * @cell_id: Name of nvmem cell to read.
1944 * nvmem_device_cell_read() - Read a given nvmem device and cell
1946 * @nvmem: nvmem device to read from.
1947 * @info: nvmem cell info to be read.
1953 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1960 if (!nvmem)
1963 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1967 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
1976 * nvmem_device_cell_write() - Write cell to a given nvmem device
1978 * @nvmem: nvmem device to be written to.
1979 * @info: nvmem cell info to be written.
1984 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1990 if (!nvmem)
1993 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
2002 * nvmem_device_read() - Read from a given nvmem device
2004 * @nvmem: nvmem device to read from.
2005 * @offset: offset in nvmem device.
2012 int nvmem_device_read(struct nvmem_device *nvmem,
2018 if (!nvmem)
2021 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
2031 * nvmem_device_write() - Write cell to a given nvmem device
2033 * @nvmem: nvmem device to be written to.
2034 * @offset: offset in nvmem device.
2040 int nvmem_device_write(struct nvmem_device *nvmem,
2046 if (!nvmem)
2049 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2121 * nvmem_dev_name() - Get the name of a given nvmem device.
2123 * @nvmem: nvmem device.
2125 * Return: name of the nvmem device.
2127 const char *nvmem_dev_name(struct nvmem_device *nvmem)
2129 return dev_name(&nvmem->dev);
2148 MODULE_DESCRIPTION("nvmem Driver Core");