Lines Matching refs:nvmem

3  * nvmem framework core.
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
54 struct nvmem_device *nvmem;
69 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
72 if (nvmem->reg_read)
73 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
78 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
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);
108 struct nvmem_device *nvmem = to_nvmem_device(dev);
110 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
125 struct nvmem_device *nvmem;
132 nvmem = to_nvmem_device(dev);
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);
165 struct nvmem_device *nvmem;
172 nvmem = to_nvmem_device(dev);
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)
223 struct nvmem_device *nvmem = to_nvmem_device(dev);
225 attr->size = nvmem->size;
227 return nvmem_bin_attr_get_umode(nvmem);
233 .name = "nvmem",
269 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
280 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
281 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
282 nvmem->eeprom.size = nvmem->size;
284 nvmem->eeprom.attr.key = &eeprom_lock_key;
286 nvmem->eeprom.private = &nvmem->dev;
287 nvmem->base_dev = config->base_dev;
289 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
291 dev_err(&nvmem->dev,
296 nvmem->flags |= FLAG_COMPAT;
301 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
305 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
310 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
315 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
324 struct nvmem_device *nvmem = to_nvmem_device(dev);
326 ida_free(&nvmem_ida, nvmem->id);
327 gpiod_put(nvmem->wp_gpio);
328 kfree(nvmem);
336 .name = "nvmem",
350 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
354 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
361 list_add_tail(&cell->node, &cell->nvmem->cells);
366 static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem,
370 cell->nvmem = nvmem;
382 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
383 dev_err(&nvmem->dev,
384 "cell %s unaligned to nvmem stride %d\n",
385 cell->name ?: "<unknown>", nvmem->stride);
392 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
398 err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell);
410 * nvmem_add_cells() - Add cell information to an nvmem device
412 * @nvmem: nvmem device to add cells to.
413 * @info: nvmem cell info to add to the device
418 static int nvmem_add_cells(struct nvmem_device *nvmem,
436 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
459 * nvmem_register_notifier() - Register a notifier block for nvmem events.
461 * @nb: notifier block to be called on nvmem events.
472 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
484 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
493 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
503 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
522 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
527 list_for_each_entry(iter, &nvmem->cells, node) {
538 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
541 struct device *dev = &nvmem->dev;
553 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
564 cell->nvmem = nvmem;
580 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
581 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
582 cell->name, nvmem->stride);
598 * nvmem_register() - Register a nvmem device for given nvmem_config.
599 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
601 * @config: nvmem device configuration with which nvmem device is created.
609 struct nvmem_device *nvmem;
618 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
619 if (!nvmem)
624 kfree(nvmem);
628 nvmem->id = rval;
630 nvmem->dev.type = &nvmem_provider_type;
631 nvmem->dev.bus = &nvmem_bus_type;
632 nvmem->dev.parent = config->dev;
634 device_initialize(&nvmem->dev);
637 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
639 if (IS_ERR(nvmem->wp_gpio)) {
640 rval = PTR_ERR(nvmem->wp_gpio);
641 nvmem->wp_gpio = NULL;
645 kref_init(&nvmem->refcnt);
646 INIT_LIST_HEAD(&nvmem->cells);
648 nvmem->owner = config->owner;
649 if (!nvmem->owner && config->dev->driver)
650 nvmem->owner = config->dev->driver->owner;
651 nvmem->stride = config->stride ?: 1;
652 nvmem->word_size = config->word_size ?: 1;
653 nvmem->size = config->size;
654 nvmem->root_only = config->root_only;
655 nvmem->priv = config->priv;
656 nvmem->type = config->type;
657 nvmem->reg_read = config->reg_read;
658 nvmem->reg_write = config->reg_write;
660 nvmem->dev.of_node = config->dev->of_node;
664 rval = dev_set_name(&nvmem->dev, "%s", config->name);
667 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
670 rval = dev_set_name(&nvmem->dev, "%s%d",
671 config->name ? : "nvmem",
672 config->name ? config->id : nvmem->id);
679 nvmem->read_only = device_property_present(config->dev, "read-only") ||
680 config->read_only || !nvmem->reg_write;
683 nvmem->dev.groups = nvmem_dev_groups;
687 rval = nvmem_sysfs_setup_compat(nvmem, config);
693 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
698 rval = nvmem_add_cells_from_table(nvmem);
702 rval = nvmem_add_cells_from_of(nvmem);
706 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
708 rval = device_add(&nvmem->dev);
712 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
714 return nvmem;
717 nvmem_device_remove_all_cells(nvmem);
719 nvmem_sysfs_remove_compat(nvmem, config);
721 put_device(&nvmem->dev);
729 struct nvmem_device *nvmem;
731 nvmem = container_of(kref, struct nvmem_device, refcnt);
733 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
735 if (nvmem->flags & FLAG_COMPAT)
736 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
738 nvmem_device_remove_all_cells(nvmem);
739 device_unregister(&nvmem->dev);
743 * nvmem_unregister() - Unregister previously registered nvmem device
745 * @nvmem: Pointer to previously registered nvmem device.
747 void nvmem_unregister(struct nvmem_device *nvmem)
749 kref_put(&nvmem->refcnt, nvmem_device_release);
759 * devm_nvmem_register() - Register a managed nvmem device for given
761 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
763 * @dev: Device that uses the nvmem device.
764 * @config: nvmem device configuration with which nvmem device is created.
772 struct nvmem_device **ptr, *nvmem;
778 nvmem = nvmem_register(config);
780 if (!IS_ERR(nvmem)) {
781 *ptr = nvmem;
787 return nvmem;
799 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
802 * @dev: Device that uses the nvmem device.
803 * @nvmem: Pointer to previously registered nvmem device.
807 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
809 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
816 struct nvmem_device *nvmem = NULL;
822 nvmem = to_nvmem_device(dev);
824 if (!nvmem)
827 if (!try_module_get(nvmem->owner)) {
828 dev_err(&nvmem->dev,
830 nvmem_dev_name(nvmem));
832 put_device(&nvmem->dev);
836 kref_get(&nvmem->refcnt);
838 return nvmem;
841 static void __nvmem_device_put(struct nvmem_device *nvmem)
843 put_device(&nvmem->dev);
844 module_put(nvmem->owner);
845 kref_put(&nvmem->refcnt, nvmem_device_release);
850 * of_nvmem_device_get() - Get nvmem device from a given id
852 * @np: Device tree node that uses the nvmem device.
853 * @id: nvmem name from nvmem-names property.
862 struct nvmem_device *nvmem;
866 index = of_property_match_string(np, "nvmem-names", id);
868 nvmem_np = of_parse_phandle(np, "nvmem", index);
872 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
874 return nvmem;
880 * nvmem_device_get() - Get nvmem device from a given id
882 * @dev: Device that uses the nvmem device.
883 * @dev_name: name of the requested nvmem device.
891 struct nvmem_device *nvmem;
893 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
895 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
896 return nvmem;
905 * nvmem_device_find() - Find nvmem device with matching function
922 struct nvmem_device **nvmem = res;
924 if (WARN_ON(!nvmem || !*nvmem))
927 return *nvmem == data;
936 * devm_nvmem_device_put() - put alredy got nvmem device
938 * @dev: Device that uses the nvmem device.
939 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
942 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
947 devm_nvmem_device_match, nvmem);
954 * nvmem_device_put() - put alredy got nvmem device
956 * @nvmem: pointer to nvmem device that needs to be released.
958 void nvmem_device_put(struct nvmem_device *nvmem)
960 __nvmem_device_put(nvmem);
965 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
967 * @dev: Device that requests the nvmem device.
968 * @id: name id for the requested nvmem device.
976 struct nvmem_device **ptr, *nvmem;
982 nvmem = nvmem_device_get(dev, id);
983 if (!IS_ERR(nvmem)) {
984 *ptr = nvmem;
990 return nvmem;
999 struct nvmem_device *nvmem;
1013 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1015 if (IS_ERR(nvmem)) {
1017 cell = ERR_CAST(nvmem);
1021 cell = nvmem_find_cell_by_name(nvmem,
1024 __nvmem_device_put(nvmem);
1037 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1042 list_for_each_entry(iter, &nvmem->cells, node) {
1054 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1056 * @np: Device tree node that uses the nvmem cell.
1057 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1059 * nvmem-cell-names property).
1068 struct nvmem_device *nvmem;
1074 index = of_property_match_string(np, "nvmem-cell-names", id);
1076 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1084 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1086 if (IS_ERR(nvmem))
1087 return ERR_CAST(nvmem);
1089 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1091 __nvmem_device_put(nvmem);
1101 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1103 * @dev: Device that requests the nvmem cell.
1104 * @id: nvmem cell name to get (this corresponds with the name from the
1105 * nvmem-cell-names property for DT systems and with the con_id from
1136 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1138 * @dev: Device that requests the nvmem cell.
1139 * @id: nvmem cell name id to get.
1176 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1179 * @dev: Device that requests the nvmem cell.
1180 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1194 * nvmem_cell_put() - Release previously allocated nvmem cell.
1196 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1200 struct nvmem_device *nvmem = cell->nvmem;
1202 __nvmem_device_put(nvmem);
1239 static int __nvmem_cell_read(struct nvmem_device *nvmem,
1245 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1261 * nvmem_cell_read() - Read a given nvmem cell
1263 * @cell: nvmem cell to be read.
1272 struct nvmem_device *nvmem = cell->nvmem;
1276 if (!nvmem)
1283 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1296 struct nvmem_device *nvmem = cell->nvmem;
1312 /* setup the first byte with lsb bits from nvmem */
1313 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1331 /* setup the last byte with msb bits from nvmem */
1332 rc = nvmem_reg_read(nvmem,
1347 * nvmem_cell_write() - Write to a given nvmem cell
1349 * @cell: nvmem cell to be written.
1351 * @len: length of buffer to be written to nvmem cell.
1357 struct nvmem_device *nvmem = cell->nvmem;
1360 if (!nvmem || nvmem->read_only ||
1370 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1414 * @dev: Device that requests the nvmem cell.
1415 * @cell_id: Name of nvmem cell to read.
1429 * @dev: Device that requests the nvmem cell.
1430 * @cell_id: Name of nvmem cell to read.
1444 * @dev: Device that requests the nvmem cell.
1445 * @cell_id: Name of nvmem cell to read.
1459 * @dev: Device that requests the nvmem cell.
1460 * @cell_id: Name of nvmem cell to read.
1472 * nvmem_device_cell_read() - Read a given nvmem device and cell
1474 * @nvmem: nvmem device to read from.
1475 * @info: nvmem cell info to be read.
1481 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1488 if (!nvmem)
1491 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1495 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1504 * nvmem_device_cell_write() - Write cell to a given nvmem device
1506 * @nvmem: nvmem device to be written to.
1507 * @info: nvmem cell info to be written.
1512 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1518 if (!nvmem)
1521 rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
1530 * nvmem_device_read() - Read from a given nvmem device
1532 * @nvmem: nvmem device to read from.
1533 * @offset: offset in nvmem device.
1540 int nvmem_device_read(struct nvmem_device *nvmem,
1546 if (!nvmem)
1549 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1559 * nvmem_device_write() - Write cell to a given nvmem device
1561 * @nvmem: nvmem device to be written to.
1562 * @offset: offset in nvmem device.
1568 int nvmem_device_write(struct nvmem_device *nvmem,
1574 if (!nvmem)
1577 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1649 * nvmem_dev_name() - Get the name of a given nvmem device.
1651 * @nvmem: nvmem device.
1653 * Return: name of the nvmem device.
1655 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1657 return dev_name(&nvmem->dev);
1676 MODULE_DESCRIPTION("nvmem Driver Core");