Lines Matching +full:cell +full:- +full:index
1 // SPDX-License-Identifier: GPL-2.0
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
44 int index; member
61 if (nvmem->reg_read) in __nvmem_reg_read()
62 return nvmem->reg_read(nvmem->priv, offset, val, bytes); in __nvmem_reg_read()
64 return -EINVAL; in __nvmem_reg_read()
72 if (nvmem->reg_write) { in __nvmem_reg_write()
73 gpiod_set_value_cansleep(nvmem->wp_gpio, 0); in __nvmem_reg_write()
74 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); in __nvmem_reg_write()
75 gpiod_set_value_cansleep(nvmem->wp_gpio, 1); in __nvmem_reg_write()
79 return -EINVAL; in __nvmem_reg_write()
89 const struct nvmem_keepout *keepout = nvmem->keepout; in nvmem_access_with_keepouts()
90 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout; in nvmem_access_with_keepouts()
97 while ((keepout < keepoutend) && (keepout->end <= offset)) in nvmem_access_with_keepouts()
102 if (offset < keepout->start) { in nvmem_access_with_keepouts()
103 kend = min(end, keepout->start); in nvmem_access_with_keepouts()
104 ksize = kend - offset; in nvmem_access_with_keepouts()
121 kend = min(end, keepout->end); in nvmem_access_with_keepouts()
122 ksize = kend - offset; in nvmem_access_with_keepouts()
124 memset(val, keepout->value, ksize); in nvmem_access_with_keepouts()
136 ksize = end - offset; in nvmem_access_with_keepouts()
149 if (!nvmem->nkeepout) in nvmem_reg_read()
158 if (!nvmem->nkeepout) in nvmem_reg_write()
182 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]); in type_show()
200 if (attr->private) in bin_attr_nvmem_read()
201 dev = attr->private; in bin_attr_nvmem_read()
207 if (pos >= nvmem->size) in bin_attr_nvmem_read()
210 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_read()
211 return -EINVAL; in bin_attr_nvmem_read()
213 if (count < nvmem->word_size) in bin_attr_nvmem_read()
214 return -EINVAL; in bin_attr_nvmem_read()
216 if (pos + count > nvmem->size) in bin_attr_nvmem_read()
217 count = nvmem->size - pos; in bin_attr_nvmem_read()
219 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_read()
221 if (!nvmem->reg_read) in bin_attr_nvmem_read()
222 return -EPERM; in bin_attr_nvmem_read()
240 if (attr->private) in bin_attr_nvmem_write()
241 dev = attr->private; in bin_attr_nvmem_write()
247 if (pos >= nvmem->size) in bin_attr_nvmem_write()
248 return -EFBIG; in bin_attr_nvmem_write()
250 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_write()
251 return -EINVAL; in bin_attr_nvmem_write()
253 if (count < nvmem->word_size) in bin_attr_nvmem_write()
254 return -EINVAL; in bin_attr_nvmem_write()
256 if (pos + count > nvmem->size) in bin_attr_nvmem_write()
257 count = nvmem->size - pos; in bin_attr_nvmem_write()
259 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_write()
261 if (!nvmem->reg_write) in bin_attr_nvmem_write()
262 return -EPERM; in bin_attr_nvmem_write()
276 if (!nvmem->root_only) in nvmem_bin_attr_get_umode()
279 if (!nvmem->read_only) in nvmem_bin_attr_get_umode()
282 if (!nvmem->reg_write) in nvmem_bin_attr_get_umode()
285 if (!nvmem->reg_read) in nvmem_bin_attr_get_umode()
297 attr->size = nvmem->size; in nvmem_bin_attr_is_visible()
303 const char *id, int index);
310 struct nvmem_cell *cell = NULL; in nvmem_cell_attr_read() local
314 entry = attr->private; in nvmem_cell_attr_read()
315 cell = nvmem_create_cell(entry, entry->name, 0); in nvmem_cell_attr_read()
316 if (IS_ERR(cell)) in nvmem_cell_attr_read()
317 return PTR_ERR(cell); in nvmem_cell_attr_read()
319 if (!cell) in nvmem_cell_attr_read()
320 return -EINVAL; in nvmem_cell_attr_read()
322 content = nvmem_cell_read(cell, &cell_sz); in nvmem_cell_attr_read()
328 read_len = min_t(unsigned int, cell_sz - pos, count); in nvmem_cell_attr_read()
333 kfree_const(cell->id); in nvmem_cell_attr_read()
334 kfree(cell); in nvmem_cell_attr_read()
360 /* Cell attributes will be dynamically allocated */
384 * nvmem_setup_compat() - Create an additional binary entry in
393 if (!config->compat) in nvmem_sysfs_setup_compat()
396 if (!config->base_dev) in nvmem_sysfs_setup_compat()
397 return -EINVAL; in nvmem_sysfs_setup_compat()
399 if (config->type == NVMEM_TYPE_FRAM) in nvmem_sysfs_setup_compat()
402 nvmem->eeprom = bin_attr_nvmem_eeprom_compat; in nvmem_sysfs_setup_compat()
403 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem); in nvmem_sysfs_setup_compat()
404 nvmem->eeprom.size = nvmem->size; in nvmem_sysfs_setup_compat()
406 nvmem->eeprom.attr.key = &eeprom_lock_key; in nvmem_sysfs_setup_compat()
408 nvmem->eeprom.private = &nvmem->dev; in nvmem_sysfs_setup_compat()
409 nvmem->base_dev = config->base_dev; in nvmem_sysfs_setup_compat()
411 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_setup_compat()
413 dev_err(&nvmem->dev, in nvmem_sysfs_setup_compat()
418 nvmem->flags |= FLAG_COMPAT; in nvmem_sysfs_setup_compat()
426 if (config->compat) in nvmem_sysfs_remove_compat()
427 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_remove_compat()
439 if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) { in nvmem_populate_sysfs_cells()
445 ncells = list_count_nodes(&nvmem->cells); in nvmem_populate_sysfs_cells()
446 cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1, in nvmem_populate_sysfs_cells()
449 ret = -ENOMEM; in nvmem_populate_sysfs_cells()
453 attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL); in nvmem_populate_sysfs_cells()
455 ret = -ENOMEM; in nvmem_populate_sysfs_cells()
459 /* Initialize each attribute to take the name and size of the cell */ in nvmem_populate_sysfs_cells()
460 list_for_each_entry(entry, &nvmem->cells, node) { in nvmem_populate_sysfs_cells()
462 attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL, in nvmem_populate_sysfs_cells()
463 "%s@%x,%x", entry->name, in nvmem_populate_sysfs_cells()
464 entry->offset, in nvmem_populate_sysfs_cells()
465 entry->bit_offset); in nvmem_populate_sysfs_cells()
467 attrs[i].size = entry->bytes; in nvmem_populate_sysfs_cells()
471 ret = -ENOMEM; in nvmem_populate_sysfs_cells()
481 ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups); in nvmem_populate_sysfs_cells()
485 nvmem->sysfs_cells_populated = true; in nvmem_populate_sysfs_cells()
498 return -ENOSYS; in nvmem_sysfs_setup_compat()
511 ida_free(&nvmem_ida, nvmem->id); in nvmem_release()
512 gpiod_put(nvmem->wp_gpio); in nvmem_release()
524 static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell) in nvmem_cell_entry_drop() argument
526 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell); in nvmem_cell_entry_drop()
528 list_del(&cell->node); in nvmem_cell_entry_drop()
530 of_node_put(cell->np); in nvmem_cell_entry_drop()
531 kfree_const(cell->name); in nvmem_cell_entry_drop()
532 kfree(cell); in nvmem_cell_entry_drop()
537 struct nvmem_cell_entry *cell, *p; in nvmem_device_remove_all_cells() local
539 list_for_each_entry_safe(cell, p, &nvmem->cells, node) in nvmem_device_remove_all_cells()
540 nvmem_cell_entry_drop(cell); in nvmem_device_remove_all_cells()
543 static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell) in nvmem_cell_entry_add() argument
546 list_add_tail(&cell->node, &cell->nvmem->cells); in nvmem_cell_entry_add()
548 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell); in nvmem_cell_entry_add()
553 struct nvmem_cell_entry *cell) in nvmem_cell_info_to_nvmem_cell_entry_nodup() argument
555 cell->nvmem = nvmem; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
556 cell->offset = info->offset; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
557 cell->raw_len = info->raw_len ?: info->bytes; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
558 cell->bytes = info->bytes; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
559 cell->name = info->name; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
560 cell->read_post_process = info->read_post_process; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
561 cell->priv = info->priv; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
563 cell->bit_offset = info->bit_offset; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
564 cell->nbits = info->nbits; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
565 cell->np = info->np; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
567 if (cell->nbits) in nvmem_cell_info_to_nvmem_cell_entry_nodup()
568 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, in nvmem_cell_info_to_nvmem_cell_entry_nodup()
571 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_cell_info_to_nvmem_cell_entry_nodup()
572 dev_err(&nvmem->dev, in nvmem_cell_info_to_nvmem_cell_entry_nodup()
573 "cell %s unaligned to nvmem stride %d\n", in nvmem_cell_info_to_nvmem_cell_entry_nodup()
574 cell->name ?: "<unknown>", nvmem->stride); in nvmem_cell_info_to_nvmem_cell_entry_nodup()
575 return -EINVAL; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
583 struct nvmem_cell_entry *cell) in nvmem_cell_info_to_nvmem_cell_entry() argument
587 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell); in nvmem_cell_info_to_nvmem_cell_entry()
591 cell->name = kstrdup_const(info->name, GFP_KERNEL); in nvmem_cell_info_to_nvmem_cell_entry()
592 if (!cell->name) in nvmem_cell_info_to_nvmem_cell_entry()
593 return -ENOMEM; in nvmem_cell_info_to_nvmem_cell_entry()
599 * nvmem_add_one_cell() - Add one cell information to an nvmem device
602 * @info: nvmem cell info to add to the device
609 struct nvmem_cell_entry *cell; in nvmem_add_one_cell() local
612 cell = kzalloc(sizeof(*cell), GFP_KERNEL); in nvmem_add_one_cell()
613 if (!cell) in nvmem_add_one_cell()
614 return -ENOMEM; in nvmem_add_one_cell()
616 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell); in nvmem_add_one_cell()
618 kfree(cell); in nvmem_add_one_cell()
622 nvmem_cell_entry_add(cell); in nvmem_add_one_cell()
629 * nvmem_add_cells() - Add cell information to an nvmem device
632 * @info: nvmem cell info to add to the device
653 * nvmem_register_notifier() - Register a notifier block for nvmem events.
666 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
682 struct nvmem_cell_entry *cell; in nvmem_add_cells_from_table() local
687 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { in nvmem_add_cells_from_table()
688 for (i = 0; i < table->ncells; i++) { in nvmem_add_cells_from_table()
689 info = &table->cells[i]; in nvmem_add_cells_from_table()
691 cell = kzalloc(sizeof(*cell), GFP_KERNEL); in nvmem_add_cells_from_table()
692 if (!cell) { in nvmem_add_cells_from_table()
693 rval = -ENOMEM; in nvmem_add_cells_from_table()
697 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell); in nvmem_add_cells_from_table()
699 kfree(cell); in nvmem_add_cells_from_table()
703 nvmem_cell_entry_add(cell); in nvmem_add_cells_from_table()
716 struct nvmem_cell_entry *iter, *cell = NULL; in nvmem_find_cell_entry_by_name() local
719 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_entry_by_name()
720 if (strcmp(cell_id, iter->name) == 0) { in nvmem_find_cell_entry_by_name()
721 cell = iter; in nvmem_find_cell_entry_by_name()
727 return cell; in nvmem_find_cell_entry_by_name()
733 const struct nvmem_keepout *keepout = nvmem->keepout; in nvmem_validate_keepouts()
734 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout; in nvmem_validate_keepouts()
738 if (keepout->start < cur) { in nvmem_validate_keepouts()
739 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
742 return -ERANGE; in nvmem_validate_keepouts()
745 if (keepout->end < keepout->start) { in nvmem_validate_keepouts()
746 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
749 return -EINVAL; in nvmem_validate_keepouts()
756 if ((keepout->end - keepout->start < nvmem->word_size) || in nvmem_validate_keepouts()
757 ((keepout->start != cur) && in nvmem_validate_keepouts()
758 (keepout->start - cur < nvmem->word_size))) { in nvmem_validate_keepouts()
760 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
763 return -ERANGE; in nvmem_validate_keepouts()
767 if (!IS_ALIGNED(keepout->start, nvmem->stride) || in nvmem_validate_keepouts()
768 !IS_ALIGNED(keepout->end, nvmem->stride)) { in nvmem_validate_keepouts()
770 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
773 return -EINVAL; in nvmem_validate_keepouts()
776 cur = keepout->end; in nvmem_validate_keepouts()
785 struct device *dev = &nvmem->dev; in nvmem_add_cells_from_dt()
799 return -EINVAL; in nvmem_add_cells_from_dt()
814 if (nvmem->fixup_dt_cell_info) in nvmem_add_cells_from_dt()
815 nvmem->fixup_dt_cell_info(nvmem, &info); in nvmem_add_cells_from_dt()
830 return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node); in nvmem_add_cells_from_legacy_of()
842 if (of_device_is_compatible(layout_np, "fixed-layout")) in nvmem_add_cells_from_fixed_layout()
854 if (!layout->add_cells) in nvmem_layout_register()
855 return -EINVAL; in nvmem_layout_register()
858 ret = layout->add_cells(layout); in nvmem_layout_register()
863 ret = nvmem_populate_sysfs_cells(layout->nvmem); in nvmem_layout_register()
865 nvmem_device_remove_all_cells(layout->nvmem); in nvmem_layout_register()
881 * nvmem_register() - Register a nvmem device for given nvmem_config.
882 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
895 if (!config->dev) in nvmem_register()
896 return ERR_PTR(-EINVAL); in nvmem_register()
898 if (!config->reg_read && !config->reg_write) in nvmem_register()
899 return ERR_PTR(-EINVAL); in nvmem_register()
903 return ERR_PTR(-ENOMEM); in nvmem_register()
911 nvmem->id = rval; in nvmem_register()
913 nvmem->dev.type = &nvmem_provider_type; in nvmem_register()
914 nvmem->dev.bus = &nvmem_bus_type; in nvmem_register()
915 nvmem->dev.parent = config->dev; in nvmem_register()
917 device_initialize(&nvmem->dev); in nvmem_register()
919 if (!config->ignore_wp) in nvmem_register()
920 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", in nvmem_register()
922 if (IS_ERR(nvmem->wp_gpio)) { in nvmem_register()
923 rval = PTR_ERR(nvmem->wp_gpio); in nvmem_register()
924 nvmem->wp_gpio = NULL; in nvmem_register()
928 kref_init(&nvmem->refcnt); in nvmem_register()
929 INIT_LIST_HEAD(&nvmem->cells); in nvmem_register()
930 nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info; in nvmem_register()
932 nvmem->owner = config->owner; in nvmem_register()
933 if (!nvmem->owner && config->dev->driver) in nvmem_register()
934 nvmem->owner = config->dev->driver->owner; in nvmem_register()
935 nvmem->stride = config->stride ?: 1; in nvmem_register()
936 nvmem->word_size = config->word_size ?: 1; in nvmem_register()
937 nvmem->size = config->size; in nvmem_register()
938 nvmem->root_only = config->root_only; in nvmem_register()
939 nvmem->priv = config->priv; in nvmem_register()
940 nvmem->type = config->type; in nvmem_register()
941 nvmem->reg_read = config->reg_read; in nvmem_register()
942 nvmem->reg_write = config->reg_write; in nvmem_register()
943 nvmem->keepout = config->keepout; in nvmem_register()
944 nvmem->nkeepout = config->nkeepout; in nvmem_register()
945 if (config->of_node) in nvmem_register()
946 nvmem->dev.of_node = config->of_node; in nvmem_register()
948 nvmem->dev.of_node = config->dev->of_node; in nvmem_register()
950 switch (config->id) { in nvmem_register()
952 rval = dev_set_name(&nvmem->dev, "%s", config->name); in nvmem_register()
955 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); in nvmem_register()
958 rval = dev_set_name(&nvmem->dev, "%s%d", in nvmem_register()
959 config->name ? : "nvmem", in nvmem_register()
960 config->name ? config->id : nvmem->id); in nvmem_register()
967 nvmem->read_only = device_property_present(config->dev, "read-only") || in nvmem_register()
968 config->read_only || !nvmem->reg_write; in nvmem_register()
971 nvmem->dev.groups = nvmem_dev_groups; in nvmem_register()
974 if (nvmem->nkeepout) { in nvmem_register()
980 if (config->compat) { in nvmem_register()
986 if (config->cells) { in nvmem_register()
987 rval = nvmem_add_cells(nvmem, config->cells, config->ncells); in nvmem_register()
996 if (config->add_legacy_fixed_of_cells) { in nvmem_register()
1006 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); in nvmem_register()
1008 rval = device_add(&nvmem->dev); in nvmem_register()
1031 device_del(&nvmem->dev); in nvmem_register()
1034 if (config->compat) in nvmem_register()
1037 put_device(&nvmem->dev); in nvmem_register()
1051 if (nvmem->flags & FLAG_COMPAT) in nvmem_device_release()
1052 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_device_release()
1056 device_unregister(&nvmem->dev); in nvmem_device_release()
1060 * nvmem_unregister() - Unregister previously registered nvmem device
1067 kref_put(&nvmem->refcnt, nvmem_device_release); in nvmem_unregister()
1077 * devm_nvmem_register() - Register a managed nvmem device for given
1079 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1117 return ERR_PTR(-EPROBE_DEFER); in __nvmem_device_get()
1119 if (!try_module_get(nvmem->owner)) { in __nvmem_device_get()
1120 dev_err(&nvmem->dev, in __nvmem_device_get()
1121 "could not increase module refcount for cell %s\n", in __nvmem_device_get()
1124 put_device(&nvmem->dev); in __nvmem_device_get()
1125 return ERR_PTR(-EINVAL); in __nvmem_device_get()
1128 kref_get(&nvmem->refcnt); in __nvmem_device_get()
1135 put_device(&nvmem->dev); in __nvmem_device_put()
1136 module_put(nvmem->owner); in __nvmem_device_put()
1137 kref_put(&nvmem->refcnt, nvmem_device_release); in __nvmem_device_put()
1142 * of_nvmem_device_get() - Get nvmem device from a given id
1145 * @id: nvmem name from nvmem-names property.
1155 int index = 0; in of_nvmem_device_get() local
1158 index = of_property_match_string(np, "nvmem-names", id); in of_nvmem_device_get()
1160 nvmem_np = of_parse_phandle(np, "nvmem", index); in of_nvmem_device_get()
1162 return ERR_PTR(-ENOENT); in of_nvmem_device_get()
1172 * nvmem_device_get() - Get nvmem device from a given id
1182 if (dev->of_node) { /* try dt first */ in nvmem_device_get()
1185 nvmem = of_nvmem_device_get(dev->of_node, dev_name); in nvmem_device_get()
1187 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) in nvmem_device_get()
1197 * nvmem_device_find() - Find nvmem device with matching function
1228 * devm_nvmem_device_put() - put alredy got nvmem device
1246 * nvmem_device_put() - put alredy got nvmem device
1257 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1272 return ERR_PTR(-ENOMEM); in devm_nvmem_device_get()
1287 const char *id, int index) in nvmem_create_cell() argument
1289 struct nvmem_cell *cell; in nvmem_create_cell() local
1292 cell = kzalloc(sizeof(*cell), GFP_KERNEL); in nvmem_create_cell()
1293 if (!cell) in nvmem_create_cell()
1294 return ERR_PTR(-ENOMEM); in nvmem_create_cell()
1299 kfree(cell); in nvmem_create_cell()
1300 return ERR_PTR(-ENOMEM); in nvmem_create_cell()
1304 cell->id = name; in nvmem_create_cell()
1305 cell->entry = entry; in nvmem_create_cell()
1306 cell->index = index; in nvmem_create_cell()
1308 return cell; in nvmem_create_cell()
1315 struct nvmem_cell *cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup() local
1321 return ERR_PTR(-EINVAL); in nvmem_cell_get_from_lookup()
1328 if ((strcmp(lookup->dev_id, dev_id) == 0) && in nvmem_cell_get_from_lookup()
1329 (strcmp(lookup->con_id, con_id) == 0)) { in nvmem_cell_get_from_lookup()
1331 nvmem = __nvmem_device_get((void *)lookup->nvmem_name, in nvmem_cell_get_from_lookup()
1335 cell = ERR_CAST(nvmem); in nvmem_cell_get_from_lookup()
1340 lookup->cell_name); in nvmem_cell_get_from_lookup()
1343 cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup()
1345 cell = nvmem_create_cell(cell_entry, con_id, 0); in nvmem_cell_get_from_lookup()
1346 if (IS_ERR(cell)) in nvmem_cell_get_from_lookup()
1354 return cell; in nvmem_cell_get_from_lookup()
1359 if (nvmem->layout && nvmem->layout->dev.driver) in nvmem_layout_module_put()
1360 module_put(nvmem->layout->dev.driver->owner); in nvmem_layout_module_put()
1367 struct nvmem_cell_entry *iter, *cell = NULL; in nvmem_find_cell_entry_by_node() local
1370 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_entry_by_node()
1371 if (np == iter->np) { in nvmem_find_cell_entry_by_node()
1372 cell = iter; in nvmem_find_cell_entry_by_node()
1378 return cell; in nvmem_find_cell_entry_by_node()
1383 if (!nvmem->layout) in nvmem_layout_module_get_optional()
1386 if (!nvmem->layout->dev.driver || in nvmem_layout_module_get_optional()
1387 !try_module_get(nvmem->layout->dev.driver->owner)) in nvmem_layout_module_get_optional()
1388 return -EPROBE_DEFER; in nvmem_layout_module_get_optional()
1394 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1396 * @np: Device tree node that uses the nvmem cell.
1397 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1398 * for the cell at index 0 (the lone cell with no accompanying
1399 * nvmem-cell-names property).
1410 struct nvmem_cell *cell; in of_nvmem_cell_get() local
1412 int index = 0; in of_nvmem_cell_get() local
1416 /* if cell name exists, find index to the name */ in of_nvmem_cell_get()
1418 index = of_property_match_string(np, "nvmem-cell-names", id); in of_nvmem_cell_get()
1420 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells", in of_nvmem_cell_get()
1421 "#nvmem-cell-cells", in of_nvmem_cell_get()
1422 index, &cell_spec); in of_nvmem_cell_get()
1424 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1427 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1436 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1439 /* nvmem layouts produce cells within the nvmem-layout container */ in of_nvmem_cell_get()
1440 if (of_node_name_eq(nvmem_np, "nvmem-layout")) { in of_nvmem_cell_get()
1444 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1467 if (nvmem->layout) in of_nvmem_cell_get()
1468 return ERR_PTR(-EPROBE_DEFER); in of_nvmem_cell_get()
1470 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1473 cell = nvmem_create_cell(cell_entry, id, cell_index); in of_nvmem_cell_get()
1474 if (IS_ERR(cell)) { in of_nvmem_cell_get()
1479 return cell; in of_nvmem_cell_get()
1485 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1487 * @dev: Device that requests the nvmem cell.
1488 * @id: nvmem cell name to get (this corresponds with the name from the
1489 * nvmem-cell-names property for DT systems and with the con_id from
1490 * the lookup entry for non-DT systems).
1498 struct nvmem_cell *cell; in nvmem_cell_get() local
1500 if (dev->of_node) { /* try dt first */ in nvmem_cell_get()
1501 cell = of_nvmem_cell_get(dev->of_node, id); in nvmem_cell_get()
1502 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) in nvmem_cell_get()
1503 return cell; in nvmem_cell_get()
1506 /* NULL cell id only allowed for device tree; invalid otherwise */ in nvmem_cell_get()
1508 return ERR_PTR(-EINVAL); in nvmem_cell_get()
1520 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1522 * @dev: Device that requests the nvmem cell.
1523 * @id: nvmem cell name id to get.
1531 struct nvmem_cell **ptr, *cell; in devm_nvmem_cell_get() local
1535 return ERR_PTR(-ENOMEM); in devm_nvmem_cell_get()
1537 cell = nvmem_cell_get(dev, id); in devm_nvmem_cell_get()
1538 if (!IS_ERR(cell)) { in devm_nvmem_cell_get()
1539 *ptr = cell; in devm_nvmem_cell_get()
1545 return cell; in devm_nvmem_cell_get()
1560 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1563 * @dev: Device that requests the nvmem cell.
1564 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1566 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) in devm_nvmem_cell_put() argument
1571 devm_nvmem_cell_match, cell); in devm_nvmem_cell_put()
1578 * nvmem_cell_put() - Release previously allocated nvmem cell.
1580 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1582 void nvmem_cell_put(struct nvmem_cell *cell) in nvmem_cell_put() argument
1584 struct nvmem_device *nvmem = cell->entry->nvmem; in nvmem_cell_put()
1586 if (cell->id) in nvmem_cell_put()
1587 kfree_const(cell->id); in nvmem_cell_put()
1589 kfree(cell); in nvmem_cell_put()
1595 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf) in nvmem_shift_read_buffer_in_place() argument
1598 int i, extra, bit_offset = cell->bit_offset; in nvmem_shift_read_buffer_in_place()
1606 for (i = 1; i < cell->bytes; i++) { in nvmem_shift_read_buffer_in_place()
1608 *p |= *b << (BITS_PER_BYTE - bit_offset); in nvmem_shift_read_buffer_in_place()
1615 p += cell->bytes - 1; in nvmem_shift_read_buffer_in_place()
1619 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE); in nvmem_shift_read_buffer_in_place()
1620 while (--extra >= 0) in nvmem_shift_read_buffer_in_place()
1621 *p-- = 0; in nvmem_shift_read_buffer_in_place()
1624 if (cell->nbits % BITS_PER_BYTE) in nvmem_shift_read_buffer_in_place()
1625 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0); in nvmem_shift_read_buffer_in_place()
1629 struct nvmem_cell_entry *cell, in __nvmem_cell_read() argument
1630 void *buf, size_t *len, const char *id, int index) in __nvmem_cell_read() argument
1634 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len); in __nvmem_cell_read()
1639 /* shift bits in-place */ in __nvmem_cell_read()
1640 if (cell->bit_offset || cell->nbits) in __nvmem_cell_read()
1641 nvmem_shift_read_buffer_in_place(cell, buf); in __nvmem_cell_read()
1643 if (cell->read_post_process) { in __nvmem_cell_read()
1644 rc = cell->read_post_process(cell->priv, id, index, in __nvmem_cell_read()
1645 cell->offset, buf, cell->raw_len); in __nvmem_cell_read()
1651 *len = cell->bytes; in __nvmem_cell_read()
1657 * nvmem_cell_read() - Read a given nvmem cell
1659 * @cell: nvmem cell to be read.
1660 * @len: pointer to length of cell which will be populated on successful read;
1666 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) in nvmem_cell_read() argument
1668 struct nvmem_cell_entry *entry = cell->entry; in nvmem_cell_read()
1669 struct nvmem_device *nvmem = entry->nvmem; in nvmem_cell_read()
1674 return ERR_PTR(-EINVAL); in nvmem_cell_read()
1676 buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL); in nvmem_cell_read()
1678 return ERR_PTR(-ENOMEM); in nvmem_cell_read()
1680 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index); in nvmem_cell_read()
1690 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell, in nvmem_cell_prepare_write_buffer() argument
1693 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_prepare_write_buffer()
1694 int i, rc, nbits, bit_offset = cell->bit_offset; in nvmem_cell_prepare_write_buffer()
1697 nbits = cell->nbits; in nvmem_cell_prepare_write_buffer()
1698 buf = kzalloc(cell->bytes, GFP_KERNEL); in nvmem_cell_prepare_write_buffer()
1700 return ERR_PTR(-ENOMEM); in nvmem_cell_prepare_write_buffer()
1710 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); in nvmem_cell_prepare_write_buffer()
1713 *b++ |= GENMASK(bit_offset - 1, 0) & v; in nvmem_cell_prepare_write_buffer()
1716 for (i = 1; i < cell->bytes; i++) { in nvmem_cell_prepare_write_buffer()
1718 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); in nvmem_cell_prepare_write_buffer()
1730 cell->offset + cell->bytes - 1, &v, 1); in nvmem_cell_prepare_write_buffer()
1743 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len) in __nvmem_cell_entry_write() argument
1745 struct nvmem_device *nvmem = cell->nvmem; in __nvmem_cell_entry_write()
1748 if (!nvmem || nvmem->read_only || in __nvmem_cell_entry_write()
1749 (cell->bit_offset == 0 && len != cell->bytes)) in __nvmem_cell_entry_write()
1750 return -EINVAL; in __nvmem_cell_entry_write()
1753 * Any cells which have a read_post_process hook are read-only because in __nvmem_cell_entry_write()
1757 if (cell->read_post_process) in __nvmem_cell_entry_write()
1758 return -EINVAL; in __nvmem_cell_entry_write()
1760 if (cell->bit_offset || cell->nbits) { in __nvmem_cell_entry_write()
1761 buf = nvmem_cell_prepare_write_buffer(cell, buf, len); in __nvmem_cell_entry_write()
1766 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); in __nvmem_cell_entry_write()
1769 if (cell->bit_offset || cell->nbits) in __nvmem_cell_entry_write()
1779 * nvmem_cell_write() - Write to a given nvmem cell
1781 * @cell: nvmem cell to be written.
1783 * @len: length of buffer to be written to nvmem cell.
1787 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len) in nvmem_cell_write() argument
1789 return __nvmem_cell_entry_write(cell->entry, buf, len); in nvmem_cell_write()
1797 struct nvmem_cell *cell; in nvmem_cell_read_common() local
1801 cell = nvmem_cell_get(dev, cell_id); in nvmem_cell_read_common()
1802 if (IS_ERR(cell)) in nvmem_cell_read_common()
1803 return PTR_ERR(cell); in nvmem_cell_read_common()
1805 buf = nvmem_cell_read(cell, &len); in nvmem_cell_read_common()
1807 nvmem_cell_put(cell); in nvmem_cell_read_common()
1812 nvmem_cell_put(cell); in nvmem_cell_read_common()
1813 return -EINVAL; in nvmem_cell_read_common()
1817 nvmem_cell_put(cell); in nvmem_cell_read_common()
1823 * nvmem_cell_read_u8() - Read a cell value as a u8
1825 * @dev: Device that requests the nvmem cell.
1826 * @cell_id: Name of nvmem cell to read.
1838 * nvmem_cell_read_u16() - Read a cell value as a u16
1840 * @dev: Device that requests the nvmem cell.
1841 * @cell_id: Name of nvmem cell to read.
1853 * nvmem_cell_read_u32() - Read a cell value as a u32
1855 * @dev: Device that requests the nvmem cell.
1856 * @cell_id: Name of nvmem cell to read.
1868 * nvmem_cell_read_u64() - Read a cell value as a u64
1870 * @dev: Device that requests the nvmem cell.
1871 * @cell_id: Name of nvmem cell to read.
1886 struct nvmem_cell *cell; in nvmem_cell_read_variable_common() local
1890 cell = nvmem_cell_get(dev, cell_id); in nvmem_cell_read_variable_common()
1891 if (IS_ERR(cell)) in nvmem_cell_read_variable_common()
1892 return cell; in nvmem_cell_read_variable_common()
1894 nbits = cell->entry->nbits; in nvmem_cell_read_variable_common()
1895 buf = nvmem_cell_read(cell, len); in nvmem_cell_read_variable_common()
1896 nvmem_cell_put(cell); in nvmem_cell_read_variable_common()
1909 return ERR_PTR(-ERANGE); in nvmem_cell_read_variable_common()
1916 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1918 * @dev: Device that requests the nvmem cell.
1919 * @cell_id: Name of nvmem cell to read.
1947 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1949 * @dev: Device that requests the nvmem cell.
1950 * @cell_id: Name of nvmem cell to read.
1978 * nvmem_device_cell_read() - Read a given nvmem device and cell
1981 * @info: nvmem cell info to be read.
1990 struct nvmem_cell_entry cell; in nvmem_device_cell_read() local
1995 return -EINVAL; in nvmem_device_cell_read()
1997 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell); in nvmem_device_cell_read()
2001 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0); in nvmem_device_cell_read()
2010 * nvmem_device_cell_write() - Write cell to a given nvmem device
2013 * @info: nvmem cell info to be written.
2014 * @buf: buffer to be written to cell.
2021 struct nvmem_cell_entry cell; in nvmem_device_cell_write() local
2025 return -EINVAL; in nvmem_device_cell_write()
2027 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell); in nvmem_device_cell_write()
2031 return __nvmem_cell_entry_write(&cell, buf, cell.bytes); in nvmem_device_cell_write()
2036 * nvmem_device_read() - Read from a given nvmem device
2053 return -EINVAL; in nvmem_device_read()
2065 * nvmem_device_write() - Write cell to a given nvmem device
2081 return -EINVAL; in nvmem_device_write()
2094 * nvmem_add_cell_table() - register a table of cell info entries
2096 * @table: table of cell info entries
2101 list_add_tail(&table->node, &nvmem_cell_tables); in nvmem_add_cell_table()
2107 * nvmem_del_cell_table() - remove a previously registered cell info table
2109 * @table: table of cell info entries
2114 list_del(&table->node); in nvmem_del_cell_table()
2120 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2122 * @entries: array of cell lookup entries
2123 * @nentries: number of cell lookup entries in the array
2137 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2140 * @entries: array of cell lookup entries
2141 * @nentries: number of cell lookup entries in the array
2155 * nvmem_dev_name() - Get the name of a given nvmem device.
2163 return dev_name(&nvmem->dev); in nvmem_dev_name()
2168 * nvmem_dev_size() - Get the size of a given nvmem device.
2176 return nvmem->size; in nvmem_dev_size()
2205 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");