Lines Matching +full:cell +full:- +full:value
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>
86 if (nvmem->reg_read) in __nvmem_reg_read()
87 return nvmem->reg_read(nvmem->priv, offset, val, bytes); in __nvmem_reg_read()
89 return -EINVAL; in __nvmem_reg_read()
97 if (nvmem->reg_write) { in __nvmem_reg_write()
98 gpiod_set_value_cansleep(nvmem->wp_gpio, 0); in __nvmem_reg_write()
99 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); in __nvmem_reg_write()
100 gpiod_set_value_cansleep(nvmem->wp_gpio, 1); in __nvmem_reg_write()
104 return -EINVAL; in __nvmem_reg_write()
114 const struct nvmem_keepout *keepout = nvmem->keepout; in nvmem_access_with_keepouts()
115 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout; in nvmem_access_with_keepouts()
122 while ((keepout < keepoutend) && (keepout->end <= offset)) in nvmem_access_with_keepouts()
127 if (offset < keepout->start) { in nvmem_access_with_keepouts()
128 kend = min(end, keepout->start); in nvmem_access_with_keepouts()
129 ksize = kend - offset; in nvmem_access_with_keepouts()
146 kend = min(end, keepout->end); in nvmem_access_with_keepouts()
147 ksize = kend - offset; in nvmem_access_with_keepouts()
149 memset(val, keepout->value, ksize); in nvmem_access_with_keepouts()
161 ksize = end - offset; in nvmem_access_with_keepouts()
174 if (!nvmem->nkeepout) in nvmem_reg_read()
183 if (!nvmem->nkeepout) in nvmem_reg_write()
207 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]); in type_show()
225 if (attr->private) in bin_attr_nvmem_read()
226 dev = attr->private; in bin_attr_nvmem_read()
232 if (pos >= nvmem->size) in bin_attr_nvmem_read()
235 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_read()
236 return -EINVAL; in bin_attr_nvmem_read()
238 if (count < nvmem->word_size) in bin_attr_nvmem_read()
239 return -EINVAL; in bin_attr_nvmem_read()
241 if (pos + count > nvmem->size) in bin_attr_nvmem_read()
242 count = nvmem->size - pos; in bin_attr_nvmem_read()
244 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_read()
246 if (!nvmem->reg_read) in bin_attr_nvmem_read()
247 return -EPERM; in bin_attr_nvmem_read()
265 if (attr->private) in bin_attr_nvmem_write()
266 dev = attr->private; in bin_attr_nvmem_write()
272 if (pos >= nvmem->size) in bin_attr_nvmem_write()
273 return -EFBIG; in bin_attr_nvmem_write()
275 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_write()
276 return -EINVAL; in bin_attr_nvmem_write()
278 if (count < nvmem->word_size) in bin_attr_nvmem_write()
279 return -EINVAL; in bin_attr_nvmem_write()
281 if (pos + count > nvmem->size) in bin_attr_nvmem_write()
282 count = nvmem->size - pos; in bin_attr_nvmem_write()
284 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_write()
286 if (!nvmem->reg_write) in bin_attr_nvmem_write()
287 return -EPERM; in bin_attr_nvmem_write()
301 if (!nvmem->root_only) in nvmem_bin_attr_get_umode()
304 if (!nvmem->read_only) in nvmem_bin_attr_get_umode()
307 if (!nvmem->reg_write) in nvmem_bin_attr_get_umode()
310 if (!nvmem->reg_read) in nvmem_bin_attr_get_umode()
322 attr->size = nvmem->size; in nvmem_bin_attr_is_visible()
362 * nvmem_setup_compat() - Create an additional binary entry in
371 if (!config->compat) in nvmem_sysfs_setup_compat()
374 if (!config->base_dev) in nvmem_sysfs_setup_compat()
375 return -EINVAL; in nvmem_sysfs_setup_compat()
377 if (config->type == NVMEM_TYPE_FRAM) in nvmem_sysfs_setup_compat()
380 nvmem->eeprom = bin_attr_nvmem_eeprom_compat; in nvmem_sysfs_setup_compat()
381 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem); in nvmem_sysfs_setup_compat()
382 nvmem->eeprom.size = nvmem->size; in nvmem_sysfs_setup_compat()
384 nvmem->eeprom.attr.key = &eeprom_lock_key; in nvmem_sysfs_setup_compat()
386 nvmem->eeprom.private = &nvmem->dev; in nvmem_sysfs_setup_compat()
387 nvmem->base_dev = config->base_dev; in nvmem_sysfs_setup_compat()
389 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_setup_compat()
391 dev_err(&nvmem->dev, in nvmem_sysfs_setup_compat()
396 nvmem->flags |= FLAG_COMPAT; in nvmem_sysfs_setup_compat()
404 if (config->compat) in nvmem_sysfs_remove_compat()
405 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_remove_compat()
413 return -ENOSYS; in nvmem_sysfs_setup_compat()
426 ida_free(&nvmem_ida, nvmem->id); in nvmem_release()
427 gpiod_put(nvmem->wp_gpio); in nvmem_release()
439 static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell) in nvmem_cell_entry_drop() argument
441 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell); in nvmem_cell_entry_drop()
443 list_del(&cell->node); in nvmem_cell_entry_drop()
445 of_node_put(cell->np); in nvmem_cell_entry_drop()
446 kfree_const(cell->name); in nvmem_cell_entry_drop()
447 kfree(cell); in nvmem_cell_entry_drop()
452 struct nvmem_cell_entry *cell, *p; in nvmem_device_remove_all_cells() local
454 list_for_each_entry_safe(cell, p, &nvmem->cells, node) in nvmem_device_remove_all_cells()
455 nvmem_cell_entry_drop(cell); in nvmem_device_remove_all_cells()
458 static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell) in nvmem_cell_entry_add() argument
461 list_add_tail(&cell->node, &cell->nvmem->cells); in nvmem_cell_entry_add()
463 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell); in nvmem_cell_entry_add()
468 struct nvmem_cell_entry *cell) in nvmem_cell_info_to_nvmem_cell_entry_nodup() argument
470 cell->nvmem = nvmem; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
471 cell->offset = info->offset; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
472 cell->raw_len = info->raw_len ?: info->bytes; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
473 cell->bytes = info->bytes; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
474 cell->name = info->name; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
475 cell->read_post_process = info->read_post_process; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
476 cell->priv = info->priv; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
478 cell->bit_offset = info->bit_offset; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
479 cell->nbits = info->nbits; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
480 cell->np = info->np; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
482 if (cell->nbits) in nvmem_cell_info_to_nvmem_cell_entry_nodup()
483 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, in nvmem_cell_info_to_nvmem_cell_entry_nodup()
486 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_cell_info_to_nvmem_cell_entry_nodup()
487 dev_err(&nvmem->dev, in nvmem_cell_info_to_nvmem_cell_entry_nodup()
488 "cell %s unaligned to nvmem stride %d\n", in nvmem_cell_info_to_nvmem_cell_entry_nodup()
489 cell->name ?: "<unknown>", nvmem->stride); in nvmem_cell_info_to_nvmem_cell_entry_nodup()
490 return -EINVAL; in nvmem_cell_info_to_nvmem_cell_entry_nodup()
498 struct nvmem_cell_entry *cell) in nvmem_cell_info_to_nvmem_cell_entry() argument
502 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell); in nvmem_cell_info_to_nvmem_cell_entry()
506 cell->name = kstrdup_const(info->name, GFP_KERNEL); in nvmem_cell_info_to_nvmem_cell_entry()
507 if (!cell->name) in nvmem_cell_info_to_nvmem_cell_entry()
508 return -ENOMEM; in nvmem_cell_info_to_nvmem_cell_entry()
514 * nvmem_add_one_cell() - Add one cell information to an nvmem device
517 * @info: nvmem cell info to add to the device
524 struct nvmem_cell_entry *cell; in nvmem_add_one_cell() local
527 cell = kzalloc(sizeof(*cell), GFP_KERNEL); in nvmem_add_one_cell()
528 if (!cell) in nvmem_add_one_cell()
529 return -ENOMEM; in nvmem_add_one_cell()
531 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell); in nvmem_add_one_cell()
533 kfree(cell); in nvmem_add_one_cell()
537 nvmem_cell_entry_add(cell); in nvmem_add_one_cell()
544 * nvmem_add_cells() - Add cell information to an nvmem device
547 * @info: nvmem cell info to add to the device
568 * nvmem_register_notifier() - Register a notifier block for nvmem events.
581 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
597 struct nvmem_cell_entry *cell; in nvmem_add_cells_from_table() local
602 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { in nvmem_add_cells_from_table()
603 for (i = 0; i < table->ncells; i++) { in nvmem_add_cells_from_table()
604 info = &table->cells[i]; in nvmem_add_cells_from_table()
606 cell = kzalloc(sizeof(*cell), GFP_KERNEL); in nvmem_add_cells_from_table()
607 if (!cell) { in nvmem_add_cells_from_table()
608 rval = -ENOMEM; in nvmem_add_cells_from_table()
612 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell); in nvmem_add_cells_from_table()
614 kfree(cell); in nvmem_add_cells_from_table()
618 nvmem_cell_entry_add(cell); in nvmem_add_cells_from_table()
631 struct nvmem_cell_entry *iter, *cell = NULL; in nvmem_find_cell_entry_by_name() local
634 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_entry_by_name()
635 if (strcmp(cell_id, iter->name) == 0) { in nvmem_find_cell_entry_by_name()
636 cell = iter; in nvmem_find_cell_entry_by_name()
642 return cell; in nvmem_find_cell_entry_by_name()
648 const struct nvmem_keepout *keepout = nvmem->keepout; in nvmem_validate_keepouts()
649 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout; in nvmem_validate_keepouts()
653 if (keepout->start < cur) { in nvmem_validate_keepouts()
654 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
657 return -ERANGE; in nvmem_validate_keepouts()
660 if (keepout->end < keepout->start) { in nvmem_validate_keepouts()
661 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
664 return -EINVAL; in nvmem_validate_keepouts()
671 if ((keepout->end - keepout->start < nvmem->word_size) || in nvmem_validate_keepouts()
672 ((keepout->start != cur) && in nvmem_validate_keepouts()
673 (keepout->start - cur < nvmem->word_size))) { in nvmem_validate_keepouts()
675 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
678 return -ERANGE; in nvmem_validate_keepouts()
682 if (!IS_ALIGNED(keepout->start, nvmem->stride) || in nvmem_validate_keepouts()
683 !IS_ALIGNED(keepout->end, nvmem->stride)) { in nvmem_validate_keepouts()
685 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
688 return -EINVAL; in nvmem_validate_keepouts()
691 cur = keepout->end; in nvmem_validate_keepouts()
700 struct nvmem_layout *layout = nvmem->layout; in nvmem_add_cells_from_dt()
701 struct device *dev = &nvmem->dev; in nvmem_add_cells_from_dt()
715 return -EINVAL; in nvmem_add_cells_from_dt()
730 if (layout && layout->fixup_cell_info) in nvmem_add_cells_from_dt()
731 layout->fixup_cell_info(nvmem, layout, &info); in nvmem_add_cells_from_dt()
746 return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node); in nvmem_add_cells_from_legacy_of()
758 if (of_device_is_compatible(layout_np, "fixed-layout")) in nvmem_add_cells_from_fixed_layout()
768 layout->owner = owner; in __nvmem_layout_register()
771 list_add(&layout->node, &nvmem_layouts); in __nvmem_layout_register()
785 list_del(&layout->node); in nvmem_layout_unregister()
793 struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER); in nvmem_layout_get()
800 * In case the nvmem device was built-in while the layout was built as a in nvmem_layout_get()
809 if (of_match_node(l->of_match_table, layout_np)) { in nvmem_layout_get()
810 if (try_module_get(l->owner)) in nvmem_layout_get()
826 module_put(layout->owner); in nvmem_layout_put()
831 struct nvmem_layout *layout = nvmem->layout; in nvmem_add_cells_from_layout()
834 if (layout && layout->add_cells) { in nvmem_add_cells_from_layout()
835 ret = layout->add_cells(&nvmem->dev, nvmem, layout); in nvmem_add_cells_from_layout()
845 * of_nvmem_layout_get_container() - Get OF node to layout container.
854 return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout"); in of_nvmem_layout_get_container()
866 match = of_match_node(layout->of_match_table, layout_np); in nvmem_layout_get_match_data()
868 return match ? match->data : NULL; in nvmem_layout_get_match_data()
873 * nvmem_register() - Register a nvmem device for given nvmem_config.
874 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
887 if (!config->dev) in nvmem_register()
888 return ERR_PTR(-EINVAL); in nvmem_register()
890 if (!config->reg_read && !config->reg_write) in nvmem_register()
891 return ERR_PTR(-EINVAL); in nvmem_register()
895 return ERR_PTR(-ENOMEM); in nvmem_register()
903 nvmem->id = rval; in nvmem_register()
905 nvmem->dev.type = &nvmem_provider_type; in nvmem_register()
906 nvmem->dev.bus = &nvmem_bus_type; in nvmem_register()
907 nvmem->dev.parent = config->dev; in nvmem_register()
909 device_initialize(&nvmem->dev); in nvmem_register()
911 if (!config->ignore_wp) in nvmem_register()
912 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", in nvmem_register()
914 if (IS_ERR(nvmem->wp_gpio)) { in nvmem_register()
915 rval = PTR_ERR(nvmem->wp_gpio); in nvmem_register()
916 nvmem->wp_gpio = NULL; in nvmem_register()
920 kref_init(&nvmem->refcnt); in nvmem_register()
921 INIT_LIST_HEAD(&nvmem->cells); in nvmem_register()
923 nvmem->owner = config->owner; in nvmem_register()
924 if (!nvmem->owner && config->dev->driver) in nvmem_register()
925 nvmem->owner = config->dev->driver->owner; in nvmem_register()
926 nvmem->stride = config->stride ?: 1; in nvmem_register()
927 nvmem->word_size = config->word_size ?: 1; in nvmem_register()
928 nvmem->size = config->size; in nvmem_register()
929 nvmem->root_only = config->root_only; in nvmem_register()
930 nvmem->priv = config->priv; in nvmem_register()
931 nvmem->type = config->type; in nvmem_register()
932 nvmem->reg_read = config->reg_read; in nvmem_register()
933 nvmem->reg_write = config->reg_write; in nvmem_register()
934 nvmem->keepout = config->keepout; in nvmem_register()
935 nvmem->nkeepout = config->nkeepout; in nvmem_register()
936 if (config->of_node) in nvmem_register()
937 nvmem->dev.of_node = config->of_node; in nvmem_register()
938 else if (!config->no_of_node) in nvmem_register()
939 nvmem->dev.of_node = config->dev->of_node; in nvmem_register()
941 switch (config->id) { in nvmem_register()
943 rval = dev_set_name(&nvmem->dev, "%s", config->name); in nvmem_register()
946 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); in nvmem_register()
949 rval = dev_set_name(&nvmem->dev, "%s%d", in nvmem_register()
950 config->name ? : "nvmem", in nvmem_register()
951 config->name ? config->id : nvmem->id); in nvmem_register()
958 nvmem->read_only = device_property_present(config->dev, "read-only") || in nvmem_register()
959 config->read_only || !nvmem->reg_write; in nvmem_register()
962 nvmem->dev.groups = nvmem_dev_groups; in nvmem_register()
965 if (nvmem->nkeepout) { in nvmem_register()
971 if (config->compat) { in nvmem_register()
978 * If the driver supplied a layout by config->layout, the module in nvmem_register()
981 nvmem->layout = config->layout ?: nvmem_layout_get(nvmem); in nvmem_register()
982 if (IS_ERR(nvmem->layout)) { in nvmem_register()
983 rval = PTR_ERR(nvmem->layout); in nvmem_register()
984 nvmem->layout = NULL; in nvmem_register()
986 if (rval == -EPROBE_DEFER) in nvmem_register()
990 if (config->cells) { in nvmem_register()
991 rval = nvmem_add_cells(nvmem, config->cells, config->ncells); in nvmem_register()
1012 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); in nvmem_register()
1014 rval = device_add(&nvmem->dev); in nvmem_register()
1024 nvmem_layout_put(nvmem->layout); in nvmem_register()
1026 if (config->compat) in nvmem_register()
1029 put_device(&nvmem->dev); in nvmem_register()
1043 if (nvmem->flags & FLAG_COMPAT) in nvmem_device_release()
1044 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_device_release()
1047 nvmem_layout_put(nvmem->layout); in nvmem_device_release()
1048 device_unregister(&nvmem->dev); in nvmem_device_release()
1052 * nvmem_unregister() - Unregister previously registered nvmem device
1059 kref_put(&nvmem->refcnt, nvmem_device_release); in nvmem_unregister()
1069 * devm_nvmem_register() - Register a managed nvmem device for given
1071 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1109 return ERR_PTR(-EPROBE_DEFER); in __nvmem_device_get()
1111 if (!try_module_get(nvmem->owner)) { in __nvmem_device_get()
1112 dev_err(&nvmem->dev, in __nvmem_device_get()
1113 "could not increase module refcount for cell %s\n", in __nvmem_device_get()
1116 put_device(&nvmem->dev); in __nvmem_device_get()
1117 return ERR_PTR(-EINVAL); in __nvmem_device_get()
1120 kref_get(&nvmem->refcnt); in __nvmem_device_get()
1127 put_device(&nvmem->dev); in __nvmem_device_put()
1128 module_put(nvmem->owner); in __nvmem_device_put()
1129 kref_put(&nvmem->refcnt, nvmem_device_release); in __nvmem_device_put()
1134 * of_nvmem_device_get() - Get nvmem device from a given id
1137 * @id: nvmem name from nvmem-names property.
1150 index = of_property_match_string(np, "nvmem-names", id); in of_nvmem_device_get()
1154 return ERR_PTR(-ENOENT); in of_nvmem_device_get()
1164 * nvmem_device_get() - Get nvmem device from a given id
1174 if (dev->of_node) { /* try dt first */ in nvmem_device_get()
1177 nvmem = of_nvmem_device_get(dev->of_node, dev_name); in nvmem_device_get()
1179 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) in nvmem_device_get()
1189 * nvmem_device_find() - Find nvmem device with matching function
1220 * devm_nvmem_device_put() - put alredy got nvmem device
1238 * nvmem_device_put() - put alredy got nvmem device
1249 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1264 return ERR_PTR(-ENOMEM); in devm_nvmem_device_get()
1281 struct nvmem_cell *cell; in nvmem_create_cell() local
1284 cell = kzalloc(sizeof(*cell), GFP_KERNEL); in nvmem_create_cell()
1285 if (!cell) in nvmem_create_cell()
1286 return ERR_PTR(-ENOMEM); in nvmem_create_cell()
1291 kfree(cell); in nvmem_create_cell()
1292 return ERR_PTR(-ENOMEM); in nvmem_create_cell()
1296 cell->id = name; in nvmem_create_cell()
1297 cell->entry = entry; in nvmem_create_cell()
1298 cell->index = index; in nvmem_create_cell()
1300 return cell; in nvmem_create_cell()
1307 struct nvmem_cell *cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup() local
1313 return ERR_PTR(-EINVAL); in nvmem_cell_get_from_lookup()
1320 if ((strcmp(lookup->dev_id, dev_id) == 0) && in nvmem_cell_get_from_lookup()
1321 (strcmp(lookup->con_id, con_id) == 0)) { in nvmem_cell_get_from_lookup()
1323 nvmem = __nvmem_device_get((void *)lookup->nvmem_name, in nvmem_cell_get_from_lookup()
1327 cell = ERR_CAST(nvmem); in nvmem_cell_get_from_lookup()
1332 lookup->cell_name); in nvmem_cell_get_from_lookup()
1335 cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup()
1337 cell = nvmem_create_cell(cell_entry, con_id, 0); in nvmem_cell_get_from_lookup()
1338 if (IS_ERR(cell)) in nvmem_cell_get_from_lookup()
1346 return cell; in nvmem_cell_get_from_lookup()
1353 struct nvmem_cell_entry *iter, *cell = NULL; in nvmem_find_cell_entry_by_node() local
1356 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_entry_by_node()
1357 if (np == iter->np) { in nvmem_find_cell_entry_by_node()
1358 cell = iter; in nvmem_find_cell_entry_by_node()
1364 return cell; in nvmem_find_cell_entry_by_node()
1368 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1370 * @np: Device tree node that uses the nvmem cell.
1371 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1372 * for the cell at index 0 (the lone cell with no accompanying
1373 * nvmem-cell-names property).
1384 struct nvmem_cell *cell; in of_nvmem_cell_get() local
1390 /* if cell name exists, find index to the name */ in of_nvmem_cell_get()
1392 index = of_property_match_string(np, "nvmem-cell-names", id); in of_nvmem_cell_get()
1394 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells", in of_nvmem_cell_get()
1395 "#nvmem-cell-cells", in of_nvmem_cell_get()
1398 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1401 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1410 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1413 /* nvmem layouts produce cells within the nvmem-layout container */ in of_nvmem_cell_get()
1414 if (of_node_name_eq(nvmem_np, "nvmem-layout")) { in of_nvmem_cell_get()
1418 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1433 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1436 cell = nvmem_create_cell(cell_entry, id, cell_index); in of_nvmem_cell_get()
1437 if (IS_ERR(cell)) in of_nvmem_cell_get()
1440 return cell; in of_nvmem_cell_get()
1446 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1448 * @dev: Device that requests the nvmem cell.
1449 * @id: nvmem cell name to get (this corresponds with the name from the
1450 * nvmem-cell-names property for DT systems and with the con_id from
1451 * the lookup entry for non-DT systems).
1459 struct nvmem_cell *cell; in nvmem_cell_get() local
1461 if (dev->of_node) { /* try dt first */ in nvmem_cell_get()
1462 cell = of_nvmem_cell_get(dev->of_node, id); in nvmem_cell_get()
1463 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) in nvmem_cell_get()
1464 return cell; in nvmem_cell_get()
1467 /* NULL cell id only allowed for device tree; invalid otherwise */ in nvmem_cell_get()
1469 return ERR_PTR(-EINVAL); in nvmem_cell_get()
1481 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1483 * @dev: Device that requests the nvmem cell.
1484 * @id: nvmem cell name id to get.
1492 struct nvmem_cell **ptr, *cell; in devm_nvmem_cell_get() local
1496 return ERR_PTR(-ENOMEM); in devm_nvmem_cell_get()
1498 cell = nvmem_cell_get(dev, id); in devm_nvmem_cell_get()
1499 if (!IS_ERR(cell)) { in devm_nvmem_cell_get()
1500 *ptr = cell; in devm_nvmem_cell_get()
1506 return cell; in devm_nvmem_cell_get()
1521 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1524 * @dev: Device that requests the nvmem cell.
1525 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1527 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell) in devm_nvmem_cell_put() argument
1532 devm_nvmem_cell_match, cell); in devm_nvmem_cell_put()
1539 * nvmem_cell_put() - Release previously allocated nvmem cell.
1541 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1543 void nvmem_cell_put(struct nvmem_cell *cell) in nvmem_cell_put() argument
1545 struct nvmem_device *nvmem = cell->entry->nvmem; in nvmem_cell_put()
1547 if (cell->id) in nvmem_cell_put()
1548 kfree_const(cell->id); in nvmem_cell_put()
1550 kfree(cell); in nvmem_cell_put()
1555 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf) in nvmem_shift_read_buffer_in_place() argument
1558 int i, extra, bit_offset = cell->bit_offset; in nvmem_shift_read_buffer_in_place()
1566 for (i = 1; i < cell->bytes; i++) { in nvmem_shift_read_buffer_in_place()
1568 *p |= *b << (BITS_PER_BYTE - bit_offset); in nvmem_shift_read_buffer_in_place()
1575 p += cell->bytes - 1; in nvmem_shift_read_buffer_in_place()
1579 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE); in nvmem_shift_read_buffer_in_place()
1580 while (--extra >= 0) in nvmem_shift_read_buffer_in_place()
1581 *p-- = 0; in nvmem_shift_read_buffer_in_place()
1584 if (cell->nbits % BITS_PER_BYTE) in nvmem_shift_read_buffer_in_place()
1585 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0); in nvmem_shift_read_buffer_in_place()
1589 struct nvmem_cell_entry *cell, in __nvmem_cell_read() argument
1594 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len); in __nvmem_cell_read()
1599 /* shift bits in-place */ in __nvmem_cell_read()
1600 if (cell->bit_offset || cell->nbits) in __nvmem_cell_read()
1601 nvmem_shift_read_buffer_in_place(cell, buf); in __nvmem_cell_read()
1603 if (cell->read_post_process) { in __nvmem_cell_read()
1604 rc = cell->read_post_process(cell->priv, id, index, in __nvmem_cell_read()
1605 cell->offset, buf, cell->raw_len); in __nvmem_cell_read()
1611 *len = cell->bytes; in __nvmem_cell_read()
1617 * nvmem_cell_read() - Read a given nvmem cell
1619 * @cell: nvmem cell to be read.
1620 * @len: pointer to length of cell which will be populated on successful read;
1626 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len) in nvmem_cell_read() argument
1628 struct nvmem_cell_entry *entry = cell->entry; in nvmem_cell_read()
1629 struct nvmem_device *nvmem = entry->nvmem; in nvmem_cell_read()
1634 return ERR_PTR(-EINVAL); in nvmem_cell_read()
1636 buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL); in nvmem_cell_read()
1638 return ERR_PTR(-ENOMEM); in nvmem_cell_read()
1640 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index); in nvmem_cell_read()
1650 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell, in nvmem_cell_prepare_write_buffer() argument
1653 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_prepare_write_buffer()
1654 int i, rc, nbits, bit_offset = cell->bit_offset; in nvmem_cell_prepare_write_buffer()
1657 nbits = cell->nbits; in nvmem_cell_prepare_write_buffer()
1658 buf = kzalloc(cell->bytes, GFP_KERNEL); in nvmem_cell_prepare_write_buffer()
1660 return ERR_PTR(-ENOMEM); in nvmem_cell_prepare_write_buffer()
1670 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); in nvmem_cell_prepare_write_buffer()
1673 *b++ |= GENMASK(bit_offset - 1, 0) & v; in nvmem_cell_prepare_write_buffer()
1676 for (i = 1; i < cell->bytes; i++) { in nvmem_cell_prepare_write_buffer()
1678 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); in nvmem_cell_prepare_write_buffer()
1690 cell->offset + cell->bytes - 1, &v, 1); in nvmem_cell_prepare_write_buffer()
1703 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len) in __nvmem_cell_entry_write() argument
1705 struct nvmem_device *nvmem = cell->nvmem; in __nvmem_cell_entry_write()
1708 if (!nvmem || nvmem->read_only || in __nvmem_cell_entry_write()
1709 (cell->bit_offset == 0 && len != cell->bytes)) in __nvmem_cell_entry_write()
1710 return -EINVAL; in __nvmem_cell_entry_write()
1713 * Any cells which have a read_post_process hook are read-only because in __nvmem_cell_entry_write()
1717 if (cell->read_post_process) in __nvmem_cell_entry_write()
1718 return -EINVAL; in __nvmem_cell_entry_write()
1720 if (cell->bit_offset || cell->nbits) { in __nvmem_cell_entry_write()
1721 buf = nvmem_cell_prepare_write_buffer(cell, buf, len); in __nvmem_cell_entry_write()
1726 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); in __nvmem_cell_entry_write()
1729 if (cell->bit_offset || cell->nbits) in __nvmem_cell_entry_write()
1739 * nvmem_cell_write() - Write to a given nvmem cell
1741 * @cell: nvmem cell to be written.
1743 * @len: length of buffer to be written to nvmem cell.
1747 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len) in nvmem_cell_write() argument
1749 return __nvmem_cell_entry_write(cell->entry, buf, len); in nvmem_cell_write()
1757 struct nvmem_cell *cell; in nvmem_cell_read_common() local
1761 cell = nvmem_cell_get(dev, cell_id); in nvmem_cell_read_common()
1762 if (IS_ERR(cell)) in nvmem_cell_read_common()
1763 return PTR_ERR(cell); in nvmem_cell_read_common()
1765 buf = nvmem_cell_read(cell, &len); in nvmem_cell_read_common()
1767 nvmem_cell_put(cell); in nvmem_cell_read_common()
1772 nvmem_cell_put(cell); in nvmem_cell_read_common()
1773 return -EINVAL; in nvmem_cell_read_common()
1777 nvmem_cell_put(cell); in nvmem_cell_read_common()
1783 * nvmem_cell_read_u8() - Read a cell value as a u8
1785 * @dev: Device that requests the nvmem cell.
1786 * @cell_id: Name of nvmem cell to read.
1787 * @val: pointer to output value.
1798 * nvmem_cell_read_u16() - Read a cell value as a u16
1800 * @dev: Device that requests the nvmem cell.
1801 * @cell_id: Name of nvmem cell to read.
1802 * @val: pointer to output value.
1813 * nvmem_cell_read_u32() - Read a cell value as a u32
1815 * @dev: Device that requests the nvmem cell.
1816 * @cell_id: Name of nvmem cell to read.
1817 * @val: pointer to output value.
1828 * nvmem_cell_read_u64() - Read a cell value as a u64
1830 * @dev: Device that requests the nvmem cell.
1831 * @cell_id: Name of nvmem cell to read.
1832 * @val: pointer to output value.
1846 struct nvmem_cell *cell; in nvmem_cell_read_variable_common() local
1850 cell = nvmem_cell_get(dev, cell_id); in nvmem_cell_read_variable_common()
1851 if (IS_ERR(cell)) in nvmem_cell_read_variable_common()
1852 return cell; in nvmem_cell_read_variable_common()
1854 nbits = cell->entry->nbits; in nvmem_cell_read_variable_common()
1855 buf = nvmem_cell_read(cell, len); in nvmem_cell_read_variable_common()
1856 nvmem_cell_put(cell); in nvmem_cell_read_variable_common()
1869 return ERR_PTR(-ERANGE); in nvmem_cell_read_variable_common()
1876 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1878 * @dev: Device that requests the nvmem cell.
1879 * @cell_id: Name of nvmem cell to read.
1880 * @val: pointer to output value.
1907 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1909 * @dev: Device that requests the nvmem cell.
1910 * @cell_id: Name of nvmem cell to read.
1911 * @val: pointer to output value.
1938 * nvmem_device_cell_read() - Read a given nvmem device and cell
1941 * @info: nvmem cell info to be read.
1950 struct nvmem_cell_entry cell; in nvmem_device_cell_read() local
1955 return -EINVAL; in nvmem_device_cell_read()
1957 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell); in nvmem_device_cell_read()
1961 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0); in nvmem_device_cell_read()
1970 * nvmem_device_cell_write() - Write cell to a given nvmem device
1973 * @info: nvmem cell info to be written.
1974 * @buf: buffer to be written to cell.
1981 struct nvmem_cell_entry cell; in nvmem_device_cell_write() local
1985 return -EINVAL; in nvmem_device_cell_write()
1987 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell); in nvmem_device_cell_write()
1991 return __nvmem_cell_entry_write(&cell, buf, cell.bytes); in nvmem_device_cell_write()
1996 * nvmem_device_read() - Read from a given nvmem device
2013 return -EINVAL; in nvmem_device_read()
2025 * nvmem_device_write() - Write cell to a given nvmem device
2041 return -EINVAL; in nvmem_device_write()
2054 * nvmem_add_cell_table() - register a table of cell info entries
2056 * @table: table of cell info entries
2061 list_add_tail(&table->node, &nvmem_cell_tables); in nvmem_add_cell_table()
2067 * nvmem_del_cell_table() - remove a previously registered cell info table
2069 * @table: table of cell info entries
2074 list_del(&table->node); in nvmem_del_cell_table()
2080 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2082 * @entries: array of cell lookup entries
2083 * @nentries: number of cell lookup entries in the array
2097 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2100 * @entries: array of cell lookup entries
2101 * @nentries: number of cell lookup entries in the array
2115 * nvmem_dev_name() - Get the name of a given nvmem device.
2123 return dev_name(&nvmem->dev); in nvmem_dev_name()
2141 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");