Lines Matching +full:nvmem +full:- +full:cells

1 // SPDX-License-Identifier: GPL-2.0
3 * nvmem framework core.
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
36 struct list_head cells; member
56 struct nvmem_device *nvmem; member
71 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, in __nvmem_reg_read() argument
74 if (nvmem->reg_read) in __nvmem_reg_read()
75 return nvmem->reg_read(nvmem->priv, offset, val, bytes); in __nvmem_reg_read()
77 return -EINVAL; in __nvmem_reg_read()
80 static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, in __nvmem_reg_write() argument
85 if (nvmem->reg_write) { in __nvmem_reg_write()
86 gpiod_set_value_cansleep(nvmem->wp_gpio, 0); in __nvmem_reg_write()
87 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); in __nvmem_reg_write()
88 gpiod_set_value_cansleep(nvmem->wp_gpio, 1); in __nvmem_reg_write()
92 return -EINVAL; in __nvmem_reg_write()
95 static int nvmem_access_with_keepouts(struct nvmem_device *nvmem, in nvmem_access_with_keepouts() argument
102 const struct nvmem_keepout *keepout = nvmem->keepout; in nvmem_access_with_keepouts()
103 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout; in nvmem_access_with_keepouts()
110 while ((keepout < keepoutend) && (keepout->end <= offset)) in nvmem_access_with_keepouts()
115 if (offset < keepout->start) { in nvmem_access_with_keepouts()
116 kend = min(end, keepout->start); in nvmem_access_with_keepouts()
117 ksize = kend - offset; in nvmem_access_with_keepouts()
119 rc = __nvmem_reg_write(nvmem, offset, val, ksize); in nvmem_access_with_keepouts()
121 rc = __nvmem_reg_read(nvmem, offset, val, ksize); in nvmem_access_with_keepouts()
134 kend = min(end, keepout->end); in nvmem_access_with_keepouts()
135 ksize = kend - offset; in nvmem_access_with_keepouts()
137 memset(val, keepout->value, ksize); in nvmem_access_with_keepouts()
149 ksize = end - offset; in nvmem_access_with_keepouts()
151 return __nvmem_reg_write(nvmem, offset, val, ksize); in nvmem_access_with_keepouts()
153 return __nvmem_reg_read(nvmem, offset, val, ksize); in nvmem_access_with_keepouts()
159 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, in nvmem_reg_read() argument
162 if (!nvmem->nkeepout) in nvmem_reg_read()
163 return __nvmem_reg_read(nvmem, offset, val, bytes); in nvmem_reg_read()
165 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false); in nvmem_reg_read()
168 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, in nvmem_reg_write() argument
171 if (!nvmem->nkeepout) in nvmem_reg_write()
172 return __nvmem_reg_write(nvmem, offset, val, bytes); in nvmem_reg_write()
174 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true); in nvmem_reg_write()
193 struct nvmem_device *nvmem = to_nvmem_device(dev); in type_show() local
195 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]); in type_show()
210 struct nvmem_device *nvmem; in bin_attr_nvmem_read() local
213 if (attr->private) in bin_attr_nvmem_read()
214 dev = attr->private; in bin_attr_nvmem_read()
217 nvmem = to_nvmem_device(dev); in bin_attr_nvmem_read()
220 if (pos >= nvmem->size) in bin_attr_nvmem_read()
223 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_read()
224 return -EINVAL; in bin_attr_nvmem_read()
226 if (count < nvmem->word_size) in bin_attr_nvmem_read()
227 return -EINVAL; in bin_attr_nvmem_read()
229 if (pos + count > nvmem->size) in bin_attr_nvmem_read()
230 count = nvmem->size - pos; in bin_attr_nvmem_read()
232 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_read()
234 if (!nvmem->reg_read) in bin_attr_nvmem_read()
235 return -EPERM; in bin_attr_nvmem_read()
237 rc = nvmem_reg_read(nvmem, pos, buf, count); in bin_attr_nvmem_read()
250 struct nvmem_device *nvmem; in bin_attr_nvmem_write() local
253 if (attr->private) in bin_attr_nvmem_write()
254 dev = attr->private; in bin_attr_nvmem_write()
257 nvmem = to_nvmem_device(dev); in bin_attr_nvmem_write()
260 if (pos >= nvmem->size) in bin_attr_nvmem_write()
261 return -EFBIG; in bin_attr_nvmem_write()
263 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_write()
264 return -EINVAL; in bin_attr_nvmem_write()
266 if (count < nvmem->word_size) in bin_attr_nvmem_write()
267 return -EINVAL; in bin_attr_nvmem_write()
269 if (pos + count > nvmem->size) in bin_attr_nvmem_write()
270 count = nvmem->size - pos; in bin_attr_nvmem_write()
272 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_write()
274 if (!nvmem->reg_write) in bin_attr_nvmem_write()
275 return -EPERM; in bin_attr_nvmem_write()
277 rc = nvmem_reg_write(nvmem, pos, buf, count); in bin_attr_nvmem_write()
285 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem) in nvmem_bin_attr_get_umode() argument
289 if (!nvmem->root_only) in nvmem_bin_attr_get_umode()
292 if (!nvmem->read_only) in nvmem_bin_attr_get_umode()
295 if (!nvmem->reg_write) in nvmem_bin_attr_get_umode()
298 if (!nvmem->reg_read) in nvmem_bin_attr_get_umode()
308 struct nvmem_device *nvmem = to_nvmem_device(dev); in nvmem_bin_attr_is_visible() local
310 return nvmem_bin_attr_get_umode(nvmem); in nvmem_bin_attr_is_visible()
316 .name = "nvmem",
348 * nvmem_setup_compat() - Create an additional binary entry in
352 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, in nvmem_sysfs_setup_compat() argument
357 if (!config->compat) in nvmem_sysfs_setup_compat()
360 if (!config->base_dev) in nvmem_sysfs_setup_compat()
361 return -EINVAL; in nvmem_sysfs_setup_compat()
363 if (config->type == NVMEM_TYPE_FRAM) in nvmem_sysfs_setup_compat()
366 nvmem->eeprom = bin_attr_nvmem_eeprom_compat; in nvmem_sysfs_setup_compat()
367 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem); in nvmem_sysfs_setup_compat()
368 nvmem->eeprom.size = nvmem->size; in nvmem_sysfs_setup_compat()
370 nvmem->eeprom.attr.key = &eeprom_lock_key; in nvmem_sysfs_setup_compat()
372 nvmem->eeprom.private = &nvmem->dev; in nvmem_sysfs_setup_compat()
373 nvmem->base_dev = config->base_dev; in nvmem_sysfs_setup_compat()
375 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_setup_compat()
377 dev_err(&nvmem->dev, in nvmem_sysfs_setup_compat()
382 nvmem->flags |= FLAG_COMPAT; in nvmem_sysfs_setup_compat()
387 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, in nvmem_sysfs_remove_compat() argument
390 if (config->compat) in nvmem_sysfs_remove_compat()
391 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_remove_compat()
396 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, in nvmem_sysfs_setup_compat() argument
399 return -ENOSYS; in nvmem_sysfs_setup_compat()
401 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, in nvmem_sysfs_remove_compat() argument
410 struct nvmem_device *nvmem = to_nvmem_device(dev); in nvmem_release() local
412 ida_free(&nvmem_ida, nvmem->id); in nvmem_release()
413 gpiod_put(nvmem->wp_gpio); in nvmem_release()
414 kfree(nvmem); in nvmem_release()
422 .name = "nvmem",
429 list_del(&cell->node); in nvmem_cell_drop()
431 of_node_put(cell->np); in nvmem_cell_drop()
432 kfree_const(cell->name); in nvmem_cell_drop()
436 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) in nvmem_device_remove_all_cells() argument
440 list_for_each_entry_safe(cell, p, &nvmem->cells, node) in nvmem_device_remove_all_cells()
447 list_add_tail(&cell->node, &cell->nvmem->cells); in nvmem_cell_add()
452 static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell_nodup() argument
456 cell->nvmem = nvmem; in nvmem_cell_info_to_nvmem_cell_nodup()
457 cell->offset = info->offset; in nvmem_cell_info_to_nvmem_cell_nodup()
458 cell->bytes = info->bytes; in nvmem_cell_info_to_nvmem_cell_nodup()
459 cell->name = info->name; in nvmem_cell_info_to_nvmem_cell_nodup()
461 cell->bit_offset = info->bit_offset; in nvmem_cell_info_to_nvmem_cell_nodup()
462 cell->nbits = info->nbits; in nvmem_cell_info_to_nvmem_cell_nodup()
464 if (cell->nbits) in nvmem_cell_info_to_nvmem_cell_nodup()
465 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, in nvmem_cell_info_to_nvmem_cell_nodup()
468 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_cell_info_to_nvmem_cell_nodup()
469 dev_err(&nvmem->dev, in nvmem_cell_info_to_nvmem_cell_nodup()
470 "cell %s unaligned to nvmem stride %d\n", in nvmem_cell_info_to_nvmem_cell_nodup()
471 cell->name ?: "<unknown>", nvmem->stride); in nvmem_cell_info_to_nvmem_cell_nodup()
472 return -EINVAL; in nvmem_cell_info_to_nvmem_cell_nodup()
478 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell() argument
484 err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell); in nvmem_cell_info_to_nvmem_cell()
488 cell->name = kstrdup_const(info->name, GFP_KERNEL); in nvmem_cell_info_to_nvmem_cell()
489 if (!cell->name) in nvmem_cell_info_to_nvmem_cell()
490 return -ENOMEM; in nvmem_cell_info_to_nvmem_cell()
496 * nvmem_add_cells() - Add cell information to an nvmem device
498 * @nvmem: nvmem device to add cells to.
499 * @info: nvmem cell info to add to the device
500 * @ncells: number of cells in info
504 static int nvmem_add_cells(struct nvmem_device *nvmem, in nvmem_add_cells() argument
508 struct nvmem_cell **cells; in nvmem_add_cells() local
511 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL); in nvmem_add_cells()
512 if (!cells) in nvmem_add_cells()
513 return -ENOMEM; in nvmem_add_cells()
516 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL); in nvmem_add_cells()
517 if (!cells[i]) { in nvmem_add_cells()
518 rval = -ENOMEM; in nvmem_add_cells()
522 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); in nvmem_add_cells()
524 kfree(cells[i]); in nvmem_add_cells()
528 nvmem_cell_add(cells[i]); in nvmem_add_cells()
532 kfree(cells); in nvmem_add_cells()
536 while (i--) in nvmem_add_cells()
537 nvmem_cell_drop(cells[i]); in nvmem_add_cells()
539 kfree(cells); in nvmem_add_cells()
545 * nvmem_register_notifier() - Register a notifier block for nvmem events.
547 * @nb: notifier block to be called on nvmem events.
558 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
570 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) in nvmem_add_cells_from_table() argument
579 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { in nvmem_add_cells_from_table()
580 for (i = 0; i < table->ncells; i++) { in nvmem_add_cells_from_table()
581 info = &table->cells[i]; in nvmem_add_cells_from_table()
585 rval = -ENOMEM; in nvmem_add_cells_from_table()
589 rval = nvmem_cell_info_to_nvmem_cell(nvmem, in nvmem_add_cells_from_table()
608 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) in nvmem_find_cell_by_name() argument
613 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_name()
614 if (strcmp(cell_id, iter->name) == 0) { in nvmem_find_cell_by_name()
624 static int nvmem_validate_keepouts(struct nvmem_device *nvmem) in nvmem_validate_keepouts() argument
627 const struct nvmem_keepout *keepout = nvmem->keepout; in nvmem_validate_keepouts()
628 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout; in nvmem_validate_keepouts()
632 if (keepout->start < cur) { in nvmem_validate_keepouts()
633 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
636 return -ERANGE; in nvmem_validate_keepouts()
639 if (keepout->end < keepout->start) { in nvmem_validate_keepouts()
640 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
643 return -EINVAL; in nvmem_validate_keepouts()
650 if ((keepout->end - keepout->start < nvmem->word_size) || in nvmem_validate_keepouts()
651 ((keepout->start != cur) && in nvmem_validate_keepouts()
652 (keepout->start - cur < nvmem->word_size))) { in nvmem_validate_keepouts()
654 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
657 return -ERANGE; in nvmem_validate_keepouts()
661 if (!IS_ALIGNED(keepout->start, nvmem->stride) || in nvmem_validate_keepouts()
662 !IS_ALIGNED(keepout->end, nvmem->stride)) { in nvmem_validate_keepouts()
664 dev_err(&nvmem->dev, in nvmem_validate_keepouts()
667 return -EINVAL; in nvmem_validate_keepouts()
670 cur = keepout->end; in nvmem_validate_keepouts()
677 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) in nvmem_add_cells_from_of() argument
680 struct device *dev = &nvmem->dev; in nvmem_add_cells_from_of()
685 parent = dev->of_node; in nvmem_add_cells_from_of()
692 dev_err(dev, "nvmem: invalid reg on %pOF\n", child); in nvmem_add_cells_from_of()
694 return -EINVAL; in nvmem_add_cells_from_of()
700 return -ENOMEM; in nvmem_add_cells_from_of()
703 cell->nvmem = nvmem; in nvmem_add_cells_from_of()
704 cell->offset = be32_to_cpup(addr++); in nvmem_add_cells_from_of()
705 cell->bytes = be32_to_cpup(addr); in nvmem_add_cells_from_of()
706 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child); in nvmem_add_cells_from_of()
710 cell->bit_offset = be32_to_cpup(addr++); in nvmem_add_cells_from_of()
711 cell->nbits = be32_to_cpup(addr); in nvmem_add_cells_from_of()
714 if (cell->nbits) in nvmem_add_cells_from_of()
715 cell->bytes = DIV_ROUND_UP( in nvmem_add_cells_from_of()
716 cell->nbits + cell->bit_offset, in nvmem_add_cells_from_of()
719 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_add_cells_from_of()
720 dev_err(dev, "cell %s unaligned to nvmem stride %d\n", in nvmem_add_cells_from_of()
721 cell->name, nvmem->stride); in nvmem_add_cells_from_of()
722 /* Cells already added will be freed later. */ in nvmem_add_cells_from_of()
723 kfree_const(cell->name); in nvmem_add_cells_from_of()
726 return -EINVAL; in nvmem_add_cells_from_of()
729 cell->np = of_node_get(child); in nvmem_add_cells_from_of()
737 * nvmem_register() - Register a nvmem device for given nvmem_config.
738 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
740 * @config: nvmem device configuration with which nvmem device is created.
748 struct nvmem_device *nvmem; in nvmem_register() local
751 if (!config->dev) in nvmem_register()
752 return ERR_PTR(-EINVAL); in nvmem_register()
754 if (!config->reg_read && !config->reg_write) in nvmem_register()
755 return ERR_PTR(-EINVAL); in nvmem_register()
757 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); in nvmem_register()
758 if (!nvmem) in nvmem_register()
759 return ERR_PTR(-ENOMEM); in nvmem_register()
763 kfree(nvmem); in nvmem_register()
767 if (config->wp_gpio) in nvmem_register()
768 nvmem->wp_gpio = config->wp_gpio; in nvmem_register()
770 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", in nvmem_register()
772 if (IS_ERR(nvmem->wp_gpio)) { in nvmem_register()
773 ida_free(&nvmem_ida, nvmem->id); in nvmem_register()
774 rval = PTR_ERR(nvmem->wp_gpio); in nvmem_register()
775 kfree(nvmem); in nvmem_register()
779 kref_init(&nvmem->refcnt); in nvmem_register()
780 INIT_LIST_HEAD(&nvmem->cells); in nvmem_register()
782 nvmem->id = rval; in nvmem_register()
783 nvmem->owner = config->owner; in nvmem_register()
784 if (!nvmem->owner && config->dev->driver) in nvmem_register()
785 nvmem->owner = config->dev->driver->owner; in nvmem_register()
786 nvmem->stride = config->stride ?: 1; in nvmem_register()
787 nvmem->word_size = config->word_size ?: 1; in nvmem_register()
788 nvmem->size = config->size; in nvmem_register()
789 nvmem->dev.type = &nvmem_provider_type; in nvmem_register()
790 nvmem->dev.bus = &nvmem_bus_type; in nvmem_register()
791 nvmem->dev.parent = config->dev; in nvmem_register()
792 nvmem->root_only = config->root_only; in nvmem_register()
793 nvmem->priv = config->priv; in nvmem_register()
794 nvmem->type = config->type; in nvmem_register()
795 nvmem->reg_read = config->reg_read; in nvmem_register()
796 nvmem->reg_write = config->reg_write; in nvmem_register()
797 nvmem->keepout = config->keepout; in nvmem_register()
798 nvmem->nkeepout = config->nkeepout; in nvmem_register()
799 if (config->of_node) in nvmem_register()
800 nvmem->dev.of_node = config->of_node; in nvmem_register()
801 else if (!config->no_of_node) in nvmem_register()
802 nvmem->dev.of_node = config->dev->of_node; in nvmem_register()
804 switch (config->id) { in nvmem_register()
806 dev_set_name(&nvmem->dev, "%s", config->name); in nvmem_register()
809 dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); in nvmem_register()
812 dev_set_name(&nvmem->dev, "%s%d", in nvmem_register()
813 config->name ? : "nvmem", in nvmem_register()
814 config->name ? config->id : nvmem->id); in nvmem_register()
818 nvmem->read_only = device_property_present(config->dev, "read-only") || in nvmem_register()
819 config->read_only || !nvmem->reg_write; in nvmem_register()
822 nvmem->dev.groups = nvmem_dev_groups; in nvmem_register()
825 if (nvmem->nkeepout) { in nvmem_register()
826 rval = nvmem_validate_keepouts(nvmem); in nvmem_register()
828 ida_free(&nvmem_ida, nvmem->id); in nvmem_register()
829 kfree(nvmem); in nvmem_register()
834 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); in nvmem_register()
836 rval = device_register(&nvmem->dev); in nvmem_register()
840 if (config->compat) { in nvmem_register()
841 rval = nvmem_sysfs_setup_compat(nvmem, config); in nvmem_register()
846 if (config->cells) { in nvmem_register()
847 rval = nvmem_add_cells(nvmem, config->cells, config->ncells); in nvmem_register()
852 rval = nvmem_add_cells_from_table(nvmem); in nvmem_register()
856 rval = nvmem_add_cells_from_of(nvmem); in nvmem_register()
860 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); in nvmem_register()
862 return nvmem; in nvmem_register()
865 nvmem_device_remove_all_cells(nvmem); in nvmem_register()
867 if (config->compat) in nvmem_register()
868 nvmem_sysfs_remove_compat(nvmem, config); in nvmem_register()
870 device_del(&nvmem->dev); in nvmem_register()
872 put_device(&nvmem->dev); in nvmem_register()
880 struct nvmem_device *nvmem; in nvmem_device_release() local
882 nvmem = container_of(kref, struct nvmem_device, refcnt); in nvmem_device_release()
884 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); in nvmem_device_release()
886 if (nvmem->flags & FLAG_COMPAT) in nvmem_device_release()
887 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_device_release()
889 nvmem_device_remove_all_cells(nvmem); in nvmem_device_release()
890 device_unregister(&nvmem->dev); in nvmem_device_release()
894 * nvmem_unregister() - Unregister previously registered nvmem device
896 * @nvmem: Pointer to previously registered nvmem device.
898 void nvmem_unregister(struct nvmem_device *nvmem) in nvmem_unregister() argument
900 kref_put(&nvmem->refcnt, nvmem_device_release); in nvmem_unregister()
910 * devm_nvmem_register() - Register a managed nvmem device for given
912 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
914 * @dev: Device that uses the nvmem device.
915 * @config: nvmem device configuration with which nvmem device is created.
923 struct nvmem_device **ptr, *nvmem; in devm_nvmem_register() local
927 return ERR_PTR(-ENOMEM); in devm_nvmem_register()
929 nvmem = nvmem_register(config); in devm_nvmem_register()
931 if (!IS_ERR(nvmem)) { in devm_nvmem_register()
932 *ptr = nvmem; in devm_nvmem_register()
938 return nvmem; in devm_nvmem_register()
950 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
953 * @dev: Device that uses the nvmem device.
954 * @nvmem: Pointer to previously registered nvmem device.
958 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_unregister() argument
960 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); in devm_nvmem_unregister()
967 struct nvmem_device *nvmem = NULL; in __nvmem_device_get() local
973 nvmem = to_nvmem_device(dev); in __nvmem_device_get()
975 if (!nvmem) in __nvmem_device_get()
976 return ERR_PTR(-EPROBE_DEFER); in __nvmem_device_get()
978 if (!try_module_get(nvmem->owner)) { in __nvmem_device_get()
979 dev_err(&nvmem->dev, in __nvmem_device_get()
981 nvmem_dev_name(nvmem)); in __nvmem_device_get()
983 put_device(&nvmem->dev); in __nvmem_device_get()
984 return ERR_PTR(-EINVAL); in __nvmem_device_get()
987 kref_get(&nvmem->refcnt); in __nvmem_device_get()
989 return nvmem; in __nvmem_device_get()
992 static void __nvmem_device_put(struct nvmem_device *nvmem) in __nvmem_device_put() argument
994 put_device(&nvmem->dev); in __nvmem_device_put()
995 module_put(nvmem->owner); in __nvmem_device_put()
996 kref_put(&nvmem->refcnt, nvmem_device_release); in __nvmem_device_put()
1001 * of_nvmem_device_get() - Get nvmem device from a given id
1003 * @np: Device tree node that uses the nvmem device.
1004 * @id: nvmem name from nvmem-names property.
1013 struct nvmem_device *nvmem; in of_nvmem_device_get() local
1017 index = of_property_match_string(np, "nvmem-names", id); in of_nvmem_device_get()
1019 nvmem_np = of_parse_phandle(np, "nvmem", index); in of_nvmem_device_get()
1021 return ERR_PTR(-ENOENT); in of_nvmem_device_get()
1023 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); in of_nvmem_device_get()
1025 return nvmem; in of_nvmem_device_get()
1031 * nvmem_device_get() - Get nvmem device from a given id
1033 * @dev: Device that uses the nvmem device.
1034 * @dev_name: name of the requested nvmem device.
1041 if (dev->of_node) { /* try dt first */ in nvmem_device_get()
1042 struct nvmem_device *nvmem; in nvmem_device_get() local
1044 nvmem = of_nvmem_device_get(dev->of_node, dev_name); in nvmem_device_get()
1046 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) in nvmem_device_get()
1047 return nvmem; in nvmem_device_get()
1056 * nvmem_device_find() - Find nvmem device with matching function
1073 struct nvmem_device **nvmem = res; in devm_nvmem_device_match() local
1075 if (WARN_ON(!nvmem || !*nvmem)) in devm_nvmem_device_match()
1078 return *nvmem == data; in devm_nvmem_device_match()
1087 * devm_nvmem_device_put() - put alredy got nvmem device
1089 * @dev: Device that uses the nvmem device.
1090 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1093 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_device_put() argument
1098 devm_nvmem_device_match, nvmem); in devm_nvmem_device_put()
1105 * nvmem_device_put() - put alredy got nvmem device
1107 * @nvmem: pointer to nvmem device that needs to be released.
1109 void nvmem_device_put(struct nvmem_device *nvmem) in nvmem_device_put() argument
1111 __nvmem_device_put(nvmem); in nvmem_device_put()
1116 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1118 * @dev: Device that requests the nvmem device.
1119 * @id: name id for the requested nvmem device.
1127 struct nvmem_device **ptr, *nvmem; in devm_nvmem_device_get() local
1131 return ERR_PTR(-ENOMEM); in devm_nvmem_device_get()
1133 nvmem = nvmem_device_get(dev, id); in devm_nvmem_device_get()
1134 if (!IS_ERR(nvmem)) { in devm_nvmem_device_get()
1135 *ptr = nvmem; in devm_nvmem_device_get()
1141 return nvmem; in devm_nvmem_device_get()
1148 struct nvmem_cell *cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup()
1150 struct nvmem_device *nvmem; in nvmem_cell_get_from_lookup() local
1154 return ERR_PTR(-EINVAL); in nvmem_cell_get_from_lookup()
1161 if ((strcmp(lookup->dev_id, dev_id) == 0) && in nvmem_cell_get_from_lookup()
1162 (strcmp(lookup->con_id, con_id) == 0)) { in nvmem_cell_get_from_lookup()
1164 nvmem = __nvmem_device_get((void *)lookup->nvmem_name, in nvmem_cell_get_from_lookup()
1166 if (IS_ERR(nvmem)) { in nvmem_cell_get_from_lookup()
1168 cell = ERR_CAST(nvmem); in nvmem_cell_get_from_lookup()
1172 cell = nvmem_find_cell_by_name(nvmem, in nvmem_cell_get_from_lookup()
1173 lookup->cell_name); in nvmem_cell_get_from_lookup()
1175 __nvmem_device_put(nvmem); in nvmem_cell_get_from_lookup()
1176 cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup()
1188 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) in nvmem_find_cell_by_node() argument
1193 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_node()
1194 if (np == iter->np) { in nvmem_find_cell_by_node()
1205 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1207 * @np: Device tree node that uses the nvmem cell.
1208 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1210 * nvmem-cell-names property).
1219 struct nvmem_device *nvmem; in of_nvmem_cell_get() local
1225 index = of_property_match_string(np, "nvmem-cell-names", id); in of_nvmem_cell_get()
1227 cell_np = of_parse_phandle(np, "nvmem-cells", index); in of_nvmem_cell_get()
1229 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1233 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1235 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); in of_nvmem_cell_get()
1237 if (IS_ERR(nvmem)) in of_nvmem_cell_get()
1238 return ERR_CAST(nvmem); in of_nvmem_cell_get()
1240 cell = nvmem_find_cell_by_node(nvmem, cell_np); in of_nvmem_cell_get()
1242 __nvmem_device_put(nvmem); in of_nvmem_cell_get()
1243 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1252 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1254 * @dev: Device that requests the nvmem cell.
1255 * @id: nvmem cell name to get (this corresponds with the name from the
1256 * nvmem-cell-names property for DT systems and with the con_id from
1257 * the lookup entry for non-DT systems).
1267 if (dev->of_node) { /* try dt first */ in nvmem_cell_get()
1268 cell = of_nvmem_cell_get(dev->of_node, id); in nvmem_cell_get()
1269 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) in nvmem_cell_get()
1275 return ERR_PTR(-EINVAL); in nvmem_cell_get()
1287 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1289 * @dev: Device that requests the nvmem cell.
1290 * @id: nvmem cell name id to get.
1302 return ERR_PTR(-ENOMEM); in devm_nvmem_cell_get()
1327 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1330 * @dev: Device that requests the nvmem cell.
1331 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1345 * nvmem_cell_put() - Release previously allocated nvmem cell.
1347 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1351 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_put() local
1353 __nvmem_device_put(nvmem); in nvmem_cell_put()
1360 int i, extra, bit_offset = cell->bit_offset; in nvmem_shift_read_buffer_in_place()
1368 for (i = 1; i < cell->bytes; i++) { in nvmem_shift_read_buffer_in_place()
1370 *p |= *b << (BITS_PER_BYTE - bit_offset); in nvmem_shift_read_buffer_in_place()
1377 p += cell->bytes - 1; in nvmem_shift_read_buffer_in_place()
1381 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE); in nvmem_shift_read_buffer_in_place()
1382 while (--extra >= 0) in nvmem_shift_read_buffer_in_place()
1383 *p-- = 0; in nvmem_shift_read_buffer_in_place()
1386 if (cell->nbits % BITS_PER_BYTE) in nvmem_shift_read_buffer_in_place()
1387 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0); in nvmem_shift_read_buffer_in_place()
1390 static int __nvmem_cell_read(struct nvmem_device *nvmem, in __nvmem_cell_read() argument
1396 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); in __nvmem_cell_read()
1401 /* shift bits in-place */ in __nvmem_cell_read()
1402 if (cell->bit_offset || cell->nbits) in __nvmem_cell_read()
1406 *len = cell->bytes; in __nvmem_cell_read()
1412 * nvmem_cell_read() - Read a given nvmem cell
1414 * @cell: nvmem cell to be read.
1423 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_read() local
1427 if (!nvmem) in nvmem_cell_read()
1428 return ERR_PTR(-EINVAL); in nvmem_cell_read()
1430 buf = kzalloc(cell->bytes, GFP_KERNEL); in nvmem_cell_read()
1432 return ERR_PTR(-ENOMEM); in nvmem_cell_read()
1434 rc = __nvmem_cell_read(nvmem, cell, buf, len); in nvmem_cell_read()
1447 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_prepare_write_buffer() local
1448 int i, rc, nbits, bit_offset = cell->bit_offset; in nvmem_cell_prepare_write_buffer()
1451 nbits = cell->nbits; in nvmem_cell_prepare_write_buffer()
1452 buf = kzalloc(cell->bytes, GFP_KERNEL); in nvmem_cell_prepare_write_buffer()
1454 return ERR_PTR(-ENOMEM); in nvmem_cell_prepare_write_buffer()
1463 /* setup the first byte with lsb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1464 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); in nvmem_cell_prepare_write_buffer()
1467 *b++ |= GENMASK(bit_offset - 1, 0) & v; in nvmem_cell_prepare_write_buffer()
1470 for (i = 1; i < cell->bytes; i++) { in nvmem_cell_prepare_write_buffer()
1472 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); in nvmem_cell_prepare_write_buffer()
1482 /* setup the last byte with msb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1483 rc = nvmem_reg_read(nvmem, in nvmem_cell_prepare_write_buffer()
1484 cell->offset + cell->bytes - 1, &v, 1); in nvmem_cell_prepare_write_buffer()
1498 * nvmem_cell_write() - Write to a given nvmem cell
1500 * @cell: nvmem cell to be written.
1502 * @len: length of buffer to be written to nvmem cell.
1508 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_write() local
1511 if (!nvmem || nvmem->read_only || in nvmem_cell_write()
1512 (cell->bit_offset == 0 && len != cell->bytes)) in nvmem_cell_write()
1513 return -EINVAL; in nvmem_cell_write()
1515 if (cell->bit_offset || cell->nbits) { in nvmem_cell_write()
1521 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); in nvmem_cell_write()
1524 if (cell->bit_offset || cell->nbits) in nvmem_cell_write()
1553 return -EINVAL; in nvmem_cell_read_common()
1563 * nvmem_cell_read_u8() - Read a cell value as a u8
1565 * @dev: Device that requests the nvmem cell.
1566 * @cell_id: Name of nvmem cell to read.
1578 * nvmem_cell_read_u16() - Read a cell value as a u16
1580 * @dev: Device that requests the nvmem cell.
1581 * @cell_id: Name of nvmem cell to read.
1593 * nvmem_cell_read_u32() - Read a cell value as a u32
1595 * @dev: Device that requests the nvmem cell.
1596 * @cell_id: Name of nvmem cell to read.
1608 * nvmem_cell_read_u64() - Read a cell value as a u64
1610 * @dev: Device that requests the nvmem cell.
1611 * @cell_id: Name of nvmem cell to read.
1634 nbits = cell->nbits; in nvmem_cell_read_variable_common()
1649 return ERR_PTR(-ERANGE); in nvmem_cell_read_variable_common()
1656 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1658 * @dev: Device that requests the nvmem cell.
1659 * @cell_id: Name of nvmem cell to read.
1687 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1689 * @dev: Device that requests the nvmem cell.
1690 * @cell_id: Name of nvmem cell to read.
1718 * nvmem_device_cell_read() - Read a given nvmem device and cell
1720 * @nvmem: nvmem device to read from.
1721 * @info: nvmem cell info to be read.
1727 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, in nvmem_device_cell_read() argument
1734 if (!nvmem) in nvmem_device_cell_read()
1735 return -EINVAL; in nvmem_device_cell_read()
1737 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); in nvmem_device_cell_read()
1741 rc = __nvmem_cell_read(nvmem, &cell, buf, &len); in nvmem_device_cell_read()
1750 * nvmem_device_cell_write() - Write cell to a given nvmem device
1752 * @nvmem: nvmem device to be written to.
1753 * @info: nvmem cell info to be written.
1758 int nvmem_device_cell_write(struct nvmem_device *nvmem, in nvmem_device_cell_write() argument
1764 if (!nvmem) in nvmem_device_cell_write()
1765 return -EINVAL; in nvmem_device_cell_write()
1767 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); in nvmem_device_cell_write()
1776 * nvmem_device_read() - Read from a given nvmem device
1778 * @nvmem: nvmem device to read from.
1779 * @offset: offset in nvmem device.
1786 int nvmem_device_read(struct nvmem_device *nvmem, in nvmem_device_read() argument
1792 if (!nvmem) in nvmem_device_read()
1793 return -EINVAL; in nvmem_device_read()
1795 rc = nvmem_reg_read(nvmem, offset, buf, bytes); in nvmem_device_read()
1805 * nvmem_device_write() - Write cell to a given nvmem device
1807 * @nvmem: nvmem device to be written to.
1808 * @offset: offset in nvmem device.
1814 int nvmem_device_write(struct nvmem_device *nvmem, in nvmem_device_write() argument
1820 if (!nvmem) in nvmem_device_write()
1821 return -EINVAL; in nvmem_device_write()
1823 rc = nvmem_reg_write(nvmem, offset, buf, bytes); in nvmem_device_write()
1834 * nvmem_add_cell_table() - register a table of cell info entries
1841 list_add_tail(&table->node, &nvmem_cell_tables); in nvmem_add_cell_table()
1847 * nvmem_del_cell_table() - remove a previously registered cell info table
1854 list_del(&table->node); in nvmem_del_cell_table()
1860 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1877 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1895 * nvmem_dev_name() - Get the name of a given nvmem device.
1897 * @nvmem: nvmem device.
1899 * Return: name of the nvmem device.
1901 const char *nvmem_dev_name(struct nvmem_device *nvmem) in nvmem_dev_name() argument
1903 return dev_name(&nvmem->dev); in nvmem_dev_name()
1921 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1922 MODULE_DESCRIPTION("nvmem Driver Core");