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 void nvmem_cell_drop(struct nvmem_cell *cell)
81 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
82 mutex_lock(&nvmem_mutex);
83 list_del(&cell->node);
84 mutex_unlock(&nvmem_mutex);
85 of_node_put(cell->np);
86 kfree_const(cell->name);
90 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
92 struct nvmem_cell *cell, *p;
94 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
95 nvmem_cell_drop(cell);
98 static void nvmem_cell_add(struct nvmem_cell *cell)
100 mutex_lock(&nvmem_mutex);
101 list_add_tail(&cell->node, &cell->nvmem->cells);
102 mutex_unlock(&nvmem_mutex);
103 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
106 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
107 const struct nvmem_cell_info *info,
108 struct nvmem_cell *cell)
111 cell->offset = info->offset;
112 cell->bytes = info->bytes;
113 cell->name = kstrdup_const(info->name, GFP_KERNEL);
117 cell->bit_offset = info->bit_offset;
118 cell->nbits = info->nbits;
121 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
124 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
126 "cell %s unaligned to nvmem stride %d\n",
127 cell->name, nvmem->stride);
135 * nvmem_add_cells() - Add cell information to an nvmem device
137 * @nvmem: nvmem device to add cells to.
138 * @info: nvmem cell info to add to the device
139 * @ncells: number of cells in info
141 * Return: 0 or negative error code on failure.
143 static int nvmem_add_cells(struct nvmem_device *nvmem,
144 const struct nvmem_cell_info *info,
147 struct nvmem_cell **cells;
150 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
154 for (i = 0; i < ncells; i++) {
155 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
161 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
167 nvmem_cell_add(cells[i]);
170 /* remove tmp array */
176 nvmem_cell_drop(cells[i]);
184 * nvmem_register_notifier() - Register a notifier block for nvmem events.
186 * @nb: notifier block to be called on nvmem events.
188 * Return: 0 on success, negative error number on failure.
190 int nvmem_register_notifier(struct notifier_block *nb)
192 return blocking_notifier_chain_register(&nvmem_notifier, nb);
194 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
197 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
199 * @nb: notifier block to be unregistered.
201 * Return: 0 on success, negative error number on failure.
203 int nvmem_unregister_notifier(struct notifier_block *nb)
205 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
207 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
209 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
211 const struct nvmem_cell_info *info;
212 struct nvmem_cell_table *table;
213 struct nvmem_cell *cell;
216 mutex_lock(&nvmem_cell_mutex);
217 list_for_each_entry(table, &nvmem_cell_tables, node) {
218 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
219 for (i = 0; i < table->ncells; i++) {
220 info = &table->cells[i];
222 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
228 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
236 nvmem_cell_add(cell);
242 mutex_unlock(&nvmem_cell_mutex);
246 static struct nvmem_cell *
247 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
249 struct nvmem_cell *iter, *cell = NULL;
251 mutex_lock(&nvmem_mutex);
252 list_for_each_entry(iter, &nvmem->cells, node) {
253 if (strcmp(cell_id, iter->name) == 0) {
258 mutex_unlock(&nvmem_mutex);
263 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
265 struct device_node *parent, *child;
266 struct device *dev = &nvmem->dev;
267 struct nvmem_cell *cell;
271 parent = dev->of_node;
273 for_each_child_of_node(parent, child) {
274 addr = of_get_property(child, "reg", &len);
275 if (!addr || (len < 2 * sizeof(u32))) {
276 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
280 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
285 cell->np = of_node_get(child);
286 cell->offset = be32_to_cpup(addr++);
287 cell->bytes = be32_to_cpup(addr);
288 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
290 addr = of_get_property(child, "bits", &len);
291 if (addr && len == (2 * sizeof(u32))) {
292 cell->bit_offset = be32_to_cpup(addr++);
293 cell->nbits = be32_to_cpup(addr);
297 cell->bytes = DIV_ROUND_UP(
298 cell->nbits + cell->bit_offset,
301 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
302 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
303 cell->name, nvmem->stride);
304 /* Cells already added will be freed later. */
305 kfree_const(cell->name);
310 nvmem_cell_add(cell);
317 * nvmem_register() - Register a nvmem device for given nvmem_config.
318 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
320 * @config: nvmem device configuration with which nvmem device is created.
322 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
326 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
328 struct nvmem_device *nvmem;
332 return ERR_PTR(-EINVAL);
334 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
336 return ERR_PTR(-ENOMEM);
338 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
341 return ERR_PTR(rval);
344 kref_init(&nvmem->refcnt);
345 INIT_LIST_HEAD(&nvmem->cells);
348 nvmem->owner = config->owner;
349 if (!nvmem->owner && config->dev->driver)
350 nvmem->owner = config->dev->driver->owner;
351 nvmem->stride = config->stride ?: 1;
352 nvmem->word_size = config->word_size ?: 1;
353 nvmem->size = config->size;
354 nvmem->dev.type = &nvmem_provider_type;
355 nvmem->dev.bus = &nvmem_bus_type;
356 nvmem->dev.parent = config->dev;
357 nvmem->priv = config->priv;
358 nvmem->type = config->type;
359 nvmem->reg_read = config->reg_read;
360 nvmem->reg_write = config->reg_write;
361 if (!config->no_of_node)
362 nvmem->dev.of_node = config->dev->of_node;
364 if (config->id == -1 && config->name) {
365 dev_set_name(&nvmem->dev, "%s", config->name);
367 dev_set_name(&nvmem->dev, "%s%d",
368 config->name ? : "nvmem",
369 config->name ? config->id : nvmem->id);
372 nvmem->read_only = device_property_present(config->dev, "read-only") ||
373 config->read_only || !nvmem->reg_write;
375 nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
377 device_initialize(&nvmem->dev);
379 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
381 rval = device_add(&nvmem->dev);
385 if (config->compat) {
386 rval = nvmem_sysfs_setup_compat(nvmem, config);
392 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
394 goto err_teardown_compat;
397 rval = nvmem_add_cells_from_table(nvmem);
399 goto err_remove_cells;
401 rval = nvmem_add_cells_from_of(nvmem);
403 goto err_remove_cells;
405 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
410 nvmem_device_remove_all_cells(nvmem);
413 nvmem_sysfs_remove_compat(nvmem, config);
415 device_del(&nvmem->dev);
417 put_device(&nvmem->dev);
419 return ERR_PTR(rval);
421 EXPORT_SYMBOL_GPL(nvmem_register);
423 static void nvmem_device_release(struct kref *kref)
425 struct nvmem_device *nvmem;
427 nvmem = container_of(kref, struct nvmem_device, refcnt);
429 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
431 if (nvmem->flags & FLAG_COMPAT)
432 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
434 nvmem_device_remove_all_cells(nvmem);
435 device_del(&nvmem->dev);
436 put_device(&nvmem->dev);
440 * nvmem_unregister() - Unregister previously registered nvmem device
442 * @nvmem: Pointer to previously registered nvmem device.
444 void nvmem_unregister(struct nvmem_device *nvmem)
446 kref_put(&nvmem->refcnt, nvmem_device_release);
448 EXPORT_SYMBOL_GPL(nvmem_unregister);
450 static void devm_nvmem_release(struct device *dev, void *res)
452 nvmem_unregister(*(struct nvmem_device **)res);
456 * devm_nvmem_register() - Register a managed nvmem device for given
458 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
460 * @dev: Device that uses the nvmem device.
461 * @config: nvmem device configuration with which nvmem device is created.
463 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
466 struct nvmem_device *devm_nvmem_register(struct device *dev,
467 const struct nvmem_config *config)
469 struct nvmem_device **ptr, *nvmem;
471 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
473 return ERR_PTR(-ENOMEM);
475 nvmem = nvmem_register(config);
477 if (!IS_ERR(nvmem)) {
479 devres_add(dev, ptr);
486 EXPORT_SYMBOL_GPL(devm_nvmem_register);
488 static int devm_nvmem_match(struct device *dev, void *res, void *data)
490 struct nvmem_device **r = res;
496 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
499 * @dev: Device that uses the nvmem device.
500 * @nvmem: Pointer to previously registered nvmem device.
502 * Return: Will be an negative on error or a zero on success.
504 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
506 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
508 EXPORT_SYMBOL(devm_nvmem_unregister);
510 static struct nvmem_device *__nvmem_device_get(void *data,
511 int (*match)(struct device *dev, const void *data))
513 struct nvmem_device *nvmem = NULL;
516 mutex_lock(&nvmem_mutex);
517 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
519 nvmem = to_nvmem_device(dev);
520 mutex_unlock(&nvmem_mutex);
522 return ERR_PTR(-EPROBE_DEFER);
524 if (!try_module_get(nvmem->owner)) {
526 "could not increase module refcount for cell %s\n",
527 nvmem_dev_name(nvmem));
529 put_device(&nvmem->dev);
530 return ERR_PTR(-EINVAL);
533 kref_get(&nvmem->refcnt);
538 static void __nvmem_device_put(struct nvmem_device *nvmem)
540 put_device(&nvmem->dev);
541 module_put(nvmem->owner);
542 kref_put(&nvmem->refcnt, nvmem_device_release);
545 #if IS_ENABLED(CONFIG_OF)
547 * of_nvmem_device_get() - Get nvmem device from a given id
549 * @np: Device tree node that uses the nvmem device.
550 * @id: nvmem name from nvmem-names property.
552 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
555 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
558 struct device_node *nvmem_np;
562 index = of_property_match_string(np, "nvmem-names", id);
564 nvmem_np = of_parse_phandle(np, "nvmem", index);
566 return ERR_PTR(-ENOENT);
568 return __nvmem_device_get(nvmem_np, device_match_of_node);
570 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
574 * nvmem_device_get() - Get nvmem device from a given id
576 * @dev: Device that uses the nvmem device.
577 * @dev_name: name of the requested nvmem device.
579 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
582 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
584 if (dev->of_node) { /* try dt first */
585 struct nvmem_device *nvmem;
587 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
589 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
594 return __nvmem_device_get((void *)dev_name, device_match_name);
596 EXPORT_SYMBOL_GPL(nvmem_device_get);
599 * nvmem_device_find() - Find nvmem device with matching function
601 * @data: Data to pass to match function
602 * @match: Callback function to check device
604 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
607 struct nvmem_device *nvmem_device_find(void *data,
608 int (*match)(struct device *dev, const void *data))
610 return __nvmem_device_get(data, match);
612 EXPORT_SYMBOL_GPL(nvmem_device_find);
614 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
616 struct nvmem_device **nvmem = res;
618 if (WARN_ON(!nvmem || !*nvmem))
621 return *nvmem == data;
624 static void devm_nvmem_device_release(struct device *dev, void *res)
626 nvmem_device_put(*(struct nvmem_device **)res);
630 * devm_nvmem_device_put() - put alredy got nvmem device
632 * @dev: Device that uses the nvmem device.
633 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
634 * that needs to be released.
636 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
640 ret = devres_release(dev, devm_nvmem_device_release,
641 devm_nvmem_device_match, nvmem);
645 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
648 * nvmem_device_put() - put alredy got nvmem device
650 * @nvmem: pointer to nvmem device that needs to be released.
652 void nvmem_device_put(struct nvmem_device *nvmem)
654 __nvmem_device_put(nvmem);
656 EXPORT_SYMBOL_GPL(nvmem_device_put);
659 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
661 * @dev: Device that requests the nvmem device.
662 * @id: name id for the requested nvmem device.
664 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
665 * on success. The nvmem_cell will be freed by the automatically once the
668 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
670 struct nvmem_device **ptr, *nvmem;
672 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
674 return ERR_PTR(-ENOMEM);
676 nvmem = nvmem_device_get(dev, id);
677 if (!IS_ERR(nvmem)) {
679 devres_add(dev, ptr);
686 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
688 static struct nvmem_cell *
689 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
691 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
692 struct nvmem_cell_lookup *lookup;
693 struct nvmem_device *nvmem;
697 return ERR_PTR(-EINVAL);
699 dev_id = dev_name(dev);
701 mutex_lock(&nvmem_lookup_mutex);
703 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
704 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
705 (strcmp(lookup->con_id, con_id) == 0)) {
706 /* This is the right entry. */
707 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
710 /* Provider may not be registered yet. */
711 cell = ERR_CAST(nvmem);
715 cell = nvmem_find_cell_by_name(nvmem,
718 __nvmem_device_put(nvmem);
719 cell = ERR_PTR(-ENOENT);
725 mutex_unlock(&nvmem_lookup_mutex);
729 #if IS_ENABLED(CONFIG_OF)
730 static struct nvmem_cell *
731 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
733 struct nvmem_cell *iter, *cell = NULL;
735 mutex_lock(&nvmem_mutex);
736 list_for_each_entry(iter, &nvmem->cells, node) {
737 if (np == iter->np) {
742 mutex_unlock(&nvmem_mutex);
748 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
750 * @np: Device tree node that uses the nvmem cell.
751 * @id: nvmem cell name from nvmem-cell-names property, or NULL
752 * for the cell at index 0 (the lone cell with no accompanying
753 * nvmem-cell-names property).
755 * Return: Will be an ERR_PTR() on error or a valid pointer
756 * to a struct nvmem_cell. The nvmem_cell will be freed by the
759 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
761 struct device_node *cell_np, *nvmem_np;
762 struct nvmem_device *nvmem;
763 struct nvmem_cell *cell;
766 /* if cell name exists, find index to the name */
768 index = of_property_match_string(np, "nvmem-cell-names", id);
770 cell_np = of_parse_phandle(np, "nvmem-cells", index);
772 return ERR_PTR(-ENOENT);
774 nvmem_np = of_get_next_parent(cell_np);
776 return ERR_PTR(-EINVAL);
778 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
779 of_node_put(nvmem_np);
781 return ERR_CAST(nvmem);
783 cell = nvmem_find_cell_by_node(nvmem, cell_np);
785 __nvmem_device_put(nvmem);
786 return ERR_PTR(-ENOENT);
791 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
795 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
797 * @dev: Device that requests the nvmem cell.
798 * @id: nvmem cell name to get (this corresponds with the name from the
799 * nvmem-cell-names property for DT systems and with the con_id from
800 * the lookup entry for non-DT systems).
802 * Return: Will be an ERR_PTR() on error or a valid pointer
803 * to a struct nvmem_cell. The nvmem_cell will be freed by the
806 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
808 struct nvmem_cell *cell;
810 if (dev->of_node) { /* try dt first */
811 cell = of_nvmem_cell_get(dev->of_node, id);
812 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
816 /* NULL cell id only allowed for device tree; invalid otherwise */
818 return ERR_PTR(-EINVAL);
820 return nvmem_cell_get_from_lookup(dev, id);
822 EXPORT_SYMBOL_GPL(nvmem_cell_get);
824 static void devm_nvmem_cell_release(struct device *dev, void *res)
826 nvmem_cell_put(*(struct nvmem_cell **)res);
830 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
832 * @dev: Device that requests the nvmem cell.
833 * @id: nvmem cell name id to get.
835 * Return: Will be an ERR_PTR() on error or a valid pointer
836 * to a struct nvmem_cell. The nvmem_cell will be freed by the
837 * automatically once the device is freed.
839 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
841 struct nvmem_cell **ptr, *cell;
843 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
845 return ERR_PTR(-ENOMEM);
847 cell = nvmem_cell_get(dev, id);
850 devres_add(dev, ptr);
857 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
859 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
861 struct nvmem_cell **c = res;
863 if (WARN_ON(!c || !*c))
870 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
871 * from devm_nvmem_cell_get.
873 * @dev: Device that requests the nvmem cell.
874 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
876 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
880 ret = devres_release(dev, devm_nvmem_cell_release,
881 devm_nvmem_cell_match, cell);
885 EXPORT_SYMBOL(devm_nvmem_cell_put);
888 * nvmem_cell_put() - Release previously allocated nvmem cell.
890 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
892 void nvmem_cell_put(struct nvmem_cell *cell)
894 struct nvmem_device *nvmem = cell->nvmem;
896 __nvmem_device_put(nvmem);
898 EXPORT_SYMBOL_GPL(nvmem_cell_put);
900 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
903 int i, extra, bit_offset = cell->bit_offset;
910 /* setup rest of the bytes if any */
911 for (i = 1; i < cell->bytes; i++) {
912 /* Get bits from next byte and shift them towards msb */
913 *p |= *b << (BITS_PER_BYTE - bit_offset);
919 /* point to the msb */
920 p += cell->bytes - 1;
923 /* result fits in less bytes */
924 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
928 /* clear msb bits if any leftover in the last byte */
929 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
932 static int __nvmem_cell_read(struct nvmem_device *nvmem,
933 struct nvmem_cell *cell,
934 void *buf, size_t *len)
938 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
943 /* shift bits in-place */
944 if (cell->bit_offset || cell->nbits)
945 nvmem_shift_read_buffer_in_place(cell, buf);
954 * nvmem_cell_read() - Read a given nvmem cell
956 * @cell: nvmem cell to be read.
957 * @len: pointer to length of cell which will be populated on successful read;
960 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
961 * buffer should be freed by the consumer with a kfree().
963 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
965 struct nvmem_device *nvmem = cell->nvmem;
970 return ERR_PTR(-EINVAL);
972 buf = kzalloc(cell->bytes, GFP_KERNEL);
974 return ERR_PTR(-ENOMEM);
976 rc = __nvmem_cell_read(nvmem, cell, buf, len);
984 EXPORT_SYMBOL_GPL(nvmem_cell_read);
986 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
989 struct nvmem_device *nvmem = cell->nvmem;
990 int i, rc, nbits, bit_offset = cell->bit_offset;
991 u8 v, *p, *buf, *b, pbyte, pbits;
994 buf = kzalloc(cell->bytes, GFP_KERNEL);
996 return ERR_PTR(-ENOMEM);
998 memcpy(buf, _buf, len);
1005 /* setup the first byte with lsb bits from nvmem */
1006 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1009 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1011 /* setup rest of the byte if any */
1012 for (i = 1; i < cell->bytes; i++) {
1013 /* Get last byte bits and shift them towards lsb */
1014 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1022 /* if it's not end on byte boundary */
1023 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1024 /* setup the last byte with msb bits from nvmem */
1025 rc = nvmem_reg_read(nvmem,
1026 cell->offset + cell->bytes - 1, &v, 1);
1029 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1040 * nvmem_cell_write() - Write to a given nvmem cell
1042 * @cell: nvmem cell to be written.
1043 * @buf: Buffer to be written.
1044 * @len: length of buffer to be written to nvmem cell.
1046 * Return: length of bytes written or negative on failure.
1048 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1050 struct nvmem_device *nvmem = cell->nvmem;
1053 if (!nvmem || nvmem->read_only ||
1054 (cell->bit_offset == 0 && len != cell->bytes))
1057 if (cell->bit_offset || cell->nbits) {
1058 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1060 return PTR_ERR(buf);
1063 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1065 /* free the tmp buffer */
1066 if (cell->bit_offset || cell->nbits)
1074 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1077 * nvmem_cell_read_u16() - Read a cell value as an u16
1079 * @dev: Device that requests the nvmem cell.
1080 * @cell_id: Name of nvmem cell to read.
1081 * @val: pointer to output value.
1083 * Return: 0 on success or negative errno.
1085 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1087 struct nvmem_cell *cell;
1091 cell = nvmem_cell_get(dev, cell_id);
1093 return PTR_ERR(cell);
1095 buf = nvmem_cell_read(cell, &len);
1097 nvmem_cell_put(cell);
1098 return PTR_ERR(buf);
1100 if (len != sizeof(*val)) {
1102 nvmem_cell_put(cell);
1105 memcpy(val, buf, sizeof(*val));
1107 nvmem_cell_put(cell);
1111 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1114 * nvmem_cell_read_u32() - Read a cell value as an u32
1116 * @dev: Device that requests the nvmem cell.
1117 * @cell_id: Name of nvmem cell to read.
1118 * @val: pointer to output value.
1120 * Return: 0 on success or negative errno.
1122 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1124 struct nvmem_cell *cell;
1128 cell = nvmem_cell_get(dev, cell_id);
1130 return PTR_ERR(cell);
1132 buf = nvmem_cell_read(cell, &len);
1134 nvmem_cell_put(cell);
1135 return PTR_ERR(buf);
1137 if (len != sizeof(*val)) {
1139 nvmem_cell_put(cell);
1142 memcpy(val, buf, sizeof(*val));
1145 nvmem_cell_put(cell);
1148 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1151 * nvmem_device_cell_read() - Read a given nvmem device and cell
1153 * @nvmem: nvmem device to read from.
1154 * @info: nvmem cell info to be read.
1155 * @buf: buffer pointer which will be populated on successful read.
1157 * Return: length of successful bytes read on success and negative
1158 * error code on error.
1160 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1161 struct nvmem_cell_info *info, void *buf)
1163 struct nvmem_cell cell;
1170 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1174 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1180 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1183 * nvmem_device_cell_write() - Write cell to a given nvmem device
1185 * @nvmem: nvmem device to be written to.
1186 * @info: nvmem cell info to be written.
1187 * @buf: buffer to be written to cell.
1189 * Return: length of bytes written or negative error code on failure.
1191 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1192 struct nvmem_cell_info *info, void *buf)
1194 struct nvmem_cell cell;
1200 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1204 return nvmem_cell_write(&cell, buf, cell.bytes);
1206 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1209 * nvmem_device_read() - Read from a given nvmem device
1211 * @nvmem: nvmem device to read from.
1212 * @offset: offset in nvmem device.
1213 * @bytes: number of bytes to read.
1214 * @buf: buffer pointer which will be populated on successful read.
1216 * Return: length of successful bytes read on success and negative
1217 * error code on error.
1219 int nvmem_device_read(struct nvmem_device *nvmem,
1220 unsigned int offset,
1221 size_t bytes, void *buf)
1228 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1235 EXPORT_SYMBOL_GPL(nvmem_device_read);
1238 * nvmem_device_write() - Write cell to a given nvmem device
1240 * @nvmem: nvmem device to be written to.
1241 * @offset: offset in nvmem device.
1242 * @bytes: number of bytes to write.
1243 * @buf: buffer to be written.
1245 * Return: length of bytes written or negative error code on failure.
1247 int nvmem_device_write(struct nvmem_device *nvmem,
1248 unsigned int offset,
1249 size_t bytes, void *buf)
1256 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1264 EXPORT_SYMBOL_GPL(nvmem_device_write);
1267 * nvmem_add_cell_table() - register a table of cell info entries
1269 * @table: table of cell info entries
1271 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1273 mutex_lock(&nvmem_cell_mutex);
1274 list_add_tail(&table->node, &nvmem_cell_tables);
1275 mutex_unlock(&nvmem_cell_mutex);
1277 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1280 * nvmem_del_cell_table() - remove a previously registered cell info table
1282 * @table: table of cell info entries
1284 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1286 mutex_lock(&nvmem_cell_mutex);
1287 list_del(&table->node);
1288 mutex_unlock(&nvmem_cell_mutex);
1290 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1293 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1295 * @entries: array of cell lookup entries
1296 * @nentries: number of cell lookup entries in the array
1298 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1302 mutex_lock(&nvmem_lookup_mutex);
1303 for (i = 0; i < nentries; i++)
1304 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1305 mutex_unlock(&nvmem_lookup_mutex);
1307 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1310 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1313 * @entries: array of cell lookup entries
1314 * @nentries: number of cell lookup entries in the array
1316 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1320 mutex_lock(&nvmem_lookup_mutex);
1321 for (i = 0; i < nentries; i++)
1322 list_del(&entries[i].node);
1323 mutex_unlock(&nvmem_lookup_mutex);
1325 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1328 * nvmem_dev_name() - Get the name of a given nvmem device.
1330 * @nvmem: nvmem device.
1332 * Return: name of the nvmem device.
1334 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1336 return dev_name(&nvmem->dev);
1338 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1340 static int __init nvmem_init(void)
1342 return bus_register(&nvmem_bus_type);
1345 static void __exit nvmem_exit(void)
1347 bus_unregister(&nvmem_bus_type);
1350 subsys_initcall(nvmem_init);
1351 module_exit(nvmem_exit);
1353 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1354 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1355 MODULE_DESCRIPTION("nvmem Driver Core");
1356 MODULE_LICENSE("GPL v2");