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 {
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->bytes = info->bytes;
473 cell->name = info->name;
474 cell->read_post_process = info->read_post_process;
475 cell->priv = info->priv;
477 cell->bit_offset = info->bit_offset;
478 cell->nbits = info->nbits;
482 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
485 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
487 "cell %s unaligned to nvmem stride %d\n",
488 cell->name ?: "<unknown>", nvmem->stride);
495 static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
496 const struct nvmem_cell_info *info,
497 struct nvmem_cell_entry *cell)
501 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
505 cell->name = kstrdup_const(info->name, GFP_KERNEL);
513 * nvmem_add_one_cell() - Add one cell information to an nvmem device
515 * @nvmem: nvmem device to add cells to.
516 * @info: nvmem cell info to add to the device
518 * Return: 0 or negative error code on failure.
520 int nvmem_add_one_cell(struct nvmem_device *nvmem,
521 const struct nvmem_cell_info *info)
523 struct nvmem_cell_entry *cell;
526 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
530 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
536 nvmem_cell_entry_add(cell);
540 EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
543 * nvmem_add_cells() - Add cell information to an nvmem device
545 * @nvmem: nvmem device to add cells to.
546 * @info: nvmem cell info to add to the device
547 * @ncells: number of cells in info
549 * Return: 0 or negative error code on failure.
551 static int nvmem_add_cells(struct nvmem_device *nvmem,
552 const struct nvmem_cell_info *info,
557 for (i = 0; i < ncells; i++) {
558 rval = nvmem_add_one_cell(nvmem, &info[i]);
567 * nvmem_register_notifier() - Register a notifier block for nvmem events.
569 * @nb: notifier block to be called on nvmem events.
571 * Return: 0 on success, negative error number on failure.
573 int nvmem_register_notifier(struct notifier_block *nb)
575 return blocking_notifier_chain_register(&nvmem_notifier, nb);
577 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
580 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
582 * @nb: notifier block to be unregistered.
584 * Return: 0 on success, negative error number on failure.
586 int nvmem_unregister_notifier(struct notifier_block *nb)
588 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
590 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
592 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
594 const struct nvmem_cell_info *info;
595 struct nvmem_cell_table *table;
596 struct nvmem_cell_entry *cell;
599 mutex_lock(&nvmem_cell_mutex);
600 list_for_each_entry(table, &nvmem_cell_tables, node) {
601 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
602 for (i = 0; i < table->ncells; i++) {
603 info = &table->cells[i];
605 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
611 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
617 nvmem_cell_entry_add(cell);
623 mutex_unlock(&nvmem_cell_mutex);
627 static struct nvmem_cell_entry *
628 nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
630 struct nvmem_cell_entry *iter, *cell = NULL;
632 mutex_lock(&nvmem_mutex);
633 list_for_each_entry(iter, &nvmem->cells, node) {
634 if (strcmp(cell_id, iter->name) == 0) {
639 mutex_unlock(&nvmem_mutex);
644 static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
646 unsigned int cur = 0;
647 const struct nvmem_keepout *keepout = nvmem->keepout;
648 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
650 while (keepout < keepoutend) {
651 /* Ensure keepouts are sorted and don't overlap. */
652 if (keepout->start < cur) {
654 "Keepout regions aren't sorted or overlap.\n");
659 if (keepout->end < keepout->start) {
661 "Invalid keepout region.\n");
667 * Validate keepouts (and holes between) don't violate
668 * word_size constraints.
670 if ((keepout->end - keepout->start < nvmem->word_size) ||
671 ((keepout->start != cur) &&
672 (keepout->start - cur < nvmem->word_size))) {
675 "Keepout regions violate word_size constraints.\n");
680 /* Validate keepouts don't violate stride (alignment). */
681 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
682 !IS_ALIGNED(keepout->end, nvmem->stride)) {
685 "Keepout regions violate stride.\n");
697 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
699 struct nvmem_layout *layout = nvmem->layout;
700 struct device *dev = &nvmem->dev;
701 struct device_node *child;
705 for_each_child_of_node(dev->of_node, child) {
706 struct nvmem_cell_info info = {0};
708 addr = of_get_property(child, "reg", &len);
711 if (len < 2 * sizeof(u32)) {
712 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
717 info.offset = be32_to_cpup(addr++);
718 info.bytes = be32_to_cpup(addr);
719 info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
721 addr = of_get_property(child, "bits", &len);
722 if (addr && len == (2 * sizeof(u32))) {
723 info.bit_offset = be32_to_cpup(addr++);
724 info.nbits = be32_to_cpup(addr);
727 info.np = of_node_get(child);
729 if (layout && layout->fixup_cell_info)
730 layout->fixup_cell_info(nvmem, layout, &info);
732 ret = nvmem_add_one_cell(nvmem, &info);
743 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner)
745 layout->owner = owner;
747 spin_lock(&nvmem_layout_lock);
748 list_add(&layout->node, &nvmem_layouts);
749 spin_unlock(&nvmem_layout_lock);
753 EXPORT_SYMBOL_GPL(__nvmem_layout_register);
755 void nvmem_layout_unregister(struct nvmem_layout *layout)
757 spin_lock(&nvmem_layout_lock);
758 list_del(&layout->node);
759 spin_unlock(&nvmem_layout_lock);
761 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
763 static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem)
765 struct device_node *layout_np, *np = nvmem->dev.of_node;
766 struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER);
768 layout_np = of_get_child_by_name(np, "nvmem-layout");
773 * In case the nvmem device was built-in while the layout was built as a
774 * module, we shall manually request the layout driver loading otherwise
775 * we'll never have any match.
777 of_request_module(layout_np);
779 spin_lock(&nvmem_layout_lock);
781 list_for_each_entry(l, &nvmem_layouts, node) {
782 if (of_match_node(l->of_match_table, layout_np)) {
783 if (try_module_get(l->owner))
790 spin_unlock(&nvmem_layout_lock);
791 of_node_put(layout_np);
796 static void nvmem_layout_put(struct nvmem_layout *layout)
799 module_put(layout->owner);
802 static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem)
804 struct nvmem_layout *layout = nvmem->layout;
807 if (layout && layout->add_cells) {
808 ret = layout->add_cells(&nvmem->dev, nvmem, layout);
816 #if IS_ENABLED(CONFIG_OF)
818 * of_nvmem_layout_get_container() - Get OF node to layout container.
820 * @nvmem: nvmem device.
822 * Return: a node pointer with refcount incremented or NULL if no
823 * container exists. Use of_node_put() on it when done.
825 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
827 return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
829 EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
832 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
833 struct nvmem_layout *layout)
835 struct device_node __maybe_unused *layout_np;
836 const struct of_device_id *match;
838 layout_np = of_nvmem_layout_get_container(nvmem);
839 match = of_match_node(layout->of_match_table, layout_np);
841 return match ? match->data : NULL;
843 EXPORT_SYMBOL_GPL(nvmem_layout_get_match_data);
846 * nvmem_register() - Register a nvmem device for given nvmem_config.
847 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
849 * @config: nvmem device configuration with which nvmem device is created.
851 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
855 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
857 struct nvmem_device *nvmem;
861 return ERR_PTR(-EINVAL);
863 if (!config->reg_read && !config->reg_write)
864 return ERR_PTR(-EINVAL);
866 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
868 return ERR_PTR(-ENOMEM);
870 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
873 return ERR_PTR(rval);
878 nvmem->dev.type = &nvmem_provider_type;
879 nvmem->dev.bus = &nvmem_bus_type;
880 nvmem->dev.parent = config->dev;
882 device_initialize(&nvmem->dev);
884 if (!config->ignore_wp)
885 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
887 if (IS_ERR(nvmem->wp_gpio)) {
888 rval = PTR_ERR(nvmem->wp_gpio);
889 nvmem->wp_gpio = NULL;
893 kref_init(&nvmem->refcnt);
894 INIT_LIST_HEAD(&nvmem->cells);
896 nvmem->owner = config->owner;
897 if (!nvmem->owner && config->dev->driver)
898 nvmem->owner = config->dev->driver->owner;
899 nvmem->stride = config->stride ?: 1;
900 nvmem->word_size = config->word_size ?: 1;
901 nvmem->size = config->size;
902 nvmem->root_only = config->root_only;
903 nvmem->priv = config->priv;
904 nvmem->type = config->type;
905 nvmem->reg_read = config->reg_read;
906 nvmem->reg_write = config->reg_write;
907 nvmem->keepout = config->keepout;
908 nvmem->nkeepout = config->nkeepout;
910 nvmem->dev.of_node = config->of_node;
911 else if (!config->no_of_node)
912 nvmem->dev.of_node = config->dev->of_node;
914 switch (config->id) {
915 case NVMEM_DEVID_NONE:
916 rval = dev_set_name(&nvmem->dev, "%s", config->name);
918 case NVMEM_DEVID_AUTO:
919 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
922 rval = dev_set_name(&nvmem->dev, "%s%d",
923 config->name ? : "nvmem",
924 config->name ? config->id : nvmem->id);
931 nvmem->read_only = device_property_present(config->dev, "read-only") ||
932 config->read_only || !nvmem->reg_write;
934 #ifdef CONFIG_NVMEM_SYSFS
935 nvmem->dev.groups = nvmem_dev_groups;
938 if (nvmem->nkeepout) {
939 rval = nvmem_validate_keepouts(nvmem);
944 if (config->compat) {
945 rval = nvmem_sysfs_setup_compat(nvmem, config);
951 * If the driver supplied a layout by config->layout, the module
952 * pointer will be NULL and nvmem_layout_put() will be a noop.
954 nvmem->layout = config->layout ?: nvmem_layout_get(nvmem);
955 if (IS_ERR(nvmem->layout)) {
956 rval = PTR_ERR(nvmem->layout);
957 nvmem->layout = NULL;
959 if (rval == -EPROBE_DEFER)
960 goto err_teardown_compat;
964 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
966 goto err_remove_cells;
969 rval = nvmem_add_cells_from_table(nvmem);
971 goto err_remove_cells;
973 rval = nvmem_add_cells_from_of(nvmem);
975 goto err_remove_cells;
977 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
979 rval = device_add(&nvmem->dev);
981 goto err_remove_cells;
983 rval = nvmem_add_cells_from_layout(nvmem);
985 goto err_remove_cells;
987 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
992 nvmem_device_remove_all_cells(nvmem);
993 nvmem_layout_put(nvmem->layout);
996 nvmem_sysfs_remove_compat(nvmem, config);
998 put_device(&nvmem->dev);
1000 return ERR_PTR(rval);
1002 EXPORT_SYMBOL_GPL(nvmem_register);
1004 static void nvmem_device_release(struct kref *kref)
1006 struct nvmem_device *nvmem;
1008 nvmem = container_of(kref, struct nvmem_device, refcnt);
1010 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1012 if (nvmem->flags & FLAG_COMPAT)
1013 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1015 nvmem_device_remove_all_cells(nvmem);
1016 nvmem_layout_put(nvmem->layout);
1017 device_unregister(&nvmem->dev);
1021 * nvmem_unregister() - Unregister previously registered nvmem device
1023 * @nvmem: Pointer to previously registered nvmem device.
1025 void nvmem_unregister(struct nvmem_device *nvmem)
1028 kref_put(&nvmem->refcnt, nvmem_device_release);
1030 EXPORT_SYMBOL_GPL(nvmem_unregister);
1032 static void devm_nvmem_unregister(void *nvmem)
1034 nvmem_unregister(nvmem);
1038 * devm_nvmem_register() - Register a managed nvmem device for given
1040 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1042 * @dev: Device that uses the nvmem device.
1043 * @config: nvmem device configuration with which nvmem device is created.
1045 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1048 struct nvmem_device *devm_nvmem_register(struct device *dev,
1049 const struct nvmem_config *config)
1051 struct nvmem_device *nvmem;
1054 nvmem = nvmem_register(config);
1058 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1060 return ERR_PTR(ret);
1064 EXPORT_SYMBOL_GPL(devm_nvmem_register);
1066 static struct nvmem_device *__nvmem_device_get(void *data,
1067 int (*match)(struct device *dev, const void *data))
1069 struct nvmem_device *nvmem = NULL;
1072 mutex_lock(&nvmem_mutex);
1073 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1075 nvmem = to_nvmem_device(dev);
1076 mutex_unlock(&nvmem_mutex);
1078 return ERR_PTR(-EPROBE_DEFER);
1080 if (!try_module_get(nvmem->owner)) {
1081 dev_err(&nvmem->dev,
1082 "could not increase module refcount for cell %s\n",
1083 nvmem_dev_name(nvmem));
1085 put_device(&nvmem->dev);
1086 return ERR_PTR(-EINVAL);
1089 kref_get(&nvmem->refcnt);
1094 static void __nvmem_device_put(struct nvmem_device *nvmem)
1096 put_device(&nvmem->dev);
1097 module_put(nvmem->owner);
1098 kref_put(&nvmem->refcnt, nvmem_device_release);
1101 #if IS_ENABLED(CONFIG_OF)
1103 * of_nvmem_device_get() - Get nvmem device from a given id
1105 * @np: Device tree node that uses the nvmem device.
1106 * @id: nvmem name from nvmem-names property.
1108 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1111 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1114 struct device_node *nvmem_np;
1115 struct nvmem_device *nvmem;
1119 index = of_property_match_string(np, "nvmem-names", id);
1121 nvmem_np = of_parse_phandle(np, "nvmem", index);
1123 return ERR_PTR(-ENOENT);
1125 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1126 of_node_put(nvmem_np);
1129 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1133 * nvmem_device_get() - Get nvmem device from a given id
1135 * @dev: Device that uses the nvmem device.
1136 * @dev_name: name of the requested nvmem device.
1138 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1141 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1143 if (dev->of_node) { /* try dt first */
1144 struct nvmem_device *nvmem;
1146 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1148 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1153 return __nvmem_device_get((void *)dev_name, device_match_name);
1155 EXPORT_SYMBOL_GPL(nvmem_device_get);
1158 * nvmem_device_find() - Find nvmem device with matching function
1160 * @data: Data to pass to match function
1161 * @match: Callback function to check device
1163 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1166 struct nvmem_device *nvmem_device_find(void *data,
1167 int (*match)(struct device *dev, const void *data))
1169 return __nvmem_device_get(data, match);
1171 EXPORT_SYMBOL_GPL(nvmem_device_find);
1173 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1175 struct nvmem_device **nvmem = res;
1177 if (WARN_ON(!nvmem || !*nvmem))
1180 return *nvmem == data;
1183 static void devm_nvmem_device_release(struct device *dev, void *res)
1185 nvmem_device_put(*(struct nvmem_device **)res);
1189 * devm_nvmem_device_put() - put alredy got nvmem device
1191 * @dev: Device that uses the nvmem device.
1192 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1193 * that needs to be released.
1195 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1199 ret = devres_release(dev, devm_nvmem_device_release,
1200 devm_nvmem_device_match, nvmem);
1204 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1207 * nvmem_device_put() - put alredy got nvmem device
1209 * @nvmem: pointer to nvmem device that needs to be released.
1211 void nvmem_device_put(struct nvmem_device *nvmem)
1213 __nvmem_device_put(nvmem);
1215 EXPORT_SYMBOL_GPL(nvmem_device_put);
1218 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1220 * @dev: Device that requests the nvmem device.
1221 * @id: name id for the requested nvmem device.
1223 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1224 * on success. The nvmem_cell will be freed by the automatically once the
1227 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1229 struct nvmem_device **ptr, *nvmem;
1231 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1233 return ERR_PTR(-ENOMEM);
1235 nvmem = nvmem_device_get(dev, id);
1236 if (!IS_ERR(nvmem)) {
1238 devres_add(dev, ptr);
1245 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1247 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1248 const char *id, int index)
1250 struct nvmem_cell *cell;
1251 const char *name = NULL;
1253 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1255 return ERR_PTR(-ENOMEM);
1258 name = kstrdup_const(id, GFP_KERNEL);
1261 return ERR_PTR(-ENOMEM);
1266 cell->entry = entry;
1267 cell->index = index;
1272 static struct nvmem_cell *
1273 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1275 struct nvmem_cell_entry *cell_entry;
1276 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1277 struct nvmem_cell_lookup *lookup;
1278 struct nvmem_device *nvmem;
1282 return ERR_PTR(-EINVAL);
1284 dev_id = dev_name(dev);
1286 mutex_lock(&nvmem_lookup_mutex);
1288 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1289 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1290 (strcmp(lookup->con_id, con_id) == 0)) {
1291 /* This is the right entry. */
1292 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1294 if (IS_ERR(nvmem)) {
1295 /* Provider may not be registered yet. */
1296 cell = ERR_CAST(nvmem);
1300 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1303 __nvmem_device_put(nvmem);
1304 cell = ERR_PTR(-ENOENT);
1306 cell = nvmem_create_cell(cell_entry, con_id, 0);
1308 __nvmem_device_put(nvmem);
1314 mutex_unlock(&nvmem_lookup_mutex);
1318 #if IS_ENABLED(CONFIG_OF)
1319 static struct nvmem_cell_entry *
1320 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1322 struct nvmem_cell_entry *iter, *cell = NULL;
1324 mutex_lock(&nvmem_mutex);
1325 list_for_each_entry(iter, &nvmem->cells, node) {
1326 if (np == iter->np) {
1331 mutex_unlock(&nvmem_mutex);
1337 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1339 * @np: Device tree node that uses the nvmem cell.
1340 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1341 * for the cell at index 0 (the lone cell with no accompanying
1342 * nvmem-cell-names property).
1344 * Return: Will be an ERR_PTR() on error or a valid pointer
1345 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1348 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1350 struct device_node *cell_np, *nvmem_np;
1351 struct nvmem_device *nvmem;
1352 struct nvmem_cell_entry *cell_entry;
1353 struct nvmem_cell *cell;
1354 struct of_phandle_args cell_spec;
1359 /* if cell name exists, find index to the name */
1361 index = of_property_match_string(np, "nvmem-cell-names", id);
1363 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1364 "#nvmem-cell-cells",
1367 return ERR_PTR(-ENOENT);
1369 if (cell_spec.args_count > 1)
1370 return ERR_PTR(-EINVAL);
1372 cell_np = cell_spec.np;
1373 if (cell_spec.args_count)
1374 cell_index = cell_spec.args[0];
1376 nvmem_np = of_get_parent(cell_np);
1378 of_node_put(cell_np);
1379 return ERR_PTR(-EINVAL);
1382 /* nvmem layouts produce cells within the nvmem-layout container */
1383 if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1384 nvmem_np = of_get_next_parent(nvmem_np);
1386 of_node_put(cell_np);
1387 return ERR_PTR(-EINVAL);
1391 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1392 of_node_put(nvmem_np);
1393 if (IS_ERR(nvmem)) {
1394 of_node_put(cell_np);
1395 return ERR_CAST(nvmem);
1398 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1399 of_node_put(cell_np);
1401 __nvmem_device_put(nvmem);
1402 return ERR_PTR(-ENOENT);
1405 cell = nvmem_create_cell(cell_entry, id, cell_index);
1407 __nvmem_device_put(nvmem);
1411 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1415 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1417 * @dev: Device that requests the nvmem cell.
1418 * @id: nvmem cell name to get (this corresponds with the name from the
1419 * nvmem-cell-names property for DT systems and with the con_id from
1420 * the lookup entry for non-DT systems).
1422 * Return: Will be an ERR_PTR() on error or a valid pointer
1423 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1426 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1428 struct nvmem_cell *cell;
1430 if (dev->of_node) { /* try dt first */
1431 cell = of_nvmem_cell_get(dev->of_node, id);
1432 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1436 /* NULL cell id only allowed for device tree; invalid otherwise */
1438 return ERR_PTR(-EINVAL);
1440 return nvmem_cell_get_from_lookup(dev, id);
1442 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1444 static void devm_nvmem_cell_release(struct device *dev, void *res)
1446 nvmem_cell_put(*(struct nvmem_cell **)res);
1450 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1452 * @dev: Device that requests the nvmem cell.
1453 * @id: nvmem cell name id to get.
1455 * Return: Will be an ERR_PTR() on error or a valid pointer
1456 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1457 * automatically once the device is freed.
1459 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1461 struct nvmem_cell **ptr, *cell;
1463 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1465 return ERR_PTR(-ENOMEM);
1467 cell = nvmem_cell_get(dev, id);
1468 if (!IS_ERR(cell)) {
1470 devres_add(dev, ptr);
1477 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1479 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1481 struct nvmem_cell **c = res;
1483 if (WARN_ON(!c || !*c))
1490 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1491 * from devm_nvmem_cell_get.
1493 * @dev: Device that requests the nvmem cell.
1494 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1496 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1500 ret = devres_release(dev, devm_nvmem_cell_release,
1501 devm_nvmem_cell_match, cell);
1505 EXPORT_SYMBOL(devm_nvmem_cell_put);
1508 * nvmem_cell_put() - Release previously allocated nvmem cell.
1510 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1512 void nvmem_cell_put(struct nvmem_cell *cell)
1514 struct nvmem_device *nvmem = cell->entry->nvmem;
1517 kfree_const(cell->id);
1520 __nvmem_device_put(nvmem);
1522 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1524 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1527 int i, extra, bit_offset = cell->bit_offset;
1532 *b++ >>= bit_offset;
1534 /* setup rest of the bytes if any */
1535 for (i = 1; i < cell->bytes; i++) {
1536 /* Get bits from next byte and shift them towards msb */
1537 *p |= *b << (BITS_PER_BYTE - bit_offset);
1540 *b++ >>= bit_offset;
1543 /* point to the msb */
1544 p += cell->bytes - 1;
1547 /* result fits in less bytes */
1548 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1549 while (--extra >= 0)
1552 /* clear msb bits if any leftover in the last byte */
1553 if (cell->nbits % BITS_PER_BYTE)
1554 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1557 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1558 struct nvmem_cell_entry *cell,
1559 void *buf, size_t *len, const char *id, int index)
1563 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1568 /* shift bits in-place */
1569 if (cell->bit_offset || cell->nbits)
1570 nvmem_shift_read_buffer_in_place(cell, buf);
1572 if (cell->read_post_process) {
1573 rc = cell->read_post_process(cell->priv, id, index,
1574 cell->offset, buf, cell->bytes);
1586 * nvmem_cell_read() - Read a given nvmem cell
1588 * @cell: nvmem cell to be read.
1589 * @len: pointer to length of cell which will be populated on successful read;
1592 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1593 * buffer should be freed by the consumer with a kfree().
1595 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1597 struct nvmem_device *nvmem = cell->entry->nvmem;
1602 return ERR_PTR(-EINVAL);
1604 buf = kzalloc(cell->entry->bytes, GFP_KERNEL);
1606 return ERR_PTR(-ENOMEM);
1608 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1616 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1618 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1621 struct nvmem_device *nvmem = cell->nvmem;
1622 int i, rc, nbits, bit_offset = cell->bit_offset;
1623 u8 v, *p, *buf, *b, pbyte, pbits;
1625 nbits = cell->nbits;
1626 buf = kzalloc(cell->bytes, GFP_KERNEL);
1628 return ERR_PTR(-ENOMEM);
1630 memcpy(buf, _buf, len);
1637 /* setup the first byte with lsb bits from nvmem */
1638 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1641 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1643 /* setup rest of the byte if any */
1644 for (i = 1; i < cell->bytes; i++) {
1645 /* Get last byte bits and shift them towards lsb */
1646 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1654 /* if it's not end on byte boundary */
1655 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1656 /* setup the last byte with msb bits from nvmem */
1657 rc = nvmem_reg_read(nvmem,
1658 cell->offset + cell->bytes - 1, &v, 1);
1661 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1671 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1673 struct nvmem_device *nvmem = cell->nvmem;
1676 if (!nvmem || nvmem->read_only ||
1677 (cell->bit_offset == 0 && len != cell->bytes))
1681 * Any cells which have a read_post_process hook are read-only because
1682 * we cannot reverse the operation and it might affect other cells,
1685 if (cell->read_post_process)
1688 if (cell->bit_offset || cell->nbits) {
1689 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1691 return PTR_ERR(buf);
1694 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1696 /* free the tmp buffer */
1697 if (cell->bit_offset || cell->nbits)
1707 * nvmem_cell_write() - Write to a given nvmem cell
1709 * @cell: nvmem cell to be written.
1710 * @buf: Buffer to be written.
1711 * @len: length of buffer to be written to nvmem cell.
1713 * Return: length of bytes written or negative on failure.
1715 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1717 return __nvmem_cell_entry_write(cell->entry, buf, len);
1720 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1722 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1723 void *val, size_t count)
1725 struct nvmem_cell *cell;
1729 cell = nvmem_cell_get(dev, cell_id);
1731 return PTR_ERR(cell);
1733 buf = nvmem_cell_read(cell, &len);
1735 nvmem_cell_put(cell);
1736 return PTR_ERR(buf);
1740 nvmem_cell_put(cell);
1743 memcpy(val, buf, count);
1745 nvmem_cell_put(cell);
1751 * nvmem_cell_read_u8() - Read a cell value as a u8
1753 * @dev: Device that requests the nvmem cell.
1754 * @cell_id: Name of nvmem cell to read.
1755 * @val: pointer to output value.
1757 * Return: 0 on success or negative errno.
1759 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1761 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1763 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1766 * nvmem_cell_read_u16() - Read a cell value as a u16
1768 * @dev: Device that requests the nvmem cell.
1769 * @cell_id: Name of nvmem cell to read.
1770 * @val: pointer to output value.
1772 * Return: 0 on success or negative errno.
1774 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1776 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1778 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1781 * nvmem_cell_read_u32() - Read a cell value as a u32
1783 * @dev: Device that requests the nvmem cell.
1784 * @cell_id: Name of nvmem cell to read.
1785 * @val: pointer to output value.
1787 * Return: 0 on success or negative errno.
1789 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1791 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1793 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1796 * nvmem_cell_read_u64() - Read a cell value as a u64
1798 * @dev: Device that requests the nvmem cell.
1799 * @cell_id: Name of nvmem cell to read.
1800 * @val: pointer to output value.
1802 * Return: 0 on success or negative errno.
1804 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1806 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1808 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1810 static const void *nvmem_cell_read_variable_common(struct device *dev,
1811 const char *cell_id,
1812 size_t max_len, size_t *len)
1814 struct nvmem_cell *cell;
1818 cell = nvmem_cell_get(dev, cell_id);
1822 nbits = cell->entry->nbits;
1823 buf = nvmem_cell_read(cell, len);
1824 nvmem_cell_put(cell);
1829 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1830 * the length of the real data. Throw away the extra junk.
1833 *len = DIV_ROUND_UP(nbits, 8);
1835 if (*len > max_len) {
1837 return ERR_PTR(-ERANGE);
1844 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1846 * @dev: Device that requests the nvmem cell.
1847 * @cell_id: Name of nvmem cell to read.
1848 * @val: pointer to output value.
1850 * Return: 0 on success or negative errno.
1852 int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1859 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1861 return PTR_ERR(buf);
1863 /* Copy w/ implicit endian conversion */
1865 for (i = 0; i < len; i++)
1866 *val |= buf[i] << (8 * i);
1872 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1875 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1877 * @dev: Device that requests the nvmem cell.
1878 * @cell_id: Name of nvmem cell to read.
1879 * @val: pointer to output value.
1881 * Return: 0 on success or negative errno.
1883 int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1890 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1892 return PTR_ERR(buf);
1894 /* Copy w/ implicit endian conversion */
1896 for (i = 0; i < len; i++)
1897 *val |= (uint64_t)buf[i] << (8 * i);
1903 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1906 * nvmem_device_cell_read() - Read a given nvmem device and cell
1908 * @nvmem: nvmem device to read from.
1909 * @info: nvmem cell info to be read.
1910 * @buf: buffer pointer which will be populated on successful read.
1912 * Return: length of successful bytes read on success and negative
1913 * error code on error.
1915 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1916 struct nvmem_cell_info *info, void *buf)
1918 struct nvmem_cell_entry cell;
1925 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1929 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
1935 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1938 * nvmem_device_cell_write() - Write cell to a given nvmem device
1940 * @nvmem: nvmem device to be written to.
1941 * @info: nvmem cell info to be written.
1942 * @buf: buffer to be written to cell.
1944 * Return: length of bytes written or negative error code on failure.
1946 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1947 struct nvmem_cell_info *info, void *buf)
1949 struct nvmem_cell_entry cell;
1955 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1959 return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
1961 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1964 * nvmem_device_read() - Read from a given nvmem device
1966 * @nvmem: nvmem device to read from.
1967 * @offset: offset in nvmem device.
1968 * @bytes: number of bytes to read.
1969 * @buf: buffer pointer which will be populated on successful read.
1971 * Return: length of successful bytes read on success and negative
1972 * error code on error.
1974 int nvmem_device_read(struct nvmem_device *nvmem,
1975 unsigned int offset,
1976 size_t bytes, void *buf)
1983 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1990 EXPORT_SYMBOL_GPL(nvmem_device_read);
1993 * nvmem_device_write() - Write cell to a given nvmem device
1995 * @nvmem: nvmem device to be written to.
1996 * @offset: offset in nvmem device.
1997 * @bytes: number of bytes to write.
1998 * @buf: buffer to be written.
2000 * Return: length of bytes written or negative error code on failure.
2002 int nvmem_device_write(struct nvmem_device *nvmem,
2003 unsigned int offset,
2004 size_t bytes, void *buf)
2011 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2019 EXPORT_SYMBOL_GPL(nvmem_device_write);
2022 * nvmem_add_cell_table() - register a table of cell info entries
2024 * @table: table of cell info entries
2026 void nvmem_add_cell_table(struct nvmem_cell_table *table)
2028 mutex_lock(&nvmem_cell_mutex);
2029 list_add_tail(&table->node, &nvmem_cell_tables);
2030 mutex_unlock(&nvmem_cell_mutex);
2032 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
2035 * nvmem_del_cell_table() - remove a previously registered cell info table
2037 * @table: table of cell info entries
2039 void nvmem_del_cell_table(struct nvmem_cell_table *table)
2041 mutex_lock(&nvmem_cell_mutex);
2042 list_del(&table->node);
2043 mutex_unlock(&nvmem_cell_mutex);
2045 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
2048 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2050 * @entries: array of cell lookup entries
2051 * @nentries: number of cell lookup entries in the array
2053 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2057 mutex_lock(&nvmem_lookup_mutex);
2058 for (i = 0; i < nentries; i++)
2059 list_add_tail(&entries[i].node, &nvmem_lookup_list);
2060 mutex_unlock(&nvmem_lookup_mutex);
2062 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2065 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2068 * @entries: array of cell lookup entries
2069 * @nentries: number of cell lookup entries in the array
2071 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2075 mutex_lock(&nvmem_lookup_mutex);
2076 for (i = 0; i < nentries; i++)
2077 list_del(&entries[i].node);
2078 mutex_unlock(&nvmem_lookup_mutex);
2080 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2083 * nvmem_dev_name() - Get the name of a given nvmem device.
2085 * @nvmem: nvmem device.
2087 * Return: name of the nvmem device.
2089 const char *nvmem_dev_name(struct nvmem_device *nvmem)
2091 return dev_name(&nvmem->dev);
2093 EXPORT_SYMBOL_GPL(nvmem_dev_name);
2095 static int __init nvmem_init(void)
2097 return bus_register(&nvmem_bus_type);
2100 static void __exit nvmem_exit(void)
2102 bus_unregister(&nvmem_bus_type);
2105 subsys_initcall(nvmem_init);
2106 module_exit(nvmem_exit);
2108 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
2109 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
2110 MODULE_DESCRIPTION("nvmem Driver Core");
2111 MODULE_LICENSE("GPL v2");