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 nvmem_reg_read_t reg_read;
38 nvmem_reg_write_t reg_write;
39 struct gpio_desc *wp_gpio;
43 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
45 #define FLAG_COMPAT BIT(0)
53 struct device_node *np;
54 struct nvmem_device *nvmem;
55 struct list_head node;
58 static DEFINE_MUTEX(nvmem_mutex);
59 static DEFINE_IDA(nvmem_ida);
61 static DEFINE_MUTEX(nvmem_cell_mutex);
62 static LIST_HEAD(nvmem_cell_tables);
64 static DEFINE_MUTEX(nvmem_lookup_mutex);
65 static LIST_HEAD(nvmem_lookup_list);
67 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
69 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
70 void *val, size_t bytes)
73 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
78 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
79 void *val, size_t bytes)
83 if (nvmem->reg_write) {
84 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
85 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
86 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
93 #ifdef CONFIG_NVMEM_SYSFS
94 static const char * const nvmem_type_str[] = {
95 [NVMEM_TYPE_UNKNOWN] = "Unknown",
96 [NVMEM_TYPE_EEPROM] = "EEPROM",
97 [NVMEM_TYPE_OTP] = "OTP",
98 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
101 #ifdef CONFIG_DEBUG_LOCK_ALLOC
102 static struct lock_class_key eeprom_lock_key;
105 static ssize_t type_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
108 struct nvmem_device *nvmem = to_nvmem_device(dev);
110 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
113 static DEVICE_ATTR_RO(type);
115 static struct attribute *nvmem_attrs[] = {
120 static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
121 struct bin_attribute *attr, char *buf,
122 loff_t pos, size_t count)
125 struct nvmem_device *nvmem;
131 dev = kobj_to_dev(kobj);
132 nvmem = to_nvmem_device(dev);
134 /* Stop the user from reading */
135 if (pos >= nvmem->size)
138 if (!IS_ALIGNED(pos, nvmem->stride))
141 if (count < nvmem->word_size)
144 if (pos + count > nvmem->size)
145 count = nvmem->size - pos;
147 count = round_down(count, nvmem->word_size);
149 if (!nvmem->reg_read)
152 rc = nvmem_reg_read(nvmem, pos, buf, count);
160 static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
161 struct bin_attribute *attr, char *buf,
162 loff_t pos, size_t count)
165 struct nvmem_device *nvmem;
171 dev = kobj_to_dev(kobj);
172 nvmem = to_nvmem_device(dev);
174 /* Stop the user from writing */
175 if (pos >= nvmem->size)
178 if (!IS_ALIGNED(pos, nvmem->stride))
181 if (count < nvmem->word_size)
184 if (pos + count > nvmem->size)
185 count = nvmem->size - pos;
187 count = round_down(count, nvmem->word_size);
189 if (!nvmem->reg_write)
192 rc = nvmem_reg_write(nvmem, pos, buf, count);
200 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
204 if (!nvmem->root_only)
207 if (!nvmem->read_only)
210 if (!nvmem->reg_write)
213 if (!nvmem->reg_read)
219 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
220 struct bin_attribute *attr, int i)
222 struct device *dev = kobj_to_dev(kobj);
223 struct nvmem_device *nvmem = to_nvmem_device(dev);
225 attr->size = nvmem->size;
227 return nvmem_bin_attr_get_umode(nvmem);
230 /* default read/write permissions */
231 static struct bin_attribute bin_attr_rw_nvmem = {
236 .read = bin_attr_nvmem_read,
237 .write = bin_attr_nvmem_write,
240 static struct bin_attribute *nvmem_bin_attributes[] = {
245 static const struct attribute_group nvmem_bin_group = {
246 .bin_attrs = nvmem_bin_attributes,
247 .attrs = nvmem_attrs,
248 .is_bin_visible = nvmem_bin_attr_is_visible,
251 static const struct attribute_group *nvmem_dev_groups[] = {
256 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
260 .read = bin_attr_nvmem_read,
261 .write = bin_attr_nvmem_write,
265 * nvmem_setup_compat() - Create an additional binary entry in
266 * drivers sys directory, to be backwards compatible with the older
267 * drivers/misc/eeprom drivers.
269 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
270 const struct nvmem_config *config)
277 if (!config->base_dev)
280 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
281 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
282 nvmem->eeprom.size = nvmem->size;
283 #ifdef CONFIG_DEBUG_LOCK_ALLOC
284 nvmem->eeprom.attr.key = &eeprom_lock_key;
286 nvmem->eeprom.private = &nvmem->dev;
287 nvmem->base_dev = config->base_dev;
289 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
292 "Failed to create eeprom binary file %d\n", rval);
296 nvmem->flags |= FLAG_COMPAT;
301 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
302 const struct nvmem_config *config)
305 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
308 #else /* CONFIG_NVMEM_SYSFS */
310 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
311 const struct nvmem_config *config)
315 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
316 const struct nvmem_config *config)
320 #endif /* CONFIG_NVMEM_SYSFS */
322 static void nvmem_release(struct device *dev)
324 struct nvmem_device *nvmem = to_nvmem_device(dev);
326 ida_free(&nvmem_ida, nvmem->id);
327 gpiod_put(nvmem->wp_gpio);
331 static const struct device_type nvmem_provider_type = {
332 .release = nvmem_release,
335 static struct bus_type nvmem_bus_type = {
339 static void nvmem_cell_drop(struct nvmem_cell *cell)
341 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
342 mutex_lock(&nvmem_mutex);
343 list_del(&cell->node);
344 mutex_unlock(&nvmem_mutex);
345 of_node_put(cell->np);
346 kfree_const(cell->name);
350 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
352 struct nvmem_cell *cell, *p;
354 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
355 nvmem_cell_drop(cell);
358 static void nvmem_cell_add(struct nvmem_cell *cell)
360 mutex_lock(&nvmem_mutex);
361 list_add_tail(&cell->node, &cell->nvmem->cells);
362 mutex_unlock(&nvmem_mutex);
363 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
366 static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem,
367 const struct nvmem_cell_info *info,
368 struct nvmem_cell *cell)
371 cell->offset = info->offset;
372 cell->bytes = info->bytes;
373 cell->name = info->name;
375 cell->bit_offset = info->bit_offset;
376 cell->nbits = info->nbits;
379 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
382 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
384 "cell %s unaligned to nvmem stride %d\n",
385 cell->name ?: "<unknown>", nvmem->stride);
392 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
393 const struct nvmem_cell_info *info,
394 struct nvmem_cell *cell)
398 err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell);
402 cell->name = kstrdup_const(info->name, GFP_KERNEL);
410 * nvmem_add_cells() - Add cell information to an nvmem device
412 * @nvmem: nvmem device to add cells to.
413 * @info: nvmem cell info to add to the device
414 * @ncells: number of cells in info
416 * Return: 0 or negative error code on failure.
418 static int nvmem_add_cells(struct nvmem_device *nvmem,
419 const struct nvmem_cell_info *info,
422 struct nvmem_cell **cells;
425 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
429 for (i = 0; i < ncells; i++) {
430 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
436 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
442 nvmem_cell_add(cells[i]);
445 /* remove tmp array */
451 nvmem_cell_drop(cells[i]);
459 * nvmem_register_notifier() - Register a notifier block for nvmem events.
461 * @nb: notifier block to be called on nvmem events.
463 * Return: 0 on success, negative error number on failure.
465 int nvmem_register_notifier(struct notifier_block *nb)
467 return blocking_notifier_chain_register(&nvmem_notifier, nb);
469 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
472 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
474 * @nb: notifier block to be unregistered.
476 * Return: 0 on success, negative error number on failure.
478 int nvmem_unregister_notifier(struct notifier_block *nb)
480 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
482 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
484 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
486 const struct nvmem_cell_info *info;
487 struct nvmem_cell_table *table;
488 struct nvmem_cell *cell;
491 mutex_lock(&nvmem_cell_mutex);
492 list_for_each_entry(table, &nvmem_cell_tables, node) {
493 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
494 for (i = 0; i < table->ncells; i++) {
495 info = &table->cells[i];
497 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
503 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
511 nvmem_cell_add(cell);
517 mutex_unlock(&nvmem_cell_mutex);
521 static struct nvmem_cell *
522 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
524 struct nvmem_cell *iter, *cell = NULL;
526 mutex_lock(&nvmem_mutex);
527 list_for_each_entry(iter, &nvmem->cells, node) {
528 if (strcmp(cell_id, iter->name) == 0) {
533 mutex_unlock(&nvmem_mutex);
538 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
540 struct device_node *parent, *child;
541 struct device *dev = &nvmem->dev;
542 struct nvmem_cell *cell;
546 parent = dev->of_node;
548 for_each_child_of_node(parent, child) {
549 addr = of_get_property(child, "reg", &len);
552 if (len < 2 * sizeof(u32)) {
553 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
558 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
565 cell->offset = be32_to_cpup(addr++);
566 cell->bytes = be32_to_cpup(addr);
567 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
569 addr = of_get_property(child, "bits", &len);
570 if (addr && len == (2 * sizeof(u32))) {
571 cell->bit_offset = be32_to_cpup(addr++);
572 cell->nbits = be32_to_cpup(addr);
576 cell->bytes = DIV_ROUND_UP(
577 cell->nbits + cell->bit_offset,
580 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
581 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
582 cell->name, nvmem->stride);
583 /* Cells already added will be freed later. */
584 kfree_const(cell->name);
590 cell->np = of_node_get(child);
591 nvmem_cell_add(cell);
598 * nvmem_register() - Register a nvmem device for given nvmem_config.
599 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
601 * @config: nvmem device configuration with which nvmem device is created.
603 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
607 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
609 struct nvmem_device *nvmem;
613 return ERR_PTR(-EINVAL);
615 if (!config->reg_read && !config->reg_write)
616 return ERR_PTR(-EINVAL);
618 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
620 return ERR_PTR(-ENOMEM);
622 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
625 return ERR_PTR(rval);
629 nvmem->wp_gpio = config->wp_gpio;
631 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
633 if (IS_ERR(nvmem->wp_gpio)) {
634 ida_free(&nvmem_ida, nvmem->id);
635 rval = PTR_ERR(nvmem->wp_gpio);
637 return ERR_PTR(rval);
640 kref_init(&nvmem->refcnt);
641 INIT_LIST_HEAD(&nvmem->cells);
644 nvmem->owner = config->owner;
645 if (!nvmem->owner && config->dev->driver)
646 nvmem->owner = config->dev->driver->owner;
647 nvmem->stride = config->stride ?: 1;
648 nvmem->word_size = config->word_size ?: 1;
649 nvmem->size = config->size;
650 nvmem->dev.type = &nvmem_provider_type;
651 nvmem->dev.bus = &nvmem_bus_type;
652 nvmem->dev.parent = config->dev;
653 nvmem->root_only = config->root_only;
654 nvmem->priv = config->priv;
655 nvmem->type = config->type;
656 nvmem->reg_read = config->reg_read;
657 nvmem->reg_write = config->reg_write;
658 if (!config->no_of_node)
659 nvmem->dev.of_node = config->dev->of_node;
661 switch (config->id) {
662 case NVMEM_DEVID_NONE:
663 dev_set_name(&nvmem->dev, "%s", config->name);
665 case NVMEM_DEVID_AUTO:
666 dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
669 dev_set_name(&nvmem->dev, "%s%d",
670 config->name ? : "nvmem",
671 config->name ? config->id : nvmem->id);
675 nvmem->read_only = device_property_present(config->dev, "read-only") ||
676 config->read_only || !nvmem->reg_write;
678 #ifdef CONFIG_NVMEM_SYSFS
679 nvmem->dev.groups = nvmem_dev_groups;
682 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
684 rval = device_register(&nvmem->dev);
688 if (config->compat) {
689 rval = nvmem_sysfs_setup_compat(nvmem, config);
695 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
697 goto err_teardown_compat;
700 rval = nvmem_add_cells_from_table(nvmem);
702 goto err_remove_cells;
704 rval = nvmem_add_cells_from_of(nvmem);
706 goto err_remove_cells;
708 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
713 nvmem_device_remove_all_cells(nvmem);
716 nvmem_sysfs_remove_compat(nvmem, config);
718 device_del(&nvmem->dev);
720 put_device(&nvmem->dev);
722 return ERR_PTR(rval);
724 EXPORT_SYMBOL_GPL(nvmem_register);
726 static void nvmem_device_release(struct kref *kref)
728 struct nvmem_device *nvmem;
730 nvmem = container_of(kref, struct nvmem_device, refcnt);
732 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
734 if (nvmem->flags & FLAG_COMPAT)
735 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
737 nvmem_device_remove_all_cells(nvmem);
738 device_unregister(&nvmem->dev);
742 * nvmem_unregister() - Unregister previously registered nvmem device
744 * @nvmem: Pointer to previously registered nvmem device.
746 void nvmem_unregister(struct nvmem_device *nvmem)
748 kref_put(&nvmem->refcnt, nvmem_device_release);
750 EXPORT_SYMBOL_GPL(nvmem_unregister);
752 static void devm_nvmem_release(struct device *dev, void *res)
754 nvmem_unregister(*(struct nvmem_device **)res);
758 * devm_nvmem_register() - Register a managed nvmem device for given
760 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
762 * @dev: Device that uses the nvmem device.
763 * @config: nvmem device configuration with which nvmem device is created.
765 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
768 struct nvmem_device *devm_nvmem_register(struct device *dev,
769 const struct nvmem_config *config)
771 struct nvmem_device **ptr, *nvmem;
773 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
775 return ERR_PTR(-ENOMEM);
777 nvmem = nvmem_register(config);
779 if (!IS_ERR(nvmem)) {
781 devres_add(dev, ptr);
788 EXPORT_SYMBOL_GPL(devm_nvmem_register);
790 static int devm_nvmem_match(struct device *dev, void *res, void *data)
792 struct nvmem_device **r = res;
798 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
801 * @dev: Device that uses the nvmem device.
802 * @nvmem: Pointer to previously registered nvmem device.
804 * Return: Will be negative on error or zero on success.
806 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
808 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
810 EXPORT_SYMBOL(devm_nvmem_unregister);
812 static struct nvmem_device *__nvmem_device_get(void *data,
813 int (*match)(struct device *dev, const void *data))
815 struct nvmem_device *nvmem = NULL;
818 mutex_lock(&nvmem_mutex);
819 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
821 nvmem = to_nvmem_device(dev);
822 mutex_unlock(&nvmem_mutex);
824 return ERR_PTR(-EPROBE_DEFER);
826 if (!try_module_get(nvmem->owner)) {
828 "could not increase module refcount for cell %s\n",
829 nvmem_dev_name(nvmem));
831 put_device(&nvmem->dev);
832 return ERR_PTR(-EINVAL);
835 kref_get(&nvmem->refcnt);
840 static void __nvmem_device_put(struct nvmem_device *nvmem)
842 put_device(&nvmem->dev);
843 module_put(nvmem->owner);
844 kref_put(&nvmem->refcnt, nvmem_device_release);
847 #if IS_ENABLED(CONFIG_OF)
849 * of_nvmem_device_get() - Get nvmem device from a given id
851 * @np: Device tree node that uses the nvmem device.
852 * @id: nvmem name from nvmem-names property.
854 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
857 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
860 struct device_node *nvmem_np;
861 struct nvmem_device *nvmem;
865 index = of_property_match_string(np, "nvmem-names", id);
867 nvmem_np = of_parse_phandle(np, "nvmem", index);
869 return ERR_PTR(-ENOENT);
871 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
872 of_node_put(nvmem_np);
875 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
879 * nvmem_device_get() - Get nvmem device from a given id
881 * @dev: Device that uses the nvmem device.
882 * @dev_name: name of the requested nvmem device.
884 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
887 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
889 if (dev->of_node) { /* try dt first */
890 struct nvmem_device *nvmem;
892 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
894 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
899 return __nvmem_device_get((void *)dev_name, device_match_name);
901 EXPORT_SYMBOL_GPL(nvmem_device_get);
904 * nvmem_device_find() - Find nvmem device with matching function
906 * @data: Data to pass to match function
907 * @match: Callback function to check device
909 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
912 struct nvmem_device *nvmem_device_find(void *data,
913 int (*match)(struct device *dev, const void *data))
915 return __nvmem_device_get(data, match);
917 EXPORT_SYMBOL_GPL(nvmem_device_find);
919 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
921 struct nvmem_device **nvmem = res;
923 if (WARN_ON(!nvmem || !*nvmem))
926 return *nvmem == data;
929 static void devm_nvmem_device_release(struct device *dev, void *res)
931 nvmem_device_put(*(struct nvmem_device **)res);
935 * devm_nvmem_device_put() - put alredy got nvmem device
937 * @dev: Device that uses the nvmem device.
938 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
939 * that needs to be released.
941 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
945 ret = devres_release(dev, devm_nvmem_device_release,
946 devm_nvmem_device_match, nvmem);
950 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
953 * nvmem_device_put() - put alredy got nvmem device
955 * @nvmem: pointer to nvmem device that needs to be released.
957 void nvmem_device_put(struct nvmem_device *nvmem)
959 __nvmem_device_put(nvmem);
961 EXPORT_SYMBOL_GPL(nvmem_device_put);
964 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
966 * @dev: Device that requests the nvmem device.
967 * @id: name id for the requested nvmem device.
969 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
970 * on success. The nvmem_cell will be freed by the automatically once the
973 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
975 struct nvmem_device **ptr, *nvmem;
977 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
979 return ERR_PTR(-ENOMEM);
981 nvmem = nvmem_device_get(dev, id);
982 if (!IS_ERR(nvmem)) {
984 devres_add(dev, ptr);
991 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
993 static struct nvmem_cell *
994 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
996 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
997 struct nvmem_cell_lookup *lookup;
998 struct nvmem_device *nvmem;
1002 return ERR_PTR(-EINVAL);
1004 dev_id = dev_name(dev);
1006 mutex_lock(&nvmem_lookup_mutex);
1008 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1009 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1010 (strcmp(lookup->con_id, con_id) == 0)) {
1011 /* This is the right entry. */
1012 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1014 if (IS_ERR(nvmem)) {
1015 /* Provider may not be registered yet. */
1016 cell = ERR_CAST(nvmem);
1020 cell = nvmem_find_cell_by_name(nvmem,
1023 __nvmem_device_put(nvmem);
1024 cell = ERR_PTR(-ENOENT);
1030 mutex_unlock(&nvmem_lookup_mutex);
1034 #if IS_ENABLED(CONFIG_OF)
1035 static struct nvmem_cell *
1036 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1038 struct nvmem_cell *iter, *cell = NULL;
1040 mutex_lock(&nvmem_mutex);
1041 list_for_each_entry(iter, &nvmem->cells, node) {
1042 if (np == iter->np) {
1047 mutex_unlock(&nvmem_mutex);
1053 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1055 * @np: Device tree node that uses the nvmem cell.
1056 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1057 * for the cell at index 0 (the lone cell with no accompanying
1058 * nvmem-cell-names property).
1060 * Return: Will be an ERR_PTR() on error or a valid pointer
1061 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1064 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1066 struct device_node *cell_np, *nvmem_np;
1067 struct nvmem_device *nvmem;
1068 struct nvmem_cell *cell;
1071 /* if cell name exists, find index to the name */
1073 index = of_property_match_string(np, "nvmem-cell-names", id);
1075 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1077 return ERR_PTR(-ENOENT);
1079 nvmem_np = of_get_next_parent(cell_np);
1081 return ERR_PTR(-EINVAL);
1083 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1084 of_node_put(nvmem_np);
1086 return ERR_CAST(nvmem);
1088 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1090 __nvmem_device_put(nvmem);
1091 return ERR_PTR(-ENOENT);
1096 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1100 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1102 * @dev: Device that requests the nvmem cell.
1103 * @id: nvmem cell name to get (this corresponds with the name from the
1104 * nvmem-cell-names property for DT systems and with the con_id from
1105 * the lookup entry for non-DT systems).
1107 * Return: Will be an ERR_PTR() on error or a valid pointer
1108 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1111 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1113 struct nvmem_cell *cell;
1115 if (dev->of_node) { /* try dt first */
1116 cell = of_nvmem_cell_get(dev->of_node, id);
1117 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1121 /* NULL cell id only allowed for device tree; invalid otherwise */
1123 return ERR_PTR(-EINVAL);
1125 return nvmem_cell_get_from_lookup(dev, id);
1127 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1129 static void devm_nvmem_cell_release(struct device *dev, void *res)
1131 nvmem_cell_put(*(struct nvmem_cell **)res);
1135 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1137 * @dev: Device that requests the nvmem cell.
1138 * @id: nvmem cell name id to get.
1140 * Return: Will be an ERR_PTR() on error or a valid pointer
1141 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1142 * automatically once the device is freed.
1144 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1146 struct nvmem_cell **ptr, *cell;
1148 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1150 return ERR_PTR(-ENOMEM);
1152 cell = nvmem_cell_get(dev, id);
1153 if (!IS_ERR(cell)) {
1155 devres_add(dev, ptr);
1162 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1164 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1166 struct nvmem_cell **c = res;
1168 if (WARN_ON(!c || !*c))
1175 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1176 * from devm_nvmem_cell_get.
1178 * @dev: Device that requests the nvmem cell.
1179 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1181 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1185 ret = devres_release(dev, devm_nvmem_cell_release,
1186 devm_nvmem_cell_match, cell);
1190 EXPORT_SYMBOL(devm_nvmem_cell_put);
1193 * nvmem_cell_put() - Release previously allocated nvmem cell.
1195 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1197 void nvmem_cell_put(struct nvmem_cell *cell)
1199 struct nvmem_device *nvmem = cell->nvmem;
1201 __nvmem_device_put(nvmem);
1203 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1205 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1208 int i, extra, bit_offset = cell->bit_offset;
1213 *b++ >>= bit_offset;
1215 /* setup rest of the bytes if any */
1216 for (i = 1; i < cell->bytes; i++) {
1217 /* Get bits from next byte and shift them towards msb */
1218 *p |= *b << (BITS_PER_BYTE - bit_offset);
1221 *b++ >>= bit_offset;
1224 /* point to the msb */
1225 p += cell->bytes - 1;
1228 /* result fits in less bytes */
1229 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1230 while (--extra >= 0)
1233 /* clear msb bits if any leftover in the last byte */
1234 if (cell->nbits % BITS_PER_BYTE)
1235 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1238 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1239 struct nvmem_cell *cell,
1240 void *buf, size_t *len)
1244 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1249 /* shift bits in-place */
1250 if (cell->bit_offset || cell->nbits)
1251 nvmem_shift_read_buffer_in_place(cell, buf);
1260 * nvmem_cell_read() - Read a given nvmem cell
1262 * @cell: nvmem cell to be read.
1263 * @len: pointer to length of cell which will be populated on successful read;
1266 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1267 * buffer should be freed by the consumer with a kfree().
1269 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1271 struct nvmem_device *nvmem = cell->nvmem;
1276 return ERR_PTR(-EINVAL);
1278 buf = kzalloc(cell->bytes, GFP_KERNEL);
1280 return ERR_PTR(-ENOMEM);
1282 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1290 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1292 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1295 struct nvmem_device *nvmem = cell->nvmem;
1296 int i, rc, nbits, bit_offset = cell->bit_offset;
1297 u8 v, *p, *buf, *b, pbyte, pbits;
1299 nbits = cell->nbits;
1300 buf = kzalloc(cell->bytes, GFP_KERNEL);
1302 return ERR_PTR(-ENOMEM);
1304 memcpy(buf, _buf, len);
1311 /* setup the first byte with lsb bits from nvmem */
1312 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1315 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1317 /* setup rest of the byte if any */
1318 for (i = 1; i < cell->bytes; i++) {
1319 /* Get last byte bits and shift them towards lsb */
1320 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1328 /* if it's not end on byte boundary */
1329 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1330 /* setup the last byte with msb bits from nvmem */
1331 rc = nvmem_reg_read(nvmem,
1332 cell->offset + cell->bytes - 1, &v, 1);
1335 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1346 * nvmem_cell_write() - Write to a given nvmem cell
1348 * @cell: nvmem cell to be written.
1349 * @buf: Buffer to be written.
1350 * @len: length of buffer to be written to nvmem cell.
1352 * Return: length of bytes written or negative on failure.
1354 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1356 struct nvmem_device *nvmem = cell->nvmem;
1359 if (!nvmem || nvmem->read_only ||
1360 (cell->bit_offset == 0 && len != cell->bytes))
1363 if (cell->bit_offset || cell->nbits) {
1364 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1366 return PTR_ERR(buf);
1369 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1371 /* free the tmp buffer */
1372 if (cell->bit_offset || cell->nbits)
1380 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1382 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1383 void *val, size_t count)
1385 struct nvmem_cell *cell;
1389 cell = nvmem_cell_get(dev, cell_id);
1391 return PTR_ERR(cell);
1393 buf = nvmem_cell_read(cell, &len);
1395 nvmem_cell_put(cell);
1396 return PTR_ERR(buf);
1400 nvmem_cell_put(cell);
1403 memcpy(val, buf, count);
1405 nvmem_cell_put(cell);
1411 * nvmem_cell_read_u8() - Read a cell value as a u8
1413 * @dev: Device that requests the nvmem cell.
1414 * @cell_id: Name of nvmem cell to read.
1415 * @val: pointer to output value.
1417 * Return: 0 on success or negative errno.
1419 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1421 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1423 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1426 * nvmem_cell_read_u16() - Read a cell value as a u16
1428 * @dev: Device that requests the nvmem cell.
1429 * @cell_id: Name of nvmem cell to read.
1430 * @val: pointer to output value.
1432 * Return: 0 on success or negative errno.
1434 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1436 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1438 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1441 * nvmem_cell_read_u32() - Read a cell value as a u32
1443 * @dev: Device that requests the nvmem cell.
1444 * @cell_id: Name of nvmem cell to read.
1445 * @val: pointer to output value.
1447 * Return: 0 on success or negative errno.
1449 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1451 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1453 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1456 * nvmem_cell_read_u64() - Read a cell value as a u64
1458 * @dev: Device that requests the nvmem cell.
1459 * @cell_id: Name of nvmem cell to read.
1460 * @val: pointer to output value.
1462 * Return: 0 on success or negative errno.
1464 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1466 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1468 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1471 * nvmem_device_cell_read() - Read a given nvmem device and cell
1473 * @nvmem: nvmem device to read from.
1474 * @info: nvmem cell info to be read.
1475 * @buf: buffer pointer which will be populated on successful read.
1477 * Return: length of successful bytes read on success and negative
1478 * error code on error.
1480 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1481 struct nvmem_cell_info *info, void *buf)
1483 struct nvmem_cell cell;
1490 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1494 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1500 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1503 * nvmem_device_cell_write() - Write cell to a given nvmem device
1505 * @nvmem: nvmem device to be written to.
1506 * @info: nvmem cell info to be written.
1507 * @buf: buffer to be written to cell.
1509 * Return: length of bytes written or negative error code on failure.
1511 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1512 struct nvmem_cell_info *info, void *buf)
1514 struct nvmem_cell cell;
1520 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1524 return nvmem_cell_write(&cell, buf, cell.bytes);
1526 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1529 * nvmem_device_read() - Read from a given nvmem device
1531 * @nvmem: nvmem device to read from.
1532 * @offset: offset in nvmem device.
1533 * @bytes: number of bytes to read.
1534 * @buf: buffer pointer which will be populated on successful read.
1536 * Return: length of successful bytes read on success and negative
1537 * error code on error.
1539 int nvmem_device_read(struct nvmem_device *nvmem,
1540 unsigned int offset,
1541 size_t bytes, void *buf)
1548 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1555 EXPORT_SYMBOL_GPL(nvmem_device_read);
1558 * nvmem_device_write() - Write cell to a given nvmem device
1560 * @nvmem: nvmem device to be written to.
1561 * @offset: offset in nvmem device.
1562 * @bytes: number of bytes to write.
1563 * @buf: buffer to be written.
1565 * Return: length of bytes written or negative error code on failure.
1567 int nvmem_device_write(struct nvmem_device *nvmem,
1568 unsigned int offset,
1569 size_t bytes, void *buf)
1576 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1584 EXPORT_SYMBOL_GPL(nvmem_device_write);
1587 * nvmem_add_cell_table() - register a table of cell info entries
1589 * @table: table of cell info entries
1591 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1593 mutex_lock(&nvmem_cell_mutex);
1594 list_add_tail(&table->node, &nvmem_cell_tables);
1595 mutex_unlock(&nvmem_cell_mutex);
1597 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1600 * nvmem_del_cell_table() - remove a previously registered cell info table
1602 * @table: table of cell info entries
1604 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1606 mutex_lock(&nvmem_cell_mutex);
1607 list_del(&table->node);
1608 mutex_unlock(&nvmem_cell_mutex);
1610 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1613 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1615 * @entries: array of cell lookup entries
1616 * @nentries: number of cell lookup entries in the array
1618 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1622 mutex_lock(&nvmem_lookup_mutex);
1623 for (i = 0; i < nentries; i++)
1624 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1625 mutex_unlock(&nvmem_lookup_mutex);
1627 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1630 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1633 * @entries: array of cell lookup entries
1634 * @nentries: number of cell lookup entries in the array
1636 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1640 mutex_lock(&nvmem_lookup_mutex);
1641 for (i = 0; i < nentries; i++)
1642 list_del(&entries[i].node);
1643 mutex_unlock(&nvmem_lookup_mutex);
1645 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1648 * nvmem_dev_name() - Get the name of a given nvmem device.
1650 * @nvmem: nvmem device.
1652 * Return: name of the nvmem device.
1654 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1656 return dev_name(&nvmem->dev);
1658 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1660 static int __init nvmem_init(void)
1662 return bus_register(&nvmem_bus_type);
1665 static void __exit nvmem_exit(void)
1667 bus_unregister(&nvmem_bus_type);
1670 subsys_initcall(nvmem_init);
1671 module_exit(nvmem_exit);
1673 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1674 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1675 MODULE_DESCRIPTION("nvmem Driver Core");
1676 MODULE_LICENSE("GPL v2");