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 return nvmem_bin_attr_get_umode(nvmem);
228 /* default read/write permissions */
229 static struct bin_attribute bin_attr_rw_nvmem = {
234 .read = bin_attr_nvmem_read,
235 .write = bin_attr_nvmem_write,
238 static struct bin_attribute *nvmem_bin_attributes[] = {
243 static const struct attribute_group nvmem_bin_group = {
244 .bin_attrs = nvmem_bin_attributes,
245 .attrs = nvmem_attrs,
246 .is_bin_visible = nvmem_bin_attr_is_visible,
249 static const struct attribute_group *nvmem_dev_groups[] = {
254 static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
258 .read = bin_attr_nvmem_read,
259 .write = bin_attr_nvmem_write,
263 * nvmem_setup_compat() - Create an additional binary entry in
264 * drivers sys directory, to be backwards compatible with the older
265 * drivers/misc/eeprom drivers.
267 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
268 const struct nvmem_config *config)
275 if (!config->base_dev)
278 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
279 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
280 nvmem->eeprom.size = nvmem->size;
281 #ifdef CONFIG_DEBUG_LOCK_ALLOC
282 nvmem->eeprom.attr.key = &eeprom_lock_key;
284 nvmem->eeprom.private = &nvmem->dev;
285 nvmem->base_dev = config->base_dev;
287 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
290 "Failed to create eeprom binary file %d\n", rval);
294 nvmem->flags |= FLAG_COMPAT;
299 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
300 const struct nvmem_config *config)
303 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
306 #else /* CONFIG_NVMEM_SYSFS */
308 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
309 const struct nvmem_config *config)
313 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
314 const struct nvmem_config *config)
318 #endif /* CONFIG_NVMEM_SYSFS */
320 static void nvmem_release(struct device *dev)
322 struct nvmem_device *nvmem = to_nvmem_device(dev);
324 ida_free(&nvmem_ida, nvmem->id);
325 gpiod_put(nvmem->wp_gpio);
329 static const struct device_type nvmem_provider_type = {
330 .release = nvmem_release,
333 static struct bus_type nvmem_bus_type = {
337 static void nvmem_cell_drop(struct nvmem_cell *cell)
339 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
340 mutex_lock(&nvmem_mutex);
341 list_del(&cell->node);
342 mutex_unlock(&nvmem_mutex);
343 of_node_put(cell->np);
344 kfree_const(cell->name);
348 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
350 struct nvmem_cell *cell, *p;
352 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
353 nvmem_cell_drop(cell);
356 static void nvmem_cell_add(struct nvmem_cell *cell)
358 mutex_lock(&nvmem_mutex);
359 list_add_tail(&cell->node, &cell->nvmem->cells);
360 mutex_unlock(&nvmem_mutex);
361 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
364 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
365 const struct nvmem_cell_info *info,
366 struct nvmem_cell *cell)
369 cell->offset = info->offset;
370 cell->bytes = info->bytes;
371 cell->name = kstrdup_const(info->name, GFP_KERNEL);
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, nvmem->stride);
393 * nvmem_add_cells() - Add cell information to an nvmem device
395 * @nvmem: nvmem device to add cells to.
396 * @info: nvmem cell info to add to the device
397 * @ncells: number of cells in info
399 * Return: 0 or negative error code on failure.
401 static int nvmem_add_cells(struct nvmem_device *nvmem,
402 const struct nvmem_cell_info *info,
405 struct nvmem_cell **cells;
408 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
412 for (i = 0; i < ncells; i++) {
413 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
419 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
425 nvmem_cell_add(cells[i]);
428 /* remove tmp array */
434 nvmem_cell_drop(cells[i]);
442 * nvmem_register_notifier() - Register a notifier block for nvmem events.
444 * @nb: notifier block to be called on nvmem events.
446 * Return: 0 on success, negative error number on failure.
448 int nvmem_register_notifier(struct notifier_block *nb)
450 return blocking_notifier_chain_register(&nvmem_notifier, nb);
452 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
455 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
457 * @nb: notifier block to be unregistered.
459 * Return: 0 on success, negative error number on failure.
461 int nvmem_unregister_notifier(struct notifier_block *nb)
463 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
465 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
467 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
469 const struct nvmem_cell_info *info;
470 struct nvmem_cell_table *table;
471 struct nvmem_cell *cell;
474 mutex_lock(&nvmem_cell_mutex);
475 list_for_each_entry(table, &nvmem_cell_tables, node) {
476 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
477 for (i = 0; i < table->ncells; i++) {
478 info = &table->cells[i];
480 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
486 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
494 nvmem_cell_add(cell);
500 mutex_unlock(&nvmem_cell_mutex);
504 static struct nvmem_cell *
505 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
507 struct nvmem_cell *iter, *cell = NULL;
509 mutex_lock(&nvmem_mutex);
510 list_for_each_entry(iter, &nvmem->cells, node) {
511 if (strcmp(cell_id, iter->name) == 0) {
516 mutex_unlock(&nvmem_mutex);
521 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
523 struct device_node *parent, *child;
524 struct device *dev = &nvmem->dev;
525 struct nvmem_cell *cell;
529 parent = dev->of_node;
531 for_each_child_of_node(parent, child) {
532 addr = of_get_property(child, "reg", &len);
533 if (!addr || (len < 2 * sizeof(u32))) {
534 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
538 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
543 cell->np = of_node_get(child);
544 cell->offset = be32_to_cpup(addr++);
545 cell->bytes = be32_to_cpup(addr);
546 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
548 addr = of_get_property(child, "bits", &len);
549 if (addr && len == (2 * sizeof(u32))) {
550 cell->bit_offset = be32_to_cpup(addr++);
551 cell->nbits = be32_to_cpup(addr);
555 cell->bytes = DIV_ROUND_UP(
556 cell->nbits + cell->bit_offset,
559 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
560 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
561 cell->name, nvmem->stride);
562 /* Cells already added will be freed later. */
563 kfree_const(cell->name);
568 nvmem_cell_add(cell);
575 * nvmem_register() - Register a nvmem device for given nvmem_config.
576 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
578 * @config: nvmem device configuration with which nvmem device is created.
580 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
584 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
586 struct nvmem_device *nvmem;
590 return ERR_PTR(-EINVAL);
592 if (!config->reg_read && !config->reg_write)
593 return ERR_PTR(-EINVAL);
595 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
597 return ERR_PTR(-ENOMEM);
599 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
602 return ERR_PTR(rval);
606 nvmem->wp_gpio = config->wp_gpio;
608 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
610 if (IS_ERR(nvmem->wp_gpio)) {
611 ida_free(&nvmem_ida, nvmem->id);
612 rval = PTR_ERR(nvmem->wp_gpio);
614 return ERR_PTR(rval);
617 kref_init(&nvmem->refcnt);
618 INIT_LIST_HEAD(&nvmem->cells);
621 nvmem->owner = config->owner;
622 if (!nvmem->owner && config->dev->driver)
623 nvmem->owner = config->dev->driver->owner;
624 nvmem->stride = config->stride ?: 1;
625 nvmem->word_size = config->word_size ?: 1;
626 nvmem->size = config->size;
627 nvmem->dev.type = &nvmem_provider_type;
628 nvmem->dev.bus = &nvmem_bus_type;
629 nvmem->dev.parent = config->dev;
630 nvmem->root_only = config->root_only;
631 nvmem->priv = config->priv;
632 nvmem->type = config->type;
633 nvmem->reg_read = config->reg_read;
634 nvmem->reg_write = config->reg_write;
635 if (!config->no_of_node)
636 nvmem->dev.of_node = config->dev->of_node;
638 switch (config->id) {
639 case NVMEM_DEVID_NONE:
640 dev_set_name(&nvmem->dev, "%s", config->name);
642 case NVMEM_DEVID_AUTO:
643 dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
646 dev_set_name(&nvmem->dev, "%s%d",
647 config->name ? : "nvmem",
648 config->name ? config->id : nvmem->id);
652 nvmem->read_only = device_property_present(config->dev, "read-only") ||
653 config->read_only || !nvmem->reg_write;
655 #ifdef CONFIG_NVMEM_SYSFS
656 nvmem->dev.groups = nvmem_dev_groups;
659 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
661 rval = device_register(&nvmem->dev);
665 if (config->compat) {
666 rval = nvmem_sysfs_setup_compat(nvmem, config);
672 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
674 goto err_teardown_compat;
677 rval = nvmem_add_cells_from_table(nvmem);
679 goto err_remove_cells;
681 rval = nvmem_add_cells_from_of(nvmem);
683 goto err_remove_cells;
685 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
690 nvmem_device_remove_all_cells(nvmem);
693 nvmem_sysfs_remove_compat(nvmem, config);
695 device_del(&nvmem->dev);
697 put_device(&nvmem->dev);
699 return ERR_PTR(rval);
701 EXPORT_SYMBOL_GPL(nvmem_register);
703 static void nvmem_device_release(struct kref *kref)
705 struct nvmem_device *nvmem;
707 nvmem = container_of(kref, struct nvmem_device, refcnt);
709 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
711 if (nvmem->flags & FLAG_COMPAT)
712 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
714 nvmem_device_remove_all_cells(nvmem);
715 device_unregister(&nvmem->dev);
719 * nvmem_unregister() - Unregister previously registered nvmem device
721 * @nvmem: Pointer to previously registered nvmem device.
723 void nvmem_unregister(struct nvmem_device *nvmem)
725 kref_put(&nvmem->refcnt, nvmem_device_release);
727 EXPORT_SYMBOL_GPL(nvmem_unregister);
729 static void devm_nvmem_release(struct device *dev, void *res)
731 nvmem_unregister(*(struct nvmem_device **)res);
735 * devm_nvmem_register() - Register a managed nvmem device for given
737 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
739 * @dev: Device that uses the nvmem device.
740 * @config: nvmem device configuration with which nvmem device is created.
742 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
745 struct nvmem_device *devm_nvmem_register(struct device *dev,
746 const struct nvmem_config *config)
748 struct nvmem_device **ptr, *nvmem;
750 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
752 return ERR_PTR(-ENOMEM);
754 nvmem = nvmem_register(config);
756 if (!IS_ERR(nvmem)) {
758 devres_add(dev, ptr);
765 EXPORT_SYMBOL_GPL(devm_nvmem_register);
767 static int devm_nvmem_match(struct device *dev, void *res, void *data)
769 struct nvmem_device **r = res;
775 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
778 * @dev: Device that uses the nvmem device.
779 * @nvmem: Pointer to previously registered nvmem device.
781 * Return: Will be negative on error or zero on success.
783 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
785 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
787 EXPORT_SYMBOL(devm_nvmem_unregister);
789 static struct nvmem_device *__nvmem_device_get(void *data,
790 int (*match)(struct device *dev, const void *data))
792 struct nvmem_device *nvmem = NULL;
795 mutex_lock(&nvmem_mutex);
796 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
798 nvmem = to_nvmem_device(dev);
799 mutex_unlock(&nvmem_mutex);
801 return ERR_PTR(-EPROBE_DEFER);
803 if (!try_module_get(nvmem->owner)) {
805 "could not increase module refcount for cell %s\n",
806 nvmem_dev_name(nvmem));
808 put_device(&nvmem->dev);
809 return ERR_PTR(-EINVAL);
812 kref_get(&nvmem->refcnt);
817 static void __nvmem_device_put(struct nvmem_device *nvmem)
819 put_device(&nvmem->dev);
820 module_put(nvmem->owner);
821 kref_put(&nvmem->refcnt, nvmem_device_release);
824 #if IS_ENABLED(CONFIG_OF)
826 * of_nvmem_device_get() - Get nvmem device from a given id
828 * @np: Device tree node that uses the nvmem device.
829 * @id: nvmem name from nvmem-names property.
831 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
834 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
837 struct device_node *nvmem_np;
838 struct nvmem_device *nvmem;
842 index = of_property_match_string(np, "nvmem-names", id);
844 nvmem_np = of_parse_phandle(np, "nvmem", index);
846 return ERR_PTR(-ENOENT);
848 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
849 of_node_put(nvmem_np);
852 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
856 * nvmem_device_get() - Get nvmem device from a given id
858 * @dev: Device that uses the nvmem device.
859 * @dev_name: name of the requested nvmem device.
861 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
864 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
866 if (dev->of_node) { /* try dt first */
867 struct nvmem_device *nvmem;
869 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
871 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
876 return __nvmem_device_get((void *)dev_name, device_match_name);
878 EXPORT_SYMBOL_GPL(nvmem_device_get);
881 * nvmem_device_find() - Find nvmem device with matching function
883 * @data: Data to pass to match function
884 * @match: Callback function to check device
886 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
889 struct nvmem_device *nvmem_device_find(void *data,
890 int (*match)(struct device *dev, const void *data))
892 return __nvmem_device_get(data, match);
894 EXPORT_SYMBOL_GPL(nvmem_device_find);
896 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
898 struct nvmem_device **nvmem = res;
900 if (WARN_ON(!nvmem || !*nvmem))
903 return *nvmem == data;
906 static void devm_nvmem_device_release(struct device *dev, void *res)
908 nvmem_device_put(*(struct nvmem_device **)res);
912 * devm_nvmem_device_put() - put alredy got nvmem device
914 * @dev: Device that uses the nvmem device.
915 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
916 * that needs to be released.
918 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
922 ret = devres_release(dev, devm_nvmem_device_release,
923 devm_nvmem_device_match, nvmem);
927 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
930 * nvmem_device_put() - put alredy got nvmem device
932 * @nvmem: pointer to nvmem device that needs to be released.
934 void nvmem_device_put(struct nvmem_device *nvmem)
936 __nvmem_device_put(nvmem);
938 EXPORT_SYMBOL_GPL(nvmem_device_put);
941 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
943 * @dev: Device that requests the nvmem device.
944 * @id: name id for the requested nvmem device.
946 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
947 * on success. The nvmem_cell will be freed by the automatically once the
950 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
952 struct nvmem_device **ptr, *nvmem;
954 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
956 return ERR_PTR(-ENOMEM);
958 nvmem = nvmem_device_get(dev, id);
959 if (!IS_ERR(nvmem)) {
961 devres_add(dev, ptr);
968 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
970 static struct nvmem_cell *
971 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
973 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
974 struct nvmem_cell_lookup *lookup;
975 struct nvmem_device *nvmem;
979 return ERR_PTR(-EINVAL);
981 dev_id = dev_name(dev);
983 mutex_lock(&nvmem_lookup_mutex);
985 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
986 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
987 (strcmp(lookup->con_id, con_id) == 0)) {
988 /* This is the right entry. */
989 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
992 /* Provider may not be registered yet. */
993 cell = ERR_CAST(nvmem);
997 cell = nvmem_find_cell_by_name(nvmem,
1000 __nvmem_device_put(nvmem);
1001 cell = ERR_PTR(-ENOENT);
1007 mutex_unlock(&nvmem_lookup_mutex);
1011 #if IS_ENABLED(CONFIG_OF)
1012 static struct nvmem_cell *
1013 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1015 struct nvmem_cell *iter, *cell = NULL;
1017 mutex_lock(&nvmem_mutex);
1018 list_for_each_entry(iter, &nvmem->cells, node) {
1019 if (np == iter->np) {
1024 mutex_unlock(&nvmem_mutex);
1030 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1032 * @np: Device tree node that uses the nvmem cell.
1033 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1034 * for the cell at index 0 (the lone cell with no accompanying
1035 * nvmem-cell-names property).
1037 * Return: Will be an ERR_PTR() on error or a valid pointer
1038 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1041 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1043 struct device_node *cell_np, *nvmem_np;
1044 struct nvmem_device *nvmem;
1045 struct nvmem_cell *cell;
1048 /* if cell name exists, find index to the name */
1050 index = of_property_match_string(np, "nvmem-cell-names", id);
1052 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1054 return ERR_PTR(-ENOENT);
1056 nvmem_np = of_get_next_parent(cell_np);
1058 return ERR_PTR(-EINVAL);
1060 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1061 of_node_put(nvmem_np);
1063 return ERR_CAST(nvmem);
1065 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1067 __nvmem_device_put(nvmem);
1068 return ERR_PTR(-ENOENT);
1073 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1077 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1079 * @dev: Device that requests the nvmem cell.
1080 * @id: nvmem cell name to get (this corresponds with the name from the
1081 * nvmem-cell-names property for DT systems and with the con_id from
1082 * the lookup entry for non-DT systems).
1084 * Return: Will be an ERR_PTR() on error or a valid pointer
1085 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1088 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1090 struct nvmem_cell *cell;
1092 if (dev->of_node) { /* try dt first */
1093 cell = of_nvmem_cell_get(dev->of_node, id);
1094 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1098 /* NULL cell id only allowed for device tree; invalid otherwise */
1100 return ERR_PTR(-EINVAL);
1102 return nvmem_cell_get_from_lookup(dev, id);
1104 EXPORT_SYMBOL_GPL(nvmem_cell_get);
1106 static void devm_nvmem_cell_release(struct device *dev, void *res)
1108 nvmem_cell_put(*(struct nvmem_cell **)res);
1112 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1114 * @dev: Device that requests the nvmem cell.
1115 * @id: nvmem cell name id to get.
1117 * Return: Will be an ERR_PTR() on error or a valid pointer
1118 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1119 * automatically once the device is freed.
1121 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1123 struct nvmem_cell **ptr, *cell;
1125 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1127 return ERR_PTR(-ENOMEM);
1129 cell = nvmem_cell_get(dev, id);
1130 if (!IS_ERR(cell)) {
1132 devres_add(dev, ptr);
1139 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1141 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1143 struct nvmem_cell **c = res;
1145 if (WARN_ON(!c || !*c))
1152 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1153 * from devm_nvmem_cell_get.
1155 * @dev: Device that requests the nvmem cell.
1156 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1158 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1162 ret = devres_release(dev, devm_nvmem_cell_release,
1163 devm_nvmem_cell_match, cell);
1167 EXPORT_SYMBOL(devm_nvmem_cell_put);
1170 * nvmem_cell_put() - Release previously allocated nvmem cell.
1172 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1174 void nvmem_cell_put(struct nvmem_cell *cell)
1176 struct nvmem_device *nvmem = cell->nvmem;
1178 __nvmem_device_put(nvmem);
1180 EXPORT_SYMBOL_GPL(nvmem_cell_put);
1182 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1185 int i, extra, bit_offset = cell->bit_offset;
1190 *b++ >>= bit_offset;
1192 /* setup rest of the bytes if any */
1193 for (i = 1; i < cell->bytes; i++) {
1194 /* Get bits from next byte and shift them towards msb */
1195 *p |= *b << (BITS_PER_BYTE - bit_offset);
1198 *b++ >>= bit_offset;
1201 /* point to the msb */
1202 p += cell->bytes - 1;
1205 /* result fits in less bytes */
1206 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1207 while (--extra >= 0)
1210 /* clear msb bits if any leftover in the last byte */
1211 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
1214 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1215 struct nvmem_cell *cell,
1216 void *buf, size_t *len)
1220 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1225 /* shift bits in-place */
1226 if (cell->bit_offset || cell->nbits)
1227 nvmem_shift_read_buffer_in_place(cell, buf);
1236 * nvmem_cell_read() - Read a given nvmem cell
1238 * @cell: nvmem cell to be read.
1239 * @len: pointer to length of cell which will be populated on successful read;
1242 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1243 * buffer should be freed by the consumer with a kfree().
1245 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1247 struct nvmem_device *nvmem = cell->nvmem;
1252 return ERR_PTR(-EINVAL);
1254 buf = kzalloc(cell->bytes, GFP_KERNEL);
1256 return ERR_PTR(-ENOMEM);
1258 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1266 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1268 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1271 struct nvmem_device *nvmem = cell->nvmem;
1272 int i, rc, nbits, bit_offset = cell->bit_offset;
1273 u8 v, *p, *buf, *b, pbyte, pbits;
1275 nbits = cell->nbits;
1276 buf = kzalloc(cell->bytes, GFP_KERNEL);
1278 return ERR_PTR(-ENOMEM);
1280 memcpy(buf, _buf, len);
1287 /* setup the first byte with lsb bits from nvmem */
1288 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1291 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1293 /* setup rest of the byte if any */
1294 for (i = 1; i < cell->bytes; i++) {
1295 /* Get last byte bits and shift them towards lsb */
1296 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1304 /* if it's not end on byte boundary */
1305 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1306 /* setup the last byte with msb bits from nvmem */
1307 rc = nvmem_reg_read(nvmem,
1308 cell->offset + cell->bytes - 1, &v, 1);
1311 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1322 * nvmem_cell_write() - Write to a given nvmem cell
1324 * @cell: nvmem cell to be written.
1325 * @buf: Buffer to be written.
1326 * @len: length of buffer to be written to nvmem cell.
1328 * Return: length of bytes written or negative on failure.
1330 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1332 struct nvmem_device *nvmem = cell->nvmem;
1335 if (!nvmem || nvmem->read_only ||
1336 (cell->bit_offset == 0 && len != cell->bytes))
1339 if (cell->bit_offset || cell->nbits) {
1340 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1342 return PTR_ERR(buf);
1345 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1347 /* free the tmp buffer */
1348 if (cell->bit_offset || cell->nbits)
1356 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1358 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1359 void *val, size_t count)
1361 struct nvmem_cell *cell;
1365 cell = nvmem_cell_get(dev, cell_id);
1367 return PTR_ERR(cell);
1369 buf = nvmem_cell_read(cell, &len);
1371 nvmem_cell_put(cell);
1372 return PTR_ERR(buf);
1376 nvmem_cell_put(cell);
1379 memcpy(val, buf, count);
1381 nvmem_cell_put(cell);
1387 * nvmem_cell_read_u8() - Read a cell value as a u8
1389 * @dev: Device that requests the nvmem cell.
1390 * @cell_id: Name of nvmem cell to read.
1391 * @val: pointer to output value.
1393 * Return: 0 on success or negative errno.
1395 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1397 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1399 EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1402 * nvmem_cell_read_u16() - Read a cell value as a u16
1404 * @dev: Device that requests the nvmem cell.
1405 * @cell_id: Name of nvmem cell to read.
1406 * @val: pointer to output value.
1408 * Return: 0 on success or negative errno.
1410 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1412 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1414 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1417 * nvmem_cell_read_u32() - Read a cell value as a u32
1419 * @dev: Device that requests the nvmem cell.
1420 * @cell_id: Name of nvmem cell to read.
1421 * @val: pointer to output value.
1423 * Return: 0 on success or negative errno.
1425 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1427 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1429 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1432 * nvmem_cell_read_u64() - Read a cell value as a u64
1434 * @dev: Device that requests the nvmem cell.
1435 * @cell_id: Name of nvmem cell to read.
1436 * @val: pointer to output value.
1438 * Return: 0 on success or negative errno.
1440 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1442 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1444 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1447 * nvmem_device_cell_read() - Read a given nvmem device and cell
1449 * @nvmem: nvmem device to read from.
1450 * @info: nvmem cell info to be read.
1451 * @buf: buffer pointer which will be populated on successful read.
1453 * Return: length of successful bytes read on success and negative
1454 * error code on error.
1456 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1457 struct nvmem_cell_info *info, void *buf)
1459 struct nvmem_cell cell;
1466 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1470 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1476 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1479 * nvmem_device_cell_write() - Write cell to a given nvmem device
1481 * @nvmem: nvmem device to be written to.
1482 * @info: nvmem cell info to be written.
1483 * @buf: buffer to be written to cell.
1485 * Return: length of bytes written or negative error code on failure.
1487 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1488 struct nvmem_cell_info *info, void *buf)
1490 struct nvmem_cell cell;
1496 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1500 return nvmem_cell_write(&cell, buf, cell.bytes);
1502 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1505 * nvmem_device_read() - Read from a given nvmem device
1507 * @nvmem: nvmem device to read from.
1508 * @offset: offset in nvmem device.
1509 * @bytes: number of bytes to read.
1510 * @buf: buffer pointer which will be populated on successful read.
1512 * Return: length of successful bytes read on success and negative
1513 * error code on error.
1515 int nvmem_device_read(struct nvmem_device *nvmem,
1516 unsigned int offset,
1517 size_t bytes, void *buf)
1524 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1531 EXPORT_SYMBOL_GPL(nvmem_device_read);
1534 * nvmem_device_write() - Write cell to a given nvmem device
1536 * @nvmem: nvmem device to be written to.
1537 * @offset: offset in nvmem device.
1538 * @bytes: number of bytes to write.
1539 * @buf: buffer to be written.
1541 * Return: length of bytes written or negative error code on failure.
1543 int nvmem_device_write(struct nvmem_device *nvmem,
1544 unsigned int offset,
1545 size_t bytes, void *buf)
1552 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1560 EXPORT_SYMBOL_GPL(nvmem_device_write);
1563 * nvmem_add_cell_table() - register a table of cell info entries
1565 * @table: table of cell info entries
1567 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1569 mutex_lock(&nvmem_cell_mutex);
1570 list_add_tail(&table->node, &nvmem_cell_tables);
1571 mutex_unlock(&nvmem_cell_mutex);
1573 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1576 * nvmem_del_cell_table() - remove a previously registered cell info table
1578 * @table: table of cell info entries
1580 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1582 mutex_lock(&nvmem_cell_mutex);
1583 list_del(&table->node);
1584 mutex_unlock(&nvmem_cell_mutex);
1586 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1589 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1591 * @entries: array of cell lookup entries
1592 * @nentries: number of cell lookup entries in the array
1594 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1598 mutex_lock(&nvmem_lookup_mutex);
1599 for (i = 0; i < nentries; i++)
1600 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1601 mutex_unlock(&nvmem_lookup_mutex);
1603 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1606 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1609 * @entries: array of cell lookup entries
1610 * @nentries: number of cell lookup entries in the array
1612 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1616 mutex_lock(&nvmem_lookup_mutex);
1617 for (i = 0; i < nentries; i++)
1618 list_del(&entries[i].node);
1619 mutex_unlock(&nvmem_lookup_mutex);
1621 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1624 * nvmem_dev_name() - Get the name of a given nvmem device.
1626 * @nvmem: nvmem device.
1628 * Return: name of the nvmem device.
1630 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1632 return dev_name(&nvmem->dev);
1634 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1636 static int __init nvmem_init(void)
1638 return bus_register(&nvmem_bus_type);
1641 static void __exit nvmem_exit(void)
1643 bus_unregister(&nvmem_bus_type);
1646 subsys_initcall(nvmem_init);
1647 module_exit(nvmem_exit);
1649 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1650 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1651 MODULE_DESCRIPTION("nvmem Driver Core");
1652 MODULE_LICENSE("GPL v2");