Lines Matching full:nvmem
2 * nvmem framework core.
23 #include <linux/nvmem-consumer.h>
24 #include <linux/nvmem-provider.h>
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
81 if (nvmem->reg_write) in nvmem_reg_write()
82 return nvmem->reg_write(nvmem->priv, offset, val, bytes); in nvmem_reg_write()
92 struct nvmem_device *nvmem; in bin_attr_nvmem_read() local
99 nvmem = to_nvmem_device(dev); in bin_attr_nvmem_read()
102 if (pos >= nvmem->size) in bin_attr_nvmem_read()
105 if (count < nvmem->word_size) in bin_attr_nvmem_read()
108 if (pos + count > nvmem->size) in bin_attr_nvmem_read()
109 count = nvmem->size - pos; in bin_attr_nvmem_read()
111 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_read()
113 rc = nvmem_reg_read(nvmem, pos, buf, count); in bin_attr_nvmem_read()
126 struct nvmem_device *nvmem; in bin_attr_nvmem_write() local
133 nvmem = to_nvmem_device(dev); in bin_attr_nvmem_write()
136 if (pos >= nvmem->size) in bin_attr_nvmem_write()
139 if (count < nvmem->word_size) in bin_attr_nvmem_write()
142 if (pos + count > nvmem->size) in bin_attr_nvmem_write()
143 count = nvmem->size - pos; in bin_attr_nvmem_write()
145 count = round_down(count, nvmem->word_size); in bin_attr_nvmem_write()
147 rc = nvmem_reg_write(nvmem, pos, buf, count); in bin_attr_nvmem_write()
158 .name = "nvmem",
182 .name = "nvmem",
205 .name = "nvmem",
229 .name = "nvmem",
251 struct nvmem_device *nvmem = to_nvmem_device(dev); in nvmem_release() local
253 ida_simple_remove(&nvmem_ida, nvmem->id); in nvmem_release()
254 kfree(nvmem); in nvmem_release()
262 .name = "nvmem",
310 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem) in nvmem_device_remove_all_cells() argument
317 if (cell->nvmem == nvmem) in nvmem_device_remove_all_cells()
329 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, in nvmem_cell_info_to_nvmem_cell() argument
333 cell->nvmem = nvmem; in nvmem_cell_info_to_nvmem_cell()
345 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in nvmem_cell_info_to_nvmem_cell()
346 dev_err(&nvmem->dev, in nvmem_cell_info_to_nvmem_cell()
347 "cell %s unaligned to nvmem stride %d\n", in nvmem_cell_info_to_nvmem_cell()
348 cell->name, nvmem->stride); in nvmem_cell_info_to_nvmem_cell()
356 * nvmem_add_cells() - Add cell information to an nvmem device
358 * @nvmem: nvmem device to add cells to.
359 * @info: nvmem cell info to add to the device
364 int nvmem_add_cells(struct nvmem_device *nvmem, in nvmem_add_cells() argument
382 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]); in nvmem_add_cells()
410 static int nvmem_setup_compat(struct nvmem_device *nvmem, in nvmem_setup_compat() argument
418 if (nvmem->read_only) { in nvmem_setup_compat()
420 nvmem->eeprom = bin_attr_ro_root_nvmem; in nvmem_setup_compat()
422 nvmem->eeprom = bin_attr_ro_nvmem; in nvmem_setup_compat()
425 nvmem->eeprom = bin_attr_rw_root_nvmem; in nvmem_setup_compat()
427 nvmem->eeprom = bin_attr_rw_nvmem; in nvmem_setup_compat()
429 nvmem->eeprom.attr.name = "eeprom"; in nvmem_setup_compat()
430 nvmem->eeprom.size = nvmem->size; in nvmem_setup_compat()
432 nvmem->eeprom.attr.key = &eeprom_lock_key; in nvmem_setup_compat()
434 nvmem->eeprom.private = &nvmem->dev; in nvmem_setup_compat()
435 nvmem->base_dev = config->base_dev; in nvmem_setup_compat()
437 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_setup_compat()
439 dev_err(&nvmem->dev, in nvmem_setup_compat()
444 nvmem->flags |= FLAG_COMPAT; in nvmem_setup_compat()
450 * nvmem_register() - Register a nvmem device for given nvmem_config.
451 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
453 * @config: nvmem device configuration with which nvmem device is created.
461 struct nvmem_device *nvmem; in nvmem_register() local
467 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL); in nvmem_register()
468 if (!nvmem) in nvmem_register()
473 kfree(nvmem); in nvmem_register()
477 nvmem->id = rval; in nvmem_register()
478 nvmem->owner = config->owner; in nvmem_register()
479 if (!nvmem->owner && config->dev->driver) in nvmem_register()
480 nvmem->owner = config->dev->driver->owner; in nvmem_register()
481 nvmem->stride = config->stride ?: 1; in nvmem_register()
482 nvmem->word_size = config->word_size ?: 1; in nvmem_register()
483 nvmem->size = config->size; in nvmem_register()
484 nvmem->dev.type = &nvmem_provider_type; in nvmem_register()
485 nvmem->dev.bus = &nvmem_bus_type; in nvmem_register()
486 nvmem->dev.parent = config->dev; in nvmem_register()
487 nvmem->priv = config->priv; in nvmem_register()
488 nvmem->reg_read = config->reg_read; in nvmem_register()
489 nvmem->reg_write = config->reg_write; in nvmem_register()
490 nvmem->dev.of_node = config->dev->of_node; in nvmem_register()
493 dev_set_name(&nvmem->dev, "%s", config->name); in nvmem_register()
495 dev_set_name(&nvmem->dev, "%s%d", in nvmem_register()
496 config->name ? : "nvmem", in nvmem_register()
497 config->name ? config->id : nvmem->id); in nvmem_register()
500 nvmem->read_only = device_property_present(config->dev, "read-only") | in nvmem_register()
504 nvmem->dev.groups = nvmem->read_only ? in nvmem_register()
508 nvmem->dev.groups = nvmem->read_only ? in nvmem_register()
512 device_initialize(&nvmem->dev); in nvmem_register()
514 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); in nvmem_register()
516 rval = device_add(&nvmem->dev); in nvmem_register()
521 rval = nvmem_setup_compat(nvmem, config); in nvmem_register()
527 rval = nvmem_add_cells(nvmem, config->cells, config->ncells); in nvmem_register()
532 return nvmem; in nvmem_register()
536 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_register()
538 device_del(&nvmem->dev); in nvmem_register()
540 put_device(&nvmem->dev); in nvmem_register()
547 * nvmem_unregister() - Unregister previously registered nvmem device
549 * @nvmem: Pointer to previously registered nvmem device.
553 int nvmem_unregister(struct nvmem_device *nvmem) in nvmem_unregister() argument
556 if (nvmem->users) { in nvmem_unregister()
562 if (nvmem->flags & FLAG_COMPAT) in nvmem_unregister()
563 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); in nvmem_unregister()
565 nvmem_device_remove_all_cells(nvmem); in nvmem_unregister()
566 device_del(&nvmem->dev); in nvmem_unregister()
567 put_device(&nvmem->dev); in nvmem_unregister()
579 * devm_nvmem_register() - Register a managed nvmem device for given
581 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
583 * @dev: Device that uses the nvmem device.
584 * @config: nvmem device configuration with which nvmem device is created.
592 struct nvmem_device **ptr, *nvmem; in devm_nvmem_register() local
598 nvmem = nvmem_register(config); in devm_nvmem_register()
600 if (!IS_ERR(nvmem)) { in devm_nvmem_register()
601 *ptr = nvmem; in devm_nvmem_register()
607 return nvmem; in devm_nvmem_register()
619 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
622 * @dev: Device that uses the nvmem device.
623 * @nvmem: Pointer to previously registered nvmem device.
627 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_unregister() argument
629 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem); in devm_nvmem_unregister()
638 struct nvmem_device *nvmem = NULL; in __nvmem_device_get() local
643 nvmem = of_nvmem_find(np); in __nvmem_device_get()
644 if (!nvmem) { in __nvmem_device_get()
652 nvmem = cell->nvmem; in __nvmem_device_get()
656 if (!nvmem) { in __nvmem_device_get()
662 nvmem->users++; in __nvmem_device_get()
665 if (!try_module_get(nvmem->owner)) { in __nvmem_device_get()
666 dev_err(&nvmem->dev, in __nvmem_device_get()
668 nvmem->name); in __nvmem_device_get()
671 nvmem->users--; in __nvmem_device_get()
677 return nvmem; in __nvmem_device_get()
680 static void __nvmem_device_put(struct nvmem_device *nvmem) in __nvmem_device_put() argument
682 module_put(nvmem->owner); in __nvmem_device_put()
684 nvmem->users--; in __nvmem_device_put()
702 * of_nvmem_device_get() - Get nvmem device from a given id
704 * @np: Device tree node that uses the nvmem device.
705 * @id: nvmem name from nvmem-names property.
716 index = of_property_match_string(np, "nvmem-names", id); in of_nvmem_device_get()
718 nvmem_np = of_parse_phandle(np, "nvmem", index); in of_nvmem_device_get()
728 * nvmem_device_get() - Get nvmem device from a given id
730 * @dev: Device that uses the nvmem device.
731 * @dev_name: name of the requested nvmem device.
739 struct nvmem_device *nvmem; in nvmem_device_get() local
741 nvmem = of_nvmem_device_get(dev->of_node, dev_name); in nvmem_device_get()
743 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER) in nvmem_device_get()
744 return nvmem; in nvmem_device_get()
754 struct nvmem_device **nvmem = res; in devm_nvmem_device_match() local
756 if (WARN_ON(!nvmem || !*nvmem)) in devm_nvmem_device_match()
759 return *nvmem == data; in devm_nvmem_device_match()
768 * devm_nvmem_device_put() - put alredy got nvmem device
770 * @dev: Device that uses the nvmem device.
771 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
774 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem) in devm_nvmem_device_put() argument
779 devm_nvmem_device_match, nvmem); in devm_nvmem_device_put()
786 * nvmem_device_put() - put alredy got nvmem device
788 * @nvmem: pointer to nvmem device that needs to be released.
790 void nvmem_device_put(struct nvmem_device *nvmem) in nvmem_device_put() argument
792 __nvmem_device_put(nvmem); in nvmem_device_put()
797 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
799 * @dev: Device that requests the nvmem device.
800 * @id: name id for the requested nvmem device.
808 struct nvmem_device **ptr, *nvmem; in devm_nvmem_device_get() local
814 nvmem = nvmem_device_get(dev, id); in devm_nvmem_device_get()
815 if (!IS_ERR(nvmem)) { in devm_nvmem_device_get()
816 *ptr = nvmem; in devm_nvmem_device_get()
822 return nvmem; in devm_nvmem_device_get()
829 struct nvmem_device *nvmem; in nvmem_cell_get_from_list() local
831 nvmem = __nvmem_device_get(NULL, &cell, cell_id); in nvmem_cell_get_from_list()
832 if (IS_ERR(nvmem)) in nvmem_cell_get_from_list()
833 return ERR_CAST(nvmem); in nvmem_cell_get_from_list()
840 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
842 * @np: Device tree node that uses the nvmem cell.
843 * @name: nvmem cell name from nvmem-cell-names property, or NULL
845 * nvmem-cell-names property).
856 struct nvmem_device *nvmem; in of_nvmem_cell_get() local
863 index = of_property_match_string(np, "nvmem-cell-names", name); in of_nvmem_cell_get()
865 cell_np = of_parse_phandle(np, "nvmem-cells", index); in of_nvmem_cell_get()
873 nvmem = __nvmem_device_get(nvmem_np, NULL, NULL); in of_nvmem_cell_get()
875 if (IS_ERR(nvmem)) in of_nvmem_cell_get()
876 return ERR_CAST(nvmem); in of_nvmem_cell_get()
880 dev_err(&nvmem->dev, "nvmem: invalid reg on %pOF\n", in of_nvmem_cell_get()
892 cell->nvmem = nvmem; in of_nvmem_cell_get()
907 if (!IS_ALIGNED(cell->offset, nvmem->stride)) { in of_nvmem_cell_get()
908 dev_err(&nvmem->dev, in of_nvmem_cell_get()
909 "cell %s unaligned to nvmem stride %d\n", in of_nvmem_cell_get()
910 cell->name, nvmem->stride); in of_nvmem_cell_get()
923 __nvmem_device_put(nvmem); in of_nvmem_cell_get()
931 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
933 * @dev: Device that requests the nvmem cell.
934 * @cell_id: nvmem cell name to get.
964 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
966 * @dev: Device that requests the nvmem cell.
967 * @id: nvmem cell name id to get.
1004 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1007 * @dev: Device that requests the nvmem cell.
1008 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1022 * nvmem_cell_put() - Release previously allocated nvmem cell.
1024 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1028 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_put() local
1030 __nvmem_device_put(nvmem); in nvmem_cell_put()
1067 static int __nvmem_cell_read(struct nvmem_device *nvmem, in __nvmem_cell_read() argument
1073 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes); in __nvmem_cell_read()
1089 * nvmem_cell_read() - Read a given nvmem cell
1091 * @cell: nvmem cell to be read.
1100 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_read() local
1104 if (!nvmem) in nvmem_cell_read()
1111 rc = __nvmem_cell_read(nvmem, cell, buf, len); in nvmem_cell_read()
1124 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_prepare_write_buffer() local
1140 /* setup the first byte with lsb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1141 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1); in nvmem_cell_prepare_write_buffer()
1159 /* setup the last byte with msb bits from nvmem */ in nvmem_cell_prepare_write_buffer()
1160 rc = nvmem_reg_read(nvmem, in nvmem_cell_prepare_write_buffer()
1175 * nvmem_cell_write() - Write to a given nvmem cell
1177 * @cell: nvmem cell to be written.
1179 * @len: length of buffer to be written to nvmem cell.
1185 struct nvmem_device *nvmem = cell->nvmem; in nvmem_cell_write() local
1188 if (!nvmem || nvmem->read_only || in nvmem_cell_write()
1198 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes); in nvmem_cell_write()
1214 * @dev: Device that requests the nvmem cell.
1215 * @cell_id: Name of nvmem cell to read.
1249 * nvmem_device_cell_read() - Read a given nvmem device and cell
1251 * @nvmem: nvmem device to read from.
1252 * @info: nvmem cell info to be read.
1258 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, in nvmem_device_cell_read() argument
1265 if (!nvmem) in nvmem_device_cell_read()
1268 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); in nvmem_device_cell_read()
1272 rc = __nvmem_cell_read(nvmem, &cell, buf, &len); in nvmem_device_cell_read()
1281 * nvmem_device_cell_write() - Write cell to a given nvmem device
1283 * @nvmem: nvmem device to be written to.
1284 * @info: nvmem cell info to be written.
1289 int nvmem_device_cell_write(struct nvmem_device *nvmem, in nvmem_device_cell_write() argument
1295 if (!nvmem) in nvmem_device_cell_write()
1298 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); in nvmem_device_cell_write()
1307 * nvmem_device_read() - Read from a given nvmem device
1309 * @nvmem: nvmem device to read from.
1310 * @offset: offset in nvmem device.
1317 int nvmem_device_read(struct nvmem_device *nvmem, in nvmem_device_read() argument
1323 if (!nvmem) in nvmem_device_read()
1326 rc = nvmem_reg_read(nvmem, offset, buf, bytes); in nvmem_device_read()
1336 * nvmem_device_write() - Write cell to a given nvmem device
1338 * @nvmem: nvmem device to be written to.
1339 * @offset: offset in nvmem device.
1345 int nvmem_device_write(struct nvmem_device *nvmem, in nvmem_device_write() argument
1351 if (!nvmem) in nvmem_device_write()
1354 rc = nvmem_reg_write(nvmem, offset, buf, bytes); in nvmem_device_write()
1379 MODULE_DESCRIPTION("nvmem Driver Core");