Lines Matching +full:nvmem +full:- +full:cells

1 // SPDX-License-Identifier: GPL-2.0
3 * nvmem framework core.
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
36 struct list_head cells; member
54 struct nvmem_device *nvmem; member
69 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, in nvmem_reg_read() argument
72 if (nvmem->reg_read) in nvmem_reg_read()
73 return nvmem->reg_read(nvmem->priv, offset, val, bytes); in nvmem_reg_read()
75 return -EINVAL; in nvmem_reg_read()
78 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, in nvmem_reg_write() argument
83 if (nvmem->reg_write) { in nvmem_reg_write()
84 gpiod_set_value_cansleep(nvmem->wp_gpio, 0); in nvmem_reg_write()
85 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes); in nvmem_reg_write()
86 gpiod_set_value_cansleep(nvmem->wp_gpio, 1); in nvmem_reg_write()
90 return -EINVAL; in nvmem_reg_write()
108 struct nvmem_device *nvmem = to_nvmem_device(dev); in type_show() local
110 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]); in type_show()
125 struct nvmem_device *nvmem; in bin_attr_nvmem_read() local
128 if (attr->private) in bin_attr_nvmem_read()
129 dev = attr->private; in bin_attr_nvmem_read()
132 nvmem = to_nvmem_device(dev); in bin_attr_nvmem_read()
135 if (pos >= nvmem->size) in bin_attr_nvmem_read()
138 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_read()
139 return -EINVAL; in bin_attr_nvmem_read()
141 if (count < nvmem->word_size) in bin_attr_nvmem_read()
142 return -EINVAL; in bin_attr_nvmem_read()
144 if (pos + count > nvmem->size) in bin_attr_nvmem_read()
145 count = nvmem->size - pos; in bin_attr_nvmem_read()
147 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_read()
149 if (!nvmem->reg_read) in bin_attr_nvmem_read()
150 return -EPERM; in bin_attr_nvmem_read()
152 rc = nvmem_reg_read(nvmem, pos, buf, count); in bin_attr_nvmem_read()
165 struct nvmem_device *nvmem; in bin_attr_nvmem_write() local
168 if (attr->private) in bin_attr_nvmem_write()
169 dev = attr->private; in bin_attr_nvmem_write()
172 nvmem = to_nvmem_device(dev); in bin_attr_nvmem_write()
175 if (pos >= nvmem->size) in bin_attr_nvmem_write()
176 return -EFBIG; in bin_attr_nvmem_write()
178 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_write()
179 return -EINVAL; in bin_attr_nvmem_write()
181 if (count < nvmem->word_size) in bin_attr_nvmem_write()
182 return -EINVAL; in bin_attr_nvmem_write()
184 if (pos + count > nvmem->size) in bin_attr_nvmem_write()
185 count = nvmem->size - pos; in bin_attr_nvmem_write()
187 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_write()
189 if (!nvmem->reg_write) in bin_attr_nvmem_write()
190 return -EPERM; in bin_attr_nvmem_write()
192 rc = nvmem_reg_write(nvmem, pos, buf, count); in bin_attr_nvmem_write()
200 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem) in nvmem_bin_attr_get_umode() argument
204 if (!nvmem->root_only) in nvmem_bin_attr_get_umode()
207 if (!nvmem->read_only) in nvmem_bin_attr_get_umode()
210 if (!nvmem->reg_write) in nvmem_bin_attr_get_umode()
213 if (!nvmem->reg_read) in nvmem_bin_attr_get_umode()
223 struct nvmem_device *nvmem = to_nvmem_device(dev); in nvmem_bin_attr_is_visible() local
225 return nvmem_bin_attr_get_umode(nvmem); in nvmem_bin_attr_is_visible()
231 .name = "nvmem",
263 * nvmem_setup_compat() - Create an additional binary entry in
267 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, in nvmem_sysfs_setup_compat() argument
272 if (!config->compat) in nvmem_sysfs_setup_compat()
275 if (!config->base_dev) in nvmem_sysfs_setup_compat()
276 return -EINVAL; in nvmem_sysfs_setup_compat()
278 nvmem->eeprom = bin_attr_nvmem_eeprom_compat; in nvmem_sysfs_setup_compat()
279 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem); in nvmem_sysfs_setup_compat()
280 nvmem->eeprom.size = nvmem->size; in nvmem_sysfs_setup_compat()
282 nvmem->eeprom.attr.key = &eeprom_lock_key; in nvmem_sysfs_setup_compat()
284 nvmem->eeprom.private = &nvmem->dev; in nvmem_sysfs_setup_compat()
285 nvmem->base_dev = config->base_dev; in nvmem_sysfs_setup_compat()
287 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_setup_compat()
289 dev_err(&nvmem->dev, in nvmem_sysfs_setup_compat()
294 nvmem->flags |= FLAG_COMPAT; in nvmem_sysfs_setup_compat()
299 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, in nvmem_sysfs_remove_compat() argument
302 if (config->compat) in nvmem_sysfs_remove_compat()
303 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_remove_compat()
308 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, in nvmem_sysfs_setup_compat() argument
311 return -ENOSYS; in nvmem_sysfs_setup_compat()
313 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, in nvmem_sysfs_remove_compat() argument
322 struct nvmem_device *nvmem = to_nvmem_device(dev); in nvmem_release() local
324 ida_free(&nvmem_ida, nvmem->id); in nvmem_release()
325 gpiod_put(nvmem->wp_gpio); in nvmem_release()
326 kfree(nvmem); in nvmem_release()
334 .name = "nvmem",
341 list_del(&cell->node); in nvmem_cell_drop()
343 of_node_put(cell->np); in nvmem_cell_drop()
344 kfree_const(cell->name); in nvmem_cell_drop()
348 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) in nvmem_device_remove_all_cells() argument
352 list_for_each_entry_safe(cell, p, &nvmem->cells, node) in nvmem_device_remove_all_cells()
359 list_add_tail(&cell->node, &cell->nvmem->cells); in nvmem_cell_add()
364 static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell_nodup() argument
368 cell->nvmem = nvmem; in nvmem_cell_info_to_nvmem_cell_nodup()
369 cell->offset = info->offset; in nvmem_cell_info_to_nvmem_cell_nodup()
370 cell->bytes = info->bytes; in nvmem_cell_info_to_nvmem_cell_nodup()
371 cell->name = info->name; in nvmem_cell_info_to_nvmem_cell_nodup()
373 cell->bit_offset = info->bit_offset; in nvmem_cell_info_to_nvmem_cell_nodup()
374 cell->nbits = info->nbits; in nvmem_cell_info_to_nvmem_cell_nodup()
376 if (cell->nbits) in nvmem_cell_info_to_nvmem_cell_nodup()
377 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset, in nvmem_cell_info_to_nvmem_cell_nodup()
380 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_cell_info_to_nvmem_cell_nodup()
381 dev_err(&nvmem->dev, in nvmem_cell_info_to_nvmem_cell_nodup()
382 "cell %s unaligned to nvmem stride %d\n", in nvmem_cell_info_to_nvmem_cell_nodup()
383 cell->name ?: "<unknown>", nvmem->stride); in nvmem_cell_info_to_nvmem_cell_nodup()
384 return -EINVAL; in nvmem_cell_info_to_nvmem_cell_nodup()
390 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell() argument
396 err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell); in nvmem_cell_info_to_nvmem_cell()
400 cell->name = kstrdup_const(info->name, GFP_KERNEL); in nvmem_cell_info_to_nvmem_cell()
401 if (!cell->name) in nvmem_cell_info_to_nvmem_cell()
402 return -ENOMEM; in nvmem_cell_info_to_nvmem_cell()
408 * nvmem_add_cells() - Add cell information to an nvmem device
410 * @nvmem: nvmem device to add cells to.
411 * @info: nvmem cell info to add to the device
412 * @ncells: number of cells in info
416 static int nvmem_add_cells(struct nvmem_device *nvmem, in nvmem_add_cells() argument
420 struct nvmem_cell **cells; in nvmem_add_cells() local
423 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL); in nvmem_add_cells()
424 if (!cells) in nvmem_add_cells()
425 return -ENOMEM; in nvmem_add_cells()
428 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL); in nvmem_add_cells()
429 if (!cells[i]) { in nvmem_add_cells()
430 rval = -ENOMEM; in nvmem_add_cells()
434 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); in nvmem_add_cells()
436 kfree(cells[i]); in nvmem_add_cells()
440 nvmem_cell_add(cells[i]); in nvmem_add_cells()
444 kfree(cells); in nvmem_add_cells()
448 while (i--) in nvmem_add_cells()
449 nvmem_cell_drop(cells[i]); in nvmem_add_cells()
451 kfree(cells); in nvmem_add_cells()
457 * nvmem_register_notifier() - Register a notifier block for nvmem events.
459 * @nb: notifier block to be called on nvmem events.
470 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
482 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) in nvmem_add_cells_from_table() argument
491 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { in nvmem_add_cells_from_table()
492 for (i = 0; i < table->ncells; i++) { in nvmem_add_cells_from_table()
493 info = &table->cells[i]; in nvmem_add_cells_from_table()
497 rval = -ENOMEM; in nvmem_add_cells_from_table()
501 rval = nvmem_cell_info_to_nvmem_cell(nvmem, in nvmem_add_cells_from_table()
520 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) in nvmem_find_cell_by_name() argument
525 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_name()
526 if (strcmp(cell_id, iter->name) == 0) { in nvmem_find_cell_by_name()
536 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) in nvmem_add_cells_from_of() argument
539 struct device *dev = &nvmem->dev; in nvmem_add_cells_from_of()
544 parent = dev->of_node; in nvmem_add_cells_from_of()
549 dev_err(dev, "nvmem: invalid reg on %pOF\n", child); in nvmem_add_cells_from_of()
550 return -EINVAL; in nvmem_add_cells_from_of()
555 return -ENOMEM; in nvmem_add_cells_from_of()
557 cell->nvmem = nvmem; in nvmem_add_cells_from_of()
558 cell->np = of_node_get(child); in nvmem_add_cells_from_of()
559 cell->offset = be32_to_cpup(addr++); in nvmem_add_cells_from_of()
560 cell->bytes = be32_to_cpup(addr); in nvmem_add_cells_from_of()
561 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child); in nvmem_add_cells_from_of()
565 cell->bit_offset = be32_to_cpup(addr++); in nvmem_add_cells_from_of()
566 cell->nbits = be32_to_cpup(addr); in nvmem_add_cells_from_of()
569 if (cell->nbits) in nvmem_add_cells_from_of()
570 cell->bytes = DIV_ROUND_UP( in nvmem_add_cells_from_of()
571 cell->nbits + cell->bit_offset, in nvmem_add_cells_from_of()
574 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_add_cells_from_of()
575 dev_err(dev, "cell %s unaligned to nvmem stride %d\n", in nvmem_add_cells_from_of()
576 cell->name, nvmem->stride); in nvmem_add_cells_from_of()
577 /* Cells already added will be freed later. */ in nvmem_add_cells_from_of()
578 kfree_const(cell->name); in nvmem_add_cells_from_of()
580 return -EINVAL; in nvmem_add_cells_from_of()
590 * nvmem_register() - Register a nvmem device for given nvmem_config.
591 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
593 * @config: nvmem device configuration with which nvmem device is created.
601 struct nvmem_device *nvmem; in nvmem_register() local
604 if (!config->dev) in nvmem_register()
605 return ERR_PTR(-EINVAL); in nvmem_register()
607 if (!config->reg_read && !config->reg_write) in nvmem_register()
608 return ERR_PTR(-EINVAL); in nvmem_register()
610 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); in nvmem_register()
611 if (!nvmem) in nvmem_register()
612 return ERR_PTR(-ENOMEM); in nvmem_register()
616 kfree(nvmem); in nvmem_register()
620 if (config->wp_gpio) in nvmem_register()
621 nvmem->wp_gpio = config->wp_gpio; in nvmem_register()
623 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", in nvmem_register()
625 if (IS_ERR(nvmem->wp_gpio)) { in nvmem_register()
626 ida_free(&nvmem_ida, nvmem->id); in nvmem_register()
627 rval = PTR_ERR(nvmem->wp_gpio); in nvmem_register()
628 kfree(nvmem); in nvmem_register()
632 kref_init(&nvmem->refcnt); in nvmem_register()
633 INIT_LIST_HEAD(&nvmem->cells); in nvmem_register()
635 nvmem->id = rval; in nvmem_register()
636 nvmem->owner = config->owner; in nvmem_register()
637 if (!nvmem->owner && config->dev->driver) in nvmem_register()
638 nvmem->owner = config->dev->driver->owner; in nvmem_register()
639 nvmem->stride = config->stride ?: 1; in nvmem_register()
640 nvmem->word_size = config->word_size ?: 1; in nvmem_register()
641 nvmem->size = config->size; in nvmem_register()
642 nvmem->dev.type = &nvmem_provider_type; in nvmem_register()
643 nvmem->dev.bus = &nvmem_bus_type; in nvmem_register()
644 nvmem->dev.parent = config->dev; in nvmem_register()
645 nvmem->root_only = config->root_only; in nvmem_register()
646 nvmem->priv = config->priv; in nvmem_register()
647 nvmem->type = config->type; in nvmem_register()
648 nvmem->reg_read = config->reg_read; in nvmem_register()
649 nvmem->reg_write = config->reg_write; in nvmem_register()
650 if (!config->no_of_node) in nvmem_register()
651 nvmem->dev.of_node = config->dev->of_node; in nvmem_register()
653 switch (config->id) { in nvmem_register()
655 dev_set_name(&nvmem->dev, "%s", config->name); in nvmem_register()
658 dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); in nvmem_register()
661 dev_set_name(&nvmem->dev, "%s%d", in nvmem_register()
662 config->name ? : "nvmem", in nvmem_register()
663 config->name ? config->id : nvmem->id); in nvmem_register()
667 nvmem->read_only = device_property_present(config->dev, "read-only") || in nvmem_register()
668 config->read_only || !nvmem->reg_write; in nvmem_register()
671 nvmem->dev.groups = nvmem_dev_groups; in nvmem_register()
674 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); in nvmem_register()
676 rval = device_register(&nvmem->dev); in nvmem_register()
680 if (config->compat) { in nvmem_register()
681 rval = nvmem_sysfs_setup_compat(nvmem, config); in nvmem_register()
686 if (config->cells) { in nvmem_register()
687 rval = nvmem_add_cells(nvmem, config->cells, config->ncells); in nvmem_register()
692 rval = nvmem_add_cells_from_table(nvmem); in nvmem_register()
696 rval = nvmem_add_cells_from_of(nvmem); in nvmem_register()
700 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); in nvmem_register()
702 return nvmem; in nvmem_register()
705 nvmem_device_remove_all_cells(nvmem); in nvmem_register()
707 if (config->compat) in nvmem_register()
708 nvmem_sysfs_remove_compat(nvmem, config); in nvmem_register()
710 device_del(&nvmem->dev); in nvmem_register()
712 put_device(&nvmem->dev); in nvmem_register()
720 struct nvmem_device *nvmem; in nvmem_device_release() local
722 nvmem = container_of(kref, struct nvmem_device, refcnt); in nvmem_device_release()
724 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); in nvmem_device_release()
726 if (nvmem->flags & FLAG_COMPAT) in nvmem_device_release()
727 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_device_release()
729 nvmem_device_remove_all_cells(nvmem); in nvmem_device_release()
730 device_unregister(&nvmem->dev); in nvmem_device_release()
734 * nvmem_unregister() - Unregister previously registered nvmem device
736 * @nvmem: Pointer to previously registered nvmem device.
738 void nvmem_unregister(struct nvmem_device *nvmem) in nvmem_unregister() argument
740 kref_put(&nvmem->refcnt, nvmem_device_release); in nvmem_unregister()
750 * devm_nvmem_register() - Register a managed nvmem device for given
752 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
754 * @dev: Device that uses the nvmem device.
755 * @config: nvmem device configuration with which nvmem device is created.
763 struct nvmem_device **ptr, *nvmem; in devm_nvmem_register() local
767 return ERR_PTR(-ENOMEM); in devm_nvmem_register()
769 nvmem = nvmem_register(config); in devm_nvmem_register()
771 if (!IS_ERR(nvmem)) { in devm_nvmem_register()
772 *ptr = nvmem; in devm_nvmem_register()
778 return nvmem; in devm_nvmem_register()
790 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
793 * @dev: Device that uses the nvmem device.
794 * @nvmem: Pointer to previously registered nvmem device.
798 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_unregister() argument
800 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); in devm_nvmem_unregister()
807 struct nvmem_device *nvmem = NULL; in __nvmem_device_get() local
813 nvmem = to_nvmem_device(dev); in __nvmem_device_get()
815 if (!nvmem) in __nvmem_device_get()
816 return ERR_PTR(-EPROBE_DEFER); in __nvmem_device_get()
818 if (!try_module_get(nvmem->owner)) { in __nvmem_device_get()
819 dev_err(&nvmem->dev, in __nvmem_device_get()
821 nvmem_dev_name(nvmem)); in __nvmem_device_get()
823 put_device(&nvmem->dev); in __nvmem_device_get()
824 return ERR_PTR(-EINVAL); in __nvmem_device_get()
827 kref_get(&nvmem->refcnt); in __nvmem_device_get()
829 return nvmem; in __nvmem_device_get()
832 static void __nvmem_device_put(struct nvmem_device *nvmem) in __nvmem_device_put() argument
834 put_device(&nvmem->dev); in __nvmem_device_put()
835 module_put(nvmem->owner); in __nvmem_device_put()
836 kref_put(&nvmem->refcnt, nvmem_device_release); in __nvmem_device_put()
841 * of_nvmem_device_get() - Get nvmem device from a given id
843 * @np: Device tree node that uses the nvmem device.
844 * @id: nvmem name from nvmem-names property.
853 struct nvmem_device *nvmem; in of_nvmem_device_get() local
857 index = of_property_match_string(np, "nvmem-names", id); in of_nvmem_device_get()
859 nvmem_np = of_parse_phandle(np, "nvmem", index); in of_nvmem_device_get()
861 return ERR_PTR(-ENOENT); in of_nvmem_device_get()
863 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); in of_nvmem_device_get()
865 return nvmem; in of_nvmem_device_get()
871 * nvmem_device_get() - Get nvmem device from a given id
873 * @dev: Device that uses the nvmem device.
874 * @dev_name: name of the requested nvmem device.
881 if (dev->of_node) { /* try dt first */ in nvmem_device_get()
882 struct nvmem_device *nvmem; in nvmem_device_get() local
884 nvmem = of_nvmem_device_get(dev->of_node, dev_name); in nvmem_device_get()
886 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) in nvmem_device_get()
887 return nvmem; in nvmem_device_get()
896 * nvmem_device_find() - Find nvmem device with matching function
913 struct nvmem_device **nvmem = res; in devm_nvmem_device_match() local
915 if (WARN_ON(!nvmem || !*nvmem)) in devm_nvmem_device_match()
918 return *nvmem == data; in devm_nvmem_device_match()
927 * devm_nvmem_device_put() - put alredy got nvmem device
929 * @dev: Device that uses the nvmem device.
930 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
933 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_device_put() argument
938 devm_nvmem_device_match, nvmem); in devm_nvmem_device_put()
945 * nvmem_device_put() - put alredy got nvmem device
947 * @nvmem: pointer to nvmem device that needs to be released.
949 void nvmem_device_put(struct nvmem_device *nvmem) in nvmem_device_put() argument
951 __nvmem_device_put(nvmem); in nvmem_device_put()
956 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
958 * @dev: Device that requests the nvmem device.
959 * @id: name id for the requested nvmem device.
967 struct nvmem_device **ptr, *nvmem; in devm_nvmem_device_get() local
971 return ERR_PTR(-ENOMEM); in devm_nvmem_device_get()
973 nvmem = nvmem_device_get(dev, id); in devm_nvmem_device_get()
974 if (!IS_ERR(nvmem)) { in devm_nvmem_device_get()
975 *ptr = nvmem; in devm_nvmem_device_get()
981 return nvmem; in devm_nvmem_device_get()
988 struct nvmem_cell *cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup()
990 struct nvmem_device *nvmem; in nvmem_cell_get_from_lookup() local
994 return ERR_PTR(-EINVAL); in nvmem_cell_get_from_lookup()
1001 if ((strcmp(lookup->dev_id, dev_id) == 0) && in nvmem_cell_get_from_lookup()
1002 (strcmp(lookup->con_id, con_id) == 0)) { in nvmem_cell_get_from_lookup()
1004 nvmem = __nvmem_device_get((void *)lookup->nvmem_name, in nvmem_cell_get_from_lookup()
1006 if (IS_ERR(nvmem)) { in nvmem_cell_get_from_lookup()
1008 cell = ERR_CAST(nvmem); in nvmem_cell_get_from_lookup()
1012 cell = nvmem_find_cell_by_name(nvmem, in nvmem_cell_get_from_lookup()
1013 lookup->cell_name); in nvmem_cell_get_from_lookup()
1015 __nvmem_device_put(nvmem); in nvmem_cell_get_from_lookup()
1016 cell = ERR_PTR(-ENOENT); in nvmem_cell_get_from_lookup()
1028 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) in nvmem_find_cell_by_node() argument
1033 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_node()
1034 if (np == iter->np) { in nvmem_find_cell_by_node()
1045 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1047 * @np: Device tree node that uses the nvmem cell.
1048 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1050 * nvmem-cell-names property).
1059 struct nvmem_device *nvmem; in of_nvmem_cell_get() local
1065 index = of_property_match_string(np, "nvmem-cell-names", id); in of_nvmem_cell_get()
1067 cell_np = of_parse_phandle(np, "nvmem-cells", index); in of_nvmem_cell_get()
1069 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1073 return ERR_PTR(-EINVAL); in of_nvmem_cell_get()
1075 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); in of_nvmem_cell_get()
1077 if (IS_ERR(nvmem)) in of_nvmem_cell_get()
1078 return ERR_CAST(nvmem); in of_nvmem_cell_get()
1080 cell = nvmem_find_cell_by_node(nvmem, cell_np); in of_nvmem_cell_get()
1082 __nvmem_device_put(nvmem); in of_nvmem_cell_get()
1083 return ERR_PTR(-ENOENT); in of_nvmem_cell_get()
1092 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1094 * @dev: Device that requests the nvmem cell.
1095 * @id: nvmem cell name to get (this corresponds with the name from the
1096 * nvmem-cell-names property for DT systems and with the con_id from
1097 * the lookup entry for non-DT systems).
1107 if (dev->of_node) { /* try dt first */ in nvmem_cell_get()
1108 cell = of_nvmem_cell_get(dev->of_node, id); in nvmem_cell_get()
1109 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER) in nvmem_cell_get()
1115 return ERR_PTR(-EINVAL); in nvmem_cell_get()
1127 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1129 * @dev: Device that requests the nvmem cell.
1130 * @id: nvmem cell name id to get.
1142 return ERR_PTR(-ENOMEM); in devm_nvmem_cell_get()
1167 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1170 * @dev: Device that requests the nvmem cell.
1171 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1185 * nvmem_cell_put() - Release previously allocated nvmem cell.
1187 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1191 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_put() local
1193 __nvmem_device_put(nvmem); in nvmem_cell_put()
1200 int i, extra, bit_offset = cell->bit_offset; in nvmem_shift_read_buffer_in_place()
1208 for (i = 1; i < cell->bytes; i++) { in nvmem_shift_read_buffer_in_place()
1210 *p |= *b << (BITS_PER_BYTE - bit_offset); in nvmem_shift_read_buffer_in_place()
1217 p += cell->bytes - 1; in nvmem_shift_read_buffer_in_place()
1221 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE); in nvmem_shift_read_buffer_in_place()
1222 while (--extra >= 0) in nvmem_shift_read_buffer_in_place()
1223 *p-- = 0; in nvmem_shift_read_buffer_in_place()
1226 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0); in nvmem_shift_read_buffer_in_place()
1229 static int __nvmem_cell_read(struct nvmem_device *nvmem, in __nvmem_cell_read() argument
1235 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); in __nvmem_cell_read()
1240 /* shift bits in-place */ in __nvmem_cell_read()
1241 if (cell->bit_offset || cell->nbits) in __nvmem_cell_read()
1245 *len = cell->bytes; in __nvmem_cell_read()
1251 * nvmem_cell_read() - Read a given nvmem cell
1253 * @cell: nvmem cell to be read.
1262 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_read() local
1266 if (!nvmem) in nvmem_cell_read()
1267 return ERR_PTR(-EINVAL); in nvmem_cell_read()
1269 buf = kzalloc(cell->bytes, GFP_KERNEL); in nvmem_cell_read()
1271 return ERR_PTR(-ENOMEM); in nvmem_cell_read()
1273 rc = __nvmem_cell_read(nvmem, cell, buf, len); in nvmem_cell_read()
1286 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_prepare_write_buffer() local
1287 int i, rc, nbits, bit_offset = cell->bit_offset; in nvmem_cell_prepare_write_buffer()
1290 nbits = cell->nbits; in nvmem_cell_prepare_write_buffer()
1291 buf = kzalloc(cell->bytes, GFP_KERNEL); in nvmem_cell_prepare_write_buffer()
1293 return ERR_PTR(-ENOMEM); in nvmem_cell_prepare_write_buffer()
1302 /* setup the first byte with lsb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1303 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); in nvmem_cell_prepare_write_buffer()
1306 *b++ |= GENMASK(bit_offset - 1, 0) & v; in nvmem_cell_prepare_write_buffer()
1309 for (i = 1; i < cell->bytes; i++) { in nvmem_cell_prepare_write_buffer()
1311 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset); in nvmem_cell_prepare_write_buffer()
1321 /* setup the last byte with msb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1322 rc = nvmem_reg_read(nvmem, in nvmem_cell_prepare_write_buffer()
1323 cell->offset + cell->bytes - 1, &v, 1); in nvmem_cell_prepare_write_buffer()
1337 * nvmem_cell_write() - Write to a given nvmem cell
1339 * @cell: nvmem cell to be written.
1341 * @len: length of buffer to be written to nvmem cell.
1347 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_write() local
1350 if (!nvmem || nvmem->read_only || in nvmem_cell_write()
1351 (cell->bit_offset == 0 && len != cell->bytes)) in nvmem_cell_write()
1352 return -EINVAL; in nvmem_cell_write()
1354 if (cell->bit_offset || cell->nbits) { in nvmem_cell_write()
1360 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); in nvmem_cell_write()
1363 if (cell->bit_offset || cell->nbits) in nvmem_cell_write()
1392 return -EINVAL; in nvmem_cell_read_common()
1402 * nvmem_cell_read_u8() - Read a cell value as a u8
1404 * @dev: Device that requests the nvmem cell.
1405 * @cell_id: Name of nvmem cell to read.
1417 * nvmem_cell_read_u16() - Read a cell value as a u16
1419 * @dev: Device that requests the nvmem cell.
1420 * @cell_id: Name of nvmem cell to read.
1432 * nvmem_cell_read_u32() - Read a cell value as a u32
1434 * @dev: Device that requests the nvmem cell.
1435 * @cell_id: Name of nvmem cell to read.
1447 * nvmem_cell_read_u64() - Read a cell value as a u64
1449 * @dev: Device that requests the nvmem cell.
1450 * @cell_id: Name of nvmem cell to read.
1462 * nvmem_device_cell_read() - Read a given nvmem device and cell
1464 * @nvmem: nvmem device to read from.
1465 * @info: nvmem cell info to be read.
1471 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, in nvmem_device_cell_read() argument
1478 if (!nvmem) in nvmem_device_cell_read()
1479 return -EINVAL; in nvmem_device_cell_read()
1481 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); in nvmem_device_cell_read()
1485 rc = __nvmem_cell_read(nvmem, &cell, buf, &len); in nvmem_device_cell_read()
1494 * nvmem_device_cell_write() - Write cell to a given nvmem device
1496 * @nvmem: nvmem device to be written to.
1497 * @info: nvmem cell info to be written.
1502 int nvmem_device_cell_write(struct nvmem_device *nvmem, in nvmem_device_cell_write() argument
1508 if (!nvmem) in nvmem_device_cell_write()
1509 return -EINVAL; in nvmem_device_cell_write()
1511 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); in nvmem_device_cell_write()
1520 * nvmem_device_read() - Read from a given nvmem device
1522 * @nvmem: nvmem device to read from.
1523 * @offset: offset in nvmem device.
1530 int nvmem_device_read(struct nvmem_device *nvmem, in nvmem_device_read() argument
1536 if (!nvmem) in nvmem_device_read()
1537 return -EINVAL; in nvmem_device_read()
1539 rc = nvmem_reg_read(nvmem, offset, buf, bytes); in nvmem_device_read()
1549 * nvmem_device_write() - Write cell to a given nvmem device
1551 * @nvmem: nvmem device to be written to.
1552 * @offset: offset in nvmem device.
1558 int nvmem_device_write(struct nvmem_device *nvmem, in nvmem_device_write() argument
1564 if (!nvmem) in nvmem_device_write()
1565 return -EINVAL; in nvmem_device_write()
1567 rc = nvmem_reg_write(nvmem, offset, buf, bytes); in nvmem_device_write()
1578 * nvmem_add_cell_table() - register a table of cell info entries
1585 list_add_tail(&table->node, &nvmem_cell_tables); in nvmem_add_cell_table()
1591 * nvmem_del_cell_table() - remove a previously registered cell info table
1598 list_del(&table->node); in nvmem_del_cell_table()
1604 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1621 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1639 * nvmem_dev_name() - Get the name of a given nvmem device.
1641 * @nvmem: nvmem device.
1643 * Return: name of the nvmem device.
1645 const char *nvmem_dev_name(struct nvmem_device *nvmem) in nvmem_dev_name() argument
1647 return dev_name(&nvmem->dev); in nvmem_dev_name()
1665 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1666 MODULE_DESCRIPTION("nvmem Driver Core");