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>
18 #include <linux/gpio/consumer.h>
20 #include <linux/slab.h>
29 struct device_node *np;
30 struct nvmem_device *nvmem;
31 struct list_head node;
34 static DEFINE_MUTEX(nvmem_mutex);
35 static DEFINE_IDA(nvmem_ida);
37 static DEFINE_MUTEX(nvmem_cell_mutex);
38 static LIST_HEAD(nvmem_cell_tables);
40 static DEFINE_MUTEX(nvmem_lookup_mutex);
41 static LIST_HEAD(nvmem_lookup_list);
43 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
46 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
47 void *val, size_t bytes)
50 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
55 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
56 void *val, size_t bytes)
60 if (nvmem->reg_write) {
61 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
62 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
63 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
70 static void nvmem_release(struct device *dev)
72 struct nvmem_device *nvmem = to_nvmem_device(dev);
74 ida_simple_remove(&nvmem_ida, nvmem->id);
78 static const struct device_type nvmem_provider_type = {
79 .release = nvmem_release,
82 static struct bus_type nvmem_bus_type = {
86 static void nvmem_cell_drop(struct nvmem_cell *cell)
88 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
89 mutex_lock(&nvmem_mutex);
90 list_del(&cell->node);
91 mutex_unlock(&nvmem_mutex);
92 of_node_put(cell->np);
93 kfree_const(cell->name);
97 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
99 struct nvmem_cell *cell, *p;
101 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
102 nvmem_cell_drop(cell);
105 static void nvmem_cell_add(struct nvmem_cell *cell)
107 mutex_lock(&nvmem_mutex);
108 list_add_tail(&cell->node, &cell->nvmem->cells);
109 mutex_unlock(&nvmem_mutex);
110 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
113 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
114 const struct nvmem_cell_info *info,
115 struct nvmem_cell *cell)
118 cell->offset = info->offset;
119 cell->bytes = info->bytes;
120 cell->name = kstrdup_const(info->name, GFP_KERNEL);
124 cell->bit_offset = info->bit_offset;
125 cell->nbits = info->nbits;
128 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
131 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
133 "cell %s unaligned to nvmem stride %d\n",
134 cell->name, nvmem->stride);
142 * nvmem_add_cells() - Add cell information to an nvmem device
144 * @nvmem: nvmem device to add cells to.
145 * @info: nvmem cell info to add to the device
146 * @ncells: number of cells in info
148 * Return: 0 or negative error code on failure.
150 static int nvmem_add_cells(struct nvmem_device *nvmem,
151 const struct nvmem_cell_info *info,
154 struct nvmem_cell **cells;
157 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
161 for (i = 0; i < ncells; i++) {
162 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
168 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
174 nvmem_cell_add(cells[i]);
177 /* remove tmp array */
183 nvmem_cell_drop(cells[i]);
191 * nvmem_register_notifier() - Register a notifier block for nvmem events.
193 * @nb: notifier block to be called on nvmem events.
195 * Return: 0 on success, negative error number on failure.
197 int nvmem_register_notifier(struct notifier_block *nb)
199 return blocking_notifier_chain_register(&nvmem_notifier, nb);
201 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
204 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
206 * @nb: notifier block to be unregistered.
208 * Return: 0 on success, negative error number on failure.
210 int nvmem_unregister_notifier(struct notifier_block *nb)
212 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
214 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
216 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
218 const struct nvmem_cell_info *info;
219 struct nvmem_cell_table *table;
220 struct nvmem_cell *cell;
223 mutex_lock(&nvmem_cell_mutex);
224 list_for_each_entry(table, &nvmem_cell_tables, node) {
225 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
226 for (i = 0; i < table->ncells; i++) {
227 info = &table->cells[i];
229 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
235 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
243 nvmem_cell_add(cell);
249 mutex_unlock(&nvmem_cell_mutex);
253 static struct nvmem_cell *
254 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
256 struct nvmem_cell *iter, *cell = NULL;
258 mutex_lock(&nvmem_mutex);
259 list_for_each_entry(iter, &nvmem->cells, node) {
260 if (strcmp(cell_id, iter->name) == 0) {
265 mutex_unlock(&nvmem_mutex);
270 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
272 struct device_node *parent, *child;
273 struct device *dev = &nvmem->dev;
274 struct nvmem_cell *cell;
278 parent = dev->of_node;
280 for_each_child_of_node(parent, child) {
281 addr = of_get_property(child, "reg", &len);
282 if (!addr || (len < 2 * sizeof(u32))) {
283 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
287 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
292 cell->np = of_node_get(child);
293 cell->offset = be32_to_cpup(addr++);
294 cell->bytes = be32_to_cpup(addr);
295 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
297 addr = of_get_property(child, "bits", &len);
298 if (addr && len == (2 * sizeof(u32))) {
299 cell->bit_offset = be32_to_cpup(addr++);
300 cell->nbits = be32_to_cpup(addr);
304 cell->bytes = DIV_ROUND_UP(
305 cell->nbits + cell->bit_offset,
308 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
309 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
310 cell->name, nvmem->stride);
311 /* Cells already added will be freed later. */
312 kfree_const(cell->name);
317 nvmem_cell_add(cell);
324 * nvmem_register() - Register a nvmem device for given nvmem_config.
325 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
327 * @config: nvmem device configuration with which nvmem device is created.
329 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
333 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
335 struct nvmem_device *nvmem;
339 return ERR_PTR(-EINVAL);
341 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
343 return ERR_PTR(-ENOMEM);
345 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
348 return ERR_PTR(rval);
351 nvmem->wp_gpio = config->wp_gpio;
353 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
355 if (IS_ERR(nvmem->wp_gpio))
356 return ERR_CAST(nvmem->wp_gpio);
359 kref_init(&nvmem->refcnt);
360 INIT_LIST_HEAD(&nvmem->cells);
363 nvmem->owner = config->owner;
364 if (!nvmem->owner && config->dev->driver)
365 nvmem->owner = config->dev->driver->owner;
366 nvmem->stride = config->stride ?: 1;
367 nvmem->word_size = config->word_size ?: 1;
368 nvmem->size = config->size;
369 nvmem->dev.type = &nvmem_provider_type;
370 nvmem->dev.bus = &nvmem_bus_type;
371 nvmem->dev.parent = config->dev;
372 nvmem->priv = config->priv;
373 nvmem->type = config->type;
374 nvmem->reg_read = config->reg_read;
375 nvmem->reg_write = config->reg_write;
376 if (!config->no_of_node)
377 nvmem->dev.of_node = config->dev->of_node;
379 if (config->id == -1 && config->name) {
380 dev_set_name(&nvmem->dev, "%s", config->name);
382 dev_set_name(&nvmem->dev, "%s%d",
383 config->name ? : "nvmem",
384 config->name ? config->id : nvmem->id);
387 nvmem->read_only = device_property_present(config->dev, "read-only") ||
388 config->read_only || !nvmem->reg_write;
390 nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
392 device_initialize(&nvmem->dev);
394 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
396 rval = device_add(&nvmem->dev);
400 if (config->compat) {
401 rval = nvmem_sysfs_setup_compat(nvmem, config);
407 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
409 goto err_teardown_compat;
412 rval = nvmem_add_cells_from_table(nvmem);
414 goto err_remove_cells;
416 rval = nvmem_add_cells_from_of(nvmem);
418 goto err_remove_cells;
420 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
425 nvmem_device_remove_all_cells(nvmem);
428 nvmem_sysfs_remove_compat(nvmem, config);
430 device_del(&nvmem->dev);
432 put_device(&nvmem->dev);
434 return ERR_PTR(rval);
436 EXPORT_SYMBOL_GPL(nvmem_register);
438 static void nvmem_device_release(struct kref *kref)
440 struct nvmem_device *nvmem;
442 nvmem = container_of(kref, struct nvmem_device, refcnt);
444 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
446 if (nvmem->flags & FLAG_COMPAT)
447 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
449 nvmem_device_remove_all_cells(nvmem);
450 device_del(&nvmem->dev);
451 put_device(&nvmem->dev);
455 * nvmem_unregister() - Unregister previously registered nvmem device
457 * @nvmem: Pointer to previously registered nvmem device.
459 void nvmem_unregister(struct nvmem_device *nvmem)
461 kref_put(&nvmem->refcnt, nvmem_device_release);
463 EXPORT_SYMBOL_GPL(nvmem_unregister);
465 static void devm_nvmem_release(struct device *dev, void *res)
467 nvmem_unregister(*(struct nvmem_device **)res);
471 * devm_nvmem_register() - Register a managed nvmem device for given
473 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
475 * @dev: Device that uses the nvmem device.
476 * @config: nvmem device configuration with which nvmem device is created.
478 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
481 struct nvmem_device *devm_nvmem_register(struct device *dev,
482 const struct nvmem_config *config)
484 struct nvmem_device **ptr, *nvmem;
486 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
488 return ERR_PTR(-ENOMEM);
490 nvmem = nvmem_register(config);
492 if (!IS_ERR(nvmem)) {
494 devres_add(dev, ptr);
501 EXPORT_SYMBOL_GPL(devm_nvmem_register);
503 static int devm_nvmem_match(struct device *dev, void *res, void *data)
505 struct nvmem_device **r = res;
511 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
514 * @dev: Device that uses the nvmem device.
515 * @nvmem: Pointer to previously registered nvmem device.
517 * Return: Will be an negative on error or a zero on success.
519 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
521 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
523 EXPORT_SYMBOL(devm_nvmem_unregister);
525 static struct nvmem_device *__nvmem_device_get(void *data,
526 int (*match)(struct device *dev, const void *data))
528 struct nvmem_device *nvmem = NULL;
531 mutex_lock(&nvmem_mutex);
532 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
534 nvmem = to_nvmem_device(dev);
535 mutex_unlock(&nvmem_mutex);
537 return ERR_PTR(-EPROBE_DEFER);
539 if (!try_module_get(nvmem->owner)) {
541 "could not increase module refcount for cell %s\n",
542 nvmem_dev_name(nvmem));
544 put_device(&nvmem->dev);
545 return ERR_PTR(-EINVAL);
548 kref_get(&nvmem->refcnt);
553 static void __nvmem_device_put(struct nvmem_device *nvmem)
555 put_device(&nvmem->dev);
556 module_put(nvmem->owner);
557 kref_put(&nvmem->refcnt, nvmem_device_release);
560 #if IS_ENABLED(CONFIG_OF)
562 * of_nvmem_device_get() - Get nvmem device from a given id
564 * @np: Device tree node that uses the nvmem device.
565 * @id: nvmem name from nvmem-names property.
567 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
570 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
573 struct device_node *nvmem_np;
577 index = of_property_match_string(np, "nvmem-names", id);
579 nvmem_np = of_parse_phandle(np, "nvmem", index);
581 return ERR_PTR(-ENOENT);
583 return __nvmem_device_get(nvmem_np, device_match_of_node);
585 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
589 * nvmem_device_get() - Get nvmem device from a given id
591 * @dev: Device that uses the nvmem device.
592 * @dev_name: name of the requested nvmem device.
594 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
597 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
599 if (dev->of_node) { /* try dt first */
600 struct nvmem_device *nvmem;
602 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
604 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
609 return __nvmem_device_get((void *)dev_name, device_match_name);
611 EXPORT_SYMBOL_GPL(nvmem_device_get);
614 * nvmem_device_find() - Find nvmem device with matching function
616 * @data: Data to pass to match function
617 * @match: Callback function to check device
619 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
622 struct nvmem_device *nvmem_device_find(void *data,
623 int (*match)(struct device *dev, const void *data))
625 return __nvmem_device_get(data, match);
627 EXPORT_SYMBOL_GPL(nvmem_device_find);
629 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
631 struct nvmem_device **nvmem = res;
633 if (WARN_ON(!nvmem || !*nvmem))
636 return *nvmem == data;
639 static void devm_nvmem_device_release(struct device *dev, void *res)
641 nvmem_device_put(*(struct nvmem_device **)res);
645 * devm_nvmem_device_put() - put alredy got nvmem device
647 * @dev: Device that uses the nvmem device.
648 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
649 * that needs to be released.
651 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
655 ret = devres_release(dev, devm_nvmem_device_release,
656 devm_nvmem_device_match, nvmem);
660 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
663 * nvmem_device_put() - put alredy got nvmem device
665 * @nvmem: pointer to nvmem device that needs to be released.
667 void nvmem_device_put(struct nvmem_device *nvmem)
669 __nvmem_device_put(nvmem);
671 EXPORT_SYMBOL_GPL(nvmem_device_put);
674 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
676 * @dev: Device that requests the nvmem device.
677 * @id: name id for the requested nvmem device.
679 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
680 * on success. The nvmem_cell will be freed by the automatically once the
683 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
685 struct nvmem_device **ptr, *nvmem;
687 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
689 return ERR_PTR(-ENOMEM);
691 nvmem = nvmem_device_get(dev, id);
692 if (!IS_ERR(nvmem)) {
694 devres_add(dev, ptr);
701 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
703 static struct nvmem_cell *
704 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
706 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
707 struct nvmem_cell_lookup *lookup;
708 struct nvmem_device *nvmem;
712 return ERR_PTR(-EINVAL);
714 dev_id = dev_name(dev);
716 mutex_lock(&nvmem_lookup_mutex);
718 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
719 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
720 (strcmp(lookup->con_id, con_id) == 0)) {
721 /* This is the right entry. */
722 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
725 /* Provider may not be registered yet. */
726 cell = ERR_CAST(nvmem);
730 cell = nvmem_find_cell_by_name(nvmem,
733 __nvmem_device_put(nvmem);
734 cell = ERR_PTR(-ENOENT);
740 mutex_unlock(&nvmem_lookup_mutex);
744 #if IS_ENABLED(CONFIG_OF)
745 static struct nvmem_cell *
746 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
748 struct nvmem_cell *iter, *cell = NULL;
750 mutex_lock(&nvmem_mutex);
751 list_for_each_entry(iter, &nvmem->cells, node) {
752 if (np == iter->np) {
757 mutex_unlock(&nvmem_mutex);
763 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
765 * @np: Device tree node that uses the nvmem cell.
766 * @id: nvmem cell name from nvmem-cell-names property, or NULL
767 * for the cell at index 0 (the lone cell with no accompanying
768 * nvmem-cell-names property).
770 * Return: Will be an ERR_PTR() on error or a valid pointer
771 * to a struct nvmem_cell. The nvmem_cell will be freed by the
774 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
776 struct device_node *cell_np, *nvmem_np;
777 struct nvmem_device *nvmem;
778 struct nvmem_cell *cell;
781 /* if cell name exists, find index to the name */
783 index = of_property_match_string(np, "nvmem-cell-names", id);
785 cell_np = of_parse_phandle(np, "nvmem-cells", index);
787 return ERR_PTR(-ENOENT);
789 nvmem_np = of_get_next_parent(cell_np);
791 return ERR_PTR(-EINVAL);
793 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
794 of_node_put(nvmem_np);
796 return ERR_CAST(nvmem);
798 cell = nvmem_find_cell_by_node(nvmem, cell_np);
800 __nvmem_device_put(nvmem);
801 return ERR_PTR(-ENOENT);
806 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
810 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
812 * @dev: Device that requests the nvmem cell.
813 * @id: nvmem cell name to get (this corresponds with the name from the
814 * nvmem-cell-names property for DT systems and with the con_id from
815 * the lookup entry for non-DT systems).
817 * Return: Will be an ERR_PTR() on error or a valid pointer
818 * to a struct nvmem_cell. The nvmem_cell will be freed by the
821 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
823 struct nvmem_cell *cell;
825 if (dev->of_node) { /* try dt first */
826 cell = of_nvmem_cell_get(dev->of_node, id);
827 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
831 /* NULL cell id only allowed for device tree; invalid otherwise */
833 return ERR_PTR(-EINVAL);
835 return nvmem_cell_get_from_lookup(dev, id);
837 EXPORT_SYMBOL_GPL(nvmem_cell_get);
839 static void devm_nvmem_cell_release(struct device *dev, void *res)
841 nvmem_cell_put(*(struct nvmem_cell **)res);
845 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
847 * @dev: Device that requests the nvmem cell.
848 * @id: nvmem cell name id to get.
850 * Return: Will be an ERR_PTR() on error or a valid pointer
851 * to a struct nvmem_cell. The nvmem_cell will be freed by the
852 * automatically once the device is freed.
854 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
856 struct nvmem_cell **ptr, *cell;
858 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
860 return ERR_PTR(-ENOMEM);
862 cell = nvmem_cell_get(dev, id);
865 devres_add(dev, ptr);
872 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
874 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
876 struct nvmem_cell **c = res;
878 if (WARN_ON(!c || !*c))
885 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
886 * from devm_nvmem_cell_get.
888 * @dev: Device that requests the nvmem cell.
889 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
891 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
895 ret = devres_release(dev, devm_nvmem_cell_release,
896 devm_nvmem_cell_match, cell);
900 EXPORT_SYMBOL(devm_nvmem_cell_put);
903 * nvmem_cell_put() - Release previously allocated nvmem cell.
905 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
907 void nvmem_cell_put(struct nvmem_cell *cell)
909 struct nvmem_device *nvmem = cell->nvmem;
911 __nvmem_device_put(nvmem);
913 EXPORT_SYMBOL_GPL(nvmem_cell_put);
915 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
918 int i, extra, bit_offset = cell->bit_offset;
925 /* setup rest of the bytes if any */
926 for (i = 1; i < cell->bytes; i++) {
927 /* Get bits from next byte and shift them towards msb */
928 *p |= *b << (BITS_PER_BYTE - bit_offset);
934 /* point to the msb */
935 p += cell->bytes - 1;
938 /* result fits in less bytes */
939 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
943 /* clear msb bits if any leftover in the last byte */
944 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
947 static int __nvmem_cell_read(struct nvmem_device *nvmem,
948 struct nvmem_cell *cell,
949 void *buf, size_t *len)
953 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
958 /* shift bits in-place */
959 if (cell->bit_offset || cell->nbits)
960 nvmem_shift_read_buffer_in_place(cell, buf);
969 * nvmem_cell_read() - Read a given nvmem cell
971 * @cell: nvmem cell to be read.
972 * @len: pointer to length of cell which will be populated on successful read;
975 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
976 * buffer should be freed by the consumer with a kfree().
978 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
980 struct nvmem_device *nvmem = cell->nvmem;
985 return ERR_PTR(-EINVAL);
987 buf = kzalloc(cell->bytes, GFP_KERNEL);
989 return ERR_PTR(-ENOMEM);
991 rc = __nvmem_cell_read(nvmem, cell, buf, len);
999 EXPORT_SYMBOL_GPL(nvmem_cell_read);
1001 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1004 struct nvmem_device *nvmem = cell->nvmem;
1005 int i, rc, nbits, bit_offset = cell->bit_offset;
1006 u8 v, *p, *buf, *b, pbyte, pbits;
1008 nbits = cell->nbits;
1009 buf = kzalloc(cell->bytes, GFP_KERNEL);
1011 return ERR_PTR(-ENOMEM);
1013 memcpy(buf, _buf, len);
1020 /* setup the first byte with lsb bits from nvmem */
1021 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1024 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1026 /* setup rest of the byte if any */
1027 for (i = 1; i < cell->bytes; i++) {
1028 /* Get last byte bits and shift them towards lsb */
1029 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1037 /* if it's not end on byte boundary */
1038 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1039 /* setup the last byte with msb bits from nvmem */
1040 rc = nvmem_reg_read(nvmem,
1041 cell->offset + cell->bytes - 1, &v, 1);
1044 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1055 * nvmem_cell_write() - Write to a given nvmem cell
1057 * @cell: nvmem cell to be written.
1058 * @buf: Buffer to be written.
1059 * @len: length of buffer to be written to nvmem cell.
1061 * Return: length of bytes written or negative on failure.
1063 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1065 struct nvmem_device *nvmem = cell->nvmem;
1068 if (!nvmem || nvmem->read_only ||
1069 (cell->bit_offset == 0 && len != cell->bytes))
1072 if (cell->bit_offset || cell->nbits) {
1073 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1075 return PTR_ERR(buf);
1078 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1080 /* free the tmp buffer */
1081 if (cell->bit_offset || cell->nbits)
1089 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1092 * nvmem_cell_read_u16() - Read a cell value as an u16
1094 * @dev: Device that requests the nvmem cell.
1095 * @cell_id: Name of nvmem cell to read.
1096 * @val: pointer to output value.
1098 * Return: 0 on success or negative errno.
1100 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1102 struct nvmem_cell *cell;
1106 cell = nvmem_cell_get(dev, cell_id);
1108 return PTR_ERR(cell);
1110 buf = nvmem_cell_read(cell, &len);
1112 nvmem_cell_put(cell);
1113 return PTR_ERR(buf);
1115 if (len != sizeof(*val)) {
1117 nvmem_cell_put(cell);
1120 memcpy(val, buf, sizeof(*val));
1122 nvmem_cell_put(cell);
1126 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1129 * nvmem_cell_read_u32() - Read a cell value as an u32
1131 * @dev: Device that requests the nvmem cell.
1132 * @cell_id: Name of nvmem cell to read.
1133 * @val: pointer to output value.
1135 * Return: 0 on success or negative errno.
1137 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1139 struct nvmem_cell *cell;
1143 cell = nvmem_cell_get(dev, cell_id);
1145 return PTR_ERR(cell);
1147 buf = nvmem_cell_read(cell, &len);
1149 nvmem_cell_put(cell);
1150 return PTR_ERR(buf);
1152 if (len != sizeof(*val)) {
1154 nvmem_cell_put(cell);
1157 memcpy(val, buf, sizeof(*val));
1160 nvmem_cell_put(cell);
1163 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1166 * nvmem_device_cell_read() - Read a given nvmem device and cell
1168 * @nvmem: nvmem device to read from.
1169 * @info: nvmem cell info to be read.
1170 * @buf: buffer pointer which will be populated on successful read.
1172 * Return: length of successful bytes read on success and negative
1173 * error code on error.
1175 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1176 struct nvmem_cell_info *info, void *buf)
1178 struct nvmem_cell cell;
1185 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1189 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1195 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1198 * nvmem_device_cell_write() - Write cell to a given nvmem device
1200 * @nvmem: nvmem device to be written to.
1201 * @info: nvmem cell info to be written.
1202 * @buf: buffer to be written to cell.
1204 * Return: length of bytes written or negative error code on failure.
1206 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1207 struct nvmem_cell_info *info, void *buf)
1209 struct nvmem_cell cell;
1215 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1219 return nvmem_cell_write(&cell, buf, cell.bytes);
1221 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1224 * nvmem_device_read() - Read from a given nvmem device
1226 * @nvmem: nvmem device to read from.
1227 * @offset: offset in nvmem device.
1228 * @bytes: number of bytes to read.
1229 * @buf: buffer pointer which will be populated on successful read.
1231 * Return: length of successful bytes read on success and negative
1232 * error code on error.
1234 int nvmem_device_read(struct nvmem_device *nvmem,
1235 unsigned int offset,
1236 size_t bytes, void *buf)
1243 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1250 EXPORT_SYMBOL_GPL(nvmem_device_read);
1253 * nvmem_device_write() - Write cell to a given nvmem device
1255 * @nvmem: nvmem device to be written to.
1256 * @offset: offset in nvmem device.
1257 * @bytes: number of bytes to write.
1258 * @buf: buffer to be written.
1260 * Return: length of bytes written or negative error code on failure.
1262 int nvmem_device_write(struct nvmem_device *nvmem,
1263 unsigned int offset,
1264 size_t bytes, void *buf)
1271 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1279 EXPORT_SYMBOL_GPL(nvmem_device_write);
1282 * nvmem_add_cell_table() - register a table of cell info entries
1284 * @table: table of cell info entries
1286 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1288 mutex_lock(&nvmem_cell_mutex);
1289 list_add_tail(&table->node, &nvmem_cell_tables);
1290 mutex_unlock(&nvmem_cell_mutex);
1292 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1295 * nvmem_del_cell_table() - remove a previously registered cell info table
1297 * @table: table of cell info entries
1299 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1301 mutex_lock(&nvmem_cell_mutex);
1302 list_del(&table->node);
1303 mutex_unlock(&nvmem_cell_mutex);
1305 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1308 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1310 * @entries: array of cell lookup entries
1311 * @nentries: number of cell lookup entries in the array
1313 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1317 mutex_lock(&nvmem_lookup_mutex);
1318 for (i = 0; i < nentries; i++)
1319 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1320 mutex_unlock(&nvmem_lookup_mutex);
1322 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1325 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1328 * @entries: array of cell lookup entries
1329 * @nentries: number of cell lookup entries in the array
1331 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1335 mutex_lock(&nvmem_lookup_mutex);
1336 for (i = 0; i < nentries; i++)
1337 list_del(&entries[i].node);
1338 mutex_unlock(&nvmem_lookup_mutex);
1340 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1343 * nvmem_dev_name() - Get the name of a given nvmem device.
1345 * @nvmem: nvmem device.
1347 * Return: name of the nvmem device.
1349 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1351 return dev_name(&nvmem->dev);
1353 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1355 static int __init nvmem_init(void)
1357 return bus_register(&nvmem_bus_type);
1360 static void __exit nvmem_exit(void)
1362 bus_unregister(&nvmem_bus_type);
1365 subsys_initcall(nvmem_init);
1366 module_exit(nvmem_exit);
1368 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1369 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1370 MODULE_DESCRIPTION("nvmem Driver Core");
1371 MODULE_LICENSE("GPL v2");