1 // SPDX-License-Identifier: GPL-2.0
3 * nvmem framework core.
9 #include <linux/device.h>
10 #include <linux/export.h>
12 #include <linux/idr.h>
13 #include <linux/init.h>
14 #include <linux/kref.h>
15 #include <linux/module.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
19 #include <linux/slab.h>
31 struct bin_attribute eeprom;
32 struct device *base_dev;
33 struct list_head cells;
34 nvmem_reg_read_t reg_read;
35 nvmem_reg_write_t reg_write;
39 #define FLAG_COMPAT BIT(0)
47 struct device_node *np;
48 struct nvmem_device *nvmem;
49 struct list_head node;
52 static DEFINE_MUTEX(nvmem_mutex);
53 static DEFINE_IDA(nvmem_ida);
55 static DEFINE_MUTEX(nvmem_cell_mutex);
56 static LIST_HEAD(nvmem_cell_tables);
58 static DEFINE_MUTEX(nvmem_lookup_mutex);
59 static LIST_HEAD(nvmem_lookup_list);
61 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
63 #ifdef CONFIG_DEBUG_LOCK_ALLOC
64 static struct lock_class_key eeprom_lock_key;
67 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
68 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
69 void *val, size_t bytes)
72 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
77 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
78 void *val, size_t bytes)
81 return nvmem->reg_write(nvmem->priv, offset, val, bytes);
86 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
87 struct bin_attribute *attr,
88 char *buf, loff_t pos, size_t count)
91 struct nvmem_device *nvmem;
97 dev = container_of(kobj, struct device, kobj);
98 nvmem = to_nvmem_device(dev);
100 /* Stop the user from reading */
101 if (pos >= nvmem->size)
104 if (count < nvmem->word_size)
107 if (pos + count > nvmem->size)
108 count = nvmem->size - pos;
110 count = round_down(count, nvmem->word_size);
112 rc = nvmem_reg_read(nvmem, pos, buf, count);
120 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
121 struct bin_attribute *attr,
122 char *buf, loff_t pos, size_t count)
125 struct nvmem_device *nvmem;
131 dev = container_of(kobj, struct device, kobj);
132 nvmem = to_nvmem_device(dev);
134 /* Stop the user from writing */
135 if (pos >= nvmem->size)
138 if (count < nvmem->word_size)
141 if (pos + count > nvmem->size)
142 count = nvmem->size - pos;
144 count = round_down(count, nvmem->word_size);
146 rc = nvmem_reg_write(nvmem, pos, buf, count);
154 /* default read/write permissions */
155 static struct bin_attribute bin_attr_rw_nvmem = {
160 .read = bin_attr_nvmem_read,
161 .write = bin_attr_nvmem_write,
164 static struct bin_attribute *nvmem_bin_rw_attributes[] = {
169 static const struct attribute_group nvmem_bin_rw_group = {
170 .bin_attrs = nvmem_bin_rw_attributes,
173 static const struct attribute_group *nvmem_rw_dev_groups[] = {
178 /* read only permission */
179 static struct bin_attribute bin_attr_ro_nvmem = {
184 .read = bin_attr_nvmem_read,
187 static struct bin_attribute *nvmem_bin_ro_attributes[] = {
192 static const struct attribute_group nvmem_bin_ro_group = {
193 .bin_attrs = nvmem_bin_ro_attributes,
196 static const struct attribute_group *nvmem_ro_dev_groups[] = {
201 /* default read/write permissions, root only */
202 static struct bin_attribute bin_attr_rw_root_nvmem = {
207 .read = bin_attr_nvmem_read,
208 .write = bin_attr_nvmem_write,
211 static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
212 &bin_attr_rw_root_nvmem,
216 static const struct attribute_group nvmem_bin_rw_root_group = {
217 .bin_attrs = nvmem_bin_rw_root_attributes,
220 static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
221 &nvmem_bin_rw_root_group,
225 /* read only permission, root only */
226 static struct bin_attribute bin_attr_ro_root_nvmem = {
231 .read = bin_attr_nvmem_read,
234 static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
235 &bin_attr_ro_root_nvmem,
239 static const struct attribute_group nvmem_bin_ro_root_group = {
240 .bin_attrs = nvmem_bin_ro_root_attributes,
243 static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
244 &nvmem_bin_ro_root_group,
248 static void nvmem_release(struct device *dev)
250 struct nvmem_device *nvmem = to_nvmem_device(dev);
252 ida_simple_remove(&nvmem_ida, nvmem->id);
256 static const struct device_type nvmem_provider_type = {
257 .release = nvmem_release,
260 static struct bus_type nvmem_bus_type = {
264 static int of_nvmem_match(struct device *dev, void *nvmem_np)
266 return dev->of_node == nvmem_np;
269 static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
276 d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
281 return to_nvmem_device(d);
284 static struct nvmem_device *nvmem_find(const char *name)
288 d = bus_find_device_by_name(&nvmem_bus_type, NULL, name);
293 return to_nvmem_device(d);
296 static void nvmem_cell_drop(struct nvmem_cell *cell)
298 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
299 mutex_lock(&nvmem_mutex);
300 list_del(&cell->node);
301 mutex_unlock(&nvmem_mutex);
302 of_node_put(cell->np);
307 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
309 struct nvmem_cell *cell, *p;
311 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
312 nvmem_cell_drop(cell);
315 static void nvmem_cell_add(struct nvmem_cell *cell)
317 mutex_lock(&nvmem_mutex);
318 list_add_tail(&cell->node, &cell->nvmem->cells);
319 mutex_unlock(&nvmem_mutex);
320 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
323 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
324 const struct nvmem_cell_info *info,
325 struct nvmem_cell *cell)
328 cell->offset = info->offset;
329 cell->bytes = info->bytes;
330 cell->name = info->name;
332 cell->bit_offset = info->bit_offset;
333 cell->nbits = info->nbits;
336 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
339 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
341 "cell %s unaligned to nvmem stride %d\n",
342 cell->name, nvmem->stride);
350 * nvmem_add_cells() - Add cell information to an nvmem device
352 * @nvmem: nvmem device to add cells to.
353 * @info: nvmem cell info to add to the device
354 * @ncells: number of cells in info
356 * Return: 0 or negative error code on failure.
358 static int nvmem_add_cells(struct nvmem_device *nvmem,
359 const struct nvmem_cell_info *info,
362 struct nvmem_cell **cells;
365 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
369 for (i = 0; i < ncells; i++) {
370 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
376 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
382 nvmem_cell_add(cells[i]);
385 /* remove tmp array */
391 nvmem_cell_drop(cells[i]);
399 * nvmem_setup_compat() - Create an additional binary entry in
400 * drivers sys directory, to be backwards compatible with the older
401 * drivers/misc/eeprom drivers.
403 static int nvmem_setup_compat(struct nvmem_device *nvmem,
404 const struct nvmem_config *config)
408 if (!config->base_dev)
411 if (nvmem->read_only)
412 nvmem->eeprom = bin_attr_ro_root_nvmem;
414 nvmem->eeprom = bin_attr_rw_root_nvmem;
415 nvmem->eeprom.attr.name = "eeprom";
416 nvmem->eeprom.size = nvmem->size;
417 #ifdef CONFIG_DEBUG_LOCK_ALLOC
418 nvmem->eeprom.attr.key = &eeprom_lock_key;
420 nvmem->eeprom.private = &nvmem->dev;
421 nvmem->base_dev = config->base_dev;
423 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
426 "Failed to create eeprom binary file %d\n", rval);
430 nvmem->flags |= FLAG_COMPAT;
436 * nvmem_register_notifier() - Register a notifier block for nvmem events.
438 * @nb: notifier block to be called on nvmem events.
440 * Return: 0 on success, negative error number on failure.
442 int nvmem_register_notifier(struct notifier_block *nb)
444 return blocking_notifier_chain_register(&nvmem_notifier, nb);
446 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
449 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
451 * @nb: notifier block to be unregistered.
453 * Return: 0 on success, negative error number on failure.
455 int nvmem_unregister_notifier(struct notifier_block *nb)
457 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
459 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
461 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
463 const struct nvmem_cell_info *info;
464 struct nvmem_cell_table *table;
465 struct nvmem_cell *cell;
468 mutex_lock(&nvmem_cell_mutex);
469 list_for_each_entry(table, &nvmem_cell_tables, node) {
470 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
471 for (i = 0; i < table->ncells; i++) {
472 info = &table->cells[i];
474 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
480 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
488 nvmem_cell_add(cell);
494 mutex_unlock(&nvmem_cell_mutex);
498 static struct nvmem_cell *
499 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
501 struct nvmem_cell *cell = NULL;
503 mutex_lock(&nvmem_mutex);
504 list_for_each_entry(cell, &nvmem->cells, node) {
505 if (strcmp(cell_id, cell->name) == 0)
508 mutex_unlock(&nvmem_mutex);
513 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
515 struct device_node *parent, *child;
516 struct device *dev = &nvmem->dev;
517 struct nvmem_cell *cell;
521 parent = dev->of_node;
523 for_each_child_of_node(parent, child) {
524 addr = of_get_property(child, "reg", &len);
525 if (!addr || (len < 2 * sizeof(u32))) {
526 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
530 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
535 cell->np = of_node_get(child);
536 cell->offset = be32_to_cpup(addr++);
537 cell->bytes = be32_to_cpup(addr);
538 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
540 addr = of_get_property(child, "bits", &len);
541 if (addr && len == (2 * sizeof(u32))) {
542 cell->bit_offset = be32_to_cpup(addr++);
543 cell->nbits = be32_to_cpup(addr);
547 cell->bytes = DIV_ROUND_UP(
548 cell->nbits + cell->bit_offset,
551 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
552 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
553 cell->name, nvmem->stride);
554 /* Cells already added will be freed later. */
560 nvmem_cell_add(cell);
567 * nvmem_register() - Register a nvmem device for given nvmem_config.
568 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
570 * @config: nvmem device configuration with which nvmem device is created.
572 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
576 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
578 struct nvmem_device *nvmem;
582 return ERR_PTR(-EINVAL);
584 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
586 return ERR_PTR(-ENOMEM);
588 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
591 return ERR_PTR(rval);
594 kref_init(&nvmem->refcnt);
595 INIT_LIST_HEAD(&nvmem->cells);
598 nvmem->owner = config->owner;
599 if (!nvmem->owner && config->dev->driver)
600 nvmem->owner = config->dev->driver->owner;
601 nvmem->stride = config->stride ?: 1;
602 nvmem->word_size = config->word_size ?: 1;
603 nvmem->size = config->size;
604 nvmem->dev.type = &nvmem_provider_type;
605 nvmem->dev.bus = &nvmem_bus_type;
606 nvmem->dev.parent = config->dev;
607 nvmem->priv = config->priv;
608 nvmem->reg_read = config->reg_read;
609 nvmem->reg_write = config->reg_write;
610 nvmem->dev.of_node = config->dev->of_node;
612 if (config->id == -1 && config->name) {
613 dev_set_name(&nvmem->dev, "%s", config->name);
615 dev_set_name(&nvmem->dev, "%s%d",
616 config->name ? : "nvmem",
617 config->name ? config->id : nvmem->id);
620 nvmem->read_only = device_property_present(config->dev, "read-only") |
623 if (config->root_only)
624 nvmem->dev.groups = nvmem->read_only ?
625 nvmem_ro_root_dev_groups :
626 nvmem_rw_root_dev_groups;
628 nvmem->dev.groups = nvmem->read_only ?
629 nvmem_ro_dev_groups :
632 device_initialize(&nvmem->dev);
634 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
636 rval = device_add(&nvmem->dev);
640 if (config->compat) {
641 rval = nvmem_setup_compat(nvmem, config);
647 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
649 goto err_teardown_compat;
652 rval = nvmem_add_cells_from_table(nvmem);
654 goto err_remove_cells;
656 rval = nvmem_add_cells_from_of(nvmem);
658 goto err_remove_cells;
660 rval = blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
662 goto err_remove_cells;
667 nvmem_device_remove_all_cells(nvmem);
670 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
672 device_del(&nvmem->dev);
674 put_device(&nvmem->dev);
676 return ERR_PTR(rval);
678 EXPORT_SYMBOL_GPL(nvmem_register);
680 static void nvmem_device_release(struct kref *kref)
682 struct nvmem_device *nvmem;
684 nvmem = container_of(kref, struct nvmem_device, refcnt);
686 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
688 if (nvmem->flags & FLAG_COMPAT)
689 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
691 nvmem_device_remove_all_cells(nvmem);
692 device_del(&nvmem->dev);
693 put_device(&nvmem->dev);
697 * nvmem_unregister() - Unregister previously registered nvmem device
699 * @nvmem: Pointer to previously registered nvmem device.
701 void nvmem_unregister(struct nvmem_device *nvmem)
703 kref_put(&nvmem->refcnt, nvmem_device_release);
705 EXPORT_SYMBOL_GPL(nvmem_unregister);
707 static void devm_nvmem_release(struct device *dev, void *res)
709 nvmem_unregister(*(struct nvmem_device **)res);
713 * devm_nvmem_register() - Register a managed nvmem device for given
715 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
717 * @dev: Device that uses the nvmem device.
718 * @config: nvmem device configuration with which nvmem device is created.
720 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
723 struct nvmem_device *devm_nvmem_register(struct device *dev,
724 const struct nvmem_config *config)
726 struct nvmem_device **ptr, *nvmem;
728 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
730 return ERR_PTR(-ENOMEM);
732 nvmem = nvmem_register(config);
734 if (!IS_ERR(nvmem)) {
736 devres_add(dev, ptr);
743 EXPORT_SYMBOL_GPL(devm_nvmem_register);
745 static int devm_nvmem_match(struct device *dev, void *res, void *data)
747 struct nvmem_device **r = res;
753 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
756 * @dev: Device that uses the nvmem device.
757 * @nvmem: Pointer to previously registered nvmem device.
759 * Return: Will be an negative on error or a zero on success.
761 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
763 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
765 EXPORT_SYMBOL(devm_nvmem_unregister);
767 static struct nvmem_device *__nvmem_device_get(struct device_node *np,
768 const char *nvmem_name)
770 struct nvmem_device *nvmem = NULL;
772 mutex_lock(&nvmem_mutex);
773 nvmem = np ? of_nvmem_find(np) : nvmem_find(nvmem_name);
774 mutex_unlock(&nvmem_mutex);
776 return ERR_PTR(-EPROBE_DEFER);
778 if (!try_module_get(nvmem->owner)) {
780 "could not increase module refcount for cell %s\n",
781 nvmem_dev_name(nvmem));
783 return ERR_PTR(-EINVAL);
786 kref_get(&nvmem->refcnt);
791 static void __nvmem_device_put(struct nvmem_device *nvmem)
793 module_put(nvmem->owner);
794 kref_put(&nvmem->refcnt, nvmem_device_release);
797 #if IS_ENABLED(CONFIG_OF)
799 * of_nvmem_device_get() - Get nvmem device from a given id
801 * @np: Device tree node that uses the nvmem device.
802 * @id: nvmem name from nvmem-names property.
804 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
807 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
810 struct device_node *nvmem_np;
813 index = of_property_match_string(np, "nvmem-names", id);
815 nvmem_np = of_parse_phandle(np, "nvmem", index);
817 return ERR_PTR(-EINVAL);
819 return __nvmem_device_get(nvmem_np, NULL);
821 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
825 * nvmem_device_get() - Get nvmem device from a given id
827 * @dev: Device that uses the nvmem device.
828 * @dev_name: name of the requested nvmem device.
830 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
833 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
835 if (dev->of_node) { /* try dt first */
836 struct nvmem_device *nvmem;
838 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
840 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
845 return nvmem_find(dev_name);
847 EXPORT_SYMBOL_GPL(nvmem_device_get);
849 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
851 struct nvmem_device **nvmem = res;
853 if (WARN_ON(!nvmem || !*nvmem))
856 return *nvmem == data;
859 static void devm_nvmem_device_release(struct device *dev, void *res)
861 nvmem_device_put(*(struct nvmem_device **)res);
865 * devm_nvmem_device_put() - put alredy got nvmem device
867 * @dev: Device that uses the nvmem device.
868 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
869 * that needs to be released.
871 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
875 ret = devres_release(dev, devm_nvmem_device_release,
876 devm_nvmem_device_match, nvmem);
880 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
883 * nvmem_device_put() - put alredy got nvmem device
885 * @nvmem: pointer to nvmem device that needs to be released.
887 void nvmem_device_put(struct nvmem_device *nvmem)
889 __nvmem_device_put(nvmem);
891 EXPORT_SYMBOL_GPL(nvmem_device_put);
894 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
896 * @dev: Device that requests the nvmem device.
897 * @id: name id for the requested nvmem device.
899 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
900 * on success. The nvmem_cell will be freed by the automatically once the
903 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
905 struct nvmem_device **ptr, *nvmem;
907 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
909 return ERR_PTR(-ENOMEM);
911 nvmem = nvmem_device_get(dev, id);
912 if (!IS_ERR(nvmem)) {
914 devres_add(dev, ptr);
921 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
923 static struct nvmem_cell *
924 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
926 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
927 struct nvmem_cell_lookup *lookup;
928 struct nvmem_device *nvmem;
932 return ERR_PTR(-EINVAL);
934 dev_id = dev_name(dev);
936 mutex_lock(&nvmem_lookup_mutex);
938 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
939 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
940 (strcmp(lookup->con_id, con_id) == 0)) {
941 /* This is the right entry. */
942 nvmem = __nvmem_device_get(NULL, lookup->nvmem_name);
944 /* Provider may not be registered yet. */
945 cell = ERR_CAST(nvmem);
949 cell = nvmem_find_cell_by_name(nvmem,
952 __nvmem_device_put(nvmem);
953 cell = ERR_PTR(-ENOENT);
960 mutex_unlock(&nvmem_lookup_mutex);
964 #if IS_ENABLED(CONFIG_OF)
965 static struct nvmem_cell *
966 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
968 struct nvmem_cell *cell = NULL;
970 mutex_lock(&nvmem_mutex);
971 list_for_each_entry(cell, &nvmem->cells, node) {
975 mutex_unlock(&nvmem_mutex);
981 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
983 * @np: Device tree node that uses the nvmem cell.
984 * @id: nvmem cell name from nvmem-cell-names property, or NULL
985 * for the cell at index 0 (the lone cell with no accompanying
986 * nvmem-cell-names property).
988 * Return: Will be an ERR_PTR() on error or a valid pointer
989 * to a struct nvmem_cell. The nvmem_cell will be freed by the
992 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
994 struct device_node *cell_np, *nvmem_np;
995 struct nvmem_device *nvmem;
996 struct nvmem_cell *cell;
999 /* if cell name exists, find index to the name */
1001 index = of_property_match_string(np, "nvmem-cell-names", id);
1003 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1005 return ERR_PTR(-EINVAL);
1007 nvmem_np = of_get_next_parent(cell_np);
1009 return ERR_PTR(-EINVAL);
1011 nvmem = __nvmem_device_get(nvmem_np, NULL);
1012 of_node_put(nvmem_np);
1014 return ERR_CAST(nvmem);
1016 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1018 __nvmem_device_put(nvmem);
1019 return ERR_PTR(-ENOENT);
1024 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1028 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1030 * @dev: Device that requests the nvmem cell.
1031 * @id: nvmem cell name to get (this corresponds with the name from the
1032 * nvmem-cell-names property for DT systems and with the con_id from
1033 * the lookup entry for non-DT systems).
1035 * Return: Will be an ERR_PTR() on error or a valid pointer
1036 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1039 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1041 struct nvmem_cell *cell;
1043 if (dev->of_node) { /* try dt first */
1044 cell = of_nvmem_cell_get(dev->of_node, id);
1045 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1049 /* NULL cell id only allowed for device tree; invalid otherwise */
1051 return ERR_PTR(-EINVAL);
1053 return nvmem_cell_get_from_lookup(dev, id);
1055 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1057 static void devm_nvmem_cell_release(struct device *dev, void *res)
1059 nvmem_cell_put(*(struct nvmem_cell **)res);
1063 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1065 * @dev: Device that requests the nvmem cell.
1066 * @id: nvmem cell name id to get.
1068 * Return: Will be an ERR_PTR() on error or a valid pointer
1069 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1070 * automatically once the device is freed.
1072 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1074 struct nvmem_cell **ptr, *cell;
1076 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1078 return ERR_PTR(-ENOMEM);
1080 cell = nvmem_cell_get(dev, id);
1081 if (!IS_ERR(cell)) {
1083 devres_add(dev, ptr);
1090 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1092 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1094 struct nvmem_cell **c = res;
1096 if (WARN_ON(!c || !*c))
1103 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1104 * from devm_nvmem_cell_get.
1106 * @dev: Device that requests the nvmem cell.
1107 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1109 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1113 ret = devres_release(dev, devm_nvmem_cell_release,
1114 devm_nvmem_cell_match, cell);
1118 EXPORT_SYMBOL(devm_nvmem_cell_put);
1121 * nvmem_cell_put() - Release previously allocated nvmem cell.
1123 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1125 void nvmem_cell_put(struct nvmem_cell *cell)
1127 struct nvmem_device *nvmem = cell->nvmem;
1129 __nvmem_device_put(nvmem);
1131 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1133 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1136 int i, bit_offset = cell->bit_offset;
1141 *b++ >>= bit_offset;
1143 /* setup rest of the bytes if any */
1144 for (i = 1; i < cell->bytes; i++) {
1145 /* Get bits from next byte and shift them towards msb */
1146 *p |= *b << (BITS_PER_BYTE - bit_offset);
1149 *b++ >>= bit_offset;
1152 /* result fits in less bytes */
1153 if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
1156 /* clear msb bits if any leftover in the last byte */
1157 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
1160 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1161 struct nvmem_cell *cell,
1162 void *buf, size_t *len)
1166 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1171 /* shift bits in-place */
1172 if (cell->bit_offset || cell->nbits)
1173 nvmem_shift_read_buffer_in_place(cell, buf);
1182 * nvmem_cell_read() - Read a given nvmem cell
1184 * @cell: nvmem cell to be read.
1185 * @len: pointer to length of cell which will be populated on successful read;
1188 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1189 * buffer should be freed by the consumer with a kfree().
1191 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1193 struct nvmem_device *nvmem = cell->nvmem;
1198 return ERR_PTR(-EINVAL);
1200 buf = kzalloc(cell->bytes, GFP_KERNEL);
1202 return ERR_PTR(-ENOMEM);
1204 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1212 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1214 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1217 struct nvmem_device *nvmem = cell->nvmem;
1218 int i, rc, nbits, bit_offset = cell->bit_offset;
1219 u8 v, *p, *buf, *b, pbyte, pbits;
1221 nbits = cell->nbits;
1222 buf = kzalloc(cell->bytes, GFP_KERNEL);
1224 return ERR_PTR(-ENOMEM);
1226 memcpy(buf, _buf, len);
1233 /* setup the first byte with lsb bits from nvmem */
1234 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1237 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1239 /* setup rest of the byte if any */
1240 for (i = 1; i < cell->bytes; i++) {
1241 /* Get last byte bits and shift them towards lsb */
1242 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1250 /* if it's not end on byte boundary */
1251 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1252 /* setup the last byte with msb bits from nvmem */
1253 rc = nvmem_reg_read(nvmem,
1254 cell->offset + cell->bytes - 1, &v, 1);
1257 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1268 * nvmem_cell_write() - Write to a given nvmem cell
1270 * @cell: nvmem cell to be written.
1271 * @buf: Buffer to be written.
1272 * @len: length of buffer to be written to nvmem cell.
1274 * Return: length of bytes written or negative on failure.
1276 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1278 struct nvmem_device *nvmem = cell->nvmem;
1281 if (!nvmem || nvmem->read_only ||
1282 (cell->bit_offset == 0 && len != cell->bytes))
1285 if (cell->bit_offset || cell->nbits) {
1286 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1288 return PTR_ERR(buf);
1291 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1293 /* free the tmp buffer */
1294 if (cell->bit_offset || cell->nbits)
1302 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1305 * nvmem_cell_read_u32() - Read a cell value as an u32
1307 * @dev: Device that requests the nvmem cell.
1308 * @cell_id: Name of nvmem cell to read.
1309 * @val: pointer to output value.
1311 * Return: 0 on success or negative errno.
1313 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1315 struct nvmem_cell *cell;
1319 cell = nvmem_cell_get(dev, cell_id);
1321 return PTR_ERR(cell);
1323 buf = nvmem_cell_read(cell, &len);
1325 nvmem_cell_put(cell);
1326 return PTR_ERR(buf);
1328 if (len != sizeof(*val)) {
1330 nvmem_cell_put(cell);
1333 memcpy(val, buf, sizeof(*val));
1336 nvmem_cell_put(cell);
1339 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1342 * nvmem_device_cell_read() - Read a given nvmem device and cell
1344 * @nvmem: nvmem device to read from.
1345 * @info: nvmem cell info to be read.
1346 * @buf: buffer pointer which will be populated on successful read.
1348 * Return: length of successful bytes read on success and negative
1349 * error code on error.
1351 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1352 struct nvmem_cell_info *info, void *buf)
1354 struct nvmem_cell cell;
1361 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1365 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1371 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1374 * nvmem_device_cell_write() - Write cell to a given nvmem device
1376 * @nvmem: nvmem device to be written to.
1377 * @info: nvmem cell info to be written.
1378 * @buf: buffer to be written to cell.
1380 * Return: length of bytes written or negative error code on failure.
1382 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1383 struct nvmem_cell_info *info, void *buf)
1385 struct nvmem_cell cell;
1391 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1395 return nvmem_cell_write(&cell, buf, cell.bytes);
1397 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1400 * nvmem_device_read() - Read from a given nvmem device
1402 * @nvmem: nvmem device to read from.
1403 * @offset: offset in nvmem device.
1404 * @bytes: number of bytes to read.
1405 * @buf: buffer pointer which will be populated on successful read.
1407 * Return: length of successful bytes read on success and negative
1408 * error code on error.
1410 int nvmem_device_read(struct nvmem_device *nvmem,
1411 unsigned int offset,
1412 size_t bytes, void *buf)
1419 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1426 EXPORT_SYMBOL_GPL(nvmem_device_read);
1429 * nvmem_device_write() - Write cell to a given nvmem device
1431 * @nvmem: nvmem device to be written to.
1432 * @offset: offset in nvmem device.
1433 * @bytes: number of bytes to write.
1434 * @buf: buffer to be written.
1436 * Return: length of bytes written or negative error code on failure.
1438 int nvmem_device_write(struct nvmem_device *nvmem,
1439 unsigned int offset,
1440 size_t bytes, void *buf)
1447 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1455 EXPORT_SYMBOL_GPL(nvmem_device_write);
1458 * nvmem_add_cell_table() - register a table of cell info entries
1460 * @table: table of cell info entries
1462 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1464 mutex_lock(&nvmem_cell_mutex);
1465 list_add_tail(&table->node, &nvmem_cell_tables);
1466 mutex_unlock(&nvmem_cell_mutex);
1468 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1471 * nvmem_del_cell_table() - remove a previously registered cell info table
1473 * @table: table of cell info entries
1475 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1477 mutex_lock(&nvmem_cell_mutex);
1478 list_del(&table->node);
1479 mutex_unlock(&nvmem_cell_mutex);
1481 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1484 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1486 * @entries: array of cell lookup entries
1487 * @nentries: number of cell lookup entries in the array
1489 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1493 mutex_lock(&nvmem_lookup_mutex);
1494 for (i = 0; i < nentries; i++)
1495 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1496 mutex_unlock(&nvmem_lookup_mutex);
1498 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1501 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1504 * @entries: array of cell lookup entries
1505 * @nentries: number of cell lookup entries in the array
1507 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1511 mutex_lock(&nvmem_lookup_mutex);
1512 for (i = 0; i < nentries; i++)
1513 list_del(&entries[i].node);
1514 mutex_unlock(&nvmem_lookup_mutex);
1516 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1519 * nvmem_dev_name() - Get the name of a given nvmem device.
1521 * @nvmem: nvmem device.
1523 * Return: name of the nvmem device.
1525 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1527 return dev_name(&nvmem->dev);
1529 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1531 static int __init nvmem_init(void)
1533 return bus_register(&nvmem_bus_type);
1536 static void __exit nvmem_exit(void)
1538 bus_unregister(&nvmem_bus_type);
1541 subsys_initcall(nvmem_init);
1542 module_exit(nvmem_exit);
1546 MODULE_DESCRIPTION("nvmem Driver Core");
1547 MODULE_LICENSE("GPL v2");