1 // SPDX-License-Identifier: GPL-2.0
3 * nvmem framework core.
5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
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>
18 #include <linux/gpio/consumer.h>
20 #include <linux/slab.h>
34 struct bin_attribute eeprom;
35 struct device *base_dev;
36 struct list_head cells;
37 const struct nvmem_keepout *keepout;
38 unsigned int nkeepout;
39 nvmem_reg_read_t reg_read;
40 nvmem_reg_write_t reg_write;
41 struct gpio_desc *wp_gpio;
42 struct nvmem_layout *layout;
46 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
48 #define FLAG_COMPAT BIT(0)
49 struct nvmem_cell_entry {
56 nvmem_cell_post_process_t read_post_process;
58 struct device_node *np;
59 struct nvmem_device *nvmem;
60 struct list_head node;
64 struct nvmem_cell_entry *entry;
69 static DEFINE_MUTEX(nvmem_mutex);
70 static DEFINE_IDA(nvmem_ida);
72 static DEFINE_MUTEX(nvmem_cell_mutex);
73 static LIST_HEAD(nvmem_cell_tables);
75 static DEFINE_MUTEX(nvmem_lookup_mutex);
76 static LIST_HEAD(nvmem_lookup_list);
78 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
80 static DEFINE_SPINLOCK(nvmem_layout_lock);
81 static LIST_HEAD(nvmem_layouts);
83 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
84 void *val, size_t bytes)
87 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
92 static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
93 void *val, size_t bytes)
97 if (nvmem->reg_write) {
98 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
99 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
100 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
107 static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
108 unsigned int offset, void *val,
109 size_t bytes, int write)
112 unsigned int end = offset + bytes;
113 unsigned int kend, ksize;
114 const struct nvmem_keepout *keepout = nvmem->keepout;
115 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
119 * Skip all keepouts before the range being accessed.
120 * Keepouts are sorted.
122 while ((keepout < keepoutend) && (keepout->end <= offset))
125 while ((offset < end) && (keepout < keepoutend)) {
126 /* Access the valid portion before the keepout. */
127 if (offset < keepout->start) {
128 kend = min(end, keepout->start);
129 ksize = kend - offset;
131 rc = __nvmem_reg_write(nvmem, offset, val, ksize);
133 rc = __nvmem_reg_read(nvmem, offset, val, ksize);
143 * Now we're aligned to the start of this keepout zone. Go
146 kend = min(end, keepout->end);
147 ksize = kend - offset;
149 memset(val, keepout->value, ksize);
157 * If we ran out of keepouts but there's still stuff to do, send it
161 ksize = end - offset;
163 return __nvmem_reg_write(nvmem, offset, val, ksize);
165 return __nvmem_reg_read(nvmem, offset, val, ksize);
171 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
172 void *val, size_t bytes)
174 if (!nvmem->nkeepout)
175 return __nvmem_reg_read(nvmem, offset, val, bytes);
177 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
180 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
181 void *val, size_t bytes)
183 if (!nvmem->nkeepout)
184 return __nvmem_reg_write(nvmem, offset, val, bytes);
186 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
189 #ifdef CONFIG_NVMEM_SYSFS
190 static const char * const nvmem_type_str[] = {
191 [NVMEM_TYPE_UNKNOWN] = "Unknown",
192 [NVMEM_TYPE_EEPROM] = "EEPROM",
193 [NVMEM_TYPE_OTP] = "OTP",
194 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
195 [NVMEM_TYPE_FRAM] = "FRAM",
198 #ifdef CONFIG_DEBUG_LOCK_ALLOC
199 static struct lock_class_key eeprom_lock_key;
202 static ssize_t type_show(struct device *dev,
203 struct device_attribute *attr, char *buf)
205 struct nvmem_device *nvmem = to_nvmem_device(dev);
207 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
210 static DEVICE_ATTR_RO(type);
212 static struct attribute *nvmem_attrs[] = {
217 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
218 struct bin_attribute *attr, char *buf,
219 loff_t pos, size_t count)
222 struct nvmem_device *nvmem;
228 dev = kobj_to_dev(kobj);
229 nvmem = to_nvmem_device(dev);
231 /* Stop the user from reading */
232 if (pos >= nvmem->size)
235 if (!IS_ALIGNED(pos, nvmem->stride))
238 if (count < nvmem->word_size)
241 if (pos + count > nvmem->size)
242 count = nvmem->size - pos;
244 count = round_down(count, nvmem->word_size);
246 if (!nvmem->reg_read)
249 rc = nvmem_reg_read(nvmem, pos, buf, count);
257 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
258 struct bin_attribute *attr, char *buf,
259 loff_t pos, size_t count)
262 struct nvmem_device *nvmem;
268 dev = kobj_to_dev(kobj);
269 nvmem = to_nvmem_device(dev);
271 /* Stop the user from writing */
272 if (pos >= nvmem->size)
275 if (!IS_ALIGNED(pos, nvmem->stride))
278 if (count < nvmem->word_size)
281 if (pos + count > nvmem->size)
282 count = nvmem->size - pos;
284 count = round_down(count, nvmem->word_size);
286 if (!nvmem->reg_write)
289 rc = nvmem_reg_write(nvmem, pos, buf, count);
297 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
301 if (!nvmem->root_only)
304 if (!nvmem->read_only)
307 if (!nvmem->reg_write)
310 if (!nvmem->reg_read)
316 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
317 struct bin_attribute *attr, int i)
319 struct device *dev = kobj_to_dev(kobj);
320 struct nvmem_device *nvmem = to_nvmem_device(dev);
322 attr->size = nvmem->size;
324 return nvmem_bin_attr_get_umode(nvmem);
327 /* default read/write permissions */
328 static struct bin_attribute bin_attr_rw_nvmem = {
333 .read = bin_attr_nvmem_read,
334 .write = bin_attr_nvmem_write,
337 static struct bin_attribute *nvmem_bin_attributes[] = {
342 static const struct attribute_group nvmem_bin_group = {
343 .bin_attrs = nvmem_bin_attributes,
344 .attrs = nvmem_attrs,
345 .is_bin_visible = nvmem_bin_attr_is_visible,
348 static const struct attribute_group *nvmem_dev_groups[] = {
353 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
357 .read = bin_attr_nvmem_read,
358 .write = bin_attr_nvmem_write,
362 * nvmem_setup_compat() - Create an additional binary entry in
363 * drivers sys directory, to be backwards compatible with the older
364 * drivers/misc/eeprom drivers.
366 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
367 const struct nvmem_config *config)
374 if (!config->base_dev)
377 if (config->type == NVMEM_TYPE_FRAM)
378 bin_attr_nvmem_eeprom_compat.attr.name = "fram";
380 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
381 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
382 nvmem->eeprom.size = nvmem->size;
383 #ifdef CONFIG_DEBUG_LOCK_ALLOC
384 nvmem->eeprom.attr.key = &eeprom_lock_key;
386 nvmem->eeprom.private = &nvmem->dev;
387 nvmem->base_dev = config->base_dev;
389 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
392 "Failed to create eeprom binary file %d\n", rval);
396 nvmem->flags |= FLAG_COMPAT;
401 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
402 const struct nvmem_config *config)
405 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
408 #else /* CONFIG_NVMEM_SYSFS */
410 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
411 const struct nvmem_config *config)
415 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
416 const struct nvmem_config *config)
420 #endif /* CONFIG_NVMEM_SYSFS */
422 static void nvmem_release(struct device *dev)
424 struct nvmem_device *nvmem = to_nvmem_device(dev);
426 ida_free(&nvmem_ida, nvmem->id);
427 gpiod_put(nvmem->wp_gpio);
431 static const struct device_type nvmem_provider_type = {
432 .release = nvmem_release,
435 static struct bus_type nvmem_bus_type = {
439 static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
441 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
442 mutex_lock(&nvmem_mutex);
443 list_del(&cell->node);
444 mutex_unlock(&nvmem_mutex);
445 of_node_put(cell->np);
446 kfree_const(cell->name);
450 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
452 struct nvmem_cell_entry *cell, *p;
454 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
455 nvmem_cell_entry_drop(cell);
458 static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
460 mutex_lock(&nvmem_mutex);
461 list_add_tail(&cell->node, &cell->nvmem->cells);
462 mutex_unlock(&nvmem_mutex);
463 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
466 static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
467 const struct nvmem_cell_info *info,
468 struct nvmem_cell_entry *cell)
471 cell->offset = info->offset;
472 cell->raw_len = info->raw_len ?: info->bytes;
473 cell->bytes = info->bytes;
474 cell->name = info->name;
475 cell->read_post_process = info->read_post_process;
476 cell->priv = info->priv;
478 cell->bit_offset = info->bit_offset;
479 cell->nbits = info->nbits;
483 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
486 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
488 "cell %s unaligned to nvmem stride %d\n",
489 cell->name ?: "<unknown>", nvmem->stride);
496 static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
497 const struct nvmem_cell_info *info,
498 struct nvmem_cell_entry *cell)
502 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
506 cell->name = kstrdup_const(info->name, GFP_KERNEL);
514 * nvmem_add_one_cell() - Add one cell information to an nvmem device
516 * @nvmem: nvmem device to add cells to.
517 * @info: nvmem cell info to add to the device
519 * Return: 0 or negative error code on failure.
521 int nvmem_add_one_cell(struct nvmem_device *nvmem,
522 const struct nvmem_cell_info *info)
524 struct nvmem_cell_entry *cell;
527 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
531 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
537 nvmem_cell_entry_add(cell);
541 EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
544 * nvmem_add_cells() - Add cell information to an nvmem device
546 * @nvmem: nvmem device to add cells to.
547 * @info: nvmem cell info to add to the device
548 * @ncells: number of cells in info
550 * Return: 0 or negative error code on failure.
552 static int nvmem_add_cells(struct nvmem_device *nvmem,
553 const struct nvmem_cell_info *info,
558 for (i = 0; i < ncells; i++) {
559 rval = nvmem_add_one_cell(nvmem, &info[i]);
568 * nvmem_register_notifier() - Register a notifier block for nvmem events.
570 * @nb: notifier block to be called on nvmem events.
572 * Return: 0 on success, negative error number on failure.
574 int nvmem_register_notifier(struct notifier_block *nb)
576 return blocking_notifier_chain_register(&nvmem_notifier, nb);
578 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
581 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
583 * @nb: notifier block to be unregistered.
585 * Return: 0 on success, negative error number on failure.
587 int nvmem_unregister_notifier(struct notifier_block *nb)
589 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
591 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
593 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
595 const struct nvmem_cell_info *info;
596 struct nvmem_cell_table *table;
597 struct nvmem_cell_entry *cell;
600 mutex_lock(&nvmem_cell_mutex);
601 list_for_each_entry(table, &nvmem_cell_tables, node) {
602 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
603 for (i = 0; i < table->ncells; i++) {
604 info = &table->cells[i];
606 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
612 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
618 nvmem_cell_entry_add(cell);
624 mutex_unlock(&nvmem_cell_mutex);
628 static struct nvmem_cell_entry *
629 nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
631 struct nvmem_cell_entry *iter, *cell = NULL;
633 mutex_lock(&nvmem_mutex);
634 list_for_each_entry(iter, &nvmem->cells, node) {
635 if (strcmp(cell_id, iter->name) == 0) {
640 mutex_unlock(&nvmem_mutex);
645 static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
647 unsigned int cur = 0;
648 const struct nvmem_keepout *keepout = nvmem->keepout;
649 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
651 while (keepout < keepoutend) {
652 /* Ensure keepouts are sorted and don't overlap. */
653 if (keepout->start < cur) {
655 "Keepout regions aren't sorted or overlap.\n");
660 if (keepout->end < keepout->start) {
662 "Invalid keepout region.\n");
668 * Validate keepouts (and holes between) don't violate
669 * word_size constraints.
671 if ((keepout->end - keepout->start < nvmem->word_size) ||
672 ((keepout->start != cur) &&
673 (keepout->start - cur < nvmem->word_size))) {
676 "Keepout regions violate word_size constraints.\n");
681 /* Validate keepouts don't violate stride (alignment). */
682 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
683 !IS_ALIGNED(keepout->end, nvmem->stride)) {
686 "Keepout regions violate stride.\n");
698 static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
700 struct nvmem_layout *layout = nvmem->layout;
701 struct device *dev = &nvmem->dev;
702 struct device_node *child;
706 for_each_child_of_node(np, child) {
707 struct nvmem_cell_info info = {0};
709 addr = of_get_property(child, "reg", &len);
712 if (len < 2 * sizeof(u32)) {
713 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
718 info.offset = be32_to_cpup(addr++);
719 info.bytes = be32_to_cpup(addr);
720 info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
722 addr = of_get_property(child, "bits", &len);
723 if (addr && len == (2 * sizeof(u32))) {
724 info.bit_offset = be32_to_cpup(addr++);
725 info.nbits = be32_to_cpup(addr);
728 info.np = of_node_get(child);
730 if (layout && layout->fixup_cell_info)
731 layout->fixup_cell_info(nvmem, layout, &info);
733 ret = nvmem_add_one_cell(nvmem, &info);
744 static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
746 return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
749 static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
751 struct device_node *layout_np;
754 layout_np = of_nvmem_layout_get_container(nvmem);
758 if (of_device_is_compatible(layout_np, "fixed-layout"))
759 err = nvmem_add_cells_from_dt(nvmem, layout_np);
761 of_node_put(layout_np);
766 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner)
768 layout->owner = owner;
770 spin_lock(&nvmem_layout_lock);
771 list_add(&layout->node, &nvmem_layouts);
772 spin_unlock(&nvmem_layout_lock);
774 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_ADD, layout);
778 EXPORT_SYMBOL_GPL(__nvmem_layout_register);
780 void nvmem_layout_unregister(struct nvmem_layout *layout)
782 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_REMOVE, layout);
784 spin_lock(&nvmem_layout_lock);
785 list_del(&layout->node);
786 spin_unlock(&nvmem_layout_lock);
788 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
790 static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem)
792 struct device_node *layout_np;
793 struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER);
795 layout_np = of_nvmem_layout_get_container(nvmem);
799 /* Fixed layouts don't have a matching driver */
800 if (of_device_is_compatible(layout_np, "fixed-layout")) {
801 of_node_put(layout_np);
806 * In case the nvmem device was built-in while the layout was built as a
807 * module, we shall manually request the layout driver loading otherwise
808 * we'll never have any match.
810 of_request_module(layout_np);
812 spin_lock(&nvmem_layout_lock);
814 list_for_each_entry(l, &nvmem_layouts, node) {
815 if (of_match_node(l->of_match_table, layout_np)) {
816 if (try_module_get(l->owner))
823 spin_unlock(&nvmem_layout_lock);
824 of_node_put(layout_np);
829 static void nvmem_layout_put(struct nvmem_layout *layout)
832 module_put(layout->owner);
835 static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem)
837 struct nvmem_layout *layout = nvmem->layout;
840 if (layout && layout->add_cells) {
841 ret = layout->add_cells(&nvmem->dev, nvmem, layout);
849 #if IS_ENABLED(CONFIG_OF)
851 * of_nvmem_layout_get_container() - Get OF node to layout container.
853 * @nvmem: nvmem device.
855 * Return: a node pointer with refcount incremented or NULL if no
856 * container exists. Use of_node_put() on it when done.
858 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
860 return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
862 EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
865 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
866 struct nvmem_layout *layout)
868 struct device_node __maybe_unused *layout_np;
869 const struct of_device_id *match;
871 layout_np = of_nvmem_layout_get_container(nvmem);
872 match = of_match_node(layout->of_match_table, layout_np);
874 return match ? match->data : NULL;
876 EXPORT_SYMBOL_GPL(nvmem_layout_get_match_data);
879 * nvmem_register() - Register a nvmem device for given nvmem_config.
880 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
882 * @config: nvmem device configuration with which nvmem device is created.
884 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
888 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
890 struct nvmem_device *nvmem;
894 return ERR_PTR(-EINVAL);
896 if (!config->reg_read && !config->reg_write)
897 return ERR_PTR(-EINVAL);
899 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
901 return ERR_PTR(-ENOMEM);
903 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
906 return ERR_PTR(rval);
911 nvmem->dev.type = &nvmem_provider_type;
912 nvmem->dev.bus = &nvmem_bus_type;
913 nvmem->dev.parent = config->dev;
915 device_initialize(&nvmem->dev);
917 if (!config->ignore_wp)
918 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
920 if (IS_ERR(nvmem->wp_gpio)) {
921 rval = PTR_ERR(nvmem->wp_gpio);
922 nvmem->wp_gpio = NULL;
926 kref_init(&nvmem->refcnt);
927 INIT_LIST_HEAD(&nvmem->cells);
929 nvmem->owner = config->owner;
930 if (!nvmem->owner && config->dev->driver)
931 nvmem->owner = config->dev->driver->owner;
932 nvmem->stride = config->stride ?: 1;
933 nvmem->word_size = config->word_size ?: 1;
934 nvmem->size = config->size;
935 nvmem->root_only = config->root_only;
936 nvmem->priv = config->priv;
937 nvmem->type = config->type;
938 nvmem->reg_read = config->reg_read;
939 nvmem->reg_write = config->reg_write;
940 nvmem->keepout = config->keepout;
941 nvmem->nkeepout = config->nkeepout;
943 nvmem->dev.of_node = config->of_node;
944 else if (!config->no_of_node)
945 nvmem->dev.of_node = config->dev->of_node;
947 switch (config->id) {
948 case NVMEM_DEVID_NONE:
949 rval = dev_set_name(&nvmem->dev, "%s", config->name);
951 case NVMEM_DEVID_AUTO:
952 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
955 rval = dev_set_name(&nvmem->dev, "%s%d",
956 config->name ? : "nvmem",
957 config->name ? config->id : nvmem->id);
964 nvmem->read_only = device_property_present(config->dev, "read-only") ||
965 config->read_only || !nvmem->reg_write;
967 #ifdef CONFIG_NVMEM_SYSFS
968 nvmem->dev.groups = nvmem_dev_groups;
971 if (nvmem->nkeepout) {
972 rval = nvmem_validate_keepouts(nvmem);
977 if (config->compat) {
978 rval = nvmem_sysfs_setup_compat(nvmem, config);
984 * If the driver supplied a layout by config->layout, the module
985 * pointer will be NULL and nvmem_layout_put() will be a noop.
987 nvmem->layout = config->layout ?: nvmem_layout_get(nvmem);
988 if (IS_ERR(nvmem->layout)) {
989 rval = PTR_ERR(nvmem->layout);
990 nvmem->layout = NULL;
992 if (rval == -EPROBE_DEFER)
993 goto err_teardown_compat;
997 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
999 goto err_remove_cells;
1002 rval = nvmem_add_cells_from_table(nvmem);
1004 goto err_remove_cells;
1006 rval = nvmem_add_cells_from_legacy_of(nvmem);
1008 goto err_remove_cells;
1010 rval = nvmem_add_cells_from_fixed_layout(nvmem);
1012 goto err_remove_cells;
1014 rval = nvmem_add_cells_from_layout(nvmem);
1016 goto err_remove_cells;
1018 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
1020 rval = device_add(&nvmem->dev);
1022 goto err_remove_cells;
1024 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
1029 nvmem_device_remove_all_cells(nvmem);
1030 nvmem_layout_put(nvmem->layout);
1031 err_teardown_compat:
1033 nvmem_sysfs_remove_compat(nvmem, config);
1035 put_device(&nvmem->dev);
1037 return ERR_PTR(rval);
1039 EXPORT_SYMBOL_GPL(nvmem_register);
1041 static void nvmem_device_release(struct kref *kref)
1043 struct nvmem_device *nvmem;
1045 nvmem = container_of(kref, struct nvmem_device, refcnt);
1047 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1049 if (nvmem->flags & FLAG_COMPAT)
1050 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1052 nvmem_device_remove_all_cells(nvmem);
1053 nvmem_layout_put(nvmem->layout);
1054 device_unregister(&nvmem->dev);
1058 * nvmem_unregister() - Unregister previously registered nvmem device
1060 * @nvmem: Pointer to previously registered nvmem device.
1062 void nvmem_unregister(struct nvmem_device *nvmem)
1065 kref_put(&nvmem->refcnt, nvmem_device_release);
1067 EXPORT_SYMBOL_GPL(nvmem_unregister);
1069 static void devm_nvmem_unregister(void *nvmem)
1071 nvmem_unregister(nvmem);
1075 * devm_nvmem_register() - Register a managed nvmem device for given
1077 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1079 * @dev: Device that uses the nvmem device.
1080 * @config: nvmem device configuration with which nvmem device is created.
1082 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1085 struct nvmem_device *devm_nvmem_register(struct device *dev,
1086 const struct nvmem_config *config)
1088 struct nvmem_device *nvmem;
1091 nvmem = nvmem_register(config);
1095 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1097 return ERR_PTR(ret);
1101 EXPORT_SYMBOL_GPL(devm_nvmem_register);
1103 static struct nvmem_device *__nvmem_device_get(void *data,
1104 int (*match)(struct device *dev, const void *data))
1106 struct nvmem_device *nvmem = NULL;
1109 mutex_lock(&nvmem_mutex);
1110 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1112 nvmem = to_nvmem_device(dev);
1113 mutex_unlock(&nvmem_mutex);
1115 return ERR_PTR(-EPROBE_DEFER);
1117 if (!try_module_get(nvmem->owner)) {
1118 dev_err(&nvmem->dev,
1119 "could not increase module refcount for cell %s\n",
1120 nvmem_dev_name(nvmem));
1122 put_device(&nvmem->dev);
1123 return ERR_PTR(-EINVAL);
1126 kref_get(&nvmem->refcnt);
1131 static void __nvmem_device_put(struct nvmem_device *nvmem)
1133 put_device(&nvmem->dev);
1134 module_put(nvmem->owner);
1135 kref_put(&nvmem->refcnt, nvmem_device_release);
1138 #if IS_ENABLED(CONFIG_OF)
1140 * of_nvmem_device_get() - Get nvmem device from a given id
1142 * @np: Device tree node that uses the nvmem device.
1143 * @id: nvmem name from nvmem-names property.
1145 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1148 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1151 struct device_node *nvmem_np;
1152 struct nvmem_device *nvmem;
1156 index = of_property_match_string(np, "nvmem-names", id);
1158 nvmem_np = of_parse_phandle(np, "nvmem", index);
1160 return ERR_PTR(-ENOENT);
1162 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1163 of_node_put(nvmem_np);
1166 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1170 * nvmem_device_get() - Get nvmem device from a given id
1172 * @dev: Device that uses the nvmem device.
1173 * @dev_name: name of the requested nvmem device.
1175 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1178 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1180 if (dev->of_node) { /* try dt first */
1181 struct nvmem_device *nvmem;
1183 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1185 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1190 return __nvmem_device_get((void *)dev_name, device_match_name);
1192 EXPORT_SYMBOL_GPL(nvmem_device_get);
1195 * nvmem_device_find() - Find nvmem device with matching function
1197 * @data: Data to pass to match function
1198 * @match: Callback function to check device
1200 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1203 struct nvmem_device *nvmem_device_find(void *data,
1204 int (*match)(struct device *dev, const void *data))
1206 return __nvmem_device_get(data, match);
1208 EXPORT_SYMBOL_GPL(nvmem_device_find);
1210 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1212 struct nvmem_device **nvmem = res;
1214 if (WARN_ON(!nvmem || !*nvmem))
1217 return *nvmem == data;
1220 static void devm_nvmem_device_release(struct device *dev, void *res)
1222 nvmem_device_put(*(struct nvmem_device **)res);
1226 * devm_nvmem_device_put() - put alredy got nvmem device
1228 * @dev: Device that uses the nvmem device.
1229 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1230 * that needs to be released.
1232 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1236 ret = devres_release(dev, devm_nvmem_device_release,
1237 devm_nvmem_device_match, nvmem);
1241 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1244 * nvmem_device_put() - put alredy got nvmem device
1246 * @nvmem: pointer to nvmem device that needs to be released.
1248 void nvmem_device_put(struct nvmem_device *nvmem)
1250 __nvmem_device_put(nvmem);
1252 EXPORT_SYMBOL_GPL(nvmem_device_put);
1255 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1257 * @dev: Device that requests the nvmem device.
1258 * @id: name id for the requested nvmem device.
1260 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1261 * on success. The nvmem_cell will be freed by the automatically once the
1264 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1266 struct nvmem_device **ptr, *nvmem;
1268 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1270 return ERR_PTR(-ENOMEM);
1272 nvmem = nvmem_device_get(dev, id);
1273 if (!IS_ERR(nvmem)) {
1275 devres_add(dev, ptr);
1282 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1284 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1285 const char *id, int index)
1287 struct nvmem_cell *cell;
1288 const char *name = NULL;
1290 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1292 return ERR_PTR(-ENOMEM);
1295 name = kstrdup_const(id, GFP_KERNEL);
1298 return ERR_PTR(-ENOMEM);
1303 cell->entry = entry;
1304 cell->index = index;
1309 static struct nvmem_cell *
1310 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1312 struct nvmem_cell_entry *cell_entry;
1313 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1314 struct nvmem_cell_lookup *lookup;
1315 struct nvmem_device *nvmem;
1319 return ERR_PTR(-EINVAL);
1321 dev_id = dev_name(dev);
1323 mutex_lock(&nvmem_lookup_mutex);
1325 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1326 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1327 (strcmp(lookup->con_id, con_id) == 0)) {
1328 /* This is the right entry. */
1329 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1331 if (IS_ERR(nvmem)) {
1332 /* Provider may not be registered yet. */
1333 cell = ERR_CAST(nvmem);
1337 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1340 __nvmem_device_put(nvmem);
1341 cell = ERR_PTR(-ENOENT);
1343 cell = nvmem_create_cell(cell_entry, con_id, 0);
1345 __nvmem_device_put(nvmem);
1351 mutex_unlock(&nvmem_lookup_mutex);
1355 #if IS_ENABLED(CONFIG_OF)
1356 static struct nvmem_cell_entry *
1357 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1359 struct nvmem_cell_entry *iter, *cell = NULL;
1361 mutex_lock(&nvmem_mutex);
1362 list_for_each_entry(iter, &nvmem->cells, node) {
1363 if (np == iter->np) {
1368 mutex_unlock(&nvmem_mutex);
1374 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1376 * @np: Device tree node that uses the nvmem cell.
1377 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1378 * for the cell at index 0 (the lone cell with no accompanying
1379 * nvmem-cell-names property).
1381 * Return: Will be an ERR_PTR() on error or a valid pointer
1382 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1385 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1387 struct device_node *cell_np, *nvmem_np;
1388 struct nvmem_device *nvmem;
1389 struct nvmem_cell_entry *cell_entry;
1390 struct nvmem_cell *cell;
1391 struct of_phandle_args cell_spec;
1396 /* if cell name exists, find index to the name */
1398 index = of_property_match_string(np, "nvmem-cell-names", id);
1400 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1401 "#nvmem-cell-cells",
1404 return ERR_PTR(-ENOENT);
1406 if (cell_spec.args_count > 1)
1407 return ERR_PTR(-EINVAL);
1409 cell_np = cell_spec.np;
1410 if (cell_spec.args_count)
1411 cell_index = cell_spec.args[0];
1413 nvmem_np = of_get_parent(cell_np);
1415 of_node_put(cell_np);
1416 return ERR_PTR(-EINVAL);
1419 /* nvmem layouts produce cells within the nvmem-layout container */
1420 if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1421 nvmem_np = of_get_next_parent(nvmem_np);
1423 of_node_put(cell_np);
1424 return ERR_PTR(-EINVAL);
1428 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1429 of_node_put(nvmem_np);
1430 if (IS_ERR(nvmem)) {
1431 of_node_put(cell_np);
1432 return ERR_CAST(nvmem);
1435 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1436 of_node_put(cell_np);
1438 __nvmem_device_put(nvmem);
1439 return ERR_PTR(-ENOENT);
1442 cell = nvmem_create_cell(cell_entry, id, cell_index);
1444 __nvmem_device_put(nvmem);
1448 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1452 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1454 * @dev: Device that requests the nvmem cell.
1455 * @id: nvmem cell name to get (this corresponds with the name from the
1456 * nvmem-cell-names property for DT systems and with the con_id from
1457 * the lookup entry for non-DT systems).
1459 * Return: Will be an ERR_PTR() on error or a valid pointer
1460 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1463 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1465 struct nvmem_cell *cell;
1467 if (dev->of_node) { /* try dt first */
1468 cell = of_nvmem_cell_get(dev->of_node, id);
1469 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1473 /* NULL cell id only allowed for device tree; invalid otherwise */
1475 return ERR_PTR(-EINVAL);
1477 return nvmem_cell_get_from_lookup(dev, id);
1479 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1481 static void devm_nvmem_cell_release(struct device *dev, void *res)
1483 nvmem_cell_put(*(struct nvmem_cell **)res);
1487 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1489 * @dev: Device that requests the nvmem cell.
1490 * @id: nvmem cell name id to get.
1492 * Return: Will be an ERR_PTR() on error or a valid pointer
1493 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1494 * automatically once the device is freed.
1496 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1498 struct nvmem_cell **ptr, *cell;
1500 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1502 return ERR_PTR(-ENOMEM);
1504 cell = nvmem_cell_get(dev, id);
1505 if (!IS_ERR(cell)) {
1507 devres_add(dev, ptr);
1514 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1516 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1518 struct nvmem_cell **c = res;
1520 if (WARN_ON(!c || !*c))
1527 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1528 * from devm_nvmem_cell_get.
1530 * @dev: Device that requests the nvmem cell.
1531 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1533 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1537 ret = devres_release(dev, devm_nvmem_cell_release,
1538 devm_nvmem_cell_match, cell);
1542 EXPORT_SYMBOL(devm_nvmem_cell_put);
1545 * nvmem_cell_put() - Release previously allocated nvmem cell.
1547 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1549 void nvmem_cell_put(struct nvmem_cell *cell)
1551 struct nvmem_device *nvmem = cell->entry->nvmem;
1554 kfree_const(cell->id);
1557 __nvmem_device_put(nvmem);
1559 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1561 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1564 int i, extra, bit_offset = cell->bit_offset;
1569 *b++ >>= bit_offset;
1571 /* setup rest of the bytes if any */
1572 for (i = 1; i < cell->bytes; i++) {
1573 /* Get bits from next byte and shift them towards msb */
1574 *p |= *b << (BITS_PER_BYTE - bit_offset);
1577 *b++ >>= bit_offset;
1580 /* point to the msb */
1581 p += cell->bytes - 1;
1584 /* result fits in less bytes */
1585 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1586 while (--extra >= 0)
1589 /* clear msb bits if any leftover in the last byte */
1590 if (cell->nbits % BITS_PER_BYTE)
1591 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1594 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1595 struct nvmem_cell_entry *cell,
1596 void *buf, size_t *len, const char *id, int index)
1600 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
1605 /* shift bits in-place */
1606 if (cell->bit_offset || cell->nbits)
1607 nvmem_shift_read_buffer_in_place(cell, buf);
1609 if (cell->read_post_process) {
1610 rc = cell->read_post_process(cell->priv, id, index,
1611 cell->offset, buf, cell->raw_len);
1623 * nvmem_cell_read() - Read a given nvmem cell
1625 * @cell: nvmem cell to be read.
1626 * @len: pointer to length of cell which will be populated on successful read;
1629 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1630 * buffer should be freed by the consumer with a kfree().
1632 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1634 struct nvmem_cell_entry *entry = cell->entry;
1635 struct nvmem_device *nvmem = entry->nvmem;
1640 return ERR_PTR(-EINVAL);
1642 buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
1644 return ERR_PTR(-ENOMEM);
1646 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1654 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1656 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1659 struct nvmem_device *nvmem = cell->nvmem;
1660 int i, rc, nbits, bit_offset = cell->bit_offset;
1661 u8 v, *p, *buf, *b, pbyte, pbits;
1663 nbits = cell->nbits;
1664 buf = kzalloc(cell->bytes, GFP_KERNEL);
1666 return ERR_PTR(-ENOMEM);
1668 memcpy(buf, _buf, len);
1675 /* setup the first byte with lsb bits from nvmem */
1676 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1679 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1681 /* setup rest of the byte if any */
1682 for (i = 1; i < cell->bytes; i++) {
1683 /* Get last byte bits and shift them towards lsb */
1684 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1692 /* if it's not end on byte boundary */
1693 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1694 /* setup the last byte with msb bits from nvmem */
1695 rc = nvmem_reg_read(nvmem,
1696 cell->offset + cell->bytes - 1, &v, 1);
1699 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1709 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1711 struct nvmem_device *nvmem = cell->nvmem;
1714 if (!nvmem || nvmem->read_only ||
1715 (cell->bit_offset == 0 && len != cell->bytes))
1719 * Any cells which have a read_post_process hook are read-only because
1720 * we cannot reverse the operation and it might affect other cells,
1723 if (cell->read_post_process)
1726 if (cell->bit_offset || cell->nbits) {
1727 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1729 return PTR_ERR(buf);
1732 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1734 /* free the tmp buffer */
1735 if (cell->bit_offset || cell->nbits)
1745 * nvmem_cell_write() - Write to a given nvmem cell
1747 * @cell: nvmem cell to be written.
1748 * @buf: Buffer to be written.
1749 * @len: length of buffer to be written to nvmem cell.
1751 * Return: length of bytes written or negative on failure.
1753 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1755 return __nvmem_cell_entry_write(cell->entry, buf, len);
1758 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1760 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1761 void *val, size_t count)
1763 struct nvmem_cell *cell;
1767 cell = nvmem_cell_get(dev, cell_id);
1769 return PTR_ERR(cell);
1771 buf = nvmem_cell_read(cell, &len);
1773 nvmem_cell_put(cell);
1774 return PTR_ERR(buf);
1778 nvmem_cell_put(cell);
1781 memcpy(val, buf, count);
1783 nvmem_cell_put(cell);
1789 * nvmem_cell_read_u8() - Read a cell value as a u8
1791 * @dev: Device that requests the nvmem cell.
1792 * @cell_id: Name of nvmem cell to read.
1793 * @val: pointer to output value.
1795 * Return: 0 on success or negative errno.
1797 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1799 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1801 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1804 * nvmem_cell_read_u16() - Read a cell value as a u16
1806 * @dev: Device that requests the nvmem cell.
1807 * @cell_id: Name of nvmem cell to read.
1808 * @val: pointer to output value.
1810 * Return: 0 on success or negative errno.
1812 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1814 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1816 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1819 * nvmem_cell_read_u32() - Read a cell value as a u32
1821 * @dev: Device that requests the nvmem cell.
1822 * @cell_id: Name of nvmem cell to read.
1823 * @val: pointer to output value.
1825 * Return: 0 on success or negative errno.
1827 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1829 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1831 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1834 * nvmem_cell_read_u64() - Read a cell value as a u64
1836 * @dev: Device that requests the nvmem cell.
1837 * @cell_id: Name of nvmem cell to read.
1838 * @val: pointer to output value.
1840 * Return: 0 on success or negative errno.
1842 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1844 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1846 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1848 static const void *nvmem_cell_read_variable_common(struct device *dev,
1849 const char *cell_id,
1850 size_t max_len, size_t *len)
1852 struct nvmem_cell *cell;
1856 cell = nvmem_cell_get(dev, cell_id);
1860 nbits = cell->entry->nbits;
1861 buf = nvmem_cell_read(cell, len);
1862 nvmem_cell_put(cell);
1867 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1868 * the length of the real data. Throw away the extra junk.
1871 *len = DIV_ROUND_UP(nbits, 8);
1873 if (*len > max_len) {
1875 return ERR_PTR(-ERANGE);
1882 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1884 * @dev: Device that requests the nvmem cell.
1885 * @cell_id: Name of nvmem cell to read.
1886 * @val: pointer to output value.
1888 * Return: 0 on success or negative errno.
1890 int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1897 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1899 return PTR_ERR(buf);
1901 /* Copy w/ implicit endian conversion */
1903 for (i = 0; i < len; i++)
1904 *val |= buf[i] << (8 * i);
1910 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1913 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1915 * @dev: Device that requests the nvmem cell.
1916 * @cell_id: Name of nvmem cell to read.
1917 * @val: pointer to output value.
1919 * Return: 0 on success or negative errno.
1921 int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1928 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1930 return PTR_ERR(buf);
1932 /* Copy w/ implicit endian conversion */
1934 for (i = 0; i < len; i++)
1935 *val |= (uint64_t)buf[i] << (8 * i);
1941 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1944 * nvmem_device_cell_read() - Read a given nvmem device and cell
1946 * @nvmem: nvmem device to read from.
1947 * @info: nvmem cell info to be read.
1948 * @buf: buffer pointer which will be populated on successful read.
1950 * Return: length of successful bytes read on success and negative
1951 * error code on error.
1953 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1954 struct nvmem_cell_info *info, void *buf)
1956 struct nvmem_cell_entry cell;
1963 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1967 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
1973 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1976 * nvmem_device_cell_write() - Write cell to a given nvmem device
1978 * @nvmem: nvmem device to be written to.
1979 * @info: nvmem cell info to be written.
1980 * @buf: buffer to be written to cell.
1982 * Return: length of bytes written or negative error code on failure.
1984 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1985 struct nvmem_cell_info *info, void *buf)
1987 struct nvmem_cell_entry cell;
1993 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1997 return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
1999 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
2002 * nvmem_device_read() - Read from a given nvmem device
2004 * @nvmem: nvmem device to read from.
2005 * @offset: offset in nvmem device.
2006 * @bytes: number of bytes to read.
2007 * @buf: buffer pointer which will be populated on successful read.
2009 * Return: length of successful bytes read on success and negative
2010 * error code on error.
2012 int nvmem_device_read(struct nvmem_device *nvmem,
2013 unsigned int offset,
2014 size_t bytes, void *buf)
2021 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
2028 EXPORT_SYMBOL_GPL(nvmem_device_read);
2031 * nvmem_device_write() - Write cell to a given nvmem device
2033 * @nvmem: nvmem device to be written to.
2034 * @offset: offset in nvmem device.
2035 * @bytes: number of bytes to write.
2036 * @buf: buffer to be written.
2038 * Return: length of bytes written or negative error code on failure.
2040 int nvmem_device_write(struct nvmem_device *nvmem,
2041 unsigned int offset,
2042 size_t bytes, void *buf)
2049 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2057 EXPORT_SYMBOL_GPL(nvmem_device_write);
2060 * nvmem_add_cell_table() - register a table of cell info entries
2062 * @table: table of cell info entries
2064 void nvmem_add_cell_table(struct nvmem_cell_table *table)
2066 mutex_lock(&nvmem_cell_mutex);
2067 list_add_tail(&table->node, &nvmem_cell_tables);
2068 mutex_unlock(&nvmem_cell_mutex);
2070 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
2073 * nvmem_del_cell_table() - remove a previously registered cell info table
2075 * @table: table of cell info entries
2077 void nvmem_del_cell_table(struct nvmem_cell_table *table)
2079 mutex_lock(&nvmem_cell_mutex);
2080 list_del(&table->node);
2081 mutex_unlock(&nvmem_cell_mutex);
2083 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
2086 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2088 * @entries: array of cell lookup entries
2089 * @nentries: number of cell lookup entries in the array
2091 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2095 mutex_lock(&nvmem_lookup_mutex);
2096 for (i = 0; i < nentries; i++)
2097 list_add_tail(&entries[i].node, &nvmem_lookup_list);
2098 mutex_unlock(&nvmem_lookup_mutex);
2100 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2103 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2106 * @entries: array of cell lookup entries
2107 * @nentries: number of cell lookup entries in the array
2109 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2113 mutex_lock(&nvmem_lookup_mutex);
2114 for (i = 0; i < nentries; i++)
2115 list_del(&entries[i].node);
2116 mutex_unlock(&nvmem_lookup_mutex);
2118 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2121 * nvmem_dev_name() - Get the name of a given nvmem device.
2123 * @nvmem: nvmem device.
2125 * Return: name of the nvmem device.
2127 const char *nvmem_dev_name(struct nvmem_device *nvmem)
2129 return dev_name(&nvmem->dev);
2131 EXPORT_SYMBOL_GPL(nvmem_dev_name);
2133 static int __init nvmem_init(void)
2135 return bus_register(&nvmem_bus_type);
2138 static void __exit nvmem_exit(void)
2140 bus_unregister(&nvmem_bus_type);
2143 subsys_initcall(nvmem_init);
2144 module_exit(nvmem_exit);
2146 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
2147 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
2148 MODULE_DESCRIPTION("nvmem Driver Core");