Lines Matching refs:nvmem
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()
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()
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
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()
141 if (count < nvmem->word_size) 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()
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
172 nvmem = to_nvmem_device(dev); in bin_attr_nvmem_write()
175 if (pos >= nvmem->size) in bin_attr_nvmem_write()
178 if (!IS_ALIGNED(pos, nvmem->stride)) in bin_attr_nvmem_write()
181 if (count < nvmem->word_size) 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()
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 attr->size = nvmem->size; in nvmem_bin_attr_is_visible()
227 return nvmem_bin_attr_get_umode(nvmem); in nvmem_bin_attr_is_visible()
269 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, in nvmem_sysfs_setup_compat() argument
280 nvmem->eeprom = bin_attr_nvmem_eeprom_compat; in nvmem_sysfs_setup_compat()
281 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem); in nvmem_sysfs_setup_compat()
282 nvmem->eeprom.size = nvmem->size; in nvmem_sysfs_setup_compat()
284 nvmem->eeprom.attr.key = &eeprom_lock_key; in nvmem_sysfs_setup_compat()
286 nvmem->eeprom.private = &nvmem->dev; in nvmem_sysfs_setup_compat()
287 nvmem->base_dev = config->base_dev; in nvmem_sysfs_setup_compat()
289 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_setup_compat()
291 dev_err(&nvmem->dev, in nvmem_sysfs_setup_compat()
296 nvmem->flags |= FLAG_COMPAT; in nvmem_sysfs_setup_compat()
301 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, in nvmem_sysfs_remove_compat() argument
305 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_sysfs_remove_compat()
310 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, in nvmem_sysfs_setup_compat() argument
315 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, in nvmem_sysfs_remove_compat() argument
324 struct nvmem_device *nvmem = to_nvmem_device(dev); in nvmem_release() local
326 ida_free(&nvmem_ida, nvmem->id); in nvmem_release()
327 gpiod_put(nvmem->wp_gpio); in nvmem_release()
328 kfree(nvmem); in nvmem_release()
350 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) in nvmem_device_remove_all_cells() argument
354 list_for_each_entry_safe(cell, p, &nvmem->cells, node) in nvmem_device_remove_all_cells()
361 list_add_tail(&cell->node, &cell->nvmem->cells); in nvmem_cell_add()
366 static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell_nodup() argument
370 cell->nvmem = nvmem; in nvmem_cell_info_to_nvmem_cell_nodup()
382 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_cell_info_to_nvmem_cell_nodup()
383 dev_err(&nvmem->dev, in nvmem_cell_info_to_nvmem_cell_nodup()
385 cell->name ?: "<unknown>", nvmem->stride); in nvmem_cell_info_to_nvmem_cell_nodup()
392 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell() argument
398 err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell); in nvmem_cell_info_to_nvmem_cell()
418 static int nvmem_add_cells(struct nvmem_device *nvmem, in nvmem_add_cells() argument
436 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); in nvmem_add_cells()
484 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) in nvmem_add_cells_from_table() argument
493 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { in nvmem_add_cells_from_table()
503 rval = nvmem_cell_info_to_nvmem_cell(nvmem, in nvmem_add_cells_from_table()
522 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) in nvmem_find_cell_by_name() argument
527 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_name()
538 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) in nvmem_add_cells_from_of() argument
541 struct device *dev = &nvmem->dev; in nvmem_add_cells_from_of()
564 cell->nvmem = nvmem; in nvmem_add_cells_from_of()
580 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_add_cells_from_of()
582 cell->name, nvmem->stride); in nvmem_add_cells_from_of()
609 struct nvmem_device *nvmem; in nvmem_register() local
618 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); in nvmem_register()
619 if (!nvmem) in nvmem_register()
624 kfree(nvmem); in nvmem_register()
629 nvmem->wp_gpio = config->wp_gpio; in nvmem_register()
631 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", in nvmem_register()
633 if (IS_ERR(nvmem->wp_gpio)) { in nvmem_register()
634 ida_free(&nvmem_ida, nvmem->id); in nvmem_register()
635 rval = PTR_ERR(nvmem->wp_gpio); in nvmem_register()
636 kfree(nvmem); in nvmem_register()
640 kref_init(&nvmem->refcnt); in nvmem_register()
641 INIT_LIST_HEAD(&nvmem->cells); in nvmem_register()
643 nvmem->id = rval; in nvmem_register()
644 nvmem->owner = config->owner; in nvmem_register()
645 if (!nvmem->owner && config->dev->driver) in nvmem_register()
646 nvmem->owner = config->dev->driver->owner; in nvmem_register()
647 nvmem->stride = config->stride ?: 1; in nvmem_register()
648 nvmem->word_size = config->word_size ?: 1; in nvmem_register()
649 nvmem->size = config->size; in nvmem_register()
650 nvmem->dev.type = &nvmem_provider_type; in nvmem_register()
651 nvmem->dev.bus = &nvmem_bus_type; in nvmem_register()
652 nvmem->dev.parent = config->dev; in nvmem_register()
653 nvmem->root_only = config->root_only; in nvmem_register()
654 nvmem->priv = config->priv; in nvmem_register()
655 nvmem->type = config->type; in nvmem_register()
656 nvmem->reg_read = config->reg_read; in nvmem_register()
657 nvmem->reg_write = config->reg_write; in nvmem_register()
659 nvmem->dev.of_node = config->dev->of_node; in nvmem_register()
663 dev_set_name(&nvmem->dev, "%s", config->name); in nvmem_register()
666 dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id); in nvmem_register()
669 dev_set_name(&nvmem->dev, "%s%d", in nvmem_register()
671 config->name ? config->id : nvmem->id); in nvmem_register()
675 nvmem->read_only = device_property_present(config->dev, "read-only") || in nvmem_register()
676 config->read_only || !nvmem->reg_write; in nvmem_register()
679 nvmem->dev.groups = nvmem_dev_groups; in nvmem_register()
682 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); in nvmem_register()
684 rval = device_register(&nvmem->dev); in nvmem_register()
689 rval = nvmem_sysfs_setup_compat(nvmem, config); in nvmem_register()
695 rval = nvmem_add_cells(nvmem, config->cells, config->ncells); in nvmem_register()
700 rval = nvmem_add_cells_from_table(nvmem); in nvmem_register()
704 rval = nvmem_add_cells_from_of(nvmem); in nvmem_register()
708 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); in nvmem_register()
710 return nvmem; in nvmem_register()
713 nvmem_device_remove_all_cells(nvmem); in nvmem_register()
716 nvmem_sysfs_remove_compat(nvmem, config); in nvmem_register()
718 device_del(&nvmem->dev); in nvmem_register()
720 put_device(&nvmem->dev); in nvmem_register()
728 struct nvmem_device *nvmem; in nvmem_device_release() local
730 nvmem = container_of(kref, struct nvmem_device, refcnt); in nvmem_device_release()
732 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); in nvmem_device_release()
734 if (nvmem->flags & FLAG_COMPAT) in nvmem_device_release()
735 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_device_release()
737 nvmem_device_remove_all_cells(nvmem); in nvmem_device_release()
738 device_unregister(&nvmem->dev); in nvmem_device_release()
746 void nvmem_unregister(struct nvmem_device *nvmem) in nvmem_unregister() argument
748 kref_put(&nvmem->refcnt, nvmem_device_release); in nvmem_unregister()
771 struct nvmem_device **ptr, *nvmem; in devm_nvmem_register() local
777 nvmem = nvmem_register(config); in devm_nvmem_register()
779 if (!IS_ERR(nvmem)) { in devm_nvmem_register()
780 *ptr = nvmem; in devm_nvmem_register()
786 return nvmem; in devm_nvmem_register()
806 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_unregister() argument
808 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); in devm_nvmem_unregister()
815 struct nvmem_device *nvmem = NULL; in __nvmem_device_get() local
821 nvmem = to_nvmem_device(dev); in __nvmem_device_get()
823 if (!nvmem) in __nvmem_device_get()
826 if (!try_module_get(nvmem->owner)) { in __nvmem_device_get()
827 dev_err(&nvmem->dev, in __nvmem_device_get()
829 nvmem_dev_name(nvmem)); in __nvmem_device_get()
831 put_device(&nvmem->dev); in __nvmem_device_get()
835 kref_get(&nvmem->refcnt); in __nvmem_device_get()
837 return nvmem; in __nvmem_device_get()
840 static void __nvmem_device_put(struct nvmem_device *nvmem) in __nvmem_device_put() argument
842 put_device(&nvmem->dev); in __nvmem_device_put()
843 module_put(nvmem->owner); in __nvmem_device_put()
844 kref_put(&nvmem->refcnt, nvmem_device_release); in __nvmem_device_put()
861 struct nvmem_device *nvmem; in of_nvmem_device_get() local
871 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); in of_nvmem_device_get()
873 return nvmem; in of_nvmem_device_get()
890 struct nvmem_device *nvmem; in nvmem_device_get() local
892 nvmem = of_nvmem_device_get(dev->of_node, dev_name); in nvmem_device_get()
894 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) in nvmem_device_get()
895 return nvmem; in nvmem_device_get()
921 struct nvmem_device **nvmem = res; in devm_nvmem_device_match() local
923 if (WARN_ON(!nvmem || !*nvmem)) in devm_nvmem_device_match()
926 return *nvmem == data; in devm_nvmem_device_match()
941 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_device_put() argument
946 devm_nvmem_device_match, nvmem); in devm_nvmem_device_put()
957 void nvmem_device_put(struct nvmem_device *nvmem) in nvmem_device_put() argument
959 __nvmem_device_put(nvmem); in nvmem_device_put()
975 struct nvmem_device **ptr, *nvmem; in devm_nvmem_device_get() local
981 nvmem = nvmem_device_get(dev, id); in devm_nvmem_device_get()
982 if (!IS_ERR(nvmem)) { in devm_nvmem_device_get()
983 *ptr = nvmem; in devm_nvmem_device_get()
989 return nvmem; in devm_nvmem_device_get()
998 struct nvmem_device *nvmem; in nvmem_cell_get_from_lookup() local
1012 nvmem = __nvmem_device_get((void *)lookup->nvmem_name, in nvmem_cell_get_from_lookup()
1014 if (IS_ERR(nvmem)) { in nvmem_cell_get_from_lookup()
1016 cell = ERR_CAST(nvmem); in nvmem_cell_get_from_lookup()
1020 cell = nvmem_find_cell_by_name(nvmem, in nvmem_cell_get_from_lookup()
1023 __nvmem_device_put(nvmem); in nvmem_cell_get_from_lookup()
1036 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) in nvmem_find_cell_by_node() argument
1041 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_node()
1067 struct nvmem_device *nvmem; in of_nvmem_cell_get() local
1083 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node); in of_nvmem_cell_get()
1085 if (IS_ERR(nvmem)) in of_nvmem_cell_get()
1086 return ERR_CAST(nvmem); in of_nvmem_cell_get()
1088 cell = nvmem_find_cell_by_node(nvmem, cell_np); in of_nvmem_cell_get()
1090 __nvmem_device_put(nvmem); in of_nvmem_cell_get()
1199 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_put() local
1201 __nvmem_device_put(nvmem); in nvmem_cell_put()
1238 static int __nvmem_cell_read(struct nvmem_device *nvmem, in __nvmem_cell_read() argument
1244 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); in __nvmem_cell_read()
1271 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_read() local
1275 if (!nvmem) in nvmem_cell_read()
1282 rc = __nvmem_cell_read(nvmem, cell, buf, len); in nvmem_cell_read()
1295 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_prepare_write_buffer() local
1312 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); in nvmem_cell_prepare_write_buffer()
1331 rc = nvmem_reg_read(nvmem, in nvmem_cell_prepare_write_buffer()
1356 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_write() local
1359 if (!nvmem || nvmem->read_only || in nvmem_cell_write()
1369 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); in nvmem_cell_write()
1480 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, in nvmem_device_cell_read() argument
1487 if (!nvmem) in nvmem_device_cell_read()
1490 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); in nvmem_device_cell_read()
1494 rc = __nvmem_cell_read(nvmem, &cell, buf, &len); in nvmem_device_cell_read()
1511 int nvmem_device_cell_write(struct nvmem_device *nvmem, in nvmem_device_cell_write() argument
1517 if (!nvmem) in nvmem_device_cell_write()
1520 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); in nvmem_device_cell_write()
1539 int nvmem_device_read(struct nvmem_device *nvmem, in nvmem_device_read() argument
1545 if (!nvmem) in nvmem_device_read()
1548 rc = nvmem_reg_read(nvmem, offset, buf, bytes); in nvmem_device_read()
1567 int nvmem_device_write(struct nvmem_device *nvmem, in nvmem_device_write() argument
1573 if (!nvmem) in nvmem_device_write()
1576 rc = nvmem_reg_write(nvmem, offset, buf, bytes); in nvmem_device_write()
1654 const char *nvmem_dev_name(struct nvmem_device *nvmem) in nvmem_dev_name() argument
1656 return dev_name(&nvmem->dev); in nvmem_dev_name()