1 // SPDX-License-Identifier: GPL-2.0
3 * nvmem framework core.
5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
9 #include <linux/device.h>
10 #include <linux/export.h>
12 #include <linux/idr.h>
13 #include <linux/init.h>
14 #include <linux/kref.h>
15 #include <linux/module.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/nvmem-provider.h>
19 #include <linux/slab.h>
28 struct device_node *np;
29 struct nvmem_device *nvmem;
30 struct list_head node;
33 static DEFINE_MUTEX(nvmem_mutex);
34 static DEFINE_IDA(nvmem_ida);
36 static DEFINE_MUTEX(nvmem_cell_mutex);
37 static LIST_HEAD(nvmem_cell_tables);
39 static DEFINE_MUTEX(nvmem_lookup_mutex);
40 static LIST_HEAD(nvmem_lookup_list);
42 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
45 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
46 void *val, size_t bytes)
49 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
54 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
55 void *val, size_t bytes)
58 return nvmem->reg_write(nvmem->priv, offset, val, bytes);
63 static void nvmem_release(struct device *dev)
65 struct nvmem_device *nvmem = to_nvmem_device(dev);
67 ida_simple_remove(&nvmem_ida, nvmem->id);
71 static const struct device_type nvmem_provider_type = {
72 .release = nvmem_release,
75 static struct bus_type nvmem_bus_type = {
79 static int of_nvmem_match(struct device *dev, void *nvmem_np)
81 return dev->of_node == nvmem_np;
84 static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
91 d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
96 return to_nvmem_device(d);
99 static struct nvmem_device *nvmem_find(const char *name)
103 d = bus_find_device_by_name(&nvmem_bus_type, NULL, name);
108 return to_nvmem_device(d);
111 static void nvmem_cell_drop(struct nvmem_cell *cell)
113 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
114 mutex_lock(&nvmem_mutex);
115 list_del(&cell->node);
116 mutex_unlock(&nvmem_mutex);
117 of_node_put(cell->np);
122 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
124 struct nvmem_cell *cell, *p;
126 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
127 nvmem_cell_drop(cell);
130 static void nvmem_cell_add(struct nvmem_cell *cell)
132 mutex_lock(&nvmem_mutex);
133 list_add_tail(&cell->node, &cell->nvmem->cells);
134 mutex_unlock(&nvmem_mutex);
135 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
138 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
139 const struct nvmem_cell_info *info,
140 struct nvmem_cell *cell)
143 cell->offset = info->offset;
144 cell->bytes = info->bytes;
145 cell->name = info->name;
147 cell->bit_offset = info->bit_offset;
148 cell->nbits = info->nbits;
151 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
154 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
156 "cell %s unaligned to nvmem stride %d\n",
157 cell->name, nvmem->stride);
165 * nvmem_add_cells() - Add cell information to an nvmem device
167 * @nvmem: nvmem device to add cells to.
168 * @info: nvmem cell info to add to the device
169 * @ncells: number of cells in info
171 * Return: 0 or negative error code on failure.
173 static int nvmem_add_cells(struct nvmem_device *nvmem,
174 const struct nvmem_cell_info *info,
177 struct nvmem_cell **cells;
180 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
184 for (i = 0; i < ncells; i++) {
185 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
191 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
197 nvmem_cell_add(cells[i]);
200 /* remove tmp array */
206 nvmem_cell_drop(cells[i]);
214 * nvmem_register_notifier() - Register a notifier block for nvmem events.
216 * @nb: notifier block to be called on nvmem events.
218 * Return: 0 on success, negative error number on failure.
220 int nvmem_register_notifier(struct notifier_block *nb)
222 return blocking_notifier_chain_register(&nvmem_notifier, nb);
224 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
227 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
229 * @nb: notifier block to be unregistered.
231 * Return: 0 on success, negative error number on failure.
233 int nvmem_unregister_notifier(struct notifier_block *nb)
235 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
237 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
239 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
241 const struct nvmem_cell_info *info;
242 struct nvmem_cell_table *table;
243 struct nvmem_cell *cell;
246 mutex_lock(&nvmem_cell_mutex);
247 list_for_each_entry(table, &nvmem_cell_tables, node) {
248 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
249 for (i = 0; i < table->ncells; i++) {
250 info = &table->cells[i];
252 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
258 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
266 nvmem_cell_add(cell);
272 mutex_unlock(&nvmem_cell_mutex);
276 static struct nvmem_cell *
277 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
279 struct nvmem_cell *iter, *cell = NULL;
281 mutex_lock(&nvmem_mutex);
282 list_for_each_entry(iter, &nvmem->cells, node) {
283 if (strcmp(cell_id, iter->name) == 0) {
288 mutex_unlock(&nvmem_mutex);
293 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
295 struct device_node *parent, *child;
296 struct device *dev = &nvmem->dev;
297 struct nvmem_cell *cell;
301 parent = dev->of_node;
303 for_each_child_of_node(parent, child) {
304 addr = of_get_property(child, "reg", &len);
305 if (!addr || (len < 2 * sizeof(u32))) {
306 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
310 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
315 cell->np = of_node_get(child);
316 cell->offset = be32_to_cpup(addr++);
317 cell->bytes = be32_to_cpup(addr);
318 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
320 addr = of_get_property(child, "bits", &len);
321 if (addr && len == (2 * sizeof(u32))) {
322 cell->bit_offset = be32_to_cpup(addr++);
323 cell->nbits = be32_to_cpup(addr);
327 cell->bytes = DIV_ROUND_UP(
328 cell->nbits + cell->bit_offset,
331 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
332 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
333 cell->name, nvmem->stride);
334 /* Cells already added will be freed later. */
340 nvmem_cell_add(cell);
347 * nvmem_register() - Register a nvmem device for given nvmem_config.
348 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
350 * @config: nvmem device configuration with which nvmem device is created.
352 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
356 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
358 struct nvmem_device *nvmem;
362 return ERR_PTR(-EINVAL);
364 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
366 return ERR_PTR(-ENOMEM);
368 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
371 return ERR_PTR(rval);
374 kref_init(&nvmem->refcnt);
375 INIT_LIST_HEAD(&nvmem->cells);
378 nvmem->owner = config->owner;
379 if (!nvmem->owner && config->dev->driver)
380 nvmem->owner = config->dev->driver->owner;
381 nvmem->stride = config->stride ?: 1;
382 nvmem->word_size = config->word_size ?: 1;
383 nvmem->size = config->size;
384 nvmem->dev.type = &nvmem_provider_type;
385 nvmem->dev.bus = &nvmem_bus_type;
386 nvmem->dev.parent = config->dev;
387 nvmem->priv = config->priv;
388 nvmem->type = config->type;
389 nvmem->reg_read = config->reg_read;
390 nvmem->reg_write = config->reg_write;
391 if (!config->no_of_node)
392 nvmem->dev.of_node = config->dev->of_node;
394 if (config->id == -1 && config->name) {
395 dev_set_name(&nvmem->dev, "%s", config->name);
397 dev_set_name(&nvmem->dev, "%s%d",
398 config->name ? : "nvmem",
399 config->name ? config->id : nvmem->id);
402 nvmem->read_only = device_property_present(config->dev, "read-only") ||
403 config->read_only || !nvmem->reg_write;
405 nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
407 device_initialize(&nvmem->dev);
409 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
411 rval = device_add(&nvmem->dev);
415 if (config->compat) {
416 rval = nvmem_sysfs_setup_compat(nvmem, config);
422 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
424 goto err_teardown_compat;
427 rval = nvmem_add_cells_from_table(nvmem);
429 goto err_remove_cells;
431 rval = nvmem_add_cells_from_of(nvmem);
433 goto err_remove_cells;
435 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
440 nvmem_device_remove_all_cells(nvmem);
443 nvmem_sysfs_remove_compat(nvmem, config);
445 device_del(&nvmem->dev);
447 put_device(&nvmem->dev);
449 return ERR_PTR(rval);
451 EXPORT_SYMBOL_GPL(nvmem_register);
453 static void nvmem_device_release(struct kref *kref)
455 struct nvmem_device *nvmem;
457 nvmem = container_of(kref, struct nvmem_device, refcnt);
459 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
461 if (nvmem->flags & FLAG_COMPAT)
462 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
464 nvmem_device_remove_all_cells(nvmem);
465 device_del(&nvmem->dev);
466 put_device(&nvmem->dev);
470 * nvmem_unregister() - Unregister previously registered nvmem device
472 * @nvmem: Pointer to previously registered nvmem device.
474 void nvmem_unregister(struct nvmem_device *nvmem)
476 kref_put(&nvmem->refcnt, nvmem_device_release);
478 EXPORT_SYMBOL_GPL(nvmem_unregister);
480 static void devm_nvmem_release(struct device *dev, void *res)
482 nvmem_unregister(*(struct nvmem_device **)res);
486 * devm_nvmem_register() - Register a managed nvmem device for given
488 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
490 * @dev: Device that uses the nvmem device.
491 * @config: nvmem device configuration with which nvmem device is created.
493 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
496 struct nvmem_device *devm_nvmem_register(struct device *dev,
497 const struct nvmem_config *config)
499 struct nvmem_device **ptr, *nvmem;
501 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
503 return ERR_PTR(-ENOMEM);
505 nvmem = nvmem_register(config);
507 if (!IS_ERR(nvmem)) {
509 devres_add(dev, ptr);
516 EXPORT_SYMBOL_GPL(devm_nvmem_register);
518 static int devm_nvmem_match(struct device *dev, void *res, void *data)
520 struct nvmem_device **r = res;
526 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
529 * @dev: Device that uses the nvmem device.
530 * @nvmem: Pointer to previously registered nvmem device.
532 * Return: Will be an negative on error or a zero on success.
534 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
536 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
538 EXPORT_SYMBOL(devm_nvmem_unregister);
540 static struct nvmem_device *__nvmem_device_get(struct device_node *np,
541 const char *nvmem_name)
543 struct nvmem_device *nvmem = NULL;
545 mutex_lock(&nvmem_mutex);
546 nvmem = np ? of_nvmem_find(np) : nvmem_find(nvmem_name);
547 mutex_unlock(&nvmem_mutex);
549 return ERR_PTR(-EPROBE_DEFER);
551 if (!try_module_get(nvmem->owner)) {
553 "could not increase module refcount for cell %s\n",
554 nvmem_dev_name(nvmem));
556 put_device(&nvmem->dev);
557 return ERR_PTR(-EINVAL);
560 kref_get(&nvmem->refcnt);
565 static void __nvmem_device_put(struct nvmem_device *nvmem)
567 put_device(&nvmem->dev);
568 module_put(nvmem->owner);
569 kref_put(&nvmem->refcnt, nvmem_device_release);
572 #if IS_ENABLED(CONFIG_OF)
574 * of_nvmem_device_get() - Get nvmem device from a given id
576 * @np: Device tree node that uses the nvmem device.
577 * @id: nvmem name from nvmem-names property.
579 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
582 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
585 struct device_node *nvmem_np;
589 index = of_property_match_string(np, "nvmem-names", id);
591 nvmem_np = of_parse_phandle(np, "nvmem", index);
593 return ERR_PTR(-ENOENT);
595 return __nvmem_device_get(nvmem_np, NULL);
597 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
601 * nvmem_device_get() - Get nvmem device from a given id
603 * @dev: Device that uses the nvmem device.
604 * @dev_name: name of the requested nvmem device.
606 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
609 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
611 if (dev->of_node) { /* try dt first */
612 struct nvmem_device *nvmem;
614 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
616 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
621 return __nvmem_device_get(NULL, dev_name);
623 EXPORT_SYMBOL_GPL(nvmem_device_get);
625 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
627 struct nvmem_device **nvmem = res;
629 if (WARN_ON(!nvmem || !*nvmem))
632 return *nvmem == data;
635 static void devm_nvmem_device_release(struct device *dev, void *res)
637 nvmem_device_put(*(struct nvmem_device **)res);
641 * devm_nvmem_device_put() - put alredy got nvmem device
643 * @dev: Device that uses the nvmem device.
644 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
645 * that needs to be released.
647 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
651 ret = devres_release(dev, devm_nvmem_device_release,
652 devm_nvmem_device_match, nvmem);
656 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
659 * nvmem_device_put() - put alredy got nvmem device
661 * @nvmem: pointer to nvmem device that needs to be released.
663 void nvmem_device_put(struct nvmem_device *nvmem)
665 __nvmem_device_put(nvmem);
667 EXPORT_SYMBOL_GPL(nvmem_device_put);
670 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
672 * @dev: Device that requests the nvmem device.
673 * @id: name id for the requested nvmem device.
675 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
676 * on success. The nvmem_cell will be freed by the automatically once the
679 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
681 struct nvmem_device **ptr, *nvmem;
683 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
685 return ERR_PTR(-ENOMEM);
687 nvmem = nvmem_device_get(dev, id);
688 if (!IS_ERR(nvmem)) {
690 devres_add(dev, ptr);
697 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
699 static struct nvmem_cell *
700 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
702 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
703 struct nvmem_cell_lookup *lookup;
704 struct nvmem_device *nvmem;
708 return ERR_PTR(-EINVAL);
710 dev_id = dev_name(dev);
712 mutex_lock(&nvmem_lookup_mutex);
714 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
715 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
716 (strcmp(lookup->con_id, con_id) == 0)) {
717 /* This is the right entry. */
718 nvmem = __nvmem_device_get(NULL, lookup->nvmem_name);
720 /* Provider may not be registered yet. */
721 cell = ERR_CAST(nvmem);
725 cell = nvmem_find_cell_by_name(nvmem,
728 __nvmem_device_put(nvmem);
729 cell = ERR_PTR(-ENOENT);
735 mutex_unlock(&nvmem_lookup_mutex);
739 #if IS_ENABLED(CONFIG_OF)
740 static struct nvmem_cell *
741 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
743 struct nvmem_cell *iter, *cell = NULL;
745 mutex_lock(&nvmem_mutex);
746 list_for_each_entry(iter, &nvmem->cells, node) {
747 if (np == iter->np) {
752 mutex_unlock(&nvmem_mutex);
758 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
760 * @np: Device tree node that uses the nvmem cell.
761 * @id: nvmem cell name from nvmem-cell-names property, or NULL
762 * for the cell at index 0 (the lone cell with no accompanying
763 * nvmem-cell-names property).
765 * Return: Will be an ERR_PTR() on error or a valid pointer
766 * to a struct nvmem_cell. The nvmem_cell will be freed by the
769 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
771 struct device_node *cell_np, *nvmem_np;
772 struct nvmem_device *nvmem;
773 struct nvmem_cell *cell;
776 /* if cell name exists, find index to the name */
778 index = of_property_match_string(np, "nvmem-cell-names", id);
780 cell_np = of_parse_phandle(np, "nvmem-cells", index);
782 return ERR_PTR(-ENOENT);
784 nvmem_np = of_get_next_parent(cell_np);
786 return ERR_PTR(-EINVAL);
788 nvmem = __nvmem_device_get(nvmem_np, NULL);
789 of_node_put(nvmem_np);
791 return ERR_CAST(nvmem);
793 cell = nvmem_find_cell_by_node(nvmem, cell_np);
795 __nvmem_device_put(nvmem);
796 return ERR_PTR(-ENOENT);
801 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
805 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
807 * @dev: Device that requests the nvmem cell.
808 * @id: nvmem cell name to get (this corresponds with the name from the
809 * nvmem-cell-names property for DT systems and with the con_id from
810 * the lookup entry for non-DT systems).
812 * Return: Will be an ERR_PTR() on error or a valid pointer
813 * to a struct nvmem_cell. The nvmem_cell will be freed by the
816 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
818 struct nvmem_cell *cell;
820 if (dev->of_node) { /* try dt first */
821 cell = of_nvmem_cell_get(dev->of_node, id);
822 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
826 /* NULL cell id only allowed for device tree; invalid otherwise */
828 return ERR_PTR(-EINVAL);
830 return nvmem_cell_get_from_lookup(dev, id);
832 EXPORT_SYMBOL_GPL(nvmem_cell_get);
834 static void devm_nvmem_cell_release(struct device *dev, void *res)
836 nvmem_cell_put(*(struct nvmem_cell **)res);
840 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
842 * @dev: Device that requests the nvmem cell.
843 * @id: nvmem cell name id to get.
845 * Return: Will be an ERR_PTR() on error or a valid pointer
846 * to a struct nvmem_cell. The nvmem_cell will be freed by the
847 * automatically once the device is freed.
849 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
851 struct nvmem_cell **ptr, *cell;
853 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
855 return ERR_PTR(-ENOMEM);
857 cell = nvmem_cell_get(dev, id);
860 devres_add(dev, ptr);
867 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
869 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
871 struct nvmem_cell **c = res;
873 if (WARN_ON(!c || !*c))
880 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
881 * from devm_nvmem_cell_get.
883 * @dev: Device that requests the nvmem cell.
884 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
886 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
890 ret = devres_release(dev, devm_nvmem_cell_release,
891 devm_nvmem_cell_match, cell);
895 EXPORT_SYMBOL(devm_nvmem_cell_put);
898 * nvmem_cell_put() - Release previously allocated nvmem cell.
900 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
902 void nvmem_cell_put(struct nvmem_cell *cell)
904 struct nvmem_device *nvmem = cell->nvmem;
906 __nvmem_device_put(nvmem);
908 EXPORT_SYMBOL_GPL(nvmem_cell_put);
910 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
913 int i, extra, bit_offset = cell->bit_offset;
920 /* setup rest of the bytes if any */
921 for (i = 1; i < cell->bytes; i++) {
922 /* Get bits from next byte and shift them towards msb */
923 *p |= *b << (BITS_PER_BYTE - bit_offset);
929 /* point to the msb */
930 p += cell->bytes - 1;
933 /* result fits in less bytes */
934 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
938 /* clear msb bits if any leftover in the last byte */
939 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
942 static int __nvmem_cell_read(struct nvmem_device *nvmem,
943 struct nvmem_cell *cell,
944 void *buf, size_t *len)
948 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
953 /* shift bits in-place */
954 if (cell->bit_offset || cell->nbits)
955 nvmem_shift_read_buffer_in_place(cell, buf);
964 * nvmem_cell_read() - Read a given nvmem cell
966 * @cell: nvmem cell to be read.
967 * @len: pointer to length of cell which will be populated on successful read;
970 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
971 * buffer should be freed by the consumer with a kfree().
973 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
975 struct nvmem_device *nvmem = cell->nvmem;
980 return ERR_PTR(-EINVAL);
982 buf = kzalloc(cell->bytes, GFP_KERNEL);
984 return ERR_PTR(-ENOMEM);
986 rc = __nvmem_cell_read(nvmem, cell, buf, len);
994 EXPORT_SYMBOL_GPL(nvmem_cell_read);
996 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
999 struct nvmem_device *nvmem = cell->nvmem;
1000 int i, rc, nbits, bit_offset = cell->bit_offset;
1001 u8 v, *p, *buf, *b, pbyte, pbits;
1003 nbits = cell->nbits;
1004 buf = kzalloc(cell->bytes, GFP_KERNEL);
1006 return ERR_PTR(-ENOMEM);
1008 memcpy(buf, _buf, len);
1015 /* setup the first byte with lsb bits from nvmem */
1016 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1019 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1021 /* setup rest of the byte if any */
1022 for (i = 1; i < cell->bytes; i++) {
1023 /* Get last byte bits and shift them towards lsb */
1024 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1032 /* if it's not end on byte boundary */
1033 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1034 /* setup the last byte with msb bits from nvmem */
1035 rc = nvmem_reg_read(nvmem,
1036 cell->offset + cell->bytes - 1, &v, 1);
1039 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1050 * nvmem_cell_write() - Write to a given nvmem cell
1052 * @cell: nvmem cell to be written.
1053 * @buf: Buffer to be written.
1054 * @len: length of buffer to be written to nvmem cell.
1056 * Return: length of bytes written or negative on failure.
1058 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1060 struct nvmem_device *nvmem = cell->nvmem;
1063 if (!nvmem || nvmem->read_only ||
1064 (cell->bit_offset == 0 && len != cell->bytes))
1067 if (cell->bit_offset || cell->nbits) {
1068 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1070 return PTR_ERR(buf);
1073 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1075 /* free the tmp buffer */
1076 if (cell->bit_offset || cell->nbits)
1084 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1087 * nvmem_cell_read_u16() - Read a cell value as an u16
1089 * @dev: Device that requests the nvmem cell.
1090 * @cell_id: Name of nvmem cell to read.
1091 * @val: pointer to output value.
1093 * Return: 0 on success or negative errno.
1095 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1097 struct nvmem_cell *cell;
1101 cell = nvmem_cell_get(dev, cell_id);
1103 return PTR_ERR(cell);
1105 buf = nvmem_cell_read(cell, &len);
1107 nvmem_cell_put(cell);
1108 return PTR_ERR(buf);
1110 if (len != sizeof(*val)) {
1112 nvmem_cell_put(cell);
1115 memcpy(val, buf, sizeof(*val));
1117 nvmem_cell_put(cell);
1121 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1124 * nvmem_cell_read_u32() - Read a cell value as an u32
1126 * @dev: Device that requests the nvmem cell.
1127 * @cell_id: Name of nvmem cell to read.
1128 * @val: pointer to output value.
1130 * Return: 0 on success or negative errno.
1132 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1134 struct nvmem_cell *cell;
1138 cell = nvmem_cell_get(dev, cell_id);
1140 return PTR_ERR(cell);
1142 buf = nvmem_cell_read(cell, &len);
1144 nvmem_cell_put(cell);
1145 return PTR_ERR(buf);
1147 if (len != sizeof(*val)) {
1149 nvmem_cell_put(cell);
1152 memcpy(val, buf, sizeof(*val));
1155 nvmem_cell_put(cell);
1158 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1161 * nvmem_device_cell_read() - Read a given nvmem device and cell
1163 * @nvmem: nvmem device to read from.
1164 * @info: nvmem cell info to be read.
1165 * @buf: buffer pointer which will be populated on successful read.
1167 * Return: length of successful bytes read on success and negative
1168 * error code on error.
1170 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1171 struct nvmem_cell_info *info, void *buf)
1173 struct nvmem_cell cell;
1180 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1184 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1190 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1193 * nvmem_device_cell_write() - Write cell to a given nvmem device
1195 * @nvmem: nvmem device to be written to.
1196 * @info: nvmem cell info to be written.
1197 * @buf: buffer to be written to cell.
1199 * Return: length of bytes written or negative error code on failure.
1201 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1202 struct nvmem_cell_info *info, void *buf)
1204 struct nvmem_cell cell;
1210 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1214 return nvmem_cell_write(&cell, buf, cell.bytes);
1216 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1219 * nvmem_device_read() - Read from a given nvmem device
1221 * @nvmem: nvmem device to read from.
1222 * @offset: offset in nvmem device.
1223 * @bytes: number of bytes to read.
1224 * @buf: buffer pointer which will be populated on successful read.
1226 * Return: length of successful bytes read on success and negative
1227 * error code on error.
1229 int nvmem_device_read(struct nvmem_device *nvmem,
1230 unsigned int offset,
1231 size_t bytes, void *buf)
1238 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1245 EXPORT_SYMBOL_GPL(nvmem_device_read);
1248 * nvmem_device_write() - Write cell to a given nvmem device
1250 * @nvmem: nvmem device to be written to.
1251 * @offset: offset in nvmem device.
1252 * @bytes: number of bytes to write.
1253 * @buf: buffer to be written.
1255 * Return: length of bytes written or negative error code on failure.
1257 int nvmem_device_write(struct nvmem_device *nvmem,
1258 unsigned int offset,
1259 size_t bytes, void *buf)
1266 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1274 EXPORT_SYMBOL_GPL(nvmem_device_write);
1277 * nvmem_add_cell_table() - register a table of cell info entries
1279 * @table: table of cell info entries
1281 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1283 mutex_lock(&nvmem_cell_mutex);
1284 list_add_tail(&table->node, &nvmem_cell_tables);
1285 mutex_unlock(&nvmem_cell_mutex);
1287 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1290 * nvmem_del_cell_table() - remove a previously registered cell info table
1292 * @table: table of cell info entries
1294 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1296 mutex_lock(&nvmem_cell_mutex);
1297 list_del(&table->node);
1298 mutex_unlock(&nvmem_cell_mutex);
1300 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1303 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1305 * @entries: array of cell lookup entries
1306 * @nentries: number of cell lookup entries in the array
1308 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1312 mutex_lock(&nvmem_lookup_mutex);
1313 for (i = 0; i < nentries; i++)
1314 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1315 mutex_unlock(&nvmem_lookup_mutex);
1317 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1320 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1323 * @entries: array of cell lookup entries
1324 * @nentries: number of cell lookup entries in the array
1326 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1330 mutex_lock(&nvmem_lookup_mutex);
1331 for (i = 0; i < nentries; i++)
1332 list_del(&entries[i].node);
1333 mutex_unlock(&nvmem_lookup_mutex);
1335 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1338 * nvmem_dev_name() - Get the name of a given nvmem device.
1340 * @nvmem: nvmem device.
1342 * Return: name of the nvmem device.
1344 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1346 return dev_name(&nvmem->dev);
1348 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1350 static int __init nvmem_init(void)
1352 return bus_register(&nvmem_bus_type);
1355 static void __exit nvmem_exit(void)
1357 bus_unregister(&nvmem_bus_type);
1360 subsys_initcall(nvmem_init);
1361 module_exit(nvmem_exit);
1363 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1364 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1365 MODULE_DESCRIPTION("nvmem Driver Core");
1366 MODULE_LICENSE("GPL v2");