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 nvmem_cell_post_process_t cell_post_process;
42 struct gpio_desc *wp_gpio;
46 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
48 #define FLAG_COMPAT BIT(0)
49 struct nvmem_cell_entry {
55 struct device_node *np;
56 struct nvmem_device *nvmem;
57 struct list_head node;
61 struct nvmem_cell_entry *entry;
66 static DEFINE_MUTEX(nvmem_mutex);
67 static DEFINE_IDA(nvmem_ida);
69 static DEFINE_MUTEX(nvmem_cell_mutex);
70 static LIST_HEAD(nvmem_cell_tables);
72 static DEFINE_MUTEX(nvmem_lookup_mutex);
73 static LIST_HEAD(nvmem_lookup_list);
75 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
77 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
78 void *val, size_t bytes)
81 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
86 static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
87 void *val, size_t bytes)
91 if (nvmem->reg_write) {
92 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
93 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
94 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
101 static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
102 unsigned int offset, void *val,
103 size_t bytes, int write)
106 unsigned int end = offset + bytes;
107 unsigned int kend, ksize;
108 const struct nvmem_keepout *keepout = nvmem->keepout;
109 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
113 * Skip all keepouts before the range being accessed.
114 * Keepouts are sorted.
116 while ((keepout < keepoutend) && (keepout->end <= offset))
119 while ((offset < end) && (keepout < keepoutend)) {
120 /* Access the valid portion before the keepout. */
121 if (offset < keepout->start) {
122 kend = min(end, keepout->start);
123 ksize = kend - offset;
125 rc = __nvmem_reg_write(nvmem, offset, val, ksize);
127 rc = __nvmem_reg_read(nvmem, offset, val, ksize);
137 * Now we're aligned to the start of this keepout zone. Go
140 kend = min(end, keepout->end);
141 ksize = kend - offset;
143 memset(val, keepout->value, ksize);
151 * If we ran out of keepouts but there's still stuff to do, send it
155 ksize = end - offset;
157 return __nvmem_reg_write(nvmem, offset, val, ksize);
159 return __nvmem_reg_read(nvmem, offset, val, ksize);
165 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
166 void *val, size_t bytes)
168 if (!nvmem->nkeepout)
169 return __nvmem_reg_read(nvmem, offset, val, bytes);
171 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
174 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
175 void *val, size_t bytes)
177 if (!nvmem->nkeepout)
178 return __nvmem_reg_write(nvmem, offset, val, bytes);
180 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
183 #ifdef CONFIG_NVMEM_SYSFS
184 static const char * const nvmem_type_str[] = {
185 [NVMEM_TYPE_UNKNOWN] = "Unknown",
186 [NVMEM_TYPE_EEPROM] = "EEPROM",
187 [NVMEM_TYPE_OTP] = "OTP",
188 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
189 [NVMEM_TYPE_FRAM] = "FRAM",
192 #ifdef CONFIG_DEBUG_LOCK_ALLOC
193 static struct lock_class_key eeprom_lock_key;
196 static ssize_t type_show(struct device *dev,
197 struct device_attribute *attr, char *buf)
199 struct nvmem_device *nvmem = to_nvmem_device(dev);
201 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
204 static DEVICE_ATTR_RO(type);
206 static struct attribute *nvmem_attrs[] = {
211 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
212 struct bin_attribute *attr, char *buf,
213 loff_t pos, size_t count)
216 struct nvmem_device *nvmem;
222 dev = kobj_to_dev(kobj);
223 nvmem = to_nvmem_device(dev);
225 /* Stop the user from reading */
226 if (pos >= nvmem->size)
229 if (!IS_ALIGNED(pos, nvmem->stride))
232 if (count < nvmem->word_size)
235 if (pos + count > nvmem->size)
236 count = nvmem->size - pos;
238 count = round_down(count, nvmem->word_size);
240 if (!nvmem->reg_read)
243 rc = nvmem_reg_read(nvmem, pos, buf, count);
251 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
252 struct bin_attribute *attr, char *buf,
253 loff_t pos, size_t count)
256 struct nvmem_device *nvmem;
262 dev = kobj_to_dev(kobj);
263 nvmem = to_nvmem_device(dev);
265 /* Stop the user from writing */
266 if (pos >= nvmem->size)
269 if (!IS_ALIGNED(pos, nvmem->stride))
272 if (count < nvmem->word_size)
275 if (pos + count > nvmem->size)
276 count = nvmem->size - pos;
278 count = round_down(count, nvmem->word_size);
280 if (!nvmem->reg_write)
283 rc = nvmem_reg_write(nvmem, pos, buf, count);
291 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
295 if (!nvmem->root_only)
298 if (!nvmem->read_only)
301 if (!nvmem->reg_write)
304 if (!nvmem->reg_read)
310 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
311 struct bin_attribute *attr, int i)
313 struct device *dev = kobj_to_dev(kobj);
314 struct nvmem_device *nvmem = to_nvmem_device(dev);
316 attr->size = nvmem->size;
318 return nvmem_bin_attr_get_umode(nvmem);
321 /* default read/write permissions */
322 static struct bin_attribute bin_attr_rw_nvmem = {
327 .read = bin_attr_nvmem_read,
328 .write = bin_attr_nvmem_write,
331 static struct bin_attribute *nvmem_bin_attributes[] = {
336 static const struct attribute_group nvmem_bin_group = {
337 .bin_attrs = nvmem_bin_attributes,
338 .attrs = nvmem_attrs,
339 .is_bin_visible = nvmem_bin_attr_is_visible,
342 static const struct attribute_group *nvmem_dev_groups[] = {
347 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
351 .read = bin_attr_nvmem_read,
352 .write = bin_attr_nvmem_write,
356 * nvmem_setup_compat() - Create an additional binary entry in
357 * drivers sys directory, to be backwards compatible with the older
358 * drivers/misc/eeprom drivers.
360 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
361 const struct nvmem_config *config)
368 if (!config->base_dev)
371 if (config->type == NVMEM_TYPE_FRAM)
372 bin_attr_nvmem_eeprom_compat.attr.name = "fram";
374 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
375 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
376 nvmem->eeprom.size = nvmem->size;
377 #ifdef CONFIG_DEBUG_LOCK_ALLOC
378 nvmem->eeprom.attr.key = &eeprom_lock_key;
380 nvmem->eeprom.private = &nvmem->dev;
381 nvmem->base_dev = config->base_dev;
383 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
386 "Failed to create eeprom binary file %d\n", rval);
390 nvmem->flags |= FLAG_COMPAT;
395 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
396 const struct nvmem_config *config)
399 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
402 #else /* CONFIG_NVMEM_SYSFS */
404 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
405 const struct nvmem_config *config)
409 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
410 const struct nvmem_config *config)
414 #endif /* CONFIG_NVMEM_SYSFS */
416 static void nvmem_release(struct device *dev)
418 struct nvmem_device *nvmem = to_nvmem_device(dev);
420 ida_free(&nvmem_ida, nvmem->id);
421 gpiod_put(nvmem->wp_gpio);
425 static const struct device_type nvmem_provider_type = {
426 .release = nvmem_release,
429 static struct bus_type nvmem_bus_type = {
433 static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
435 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
436 mutex_lock(&nvmem_mutex);
437 list_del(&cell->node);
438 mutex_unlock(&nvmem_mutex);
439 of_node_put(cell->np);
440 kfree_const(cell->name);
444 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
446 struct nvmem_cell_entry *cell, *p;
448 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
449 nvmem_cell_entry_drop(cell);
452 static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
454 mutex_lock(&nvmem_mutex);
455 list_add_tail(&cell->node, &cell->nvmem->cells);
456 mutex_unlock(&nvmem_mutex);
457 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
460 static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
461 const struct nvmem_cell_info *info,
462 struct nvmem_cell_entry *cell)
465 cell->offset = info->offset;
466 cell->bytes = info->bytes;
467 cell->name = info->name;
469 cell->bit_offset = info->bit_offset;
470 cell->nbits = info->nbits;
474 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
477 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
479 "cell %s unaligned to nvmem stride %d\n",
480 cell->name ?: "<unknown>", nvmem->stride);
487 static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
488 const struct nvmem_cell_info *info,
489 struct nvmem_cell_entry *cell)
493 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
497 cell->name = kstrdup_const(info->name, GFP_KERNEL);
505 * nvmem_add_one_cell() - Add one cell information to an nvmem device
507 * @nvmem: nvmem device to add cells to.
508 * @info: nvmem cell info to add to the device
510 * Return: 0 or negative error code on failure.
512 int nvmem_add_one_cell(struct nvmem_device *nvmem,
513 const struct nvmem_cell_info *info)
515 struct nvmem_cell_entry *cell;
518 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
522 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
528 nvmem_cell_entry_add(cell);
532 EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
535 * nvmem_add_cells() - Add cell information to an nvmem device
537 * @nvmem: nvmem device to add cells to.
538 * @info: nvmem cell info to add to the device
539 * @ncells: number of cells in info
541 * Return: 0 or negative error code on failure.
543 static int nvmem_add_cells(struct nvmem_device *nvmem,
544 const struct nvmem_cell_info *info,
549 for (i = 0; i < ncells; i++) {
550 rval = nvmem_add_one_cell(nvmem, &info[i]);
559 * nvmem_register_notifier() - Register a notifier block for nvmem events.
561 * @nb: notifier block to be called on nvmem events.
563 * Return: 0 on success, negative error number on failure.
565 int nvmem_register_notifier(struct notifier_block *nb)
567 return blocking_notifier_chain_register(&nvmem_notifier, nb);
569 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
572 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
574 * @nb: notifier block to be unregistered.
576 * Return: 0 on success, negative error number on failure.
578 int nvmem_unregister_notifier(struct notifier_block *nb)
580 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
582 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
584 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
586 const struct nvmem_cell_info *info;
587 struct nvmem_cell_table *table;
588 struct nvmem_cell_entry *cell;
591 mutex_lock(&nvmem_cell_mutex);
592 list_for_each_entry(table, &nvmem_cell_tables, node) {
593 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
594 for (i = 0; i < table->ncells; i++) {
595 info = &table->cells[i];
597 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
603 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
609 nvmem_cell_entry_add(cell);
615 mutex_unlock(&nvmem_cell_mutex);
619 static struct nvmem_cell_entry *
620 nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
622 struct nvmem_cell_entry *iter, *cell = NULL;
624 mutex_lock(&nvmem_mutex);
625 list_for_each_entry(iter, &nvmem->cells, node) {
626 if (strcmp(cell_id, iter->name) == 0) {
631 mutex_unlock(&nvmem_mutex);
636 static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
638 unsigned int cur = 0;
639 const struct nvmem_keepout *keepout = nvmem->keepout;
640 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
642 while (keepout < keepoutend) {
643 /* Ensure keepouts are sorted and don't overlap. */
644 if (keepout->start < cur) {
646 "Keepout regions aren't sorted or overlap.\n");
651 if (keepout->end < keepout->start) {
653 "Invalid keepout region.\n");
659 * Validate keepouts (and holes between) don't violate
660 * word_size constraints.
662 if ((keepout->end - keepout->start < nvmem->word_size) ||
663 ((keepout->start != cur) &&
664 (keepout->start - cur < nvmem->word_size))) {
667 "Keepout regions violate word_size constraints.\n");
672 /* Validate keepouts don't violate stride (alignment). */
673 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
674 !IS_ALIGNED(keepout->end, nvmem->stride)) {
677 "Keepout regions violate stride.\n");
689 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
691 struct device *dev = &nvmem->dev;
692 struct device_node *child;
696 for_each_child_of_node(dev->of_node, child) {
697 struct nvmem_cell_info info = {0};
699 addr = of_get_property(child, "reg", &len);
702 if (len < 2 * sizeof(u32)) {
703 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
708 info.offset = be32_to_cpup(addr++);
709 info.bytes = be32_to_cpup(addr);
710 info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
712 addr = of_get_property(child, "bits", &len);
713 if (addr && len == (2 * sizeof(u32))) {
714 info.bit_offset = be32_to_cpup(addr++);
715 info.nbits = be32_to_cpup(addr);
718 info.np = of_node_get(child);
720 ret = nvmem_add_one_cell(nvmem, &info);
732 * nvmem_register() - Register a nvmem device for given nvmem_config.
733 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
735 * @config: nvmem device configuration with which nvmem device is created.
737 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
741 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
743 struct nvmem_device *nvmem;
747 return ERR_PTR(-EINVAL);
749 if (!config->reg_read && !config->reg_write)
750 return ERR_PTR(-EINVAL);
752 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
754 return ERR_PTR(-ENOMEM);
756 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
759 return ERR_PTR(rval);
764 nvmem->dev.type = &nvmem_provider_type;
765 nvmem->dev.bus = &nvmem_bus_type;
766 nvmem->dev.parent = config->dev;
768 device_initialize(&nvmem->dev);
770 if (!config->ignore_wp)
771 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
773 if (IS_ERR(nvmem->wp_gpio)) {
774 rval = PTR_ERR(nvmem->wp_gpio);
775 nvmem->wp_gpio = NULL;
779 kref_init(&nvmem->refcnt);
780 INIT_LIST_HEAD(&nvmem->cells);
782 nvmem->owner = config->owner;
783 if (!nvmem->owner && config->dev->driver)
784 nvmem->owner = config->dev->driver->owner;
785 nvmem->stride = config->stride ?: 1;
786 nvmem->word_size = config->word_size ?: 1;
787 nvmem->size = config->size;
788 nvmem->root_only = config->root_only;
789 nvmem->priv = config->priv;
790 nvmem->type = config->type;
791 nvmem->reg_read = config->reg_read;
792 nvmem->reg_write = config->reg_write;
793 nvmem->cell_post_process = config->cell_post_process;
794 nvmem->keepout = config->keepout;
795 nvmem->nkeepout = config->nkeepout;
797 nvmem->dev.of_node = config->of_node;
798 else if (!config->no_of_node)
799 nvmem->dev.of_node = config->dev->of_node;
801 switch (config->id) {
802 case NVMEM_DEVID_NONE:
803 rval = dev_set_name(&nvmem->dev, "%s", config->name);
805 case NVMEM_DEVID_AUTO:
806 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
809 rval = dev_set_name(&nvmem->dev, "%s%d",
810 config->name ? : "nvmem",
811 config->name ? config->id : nvmem->id);
818 nvmem->read_only = device_property_present(config->dev, "read-only") ||
819 config->read_only || !nvmem->reg_write;
821 #ifdef CONFIG_NVMEM_SYSFS
822 nvmem->dev.groups = nvmem_dev_groups;
825 if (nvmem->nkeepout) {
826 rval = nvmem_validate_keepouts(nvmem);
831 if (config->compat) {
832 rval = nvmem_sysfs_setup_compat(nvmem, config);
838 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
840 goto err_remove_cells;
843 rval = nvmem_add_cells_from_table(nvmem);
845 goto err_remove_cells;
847 rval = nvmem_add_cells_from_of(nvmem);
849 goto err_remove_cells;
851 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
853 rval = device_add(&nvmem->dev);
855 goto err_remove_cells;
857 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
862 nvmem_device_remove_all_cells(nvmem);
864 nvmem_sysfs_remove_compat(nvmem, config);
866 put_device(&nvmem->dev);
868 return ERR_PTR(rval);
870 EXPORT_SYMBOL_GPL(nvmem_register);
872 static void nvmem_device_release(struct kref *kref)
874 struct nvmem_device *nvmem;
876 nvmem = container_of(kref, struct nvmem_device, refcnt);
878 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
880 if (nvmem->flags & FLAG_COMPAT)
881 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
883 nvmem_device_remove_all_cells(nvmem);
884 device_unregister(&nvmem->dev);
888 * nvmem_unregister() - Unregister previously registered nvmem device
890 * @nvmem: Pointer to previously registered nvmem device.
892 void nvmem_unregister(struct nvmem_device *nvmem)
895 kref_put(&nvmem->refcnt, nvmem_device_release);
897 EXPORT_SYMBOL_GPL(nvmem_unregister);
899 static void devm_nvmem_unregister(void *nvmem)
901 nvmem_unregister(nvmem);
905 * devm_nvmem_register() - Register a managed nvmem device for given
907 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
909 * @dev: Device that uses the nvmem device.
910 * @config: nvmem device configuration with which nvmem device is created.
912 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
915 struct nvmem_device *devm_nvmem_register(struct device *dev,
916 const struct nvmem_config *config)
918 struct nvmem_device *nvmem;
921 nvmem = nvmem_register(config);
925 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
931 EXPORT_SYMBOL_GPL(devm_nvmem_register);
933 static struct nvmem_device *__nvmem_device_get(void *data,
934 int (*match)(struct device *dev, const void *data))
936 struct nvmem_device *nvmem = NULL;
939 mutex_lock(&nvmem_mutex);
940 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
942 nvmem = to_nvmem_device(dev);
943 mutex_unlock(&nvmem_mutex);
945 return ERR_PTR(-EPROBE_DEFER);
947 if (!try_module_get(nvmem->owner)) {
949 "could not increase module refcount for cell %s\n",
950 nvmem_dev_name(nvmem));
952 put_device(&nvmem->dev);
953 return ERR_PTR(-EINVAL);
956 kref_get(&nvmem->refcnt);
961 static void __nvmem_device_put(struct nvmem_device *nvmem)
963 put_device(&nvmem->dev);
964 module_put(nvmem->owner);
965 kref_put(&nvmem->refcnt, nvmem_device_release);
968 #if IS_ENABLED(CONFIG_OF)
970 * of_nvmem_device_get() - Get nvmem device from a given id
972 * @np: Device tree node that uses the nvmem device.
973 * @id: nvmem name from nvmem-names property.
975 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
978 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
981 struct device_node *nvmem_np;
982 struct nvmem_device *nvmem;
986 index = of_property_match_string(np, "nvmem-names", id);
988 nvmem_np = of_parse_phandle(np, "nvmem", index);
990 return ERR_PTR(-ENOENT);
992 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
993 of_node_put(nvmem_np);
996 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1000 * nvmem_device_get() - Get nvmem device from a given id
1002 * @dev: Device that uses the nvmem device.
1003 * @dev_name: name of the requested nvmem device.
1005 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1008 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1010 if (dev->of_node) { /* try dt first */
1011 struct nvmem_device *nvmem;
1013 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1015 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1020 return __nvmem_device_get((void *)dev_name, device_match_name);
1022 EXPORT_SYMBOL_GPL(nvmem_device_get);
1025 * nvmem_device_find() - Find nvmem device with matching function
1027 * @data: Data to pass to match function
1028 * @match: Callback function to check device
1030 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1033 struct nvmem_device *nvmem_device_find(void *data,
1034 int (*match)(struct device *dev, const void *data))
1036 return __nvmem_device_get(data, match);
1038 EXPORT_SYMBOL_GPL(nvmem_device_find);
1040 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1042 struct nvmem_device **nvmem = res;
1044 if (WARN_ON(!nvmem || !*nvmem))
1047 return *nvmem == data;
1050 static void devm_nvmem_device_release(struct device *dev, void *res)
1052 nvmem_device_put(*(struct nvmem_device **)res);
1056 * devm_nvmem_device_put() - put alredy got nvmem device
1058 * @dev: Device that uses the nvmem device.
1059 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1060 * that needs to be released.
1062 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1066 ret = devres_release(dev, devm_nvmem_device_release,
1067 devm_nvmem_device_match, nvmem);
1071 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1074 * nvmem_device_put() - put alredy got nvmem device
1076 * @nvmem: pointer to nvmem device that needs to be released.
1078 void nvmem_device_put(struct nvmem_device *nvmem)
1080 __nvmem_device_put(nvmem);
1082 EXPORT_SYMBOL_GPL(nvmem_device_put);
1085 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1087 * @dev: Device that requests the nvmem device.
1088 * @id: name id for the requested nvmem device.
1090 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1091 * on success. The nvmem_cell will be freed by the automatically once the
1094 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1096 struct nvmem_device **ptr, *nvmem;
1098 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1100 return ERR_PTR(-ENOMEM);
1102 nvmem = nvmem_device_get(dev, id);
1103 if (!IS_ERR(nvmem)) {
1105 devres_add(dev, ptr);
1112 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1114 static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1115 const char *id, int index)
1117 struct nvmem_cell *cell;
1118 const char *name = NULL;
1120 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1122 return ERR_PTR(-ENOMEM);
1125 name = kstrdup_const(id, GFP_KERNEL);
1128 return ERR_PTR(-ENOMEM);
1133 cell->entry = entry;
1134 cell->index = index;
1139 static struct nvmem_cell *
1140 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1142 struct nvmem_cell_entry *cell_entry;
1143 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1144 struct nvmem_cell_lookup *lookup;
1145 struct nvmem_device *nvmem;
1149 return ERR_PTR(-EINVAL);
1151 dev_id = dev_name(dev);
1153 mutex_lock(&nvmem_lookup_mutex);
1155 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1156 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1157 (strcmp(lookup->con_id, con_id) == 0)) {
1158 /* This is the right entry. */
1159 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1161 if (IS_ERR(nvmem)) {
1162 /* Provider may not be registered yet. */
1163 cell = ERR_CAST(nvmem);
1167 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1170 __nvmem_device_put(nvmem);
1171 cell = ERR_PTR(-ENOENT);
1173 cell = nvmem_create_cell(cell_entry, con_id, 0);
1175 __nvmem_device_put(nvmem);
1181 mutex_unlock(&nvmem_lookup_mutex);
1185 #if IS_ENABLED(CONFIG_OF)
1186 static struct nvmem_cell_entry *
1187 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1189 struct nvmem_cell_entry *iter, *cell = NULL;
1191 mutex_lock(&nvmem_mutex);
1192 list_for_each_entry(iter, &nvmem->cells, node) {
1193 if (np == iter->np) {
1198 mutex_unlock(&nvmem_mutex);
1204 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1206 * @np: Device tree node that uses the nvmem cell.
1207 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1208 * for the cell at index 0 (the lone cell with no accompanying
1209 * nvmem-cell-names property).
1211 * Return: Will be an ERR_PTR() on error or a valid pointer
1212 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1215 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1217 struct device_node *cell_np, *nvmem_np;
1218 struct nvmem_device *nvmem;
1219 struct nvmem_cell_entry *cell_entry;
1220 struct nvmem_cell *cell;
1221 struct of_phandle_args cell_spec;
1226 /* if cell name exists, find index to the name */
1228 index = of_property_match_string(np, "nvmem-cell-names", id);
1230 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1231 "#nvmem-cell-cells",
1234 return ERR_PTR(ret);
1236 if (cell_spec.args_count > 1)
1237 return ERR_PTR(-EINVAL);
1239 cell_np = cell_spec.np;
1240 if (cell_spec.args_count)
1241 cell_index = cell_spec.args[0];
1243 nvmem_np = of_get_parent(cell_np);
1245 of_node_put(cell_np);
1246 return ERR_PTR(-EINVAL);
1249 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1250 of_node_put(nvmem_np);
1251 if (IS_ERR(nvmem)) {
1252 of_node_put(cell_np);
1253 return ERR_CAST(nvmem);
1256 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1257 of_node_put(cell_np);
1259 __nvmem_device_put(nvmem);
1260 return ERR_PTR(-ENOENT);
1263 cell = nvmem_create_cell(cell_entry, id, cell_index);
1265 __nvmem_device_put(nvmem);
1269 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1273 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1275 * @dev: Device that requests the nvmem cell.
1276 * @id: nvmem cell name to get (this corresponds with the name from the
1277 * nvmem-cell-names property for DT systems and with the con_id from
1278 * the lookup entry for non-DT systems).
1280 * Return: Will be an ERR_PTR() on error or a valid pointer
1281 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1284 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1286 struct nvmem_cell *cell;
1288 if (dev->of_node) { /* try dt first */
1289 cell = of_nvmem_cell_get(dev->of_node, id);
1290 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1294 /* NULL cell id only allowed for device tree; invalid otherwise */
1296 return ERR_PTR(-EINVAL);
1298 return nvmem_cell_get_from_lookup(dev, id);
1300 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1302 static void devm_nvmem_cell_release(struct device *dev, void *res)
1304 nvmem_cell_put(*(struct nvmem_cell **)res);
1308 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1310 * @dev: Device that requests the nvmem cell.
1311 * @id: nvmem cell name id to get.
1313 * Return: Will be an ERR_PTR() on error or a valid pointer
1314 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1315 * automatically once the device is freed.
1317 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1319 struct nvmem_cell **ptr, *cell;
1321 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1323 return ERR_PTR(-ENOMEM);
1325 cell = nvmem_cell_get(dev, id);
1326 if (!IS_ERR(cell)) {
1328 devres_add(dev, ptr);
1335 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1337 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1339 struct nvmem_cell **c = res;
1341 if (WARN_ON(!c || !*c))
1348 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1349 * from devm_nvmem_cell_get.
1351 * @dev: Device that requests the nvmem cell.
1352 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1354 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1358 ret = devres_release(dev, devm_nvmem_cell_release,
1359 devm_nvmem_cell_match, cell);
1363 EXPORT_SYMBOL(devm_nvmem_cell_put);
1366 * nvmem_cell_put() - Release previously allocated nvmem cell.
1368 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1370 void nvmem_cell_put(struct nvmem_cell *cell)
1372 struct nvmem_device *nvmem = cell->entry->nvmem;
1375 kfree_const(cell->id);
1378 __nvmem_device_put(nvmem);
1380 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1382 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1385 int i, extra, bit_offset = cell->bit_offset;
1390 *b++ >>= bit_offset;
1392 /* setup rest of the bytes if any */
1393 for (i = 1; i < cell->bytes; i++) {
1394 /* Get bits from next byte and shift them towards msb */
1395 *p |= *b << (BITS_PER_BYTE - bit_offset);
1398 *b++ >>= bit_offset;
1401 /* point to the msb */
1402 p += cell->bytes - 1;
1405 /* result fits in less bytes */
1406 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1407 while (--extra >= 0)
1410 /* clear msb bits if any leftover in the last byte */
1411 if (cell->nbits % BITS_PER_BYTE)
1412 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1415 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1416 struct nvmem_cell_entry *cell,
1417 void *buf, size_t *len, const char *id, int index)
1421 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1426 /* shift bits in-place */
1427 if (cell->bit_offset || cell->nbits)
1428 nvmem_shift_read_buffer_in_place(cell, buf);
1430 if (nvmem->cell_post_process) {
1431 rc = nvmem->cell_post_process(nvmem->priv, id, index,
1432 cell->offset, buf, cell->bytes);
1444 * nvmem_cell_read() - Read a given nvmem cell
1446 * @cell: nvmem cell to be read.
1447 * @len: pointer to length of cell which will be populated on successful read;
1450 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1451 * buffer should be freed by the consumer with a kfree().
1453 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1455 struct nvmem_device *nvmem = cell->entry->nvmem;
1460 return ERR_PTR(-EINVAL);
1462 buf = kzalloc(cell->entry->bytes, GFP_KERNEL);
1464 return ERR_PTR(-ENOMEM);
1466 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1474 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1476 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1479 struct nvmem_device *nvmem = cell->nvmem;
1480 int i, rc, nbits, bit_offset = cell->bit_offset;
1481 u8 v, *p, *buf, *b, pbyte, pbits;
1483 nbits = cell->nbits;
1484 buf = kzalloc(cell->bytes, GFP_KERNEL);
1486 return ERR_PTR(-ENOMEM);
1488 memcpy(buf, _buf, len);
1495 /* setup the first byte with lsb bits from nvmem */
1496 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1499 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1501 /* setup rest of the byte if any */
1502 for (i = 1; i < cell->bytes; i++) {
1503 /* Get last byte bits and shift them towards lsb */
1504 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1512 /* if it's not end on byte boundary */
1513 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1514 /* setup the last byte with msb bits from nvmem */
1515 rc = nvmem_reg_read(nvmem,
1516 cell->offset + cell->bytes - 1, &v, 1);
1519 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1529 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1531 struct nvmem_device *nvmem = cell->nvmem;
1534 if (!nvmem || nvmem->read_only ||
1535 (cell->bit_offset == 0 && len != cell->bytes))
1538 if (cell->bit_offset || cell->nbits) {
1539 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1541 return PTR_ERR(buf);
1544 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1546 /* free the tmp buffer */
1547 if (cell->bit_offset || cell->nbits)
1557 * nvmem_cell_write() - Write to a given nvmem cell
1559 * @cell: nvmem cell to be written.
1560 * @buf: Buffer to be written.
1561 * @len: length of buffer to be written to nvmem cell.
1563 * Return: length of bytes written or negative on failure.
1565 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1567 return __nvmem_cell_entry_write(cell->entry, buf, len);
1570 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1572 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1573 void *val, size_t count)
1575 struct nvmem_cell *cell;
1579 cell = nvmem_cell_get(dev, cell_id);
1581 return PTR_ERR(cell);
1583 buf = nvmem_cell_read(cell, &len);
1585 nvmem_cell_put(cell);
1586 return PTR_ERR(buf);
1590 nvmem_cell_put(cell);
1593 memcpy(val, buf, count);
1595 nvmem_cell_put(cell);
1601 * nvmem_cell_read_u8() - Read a cell value as a u8
1603 * @dev: Device that requests the nvmem cell.
1604 * @cell_id: Name of nvmem cell to read.
1605 * @val: pointer to output value.
1607 * Return: 0 on success or negative errno.
1609 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1611 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1613 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1616 * nvmem_cell_read_u16() - Read a cell value as a u16
1618 * @dev: Device that requests the nvmem cell.
1619 * @cell_id: Name of nvmem cell to read.
1620 * @val: pointer to output value.
1622 * Return: 0 on success or negative errno.
1624 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1626 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1628 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1631 * nvmem_cell_read_u32() - Read a cell value as a u32
1633 * @dev: Device that requests the nvmem cell.
1634 * @cell_id: Name of nvmem cell to read.
1635 * @val: pointer to output value.
1637 * Return: 0 on success or negative errno.
1639 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1641 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1643 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1646 * nvmem_cell_read_u64() - Read a cell value as a u64
1648 * @dev: Device that requests the nvmem cell.
1649 * @cell_id: Name of nvmem cell to read.
1650 * @val: pointer to output value.
1652 * Return: 0 on success or negative errno.
1654 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1656 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1658 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1660 static const void *nvmem_cell_read_variable_common(struct device *dev,
1661 const char *cell_id,
1662 size_t max_len, size_t *len)
1664 struct nvmem_cell *cell;
1668 cell = nvmem_cell_get(dev, cell_id);
1672 nbits = cell->entry->nbits;
1673 buf = nvmem_cell_read(cell, len);
1674 nvmem_cell_put(cell);
1679 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1680 * the length of the real data. Throw away the extra junk.
1683 *len = DIV_ROUND_UP(nbits, 8);
1685 if (*len > max_len) {
1687 return ERR_PTR(-ERANGE);
1694 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1696 * @dev: Device that requests the nvmem cell.
1697 * @cell_id: Name of nvmem cell to read.
1698 * @val: pointer to output value.
1700 * Return: 0 on success or negative errno.
1702 int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1709 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1711 return PTR_ERR(buf);
1713 /* Copy w/ implicit endian conversion */
1715 for (i = 0; i < len; i++)
1716 *val |= buf[i] << (8 * i);
1722 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1725 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1727 * @dev: Device that requests the nvmem cell.
1728 * @cell_id: Name of nvmem cell to read.
1729 * @val: pointer to output value.
1731 * Return: 0 on success or negative errno.
1733 int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1740 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1742 return PTR_ERR(buf);
1744 /* Copy w/ implicit endian conversion */
1746 for (i = 0; i < len; i++)
1747 *val |= (uint64_t)buf[i] << (8 * i);
1753 EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1756 * nvmem_device_cell_read() - Read a given nvmem device and cell
1758 * @nvmem: nvmem device to read from.
1759 * @info: nvmem cell info to be read.
1760 * @buf: buffer pointer which will be populated on successful read.
1762 * Return: length of successful bytes read on success and negative
1763 * error code on error.
1765 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1766 struct nvmem_cell_info *info, void *buf)
1768 struct nvmem_cell_entry cell;
1775 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1779 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
1785 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1788 * nvmem_device_cell_write() - Write cell to a given nvmem device
1790 * @nvmem: nvmem device to be written to.
1791 * @info: nvmem cell info to be written.
1792 * @buf: buffer to be written to cell.
1794 * Return: length of bytes written or negative error code on failure.
1796 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1797 struct nvmem_cell_info *info, void *buf)
1799 struct nvmem_cell_entry cell;
1805 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1809 return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
1811 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1814 * nvmem_device_read() - Read from a given nvmem device
1816 * @nvmem: nvmem device to read from.
1817 * @offset: offset in nvmem device.
1818 * @bytes: number of bytes to read.
1819 * @buf: buffer pointer which will be populated on successful read.
1821 * Return: length of successful bytes read on success and negative
1822 * error code on error.
1824 int nvmem_device_read(struct nvmem_device *nvmem,
1825 unsigned int offset,
1826 size_t bytes, void *buf)
1833 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1840 EXPORT_SYMBOL_GPL(nvmem_device_read);
1843 * nvmem_device_write() - Write cell to a given nvmem device
1845 * @nvmem: nvmem device to be written to.
1846 * @offset: offset in nvmem device.
1847 * @bytes: number of bytes to write.
1848 * @buf: buffer to be written.
1850 * Return: length of bytes written or negative error code on failure.
1852 int nvmem_device_write(struct nvmem_device *nvmem,
1853 unsigned int offset,
1854 size_t bytes, void *buf)
1861 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1869 EXPORT_SYMBOL_GPL(nvmem_device_write);
1872 * nvmem_add_cell_table() - register a table of cell info entries
1874 * @table: table of cell info entries
1876 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1878 mutex_lock(&nvmem_cell_mutex);
1879 list_add_tail(&table->node, &nvmem_cell_tables);
1880 mutex_unlock(&nvmem_cell_mutex);
1882 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1885 * nvmem_del_cell_table() - remove a previously registered cell info table
1887 * @table: table of cell info entries
1889 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1891 mutex_lock(&nvmem_cell_mutex);
1892 list_del(&table->node);
1893 mutex_unlock(&nvmem_cell_mutex);
1895 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1898 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1900 * @entries: array of cell lookup entries
1901 * @nentries: number of cell lookup entries in the array
1903 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1907 mutex_lock(&nvmem_lookup_mutex);
1908 for (i = 0; i < nentries; i++)
1909 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1910 mutex_unlock(&nvmem_lookup_mutex);
1912 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1915 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1918 * @entries: array of cell lookup entries
1919 * @nentries: number of cell lookup entries in the array
1921 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1925 mutex_lock(&nvmem_lookup_mutex);
1926 for (i = 0; i < nentries; i++)
1927 list_del(&entries[i].node);
1928 mutex_unlock(&nvmem_lookup_mutex);
1930 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1933 * nvmem_dev_name() - Get the name of a given nvmem device.
1935 * @nvmem: nvmem device.
1937 * Return: name of the nvmem device.
1939 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1941 return dev_name(&nvmem->dev);
1943 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1945 static int __init nvmem_init(void)
1947 return bus_register(&nvmem_bus_type);
1950 static void __exit nvmem_exit(void)
1952 bus_unregister(&nvmem_bus_type);
1955 subsys_initcall(nvmem_init);
1956 module_exit(nvmem_exit);
1958 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1959 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1960 MODULE_DESCRIPTION("nvmem Driver Core");
1961 MODULE_LICENSE("GPL v2");