Lines Matching full:nvmem
3 * nvmem framework core.
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
20 #include "nvmem.h"
29 struct nvmem_device *nvmem; member
45 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, in nvmem_reg_read() argument
48 if (nvmem->reg_read) in nvmem_reg_read()
49 return nvmem->reg_read(nvmem->priv, offset, val, bytes); in nvmem_reg_read()
54 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset, in nvmem_reg_write() argument
57 if (nvmem->reg_write) in nvmem_reg_write()
58 return nvmem->reg_write(nvmem->priv, offset, val, bytes); in nvmem_reg_write()
65 struct nvmem_device *nvmem = to_nvmem_device(dev); in nvmem_release() local
67 ida_simple_remove(&nvmem_ida, nvmem->id); in nvmem_release()
68 kfree(nvmem); in nvmem_release()
76 .name = "nvmem",
117 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) in nvmem_device_remove_all_cells() argument
121 list_for_each_entry_safe(cell, p, &nvmem->cells, node) in nvmem_device_remove_all_cells()
128 list_add_tail(&cell->node, &cell->nvmem->cells); in nvmem_cell_add()
133 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell() argument
137 cell->nvmem = nvmem; in nvmem_cell_info_to_nvmem_cell()
149 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_cell_info_to_nvmem_cell()
150 dev_err(&nvmem->dev, in nvmem_cell_info_to_nvmem_cell()
151 "cell %s unaligned to nvmem stride %d\n", in nvmem_cell_info_to_nvmem_cell()
152 cell->name, nvmem->stride); in nvmem_cell_info_to_nvmem_cell()
160 * nvmem_add_cells() - Add cell information to an nvmem device
162 * @nvmem: nvmem device to add cells to.
163 * @info: nvmem cell info to add to the device
168 static int nvmem_add_cells(struct nvmem_device *nvmem, in nvmem_add_cells() argument
186 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); in nvmem_add_cells()
209 * nvmem_register_notifier() - Register a notifier block for nvmem events.
211 * @nb: notifier block to be called on nvmem events.
222 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
234 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem) in nvmem_add_cells_from_table() argument
243 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) { in nvmem_add_cells_from_table()
253 rval = nvmem_cell_info_to_nvmem_cell(nvmem, in nvmem_add_cells_from_table()
272 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id) in nvmem_find_cell_by_name() argument
277 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_name()
288 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem) in nvmem_add_cells_from_of() argument
291 struct device *dev = &nvmem->dev; in nvmem_add_cells_from_of()
301 dev_err(dev, "nvmem: invalid reg on %pOF\n", child); in nvmem_add_cells_from_of()
309 cell->nvmem = nvmem; in nvmem_add_cells_from_of()
326 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_add_cells_from_of()
327 dev_err(dev, "cell %s unaligned to nvmem stride %d\n", in nvmem_add_cells_from_of()
328 cell->name, nvmem->stride); in nvmem_add_cells_from_of()
342 * nvmem_register() - Register a nvmem device for given nvmem_config.
343 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
345 * @config: nvmem device configuration with which nvmem device is created.
353 struct nvmem_device *nvmem; in nvmem_register() local
359 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); in nvmem_register()
360 if (!nvmem) in nvmem_register()
365 kfree(nvmem); in nvmem_register()
369 kref_init(&nvmem->refcnt); in nvmem_register()
370 INIT_LIST_HEAD(&nvmem->cells); in nvmem_register()
372 nvmem->id = rval; in nvmem_register()
373 nvmem->owner = config->owner; in nvmem_register()
374 if (!nvmem->owner && config->dev->driver) in nvmem_register()
375 nvmem->owner = config->dev->driver->owner; in nvmem_register()
376 nvmem->stride = config->stride ?: 1; in nvmem_register()
377 nvmem->word_size = config->word_size ?: 1; in nvmem_register()
378 nvmem->size = config->size; in nvmem_register()
379 nvmem->dev.type = &nvmem_provider_type; in nvmem_register()
380 nvmem->dev.bus = &nvmem_bus_type; in nvmem_register()
381 nvmem->dev.parent = config->dev; in nvmem_register()
382 nvmem->priv = config->priv; in nvmem_register()
383 nvmem->type = config->type; in nvmem_register()
384 nvmem->reg_read = config->reg_read; in nvmem_register()
385 nvmem->reg_write = config->reg_write; in nvmem_register()
387 nvmem->dev.of_node = config->dev->of_node; in nvmem_register()
390 dev_set_name(&nvmem->dev, "%s", config->name); in nvmem_register()
392 dev_set_name(&nvmem->dev, "%s%d", in nvmem_register()
393 config->name ? : "nvmem", in nvmem_register()
394 config->name ? config->id : nvmem->id); in nvmem_register()
397 nvmem->read_only = device_property_present(config->dev, "read-only") || in nvmem_register()
398 config->read_only || !nvmem->reg_write; in nvmem_register()
400 nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config); in nvmem_register()
402 device_initialize(&nvmem->dev); in nvmem_register()
404 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); in nvmem_register()
406 rval = device_add(&nvmem->dev); in nvmem_register()
411 rval = nvmem_sysfs_setup_compat(nvmem, config); in nvmem_register()
417 rval = nvmem_add_cells(nvmem, config->cells, config->ncells); in nvmem_register()
422 rval = nvmem_add_cells_from_table(nvmem); in nvmem_register()
426 rval = nvmem_add_cells_from_of(nvmem); in nvmem_register()
430 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); in nvmem_register()
432 return nvmem; in nvmem_register()
435 nvmem_device_remove_all_cells(nvmem); in nvmem_register()
438 nvmem_sysfs_remove_compat(nvmem, config); in nvmem_register()
440 device_del(&nvmem->dev); in nvmem_register()
442 put_device(&nvmem->dev); in nvmem_register()
450 struct nvmem_device *nvmem; in nvmem_device_release() local
452 nvmem = container_of(kref, struct nvmem_device, refcnt); in nvmem_device_release()
454 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem); in nvmem_device_release()
456 if (nvmem->flags & FLAG_COMPAT) in nvmem_device_release()
457 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_device_release()
459 nvmem_device_remove_all_cells(nvmem); in nvmem_device_release()
460 device_del(&nvmem->dev); in nvmem_device_release()
461 put_device(&nvmem->dev); in nvmem_device_release()
465 * nvmem_unregister() - Unregister previously registered nvmem device
467 * @nvmem: Pointer to previously registered nvmem device.
469 void nvmem_unregister(struct nvmem_device *nvmem) in nvmem_unregister() argument
471 kref_put(&nvmem->refcnt, nvmem_device_release); in nvmem_unregister()
481 * devm_nvmem_register() - Register a managed nvmem device for given
483 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
485 * @dev: Device that uses the nvmem device.
486 * @config: nvmem device configuration with which nvmem device is created.
494 struct nvmem_device **ptr, *nvmem; in devm_nvmem_register() local
500 nvmem = nvmem_register(config); in devm_nvmem_register()
502 if (!IS_ERR(nvmem)) { in devm_nvmem_register()
503 *ptr = nvmem; in devm_nvmem_register()
509 return nvmem; in devm_nvmem_register()
521 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
524 * @dev: Device that uses the nvmem device.
525 * @nvmem: Pointer to previously registered nvmem device.
529 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_unregister() argument
531 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); in devm_nvmem_unregister()
538 struct nvmem_device *nvmem = NULL; in __nvmem_device_get() local
541 nvmem = np ? of_nvmem_find(np) : nvmem_find(nvmem_name); in __nvmem_device_get()
543 if (!nvmem) in __nvmem_device_get()
546 if (!try_module_get(nvmem->owner)) { in __nvmem_device_get()
547 dev_err(&nvmem->dev, in __nvmem_device_get()
549 nvmem_dev_name(nvmem)); in __nvmem_device_get()
551 put_device(&nvmem->dev); in __nvmem_device_get()
555 kref_get(&nvmem->refcnt); in __nvmem_device_get()
557 return nvmem; in __nvmem_device_get()
560 static void __nvmem_device_put(struct nvmem_device *nvmem) in __nvmem_device_put() argument
562 put_device(&nvmem->dev); in __nvmem_device_put()
563 module_put(nvmem->owner); in __nvmem_device_put()
564 kref_put(&nvmem->refcnt, nvmem_device_release); in __nvmem_device_put()
569 * of_nvmem_device_get() - Get nvmem device from a given id
571 * @np: Device tree node that uses the nvmem device.
572 * @id: nvmem name from nvmem-names property.
584 index = of_property_match_string(np, "nvmem-names", id); in of_nvmem_device_get()
586 nvmem_np = of_parse_phandle(np, "nvmem", index); in of_nvmem_device_get()
596 * nvmem_device_get() - Get nvmem device from a given id
598 * @dev: Device that uses the nvmem device.
599 * @dev_name: name of the requested nvmem device.
607 struct nvmem_device *nvmem; in nvmem_device_get() local
609 nvmem = of_nvmem_device_get(dev->of_node, dev_name); in nvmem_device_get()
611 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) in nvmem_device_get()
612 return nvmem; in nvmem_device_get()
622 struct nvmem_device **nvmem = res; in devm_nvmem_device_match() local
624 if (WARN_ON(!nvmem || !*nvmem)) in devm_nvmem_device_match()
627 return *nvmem == data; in devm_nvmem_device_match()
636 * devm_nvmem_device_put() - put alredy got nvmem device
638 * @dev: Device that uses the nvmem device.
639 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
642 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_device_put() argument
647 devm_nvmem_device_match, nvmem); in devm_nvmem_device_put()
654 * nvmem_device_put() - put alredy got nvmem device
656 * @nvmem: pointer to nvmem device that needs to be released.
658 void nvmem_device_put(struct nvmem_device *nvmem) in nvmem_device_put() argument
660 __nvmem_device_put(nvmem); in nvmem_device_put()
665 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
667 * @dev: Device that requests the nvmem device.
668 * @id: name id for the requested nvmem device.
676 struct nvmem_device **ptr, *nvmem; in devm_nvmem_device_get() local
682 nvmem = nvmem_device_get(dev, id); in devm_nvmem_device_get()
683 if (!IS_ERR(nvmem)) { in devm_nvmem_device_get()
684 *ptr = nvmem; in devm_nvmem_device_get()
690 return nvmem; in devm_nvmem_device_get()
699 struct nvmem_device *nvmem; in nvmem_cell_get_from_lookup() local
713 nvmem = __nvmem_device_get(NULL, lookup->nvmem_name); in nvmem_cell_get_from_lookup()
714 if (IS_ERR(nvmem)) { in nvmem_cell_get_from_lookup()
716 cell = ERR_CAST(nvmem); in nvmem_cell_get_from_lookup()
720 cell = nvmem_find_cell_by_name(nvmem, in nvmem_cell_get_from_lookup()
723 __nvmem_device_put(nvmem); in nvmem_cell_get_from_lookup()
736 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np) in nvmem_find_cell_by_node() argument
741 list_for_each_entry(iter, &nvmem->cells, node) { in nvmem_find_cell_by_node()
753 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
755 * @np: Device tree node that uses the nvmem cell.
756 * @id: nvmem cell name from nvmem-cell-names property, or NULL
758 * nvmem-cell-names property).
767 struct nvmem_device *nvmem; in of_nvmem_cell_get() local
773 index = of_property_match_string(np, "nvmem-cell-names", id); in of_nvmem_cell_get()
775 cell_np = of_parse_phandle(np, "nvmem-cells", index); in of_nvmem_cell_get()
783 nvmem = __nvmem_device_get(nvmem_np, NULL); in of_nvmem_cell_get()
785 if (IS_ERR(nvmem)) in of_nvmem_cell_get()
786 return ERR_CAST(nvmem); in of_nvmem_cell_get()
788 cell = nvmem_find_cell_by_node(nvmem, cell_np); in of_nvmem_cell_get()
790 __nvmem_device_put(nvmem); in of_nvmem_cell_get()
800 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
802 * @dev: Device that requests the nvmem cell.
803 * @id: nvmem cell name to get (this corresponds with the name from the
804 * nvmem-cell-names property for DT systems and with the con_id from
835 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
837 * @dev: Device that requests the nvmem cell.
838 * @id: nvmem cell name id to get.
875 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
878 * @dev: Device that requests the nvmem cell.
879 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
893 * nvmem_cell_put() - Release previously allocated nvmem cell.
895 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
899 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_put() local
901 __nvmem_device_put(nvmem); in nvmem_cell_put()
937 static int __nvmem_cell_read(struct nvmem_device *nvmem, in __nvmem_cell_read() argument
943 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); in __nvmem_cell_read()
959 * nvmem_cell_read() - Read a given nvmem cell
961 * @cell: nvmem cell to be read.
970 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_read() local
974 if (!nvmem) in nvmem_cell_read()
981 rc = __nvmem_cell_read(nvmem, cell, buf, len); in nvmem_cell_read()
994 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_prepare_write_buffer() local
1010 /* setup the first byte with lsb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1011 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); in nvmem_cell_prepare_write_buffer()
1029 /* setup the last byte with msb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1030 rc = nvmem_reg_read(nvmem, in nvmem_cell_prepare_write_buffer()
1045 * nvmem_cell_write() - Write to a given nvmem cell
1047 * @cell: nvmem cell to be written.
1049 * @len: length of buffer to be written to nvmem cell.
1055 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_write() local
1058 if (!nvmem || nvmem->read_only || in nvmem_cell_write()
1068 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); in nvmem_cell_write()
1084 * @dev: Device that requests the nvmem cell.
1085 * @cell_id: Name of nvmem cell to read.
1121 * @dev: Device that requests the nvmem cell.
1122 * @cell_id: Name of nvmem cell to read.
1156 * nvmem_device_cell_read() - Read a given nvmem device and cell
1158 * @nvmem: nvmem device to read from.
1159 * @info: nvmem cell info to be read.
1165 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, in nvmem_device_cell_read() argument
1172 if (!nvmem) in nvmem_device_cell_read()
1175 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); in nvmem_device_cell_read()
1179 rc = __nvmem_cell_read(nvmem, &cell, buf, &len); in nvmem_device_cell_read()
1188 * nvmem_device_cell_write() - Write cell to a given nvmem device
1190 * @nvmem: nvmem device to be written to.
1191 * @info: nvmem cell info to be written.
1196 int nvmem_device_cell_write(struct nvmem_device *nvmem, in nvmem_device_cell_write() argument
1202 if (!nvmem) in nvmem_device_cell_write()
1205 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); in nvmem_device_cell_write()
1214 * nvmem_device_read() - Read from a given nvmem device
1216 * @nvmem: nvmem device to read from.
1217 * @offset: offset in nvmem device.
1224 int nvmem_device_read(struct nvmem_device *nvmem, in nvmem_device_read() argument
1230 if (!nvmem) in nvmem_device_read()
1233 rc = nvmem_reg_read(nvmem, offset, buf, bytes); in nvmem_device_read()
1243 * nvmem_device_write() - Write cell to a given nvmem device
1245 * @nvmem: nvmem device to be written to.
1246 * @offset: offset in nvmem device.
1252 int nvmem_device_write(struct nvmem_device *nvmem, in nvmem_device_write() argument
1258 if (!nvmem) in nvmem_device_write()
1261 rc = nvmem_reg_write(nvmem, offset, buf, bytes); in nvmem_device_write()
1333 * nvmem_dev_name() - Get the name of a given nvmem device.
1335 * @nvmem: nvmem device.
1337 * Return: name of the nvmem device.
1339 const char *nvmem_dev_name(struct nvmem_device *nvmem) in nvmem_dev_name() argument
1341 return dev_name(&nvmem->dev); in nvmem_dev_name()
1360 MODULE_DESCRIPTION("nvmem Driver Core");