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);
630 nvmem->dev.type = &nvmem_provider_type;
631 nvmem->dev.bus = &nvmem_bus_type;
632 nvmem->dev.parent = config->dev;
634 device_initialize(&nvmem->dev);
636 if (!config->ignore_wp)
637 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
639 if (IS_ERR(nvmem->wp_gpio)) {
640 rval = PTR_ERR(nvmem->wp_gpio);
641 nvmem->wp_gpio = NULL;
645 kref_init(&nvmem->refcnt);
646 INIT_LIST_HEAD(&nvmem->cells);
648 nvmem->owner = config->owner;
649 if (!nvmem->owner && config->dev->driver)
650 nvmem->owner = config->dev->driver->owner;
651 nvmem->stride = config->stride ?: 1;
652 nvmem->word_size = config->word_size ?: 1;
653 nvmem->size = config->size;
654 nvmem->root_only = config->root_only;
655 nvmem->priv = config->priv;
656 nvmem->type = config->type;
657 nvmem->reg_read = config->reg_read;
658 nvmem->reg_write = config->reg_write;
659 if (!config->no_of_node)
660 nvmem->dev.of_node = config->dev->of_node;
662 switch (config->id) {
663 case NVMEM_DEVID_NONE:
664 rval = dev_set_name(&nvmem->dev, "%s", config->name);
666 case NVMEM_DEVID_AUTO:
667 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
670 rval = dev_set_name(&nvmem->dev, "%s%d",
671 config->name ? : "nvmem",
672 config->name ? config->id : nvmem->id);
679 nvmem->read_only = device_property_present(config->dev, "read-only") ||
680 config->read_only || !nvmem->reg_write;
682 #ifdef CONFIG_NVMEM_SYSFS
683 nvmem->dev.groups = nvmem_dev_groups;
686 if (config->compat) {
687 rval = nvmem_sysfs_setup_compat(nvmem, config);
693 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
695 goto err_remove_cells;
698 rval = nvmem_add_cells_from_table(nvmem);
700 goto err_remove_cells;
702 rval = nvmem_add_cells_from_of(nvmem);
704 goto err_remove_cells;
706 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
708 rval = device_add(&nvmem->dev);
710 goto err_remove_cells;
712 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
717 nvmem_device_remove_all_cells(nvmem);
719 nvmem_sysfs_remove_compat(nvmem, config);
721 put_device(&nvmem->dev);
723 return ERR_PTR(rval);
725 EXPORT_SYMBOL_GPL(nvmem_register);
727 static void nvmem_device_release(struct kref *kref)
729 struct nvmem_device *nvmem;
731 nvmem = container_of(kref, struct nvmem_device, refcnt);
733 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
735 if (nvmem->flags & FLAG_COMPAT)
736 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
738 nvmem_device_remove_all_cells(nvmem);
739 device_unregister(&nvmem->dev);
743 * nvmem_unregister() - Unregister previously registered nvmem device
745 * @nvmem: Pointer to previously registered nvmem device.
747 void nvmem_unregister(struct nvmem_device *nvmem)
749 kref_put(&nvmem->refcnt, nvmem_device_release);
751 EXPORT_SYMBOL_GPL(nvmem_unregister);
753 static void devm_nvmem_release(struct device *dev, void *res)
755 nvmem_unregister(*(struct nvmem_device **)res);
759 * devm_nvmem_register() - Register a managed nvmem device for given
761 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
763 * @dev: Device that uses the nvmem device.
764 * @config: nvmem device configuration with which nvmem device is created.
766 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
769 struct nvmem_device *devm_nvmem_register(struct device *dev,
770 const struct nvmem_config *config)
772 struct nvmem_device **ptr, *nvmem;
774 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
776 return ERR_PTR(-ENOMEM);
778 nvmem = nvmem_register(config);
780 if (!IS_ERR(nvmem)) {
782 devres_add(dev, ptr);
789 EXPORT_SYMBOL_GPL(devm_nvmem_register);
791 static int devm_nvmem_match(struct device *dev, void *res, void *data)
793 struct nvmem_device **r = res;
799 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
802 * @dev: Device that uses the nvmem device.
803 * @nvmem: Pointer to previously registered nvmem device.
805 * Return: Will be negative on error or zero on success.
807 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
809 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
811 EXPORT_SYMBOL(devm_nvmem_unregister);
813 static struct nvmem_device *__nvmem_device_get(void *data,
814 int (*match)(struct device *dev, const void *data))
816 struct nvmem_device *nvmem = NULL;
819 mutex_lock(&nvmem_mutex);
820 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
822 nvmem = to_nvmem_device(dev);
823 mutex_unlock(&nvmem_mutex);
825 return ERR_PTR(-EPROBE_DEFER);
827 if (!try_module_get(nvmem->owner)) {
829 "could not increase module refcount for cell %s\n",
830 nvmem_dev_name(nvmem));
832 put_device(&nvmem->dev);
833 return ERR_PTR(-EINVAL);
836 kref_get(&nvmem->refcnt);
841 static void __nvmem_device_put(struct nvmem_device *nvmem)
843 put_device(&nvmem->dev);
844 module_put(nvmem->owner);
845 kref_put(&nvmem->refcnt, nvmem_device_release);
848 #if IS_ENABLED(CONFIG_OF)
850 * of_nvmem_device_get() - Get nvmem device from a given id
852 * @np: Device tree node that uses the nvmem device.
853 * @id: nvmem name from nvmem-names property.
855 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
858 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
861 struct device_node *nvmem_np;
862 struct nvmem_device *nvmem;
866 index = of_property_match_string(np, "nvmem-names", id);
868 nvmem_np = of_parse_phandle(np, "nvmem", index);
870 return ERR_PTR(-ENOENT);
872 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
873 of_node_put(nvmem_np);
876 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
880 * nvmem_device_get() - Get nvmem device from a given id
882 * @dev: Device that uses the nvmem device.
883 * @dev_name: name of the requested nvmem device.
885 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
888 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
890 if (dev->of_node) { /* try dt first */
891 struct nvmem_device *nvmem;
893 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
895 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
900 return __nvmem_device_get((void *)dev_name, device_match_name);
902 EXPORT_SYMBOL_GPL(nvmem_device_get);
905 * nvmem_device_find() - Find nvmem device with matching function
907 * @data: Data to pass to match function
908 * @match: Callback function to check device
910 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
913 struct nvmem_device *nvmem_device_find(void *data,
914 int (*match)(struct device *dev, const void *data))
916 return __nvmem_device_get(data, match);
918 EXPORT_SYMBOL_GPL(nvmem_device_find);
920 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
922 struct nvmem_device **nvmem = res;
924 if (WARN_ON(!nvmem || !*nvmem))
927 return *nvmem == data;
930 static void devm_nvmem_device_release(struct device *dev, void *res)
932 nvmem_device_put(*(struct nvmem_device **)res);
936 * devm_nvmem_device_put() - put alredy got nvmem device
938 * @dev: Device that uses the nvmem device.
939 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
940 * that needs to be released.
942 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
946 ret = devres_release(dev, devm_nvmem_device_release,
947 devm_nvmem_device_match, nvmem);
951 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
954 * nvmem_device_put() - put alredy got nvmem device
956 * @nvmem: pointer to nvmem device that needs to be released.
958 void nvmem_device_put(struct nvmem_device *nvmem)
960 __nvmem_device_put(nvmem);
962 EXPORT_SYMBOL_GPL(nvmem_device_put);
965 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
967 * @dev: Device that requests the nvmem device.
968 * @id: name id for the requested nvmem device.
970 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
971 * on success. The nvmem_cell will be freed by the automatically once the
974 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
976 struct nvmem_device **ptr, *nvmem;
978 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
980 return ERR_PTR(-ENOMEM);
982 nvmem = nvmem_device_get(dev, id);
983 if (!IS_ERR(nvmem)) {
985 devres_add(dev, ptr);
992 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
994 static struct nvmem_cell *
995 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
997 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
998 struct nvmem_cell_lookup *lookup;
999 struct nvmem_device *nvmem;
1003 return ERR_PTR(-EINVAL);
1005 dev_id = dev_name(dev);
1007 mutex_lock(&nvmem_lookup_mutex);
1009 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1010 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1011 (strcmp(lookup->con_id, con_id) == 0)) {
1012 /* This is the right entry. */
1013 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1015 if (IS_ERR(nvmem)) {
1016 /* Provider may not be registered yet. */
1017 cell = ERR_CAST(nvmem);
1021 cell = nvmem_find_cell_by_name(nvmem,
1024 __nvmem_device_put(nvmem);
1025 cell = ERR_PTR(-ENOENT);
1031 mutex_unlock(&nvmem_lookup_mutex);
1035 #if IS_ENABLED(CONFIG_OF)
1036 static struct nvmem_cell *
1037 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1039 struct nvmem_cell *iter, *cell = NULL;
1041 mutex_lock(&nvmem_mutex);
1042 list_for_each_entry(iter, &nvmem->cells, node) {
1043 if (np == iter->np) {
1048 mutex_unlock(&nvmem_mutex);
1054 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1056 * @np: Device tree node that uses the nvmem cell.
1057 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1058 * for the cell at index 0 (the lone cell with no accompanying
1059 * nvmem-cell-names property).
1061 * Return: Will be an ERR_PTR() on error or a valid pointer
1062 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1065 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1067 struct device_node *cell_np, *nvmem_np;
1068 struct nvmem_device *nvmem;
1069 struct nvmem_cell *cell;
1072 /* if cell name exists, find index to the name */
1074 index = of_property_match_string(np, "nvmem-cell-names", id);
1076 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1078 return ERR_PTR(-ENOENT);
1080 nvmem_np = of_get_next_parent(cell_np);
1082 return ERR_PTR(-EINVAL);
1084 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1085 of_node_put(nvmem_np);
1087 return ERR_CAST(nvmem);
1089 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1091 __nvmem_device_put(nvmem);
1092 return ERR_PTR(-ENOENT);
1097 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1101 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1103 * @dev: Device that requests the nvmem cell.
1104 * @id: nvmem cell name to get (this corresponds with the name from the
1105 * nvmem-cell-names property for DT systems and with the con_id from
1106 * the lookup entry for non-DT systems).
1108 * Return: Will be an ERR_PTR() on error or a valid pointer
1109 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1112 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1114 struct nvmem_cell *cell;
1116 if (dev->of_node) { /* try dt first */
1117 cell = of_nvmem_cell_get(dev->of_node, id);
1118 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1122 /* NULL cell id only allowed for device tree; invalid otherwise */
1124 return ERR_PTR(-EINVAL);
1126 return nvmem_cell_get_from_lookup(dev, id);
1128 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1130 static void devm_nvmem_cell_release(struct device *dev, void *res)
1132 nvmem_cell_put(*(struct nvmem_cell **)res);
1136 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1138 * @dev: Device that requests the nvmem cell.
1139 * @id: nvmem cell name id to get.
1141 * Return: Will be an ERR_PTR() on error or a valid pointer
1142 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1143 * automatically once the device is freed.
1145 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1147 struct nvmem_cell **ptr, *cell;
1149 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1151 return ERR_PTR(-ENOMEM);
1153 cell = nvmem_cell_get(dev, id);
1154 if (!IS_ERR(cell)) {
1156 devres_add(dev, ptr);
1163 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1165 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1167 struct nvmem_cell **c = res;
1169 if (WARN_ON(!c || !*c))
1176 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1177 * from devm_nvmem_cell_get.
1179 * @dev: Device that requests the nvmem cell.
1180 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1182 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1186 ret = devres_release(dev, devm_nvmem_cell_release,
1187 devm_nvmem_cell_match, cell);
1191 EXPORT_SYMBOL(devm_nvmem_cell_put);
1194 * nvmem_cell_put() - Release previously allocated nvmem cell.
1196 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1198 void nvmem_cell_put(struct nvmem_cell *cell)
1200 struct nvmem_device *nvmem = cell->nvmem;
1202 __nvmem_device_put(nvmem);
1204 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1206 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1209 int i, extra, bit_offset = cell->bit_offset;
1214 *b++ >>= bit_offset;
1216 /* setup rest of the bytes if any */
1217 for (i = 1; i < cell->bytes; i++) {
1218 /* Get bits from next byte and shift them towards msb */
1219 *p |= *b << (BITS_PER_BYTE - bit_offset);
1222 *b++ >>= bit_offset;
1225 /* point to the msb */
1226 p += cell->bytes - 1;
1229 /* result fits in less bytes */
1230 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1231 while (--extra >= 0)
1234 /* clear msb bits if any leftover in the last byte */
1235 if (cell->nbits % BITS_PER_BYTE)
1236 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1239 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1240 struct nvmem_cell *cell,
1241 void *buf, size_t *len)
1245 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1250 /* shift bits in-place */
1251 if (cell->bit_offset || cell->nbits)
1252 nvmem_shift_read_buffer_in_place(cell, buf);
1261 * nvmem_cell_read() - Read a given nvmem cell
1263 * @cell: nvmem cell to be read.
1264 * @len: pointer to length of cell which will be populated on successful read;
1267 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1268 * buffer should be freed by the consumer with a kfree().
1270 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1272 struct nvmem_device *nvmem = cell->nvmem;
1277 return ERR_PTR(-EINVAL);
1279 buf = kzalloc(cell->bytes, GFP_KERNEL);
1281 return ERR_PTR(-ENOMEM);
1283 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1291 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1293 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1296 struct nvmem_device *nvmem = cell->nvmem;
1297 int i, rc, nbits, bit_offset = cell->bit_offset;
1298 u8 v, *p, *buf, *b, pbyte, pbits;
1300 nbits = cell->nbits;
1301 buf = kzalloc(cell->bytes, GFP_KERNEL);
1303 return ERR_PTR(-ENOMEM);
1305 memcpy(buf, _buf, len);
1312 /* setup the first byte with lsb bits from nvmem */
1313 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1316 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1318 /* setup rest of the byte if any */
1319 for (i = 1; i < cell->bytes; i++) {
1320 /* Get last byte bits and shift them towards lsb */
1321 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1329 /* if it's not end on byte boundary */
1330 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1331 /* setup the last byte with msb bits from nvmem */
1332 rc = nvmem_reg_read(nvmem,
1333 cell->offset + cell->bytes - 1, &v, 1);
1336 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1347 * nvmem_cell_write() - Write to a given nvmem cell
1349 * @cell: nvmem cell to be written.
1350 * @buf: Buffer to be written.
1351 * @len: length of buffer to be written to nvmem cell.
1353 * Return: length of bytes written or negative on failure.
1355 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1357 struct nvmem_device *nvmem = cell->nvmem;
1360 if (!nvmem || nvmem->read_only ||
1361 (cell->bit_offset == 0 && len != cell->bytes))
1364 if (cell->bit_offset || cell->nbits) {
1365 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1367 return PTR_ERR(buf);
1370 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1372 /* free the tmp buffer */
1373 if (cell->bit_offset || cell->nbits)
1381 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1383 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1384 void *val, size_t count)
1386 struct nvmem_cell *cell;
1390 cell = nvmem_cell_get(dev, cell_id);
1392 return PTR_ERR(cell);
1394 buf = nvmem_cell_read(cell, &len);
1396 nvmem_cell_put(cell);
1397 return PTR_ERR(buf);
1401 nvmem_cell_put(cell);
1404 memcpy(val, buf, count);
1406 nvmem_cell_put(cell);
1412 * nvmem_cell_read_u8() - Read a cell value as a u8
1414 * @dev: Device that requests the nvmem cell.
1415 * @cell_id: Name of nvmem cell to read.
1416 * @val: pointer to output value.
1418 * Return: 0 on success or negative errno.
1420 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1422 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1424 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1427 * nvmem_cell_read_u16() - Read a cell value as a u16
1429 * @dev: Device that requests the nvmem cell.
1430 * @cell_id: Name of nvmem cell to read.
1431 * @val: pointer to output value.
1433 * Return: 0 on success or negative errno.
1435 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1437 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1439 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1442 * nvmem_cell_read_u32() - Read a cell value as a u32
1444 * @dev: Device that requests the nvmem cell.
1445 * @cell_id: Name of nvmem cell to read.
1446 * @val: pointer to output value.
1448 * Return: 0 on success or negative errno.
1450 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1452 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1454 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1457 * nvmem_cell_read_u64() - Read a cell value as a u64
1459 * @dev: Device that requests the nvmem cell.
1460 * @cell_id: Name of nvmem cell to read.
1461 * @val: pointer to output value.
1463 * Return: 0 on success or negative errno.
1465 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1467 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1469 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1472 * nvmem_device_cell_read() - Read a given nvmem device and cell
1474 * @nvmem: nvmem device to read from.
1475 * @info: nvmem cell info to be read.
1476 * @buf: buffer pointer which will be populated on successful read.
1478 * Return: length of successful bytes read on success and negative
1479 * error code on error.
1481 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1482 struct nvmem_cell_info *info, void *buf)
1484 struct nvmem_cell cell;
1491 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1495 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1501 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1504 * nvmem_device_cell_write() - Write cell to a given nvmem device
1506 * @nvmem: nvmem device to be written to.
1507 * @info: nvmem cell info to be written.
1508 * @buf: buffer to be written to cell.
1510 * Return: length of bytes written or negative error code on failure.
1512 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1513 struct nvmem_cell_info *info, void *buf)
1515 struct nvmem_cell cell;
1521 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1525 return nvmem_cell_write(&cell, buf, cell.bytes);
1527 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1530 * nvmem_device_read() - Read from a given nvmem device
1532 * @nvmem: nvmem device to read from.
1533 * @offset: offset in nvmem device.
1534 * @bytes: number of bytes to read.
1535 * @buf: buffer pointer which will be populated on successful read.
1537 * Return: length of successful bytes read on success and negative
1538 * error code on error.
1540 int nvmem_device_read(struct nvmem_device *nvmem,
1541 unsigned int offset,
1542 size_t bytes, void *buf)
1549 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1556 EXPORT_SYMBOL_GPL(nvmem_device_read);
1559 * nvmem_device_write() - Write cell to a given nvmem device
1561 * @nvmem: nvmem device to be written to.
1562 * @offset: offset in nvmem device.
1563 * @bytes: number of bytes to write.
1564 * @buf: buffer to be written.
1566 * Return: length of bytes written or negative error code on failure.
1568 int nvmem_device_write(struct nvmem_device *nvmem,
1569 unsigned int offset,
1570 size_t bytes, void *buf)
1577 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1585 EXPORT_SYMBOL_GPL(nvmem_device_write);
1588 * nvmem_add_cell_table() - register a table of cell info entries
1590 * @table: table of cell info entries
1592 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1594 mutex_lock(&nvmem_cell_mutex);
1595 list_add_tail(&table->node, &nvmem_cell_tables);
1596 mutex_unlock(&nvmem_cell_mutex);
1598 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1601 * nvmem_del_cell_table() - remove a previously registered cell info table
1603 * @table: table of cell info entries
1605 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1607 mutex_lock(&nvmem_cell_mutex);
1608 list_del(&table->node);
1609 mutex_unlock(&nvmem_cell_mutex);
1611 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1614 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1616 * @entries: array of cell lookup entries
1617 * @nentries: number of cell lookup entries in the array
1619 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1623 mutex_lock(&nvmem_lookup_mutex);
1624 for (i = 0; i < nentries; i++)
1625 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1626 mutex_unlock(&nvmem_lookup_mutex);
1628 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1631 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1634 * @entries: array of cell lookup entries
1635 * @nentries: number of cell lookup entries in the array
1637 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1641 mutex_lock(&nvmem_lookup_mutex);
1642 for (i = 0; i < nentries; i++)
1643 list_del(&entries[i].node);
1644 mutex_unlock(&nvmem_lookup_mutex);
1646 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1649 * nvmem_dev_name() - Get the name of a given nvmem device.
1651 * @nvmem: nvmem device.
1653 * Return: name of the nvmem device.
1655 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1657 return dev_name(&nvmem->dev);
1659 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1661 static int __init nvmem_init(void)
1663 return bus_register(&nvmem_bus_type);
1666 static void __exit nvmem_exit(void)
1668 bus_unregister(&nvmem_bus_type);
1671 subsys_initcall(nvmem_init);
1672 module_exit(nvmem_exit);
1674 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1675 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1676 MODULE_DESCRIPTION("nvmem Driver Core");
1677 MODULE_LICENSE("GPL v2");