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/of_device.h>
21 #include <linux/slab.h>
35 struct bin_attribute eeprom;
36 struct device *base_dev;
37 struct list_head cells;
38 const struct nvmem_keepout *keepout;
39 unsigned int nkeepout;
40 nvmem_reg_read_t reg_read;
41 nvmem_reg_write_t reg_write;
42 struct gpio_desc *wp_gpio;
43 struct nvmem_layout *layout;
47 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
49 #define FLAG_COMPAT BIT(0)
50 struct nvmem_cell_entry {
57 nvmem_cell_post_process_t read_post_process;
59 struct device_node *np;
60 struct nvmem_device *nvmem;
61 struct list_head node;
65 struct nvmem_cell_entry *entry;
70 static DEFINE_MUTEX(nvmem_mutex);
71 static DEFINE_IDA(nvmem_ida);
73 static DEFINE_MUTEX(nvmem_cell_mutex);
74 static LIST_HEAD(nvmem_cell_tables);
76 static DEFINE_MUTEX(nvmem_lookup_mutex);
77 static LIST_HEAD(nvmem_lookup_list);
79 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
81 static DEFINE_SPINLOCK(nvmem_layout_lock);
82 static LIST_HEAD(nvmem_layouts);
84 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
85 void *val, size_t bytes)
88 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
93 static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
94 void *val, size_t bytes)
98 if (nvmem->reg_write) {
99 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
100 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
101 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
108 static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
109 unsigned int offset, void *val,
110 size_t bytes, int write)
113 unsigned int end = offset + bytes;
114 unsigned int kend, ksize;
115 const struct nvmem_keepout *keepout = nvmem->keepout;
116 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
120 * Skip all keepouts before the range being accessed.
121 * Keepouts are sorted.
123 while ((keepout < keepoutend) && (keepout->end <= offset))
126 while ((offset < end) && (keepout < keepoutend)) {
127 /* Access the valid portion before the keepout. */
128 if (offset < keepout->start) {
129 kend = min(end, keepout->start);
130 ksize = kend - offset;
132 rc = __nvmem_reg_write(nvmem, offset, val, ksize);
134 rc = __nvmem_reg_read(nvmem, offset, val, ksize);
144 * Now we're aligned to the start of this keepout zone. Go
147 kend = min(end, keepout->end);
148 ksize = kend - offset;
150 memset(val, keepout->value, ksize);
158 * If we ran out of keepouts but there's still stuff to do, send it
162 ksize = end - offset;
164 return __nvmem_reg_write(nvmem, offset, val, ksize);
166 return __nvmem_reg_read(nvmem, offset, val, ksize);
172 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
173 void *val, size_t bytes)
175 if (!nvmem->nkeepout)
176 return __nvmem_reg_read(nvmem, offset, val, bytes);
178 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
181 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
182 void *val, size_t bytes)
184 if (!nvmem->nkeepout)
185 return __nvmem_reg_write(nvmem, offset, val, bytes);
187 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
190 #ifdef CONFIG_NVMEM_SYSFS
191 static const char * const nvmem_type_str[] = {
192 [NVMEM_TYPE_UNKNOWN] = "Unknown",
193 [NVMEM_TYPE_EEPROM] = "EEPROM",
194 [NVMEM_TYPE_OTP] = "OTP",
195 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
196 [NVMEM_TYPE_FRAM] = "FRAM",
199 #ifdef CONFIG_DEBUG_LOCK_ALLOC
200 static struct lock_class_key eeprom_lock_key;
203 static ssize_t type_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
206 struct nvmem_device *nvmem = to_nvmem_device(dev);
208 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
211 static DEVICE_ATTR_RO(type);
213 static struct attribute *nvmem_attrs[] = {
218 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
219 struct bin_attribute *attr, char *buf,
220 loff_t pos, size_t count)
223 struct nvmem_device *nvmem;
229 dev = kobj_to_dev(kobj);
230 nvmem = to_nvmem_device(dev);
232 /* Stop the user from reading */
233 if (pos >= nvmem->size)
236 if (!IS_ALIGNED(pos, nvmem->stride))
239 if (count < nvmem->word_size)
242 if (pos + count > nvmem->size)
243 count = nvmem->size - pos;
245 count = round_down(count, nvmem->word_size);
247 if (!nvmem->reg_read)
250 rc = nvmem_reg_read(nvmem, pos, buf, count);
258 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
259 struct bin_attribute *attr, char *buf,
260 loff_t pos, size_t count)
263 struct nvmem_device *nvmem;
269 dev = kobj_to_dev(kobj);
270 nvmem = to_nvmem_device(dev);
272 /* Stop the user from writing */
273 if (pos >= nvmem->size)
276 if (!IS_ALIGNED(pos, nvmem->stride))
279 if (count < nvmem->word_size)
282 if (pos + count > nvmem->size)
283 count = nvmem->size - pos;
285 count = round_down(count, nvmem->word_size);
287 if (!nvmem->reg_write)
290 rc = nvmem_reg_write(nvmem, pos, buf, count);
298 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
302 if (!nvmem->root_only)
305 if (!nvmem->read_only)
308 if (!nvmem->reg_write)
311 if (!nvmem->reg_read)
317 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
318 struct bin_attribute *attr, int i)
320 struct device *dev = kobj_to_dev(kobj);
321 struct nvmem_device *nvmem = to_nvmem_device(dev);
323 attr->size = nvmem->size;
325 return nvmem_bin_attr_get_umode(nvmem);
328 /* default read/write permissions */
329 static struct bin_attribute bin_attr_rw_nvmem = {
334 .read = bin_attr_nvmem_read,
335 .write = bin_attr_nvmem_write,
338 static struct bin_attribute *nvmem_bin_attributes[] = {
343 static const struct attribute_group nvmem_bin_group = {
344 .bin_attrs = nvmem_bin_attributes,
345 .attrs = nvmem_attrs,
346 .is_bin_visible = nvmem_bin_attr_is_visible,
349 static const struct attribute_group *nvmem_dev_groups[] = {
354 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
358 .read = bin_attr_nvmem_read,
359 .write = bin_attr_nvmem_write,
363 * nvmem_setup_compat() - Create an additional binary entry in
364 * drivers sys directory, to be backwards compatible with the older
365 * drivers/misc/eeprom drivers.
367 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
368 const struct nvmem_config *config)
375 if (!config->base_dev)
378 if (config->type == NVMEM_TYPE_FRAM)
379 bin_attr_nvmem_eeprom_compat.attr.name = "fram";
381 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
382 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
383 nvmem->eeprom.size = nvmem->size;
384 #ifdef CONFIG_DEBUG_LOCK_ALLOC
385 nvmem->eeprom.attr.key = &eeprom_lock_key;
387 nvmem->eeprom.private = &nvmem->dev;
388 nvmem->base_dev = config->base_dev;
390 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
393 "Failed to create eeprom binary file %d\n", rval);
397 nvmem->flags |= FLAG_COMPAT;
402 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
403 const struct nvmem_config *config)
406 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
409 #else /* CONFIG_NVMEM_SYSFS */
411 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
412 const struct nvmem_config *config)
416 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
417 const struct nvmem_config *config)
421 #endif /* CONFIG_NVMEM_SYSFS */
423 static void nvmem_release(struct device *dev)
425 struct nvmem_device *nvmem = to_nvmem_device(dev);
427 ida_free(&nvmem_ida, nvmem->id);
428 gpiod_put(nvmem->wp_gpio);
432 static const struct device_type nvmem_provider_type = {
433 .release = nvmem_release,
436 static struct bus_type nvmem_bus_type = {
440 static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
442 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
443 mutex_lock(&nvmem_mutex);
444 list_del(&cell->node);
445 mutex_unlock(&nvmem_mutex);
446 of_node_put(cell->np);
447 kfree_const(cell->name);
451 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
453 struct nvmem_cell_entry *cell, *p;
455 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
456 nvmem_cell_entry_drop(cell);
459 static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
461 mutex_lock(&nvmem_mutex);
462 list_add_tail(&cell->node, &cell->nvmem->cells);
463 mutex_unlock(&nvmem_mutex);
464 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
467 static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
468 const struct nvmem_cell_info *info,
469 struct nvmem_cell_entry *cell)
472 cell->offset = info->offset;
473 cell->raw_len = info->raw_len ?: info->bytes;
474 cell->bytes = info->bytes;
475 cell->name = info->name;
476 cell->read_post_process = info->read_post_process;
477 cell->priv = info->priv;
479 cell->bit_offset = info->bit_offset;
480 cell->nbits = info->nbits;
484 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
487 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
489 "cell %s unaligned to nvmem stride %d\n",
490 cell->name ?: "<unknown>", nvmem->stride);
497 static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
498 const struct nvmem_cell_info *info,
499 struct nvmem_cell_entry *cell)
503 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
507 cell->name = kstrdup_const(info->name, GFP_KERNEL);
515 * nvmem_add_one_cell() - Add one cell information to an nvmem device
517 * @nvmem: nvmem device to add cells to.
518 * @info: nvmem cell info to add to the device
520 * Return: 0 or negative error code on failure.
522 int nvmem_add_one_cell(struct nvmem_device *nvmem,
523 const struct nvmem_cell_info *info)
525 struct nvmem_cell_entry *cell;
528 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
532 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
538 nvmem_cell_entry_add(cell);
542 EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
545 * nvmem_add_cells() - Add cell information to an nvmem device
547 * @nvmem: nvmem device to add cells to.
548 * @info: nvmem cell info to add to the device
549 * @ncells: number of cells in info
551 * Return: 0 or negative error code on failure.
553 static int nvmem_add_cells(struct nvmem_device *nvmem,
554 const struct nvmem_cell_info *info,
559 for (i = 0; i < ncells; i++) {
560 rval = nvmem_add_one_cell(nvmem, &info[i]);
569 * nvmem_register_notifier() - Register a notifier block for nvmem events.
571 * @nb: notifier block to be called on nvmem events.
573 * Return: 0 on success, negative error number on failure.
575 int nvmem_register_notifier(struct notifier_block *nb)
577 return blocking_notifier_chain_register(&nvmem_notifier, nb);
579 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
582 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
584 * @nb: notifier block to be unregistered.
586 * Return: 0 on success, negative error number on failure.
588 int nvmem_unregister_notifier(struct notifier_block *nb)
590 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
592 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
594 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
596 const struct nvmem_cell_info *info;
597 struct nvmem_cell_table *table;
598 struct nvmem_cell_entry *cell;
601 mutex_lock(&nvmem_cell_mutex);
602 list_for_each_entry(table, &nvmem_cell_tables, node) {
603 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
604 for (i = 0; i < table->ncells; i++) {
605 info = &table->cells[i];
607 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
613 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
619 nvmem_cell_entry_add(cell);
625 mutex_unlock(&nvmem_cell_mutex);
629 static struct nvmem_cell_entry *
630 nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
632 struct nvmem_cell_entry *iter, *cell = NULL;
634 mutex_lock(&nvmem_mutex);
635 list_for_each_entry(iter, &nvmem->cells, node) {
636 if (strcmp(cell_id, iter->name) == 0) {
641 mutex_unlock(&nvmem_mutex);
646 static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
648 unsigned int cur = 0;
649 const struct nvmem_keepout *keepout = nvmem->keepout;
650 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
652 while (keepout < keepoutend) {
653 /* Ensure keepouts are sorted and don't overlap. */
654 if (keepout->start < cur) {
656 "Keepout regions aren't sorted or overlap.\n");
661 if (keepout->end < keepout->start) {
663 "Invalid keepout region.\n");
669 * Validate keepouts (and holes between) don't violate
670 * word_size constraints.
672 if ((keepout->end - keepout->start < nvmem->word_size) ||
673 ((keepout->start != cur) &&
674 (keepout->start - cur < nvmem->word_size))) {
677 "Keepout regions violate word_size constraints.\n");
682 /* Validate keepouts don't violate stride (alignment). */
683 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
684 !IS_ALIGNED(keepout->end, nvmem->stride)) {
687 "Keepout regions violate stride.\n");
699 static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
701 struct nvmem_layout *layout = nvmem->layout;
702 struct device *dev = &nvmem->dev;
703 struct device_node *child;
707 for_each_child_of_node(np, child) {
708 struct nvmem_cell_info info = {0};
710 addr = of_get_property(child, "reg", &len);
713 if (len < 2 * sizeof(u32)) {
714 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
719 info.offset = be32_to_cpup(addr++);
720 info.bytes = be32_to_cpup(addr);
721 info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
723 addr = of_get_property(child, "bits", &len);
724 if (addr && len == (2 * sizeof(u32))) {
725 info.bit_offset = be32_to_cpup(addr++);
726 info.nbits = be32_to_cpup(addr);
729 info.np = of_node_get(child);
731 if (layout && layout->fixup_cell_info)
732 layout->fixup_cell_info(nvmem, layout, &info);
734 ret = nvmem_add_one_cell(nvmem, &info);
745 static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
747 return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
750 static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
752 struct device_node *layout_np;
755 layout_np = of_nvmem_layout_get_container(nvmem);
759 if (of_device_is_compatible(layout_np, "fixed-layout"))
760 err = nvmem_add_cells_from_dt(nvmem, layout_np);
762 of_node_put(layout_np);
767 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner)
769 layout->owner = owner;
771 spin_lock(&nvmem_layout_lock);
772 list_add(&layout->node, &nvmem_layouts);
773 spin_unlock(&nvmem_layout_lock);
777 EXPORT_SYMBOL_GPL(__nvmem_layout_register);
779 void nvmem_layout_unregister(struct nvmem_layout *layout)
781 spin_lock(&nvmem_layout_lock);
782 list_del(&layout->node);
783 spin_unlock(&nvmem_layout_lock);
785 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
787 static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem)
789 struct device_node *layout_np, *np = nvmem->dev.of_node;
790 struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER);
792 layout_np = of_get_child_by_name(np, "nvmem-layout");
797 * In case the nvmem device was built-in while the layout was built as a
798 * module, we shall manually request the layout driver loading otherwise
799 * we'll never have any match.
801 of_request_module(layout_np);
803 spin_lock(&nvmem_layout_lock);
805 list_for_each_entry(l, &nvmem_layouts, node) {
806 if (of_match_node(l->of_match_table, layout_np)) {
807 if (try_module_get(l->owner))
814 spin_unlock(&nvmem_layout_lock);
815 of_node_put(layout_np);
820 static void nvmem_layout_put(struct nvmem_layout *layout)
823 module_put(layout->owner);
826 static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem)
828 struct nvmem_layout *layout = nvmem->layout;
831 if (layout && layout->add_cells) {
832 ret = layout->add_cells(&nvmem->dev, nvmem, layout);
840 #if IS_ENABLED(CONFIG_OF)
842 * of_nvmem_layout_get_container() - Get OF node to layout container.
844 * @nvmem: nvmem device.
846 * Return: a node pointer with refcount incremented or NULL if no
847 * container exists. Use of_node_put() on it when done.
849 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
851 return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
853 EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
856 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
857 struct nvmem_layout *layout)
859 struct device_node __maybe_unused *layout_np;
860 const struct of_device_id *match;
862 layout_np = of_nvmem_layout_get_container(nvmem);
863 match = of_match_node(layout->of_match_table, layout_np);
865 return match ? match->data : NULL;
867 EXPORT_SYMBOL_GPL(nvmem_layout_get_match_data);
870 * nvmem_register() - Register a nvmem device for given nvmem_config.
871 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
873 * @config: nvmem device configuration with which nvmem device is created.
875 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
879 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
881 struct nvmem_device *nvmem;
885 return ERR_PTR(-EINVAL);
887 if (!config->reg_read && !config->reg_write)
888 return ERR_PTR(-EINVAL);
890 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
892 return ERR_PTR(-ENOMEM);
894 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
897 return ERR_PTR(rval);
902 nvmem->dev.type = &nvmem_provider_type;
903 nvmem->dev.bus = &nvmem_bus_type;
904 nvmem->dev.parent = config->dev;
906 device_initialize(&nvmem->dev);
908 if (!config->ignore_wp)
909 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
911 if (IS_ERR(nvmem->wp_gpio)) {
912 rval = PTR_ERR(nvmem->wp_gpio);
913 nvmem->wp_gpio = NULL;
917 kref_init(&nvmem->refcnt);
918 INIT_LIST_HEAD(&nvmem->cells);
920 nvmem->owner = config->owner;
921 if (!nvmem->owner && config->dev->driver)
922 nvmem->owner = config->dev->driver->owner;
923 nvmem->stride = config->stride ?: 1;
924 nvmem->word_size = config->word_size ?: 1;
925 nvmem->size = config->size;
926 nvmem->root_only = config->root_only;
927 nvmem->priv = config->priv;
928 nvmem->type = config->type;
929 nvmem->reg_read = config->reg_read;
930 nvmem->reg_write = config->reg_write;
931 nvmem->keepout = config->keepout;
932 nvmem->nkeepout = config->nkeepout;
934 nvmem->dev.of_node = config->of_node;
935 else if (!config->no_of_node)
936 nvmem->dev.of_node = config->dev->of_node;
938 switch (config->id) {
939 case NVMEM_DEVID_NONE:
940 rval = dev_set_name(&nvmem->dev, "%s", config->name);
942 case NVMEM_DEVID_AUTO:
943 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
946 rval = dev_set_name(&nvmem->dev, "%s%d",
947 config->name ? : "nvmem",
948 config->name ? config->id : nvmem->id);
955 nvmem->read_only = device_property_present(config->dev, "read-only") ||
956 config->read_only || !nvmem->reg_write;
958 #ifdef CONFIG_NVMEM_SYSFS
959 nvmem->dev.groups = nvmem_dev_groups;
962 if (nvmem->nkeepout) {
963 rval = nvmem_validate_keepouts(nvmem);
968 if (config->compat) {
969 rval = nvmem_sysfs_setup_compat(nvmem, config);
975 * If the driver supplied a layout by config->layout, the module
976 * pointer will be NULL and nvmem_layout_put() will be a noop.
978 nvmem->layout = config->layout ?: nvmem_layout_get(nvmem);
979 if (IS_ERR(nvmem->layout)) {
980 rval = PTR_ERR(nvmem->layout);
981 nvmem->layout = NULL;
983 if (rval == -EPROBE_DEFER)
984 goto err_teardown_compat;
988 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
990 goto err_remove_cells;
993 rval = nvmem_add_cells_from_table(nvmem);
995 goto err_remove_cells;
997 rval = nvmem_add_cells_from_legacy_of(nvmem);
999 goto err_remove_cells;
1001 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
1003 rval = device_add(&nvmem->dev);
1005 goto err_remove_cells;
1007 rval = nvmem_add_cells_from_fixed_layout(nvmem);
1009 goto err_remove_cells;
1011 rval = nvmem_add_cells_from_layout(nvmem);
1013 goto err_remove_cells;
1015 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
1020 nvmem_device_remove_all_cells(nvmem);
1021 nvmem_layout_put(nvmem->layout);
1022 err_teardown_compat:
1024 nvmem_sysfs_remove_compat(nvmem, config);
1026 put_device(&nvmem->dev);
1028 return ERR_PTR(rval);
1030 EXPORT_SYMBOL_GPL(nvmem_register);
1032 static void nvmem_device_release(struct kref *kref)
1034 struct nvmem_device *nvmem;
1036 nvmem = container_of(kref, struct nvmem_device, refcnt);
1038 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1040 if (nvmem->flags & FLAG_COMPAT)
1041 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1043 nvmem_device_remove_all_cells(nvmem);
1044 nvmem_layout_put(nvmem->layout);
1045 device_unregister(&nvmem->dev);
1049 * nvmem_unregister() - Unregister previously registered nvmem device
1051 * @nvmem: Pointer to previously registered nvmem device.
1053 void nvmem_unregister(struct nvmem_device *nvmem)
1056 kref_put(&nvmem->refcnt, nvmem_device_release);
1058 EXPORT_SYMBOL_GPL(nvmem_unregister);
1060 static void devm_nvmem_unregister(void *nvmem)
1062 nvmem_unregister(nvmem);
1066 * devm_nvmem_register() - Register a managed nvmem device for given
1068 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1070 * @dev: Device that uses the nvmem device.
1071 * @config: nvmem device configuration with which nvmem device is created.
1073 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1076 struct nvmem_device *devm_nvmem_register(struct device *dev,
1077 const struct nvmem_config *config)
1079 struct nvmem_device *nvmem;
1082 nvmem = nvmem_register(config);
1086 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1088 return ERR_PTR(ret);
1092 EXPORT_SYMBOL_GPL(devm_nvmem_register);
1094 static struct nvmem_device *__nvmem_device_get(void *data,
1095 int (*match)(struct device *dev, const void *data))
1097 struct nvmem_device *nvmem = NULL;
1100 mutex_lock(&nvmem_mutex);
1101 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1103 nvmem = to_nvmem_device(dev);
1104 mutex_unlock(&nvmem_mutex);
1106 return ERR_PTR(-EPROBE_DEFER);
1108 if (!try_module_get(nvmem->owner)) {
1109 dev_err(&nvmem->dev,
1110 "could not increase module refcount for cell %s\n",
1111 nvmem_dev_name(nvmem));
1113 put_device(&nvmem->dev);
1114 return ERR_PTR(-EINVAL);
1117 kref_get(&nvmem->refcnt);
1122 static void __nvmem_device_put(struct nvmem_device *nvmem)
1124 put_device(&nvmem->dev);
1125 module_put(nvmem->owner);
1126 kref_put(&nvmem->refcnt, nvmem_device_release);
1129 #if IS_ENABLED(CONFIG_OF)
1131 * of_nvmem_device_get() - Get nvmem device from a given id
1133 * @np: Device tree node that uses the nvmem device.
1134 * @id: nvmem name from nvmem-names property.
1136 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1139 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1142 struct device_node *nvmem_np;
1143 struct nvmem_device *nvmem;
1147 index = of_property_match_string(np, "nvmem-names", id);
1149 nvmem_np = of_parse_phandle(np, "nvmem", index);
1151 return ERR_PTR(-ENOENT);
1153 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1154 of_node_put(nvmem_np);
1157 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1161 * nvmem_device_get() - Get nvmem device from a given id
1163 * @dev: Device that uses the nvmem device.
1164 * @dev_name: name of the requested nvmem device.
1166 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1169 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1171 if (dev->of_node) { /* try dt first */
1172 struct nvmem_device *nvmem;
1174 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1176 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1181 return __nvmem_device_get((void *)dev_name, device_match_name);
1183 EXPORT_SYMBOL_GPL(nvmem_device_get);
1186 * nvmem_device_find() - Find nvmem device with matching function
1188 * @data: Data to pass to match function
1189 * @match: Callback function to check device
1191 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1194 struct nvmem_device *nvmem_device_find(void *data,
1195 int (*match)(struct device *dev, const void *data))
1197 return __nvmem_device_get(data, match);
1199 EXPORT_SYMBOL_GPL(nvmem_device_find);
1201 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1203 struct nvmem_device **nvmem = res;
1205 if (WARN_ON(!nvmem || !*nvmem))
1208 return *nvmem == data;
1211 static void devm_nvmem_device_release(struct device *dev, void *res)
1213 nvmem_device_put(*(struct nvmem_device **)res);
1217 * devm_nvmem_device_put() - put alredy got nvmem device
1219 * @dev: Device that uses the nvmem device.
1220 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1221 * that needs to be released.
1223 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1227 ret = devres_release(dev, devm_nvmem_device_release,
1228 devm_nvmem_device_match, nvmem);
1232 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1235 * nvmem_device_put() - put alredy got nvmem device
1237 * @nvmem: pointer to nvmem device that needs to be released.
1239 void nvmem_device_put(struct nvmem_device *nvmem)
1241 __nvmem_device_put(nvmem);
1243 EXPORT_SYMBOL_GPL(nvmem_device_put);
1246 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1248 * @dev: Device that requests the nvmem device.
1249 * @id: name id for the requested nvmem device.
1251 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1252 * on success. The nvmem_cell will be freed by the automatically once the
1255 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1257 struct nvmem_device **ptr, *nvmem;
1259 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1261 return ERR_PTR(-ENOMEM);
1263 nvmem = nvmem_device_get(dev, id);
1264 if (!IS_ERR(nvmem)) {
1266 devres_add(dev, ptr);
1273 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1275 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1276 const char *id, int index)
1278 struct nvmem_cell *cell;
1279 const char *name = NULL;
1281 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1283 return ERR_PTR(-ENOMEM);
1286 name = kstrdup_const(id, GFP_KERNEL);
1289 return ERR_PTR(-ENOMEM);
1294 cell->entry = entry;
1295 cell->index = index;
1300 static struct nvmem_cell *
1301 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1303 struct nvmem_cell_entry *cell_entry;
1304 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1305 struct nvmem_cell_lookup *lookup;
1306 struct nvmem_device *nvmem;
1310 return ERR_PTR(-EINVAL);
1312 dev_id = dev_name(dev);
1314 mutex_lock(&nvmem_lookup_mutex);
1316 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1317 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1318 (strcmp(lookup->con_id, con_id) == 0)) {
1319 /* This is the right entry. */
1320 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1322 if (IS_ERR(nvmem)) {
1323 /* Provider may not be registered yet. */
1324 cell = ERR_CAST(nvmem);
1328 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1331 __nvmem_device_put(nvmem);
1332 cell = ERR_PTR(-ENOENT);
1334 cell = nvmem_create_cell(cell_entry, con_id, 0);
1336 __nvmem_device_put(nvmem);
1342 mutex_unlock(&nvmem_lookup_mutex);
1346 #if IS_ENABLED(CONFIG_OF)
1347 static struct nvmem_cell_entry *
1348 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1350 struct nvmem_cell_entry *iter, *cell = NULL;
1352 mutex_lock(&nvmem_mutex);
1353 list_for_each_entry(iter, &nvmem->cells, node) {
1354 if (np == iter->np) {
1359 mutex_unlock(&nvmem_mutex);
1365 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1367 * @np: Device tree node that uses the nvmem cell.
1368 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1369 * for the cell at index 0 (the lone cell with no accompanying
1370 * nvmem-cell-names property).
1372 * Return: Will be an ERR_PTR() on error or a valid pointer
1373 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1376 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1378 struct device_node *cell_np, *nvmem_np;
1379 struct nvmem_device *nvmem;
1380 struct nvmem_cell_entry *cell_entry;
1381 struct nvmem_cell *cell;
1382 struct of_phandle_args cell_spec;
1387 /* if cell name exists, find index to the name */
1389 index = of_property_match_string(np, "nvmem-cell-names", id);
1391 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1392 "#nvmem-cell-cells",
1395 return ERR_PTR(-ENOENT);
1397 if (cell_spec.args_count > 1)
1398 return ERR_PTR(-EINVAL);
1400 cell_np = cell_spec.np;
1401 if (cell_spec.args_count)
1402 cell_index = cell_spec.args[0];
1404 nvmem_np = of_get_parent(cell_np);
1406 of_node_put(cell_np);
1407 return ERR_PTR(-EINVAL);
1410 /* nvmem layouts produce cells within the nvmem-layout container */
1411 if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1412 nvmem_np = of_get_next_parent(nvmem_np);
1414 of_node_put(cell_np);
1415 return ERR_PTR(-EINVAL);
1419 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1420 of_node_put(nvmem_np);
1421 if (IS_ERR(nvmem)) {
1422 of_node_put(cell_np);
1423 return ERR_CAST(nvmem);
1426 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1427 of_node_put(cell_np);
1429 __nvmem_device_put(nvmem);
1430 return ERR_PTR(-ENOENT);
1433 cell = nvmem_create_cell(cell_entry, id, cell_index);
1435 __nvmem_device_put(nvmem);
1439 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1443 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1445 * @dev: Device that requests the nvmem cell.
1446 * @id: nvmem cell name to get (this corresponds with the name from the
1447 * nvmem-cell-names property for DT systems and with the con_id from
1448 * the lookup entry for non-DT systems).
1450 * Return: Will be an ERR_PTR() on error or a valid pointer
1451 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1454 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1456 struct nvmem_cell *cell;
1458 if (dev->of_node) { /* try dt first */
1459 cell = of_nvmem_cell_get(dev->of_node, id);
1460 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1464 /* NULL cell id only allowed for device tree; invalid otherwise */
1466 return ERR_PTR(-EINVAL);
1468 return nvmem_cell_get_from_lookup(dev, id);
1470 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1472 static void devm_nvmem_cell_release(struct device *dev, void *res)
1474 nvmem_cell_put(*(struct nvmem_cell **)res);
1478 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1480 * @dev: Device that requests the nvmem cell.
1481 * @id: nvmem cell name id to get.
1483 * Return: Will be an ERR_PTR() on error or a valid pointer
1484 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1485 * automatically once the device is freed.
1487 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1489 struct nvmem_cell **ptr, *cell;
1491 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1493 return ERR_PTR(-ENOMEM);
1495 cell = nvmem_cell_get(dev, id);
1496 if (!IS_ERR(cell)) {
1498 devres_add(dev, ptr);
1505 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1507 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1509 struct nvmem_cell **c = res;
1511 if (WARN_ON(!c || !*c))
1518 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1519 * from devm_nvmem_cell_get.
1521 * @dev: Device that requests the nvmem cell.
1522 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1524 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1528 ret = devres_release(dev, devm_nvmem_cell_release,
1529 devm_nvmem_cell_match, cell);
1533 EXPORT_SYMBOL(devm_nvmem_cell_put);
1536 * nvmem_cell_put() - Release previously allocated nvmem cell.
1538 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1540 void nvmem_cell_put(struct nvmem_cell *cell)
1542 struct nvmem_device *nvmem = cell->entry->nvmem;
1545 kfree_const(cell->id);
1548 __nvmem_device_put(nvmem);
1550 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1552 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1555 int i, extra, bit_offset = cell->bit_offset;
1560 *b++ >>= bit_offset;
1562 /* setup rest of the bytes if any */
1563 for (i = 1; i < cell->bytes; i++) {
1564 /* Get bits from next byte and shift them towards msb */
1565 *p |= *b << (BITS_PER_BYTE - bit_offset);
1568 *b++ >>= bit_offset;
1571 /* point to the msb */
1572 p += cell->bytes - 1;
1575 /* result fits in less bytes */
1576 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1577 while (--extra >= 0)
1580 /* clear msb bits if any leftover in the last byte */
1581 if (cell->nbits % BITS_PER_BYTE)
1582 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1585 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1586 struct nvmem_cell_entry *cell,
1587 void *buf, size_t *len, const char *id, int index)
1591 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
1596 /* shift bits in-place */
1597 if (cell->bit_offset || cell->nbits)
1598 nvmem_shift_read_buffer_in_place(cell, buf);
1600 if (cell->read_post_process) {
1601 rc = cell->read_post_process(cell->priv, id, index,
1602 cell->offset, buf, cell->raw_len);
1614 * nvmem_cell_read() - Read a given nvmem cell
1616 * @cell: nvmem cell to be read.
1617 * @len: pointer to length of cell which will be populated on successful read;
1620 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1621 * buffer should be freed by the consumer with a kfree().
1623 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1625 struct nvmem_cell_entry *entry = cell->entry;
1626 struct nvmem_device *nvmem = entry->nvmem;
1631 return ERR_PTR(-EINVAL);
1633 buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
1635 return ERR_PTR(-ENOMEM);
1637 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1645 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1647 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1650 struct nvmem_device *nvmem = cell->nvmem;
1651 int i, rc, nbits, bit_offset = cell->bit_offset;
1652 u8 v, *p, *buf, *b, pbyte, pbits;
1654 nbits = cell->nbits;
1655 buf = kzalloc(cell->bytes, GFP_KERNEL);
1657 return ERR_PTR(-ENOMEM);
1659 memcpy(buf, _buf, len);
1666 /* setup the first byte with lsb bits from nvmem */
1667 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1670 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1672 /* setup rest of the byte if any */
1673 for (i = 1; i < cell->bytes; i++) {
1674 /* Get last byte bits and shift them towards lsb */
1675 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1683 /* if it's not end on byte boundary */
1684 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1685 /* setup the last byte with msb bits from nvmem */
1686 rc = nvmem_reg_read(nvmem,
1687 cell->offset + cell->bytes - 1, &v, 1);
1690 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1700 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1702 struct nvmem_device *nvmem = cell->nvmem;
1705 if (!nvmem || nvmem->read_only ||
1706 (cell->bit_offset == 0 && len != cell->bytes))
1710 * Any cells which have a read_post_process hook are read-only because
1711 * we cannot reverse the operation and it might affect other cells,
1714 if (cell->read_post_process)
1717 if (cell->bit_offset || cell->nbits) {
1718 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1720 return PTR_ERR(buf);
1723 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1725 /* free the tmp buffer */
1726 if (cell->bit_offset || cell->nbits)
1736 * nvmem_cell_write() - Write to a given nvmem cell
1738 * @cell: nvmem cell to be written.
1739 * @buf: Buffer to be written.
1740 * @len: length of buffer to be written to nvmem cell.
1742 * Return: length of bytes written or negative on failure.
1744 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1746 return __nvmem_cell_entry_write(cell->entry, buf, len);
1749 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1751 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1752 void *val, size_t count)
1754 struct nvmem_cell *cell;
1758 cell = nvmem_cell_get(dev, cell_id);
1760 return PTR_ERR(cell);
1762 buf = nvmem_cell_read(cell, &len);
1764 nvmem_cell_put(cell);
1765 return PTR_ERR(buf);
1769 nvmem_cell_put(cell);
1772 memcpy(val, buf, count);
1774 nvmem_cell_put(cell);
1780 * nvmem_cell_read_u8() - Read a cell value as a u8
1782 * @dev: Device that requests the nvmem cell.
1783 * @cell_id: Name of nvmem cell to read.
1784 * @val: pointer to output value.
1786 * Return: 0 on success or negative errno.
1788 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1790 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1792 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1795 * nvmem_cell_read_u16() - Read a cell value as a u16
1797 * @dev: Device that requests the nvmem cell.
1798 * @cell_id: Name of nvmem cell to read.
1799 * @val: pointer to output value.
1801 * Return: 0 on success or negative errno.
1803 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1805 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1807 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1810 * nvmem_cell_read_u32() - Read a cell value as a u32
1812 * @dev: Device that requests the nvmem cell.
1813 * @cell_id: Name of nvmem cell to read.
1814 * @val: pointer to output value.
1816 * Return: 0 on success or negative errno.
1818 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1820 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1822 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1825 * nvmem_cell_read_u64() - Read a cell value as a u64
1827 * @dev: Device that requests the nvmem cell.
1828 * @cell_id: Name of nvmem cell to read.
1829 * @val: pointer to output value.
1831 * Return: 0 on success or negative errno.
1833 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1835 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1837 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1839 static const void *nvmem_cell_read_variable_common(struct device *dev,
1840 const char *cell_id,
1841 size_t max_len, size_t *len)
1843 struct nvmem_cell *cell;
1847 cell = nvmem_cell_get(dev, cell_id);
1851 nbits = cell->entry->nbits;
1852 buf = nvmem_cell_read(cell, len);
1853 nvmem_cell_put(cell);
1858 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1859 * the length of the real data. Throw away the extra junk.
1862 *len = DIV_ROUND_UP(nbits, 8);
1864 if (*len > max_len) {
1866 return ERR_PTR(-ERANGE);
1873 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1875 * @dev: Device that requests the nvmem cell.
1876 * @cell_id: Name of nvmem cell to read.
1877 * @val: pointer to output value.
1879 * Return: 0 on success or negative errno.
1881 int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1888 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1890 return PTR_ERR(buf);
1892 /* Copy w/ implicit endian conversion */
1894 for (i = 0; i < len; i++)
1895 *val |= buf[i] << (8 * i);
1901 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1904 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1906 * @dev: Device that requests the nvmem cell.
1907 * @cell_id: Name of nvmem cell to read.
1908 * @val: pointer to output value.
1910 * Return: 0 on success or negative errno.
1912 int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1919 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1921 return PTR_ERR(buf);
1923 /* Copy w/ implicit endian conversion */
1925 for (i = 0; i < len; i++)
1926 *val |= (uint64_t)buf[i] << (8 * i);
1932 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1935 * nvmem_device_cell_read() - Read a given nvmem device and cell
1937 * @nvmem: nvmem device to read from.
1938 * @info: nvmem cell info to be read.
1939 * @buf: buffer pointer which will be populated on successful read.
1941 * Return: length of successful bytes read on success and negative
1942 * error code on error.
1944 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1945 struct nvmem_cell_info *info, void *buf)
1947 struct nvmem_cell_entry cell;
1954 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1958 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
1964 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1967 * nvmem_device_cell_write() - Write cell to a given nvmem device
1969 * @nvmem: nvmem device to be written to.
1970 * @info: nvmem cell info to be written.
1971 * @buf: buffer to be written to cell.
1973 * Return: length of bytes written or negative error code on failure.
1975 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1976 struct nvmem_cell_info *info, void *buf)
1978 struct nvmem_cell_entry cell;
1984 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1988 return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
1990 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1993 * nvmem_device_read() - Read from a given nvmem device
1995 * @nvmem: nvmem device to read from.
1996 * @offset: offset in nvmem device.
1997 * @bytes: number of bytes to read.
1998 * @buf: buffer pointer which will be populated on successful read.
2000 * Return: length of successful bytes read on success and negative
2001 * error code on error.
2003 int nvmem_device_read(struct nvmem_device *nvmem,
2004 unsigned int offset,
2005 size_t bytes, void *buf)
2012 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
2019 EXPORT_SYMBOL_GPL(nvmem_device_read);
2022 * nvmem_device_write() - Write cell to a given nvmem device
2024 * @nvmem: nvmem device to be written to.
2025 * @offset: offset in nvmem device.
2026 * @bytes: number of bytes to write.
2027 * @buf: buffer to be written.
2029 * Return: length of bytes written or negative error code on failure.
2031 int nvmem_device_write(struct nvmem_device *nvmem,
2032 unsigned int offset,
2033 size_t bytes, void *buf)
2040 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2048 EXPORT_SYMBOL_GPL(nvmem_device_write);
2051 * nvmem_add_cell_table() - register a table of cell info entries
2053 * @table: table of cell info entries
2055 void nvmem_add_cell_table(struct nvmem_cell_table *table)
2057 mutex_lock(&nvmem_cell_mutex);
2058 list_add_tail(&table->node, &nvmem_cell_tables);
2059 mutex_unlock(&nvmem_cell_mutex);
2061 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
2064 * nvmem_del_cell_table() - remove a previously registered cell info table
2066 * @table: table of cell info entries
2068 void nvmem_del_cell_table(struct nvmem_cell_table *table)
2070 mutex_lock(&nvmem_cell_mutex);
2071 list_del(&table->node);
2072 mutex_unlock(&nvmem_cell_mutex);
2074 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
2077 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2079 * @entries: array of cell lookup entries
2080 * @nentries: number of cell lookup entries in the array
2082 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2086 mutex_lock(&nvmem_lookup_mutex);
2087 for (i = 0; i < nentries; i++)
2088 list_add_tail(&entries[i].node, &nvmem_lookup_list);
2089 mutex_unlock(&nvmem_lookup_mutex);
2091 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2094 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2097 * @entries: array of cell lookup entries
2098 * @nentries: number of cell lookup entries in the array
2100 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2104 mutex_lock(&nvmem_lookup_mutex);
2105 for (i = 0; i < nentries; i++)
2106 list_del(&entries[i].node);
2107 mutex_unlock(&nvmem_lookup_mutex);
2109 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2112 * nvmem_dev_name() - Get the name of a given nvmem device.
2114 * @nvmem: nvmem device.
2116 * Return: name of the nvmem device.
2118 const char *nvmem_dev_name(struct nvmem_device *nvmem)
2120 return dev_name(&nvmem->dev);
2122 EXPORT_SYMBOL_GPL(nvmem_dev_name);
2124 static int __init nvmem_init(void)
2126 return bus_register(&nvmem_bus_type);
2129 static void __exit nvmem_exit(void)
2131 bus_unregister(&nvmem_bus_type);
2134 subsys_initcall(nvmem_init);
2135 module_exit(nvmem_exit);
2137 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
2138 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
2139 MODULE_DESCRIPTION("nvmem Driver Core");