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);
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 = info->name;
122 cell->bit_offset = info->bit_offset;
123 cell->nbits = info->nbits;
126 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
129 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
131 "cell %s unaligned to nvmem stride %d\n",
132 cell->name, nvmem->stride);
140 * nvmem_add_cells() - Add cell information to an nvmem device
142 * @nvmem: nvmem device to add cells to.
143 * @info: nvmem cell info to add to the device
144 * @ncells: number of cells in info
146 * Return: 0 or negative error code on failure.
148 static int nvmem_add_cells(struct nvmem_device *nvmem,
149 const struct nvmem_cell_info *info,
152 struct nvmem_cell **cells;
155 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
159 for (i = 0; i < ncells; i++) {
160 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
166 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
172 nvmem_cell_add(cells[i]);
175 /* remove tmp array */
181 nvmem_cell_drop(cells[i]);
189 * nvmem_register_notifier() - Register a notifier block for nvmem events.
191 * @nb: notifier block to be called on nvmem events.
193 * Return: 0 on success, negative error number on failure.
195 int nvmem_register_notifier(struct notifier_block *nb)
197 return blocking_notifier_chain_register(&nvmem_notifier, nb);
199 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
202 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
204 * @nb: notifier block to be unregistered.
206 * Return: 0 on success, negative error number on failure.
208 int nvmem_unregister_notifier(struct notifier_block *nb)
210 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
212 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
214 static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
216 const struct nvmem_cell_info *info;
217 struct nvmem_cell_table *table;
218 struct nvmem_cell *cell;
221 mutex_lock(&nvmem_cell_mutex);
222 list_for_each_entry(table, &nvmem_cell_tables, node) {
223 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
224 for (i = 0; i < table->ncells; i++) {
225 info = &table->cells[i];
227 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
233 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
241 nvmem_cell_add(cell);
247 mutex_unlock(&nvmem_cell_mutex);
251 static struct nvmem_cell *
252 nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
254 struct nvmem_cell *iter, *cell = NULL;
256 mutex_lock(&nvmem_mutex);
257 list_for_each_entry(iter, &nvmem->cells, node) {
258 if (strcmp(cell_id, iter->name) == 0) {
263 mutex_unlock(&nvmem_mutex);
268 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
270 struct device_node *parent, *child;
271 struct device *dev = &nvmem->dev;
272 struct nvmem_cell *cell;
276 parent = dev->of_node;
278 for_each_child_of_node(parent, child) {
279 addr = of_get_property(child, "reg", &len);
280 if (!addr || (len < 2 * sizeof(u32))) {
281 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
285 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
290 cell->np = of_node_get(child);
291 cell->offset = be32_to_cpup(addr++);
292 cell->bytes = be32_to_cpup(addr);
293 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
295 addr = of_get_property(child, "bits", &len);
296 if (addr && len == (2 * sizeof(u32))) {
297 cell->bit_offset = be32_to_cpup(addr++);
298 cell->nbits = be32_to_cpup(addr);
302 cell->bytes = DIV_ROUND_UP(
303 cell->nbits + cell->bit_offset,
306 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
307 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
308 cell->name, nvmem->stride);
309 /* Cells already added will be freed later. */
315 nvmem_cell_add(cell);
322 * nvmem_register() - Register a nvmem device for given nvmem_config.
323 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
325 * @config: nvmem device configuration with which nvmem device is created.
327 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
331 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
333 struct nvmem_device *nvmem;
337 return ERR_PTR(-EINVAL);
339 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
341 return ERR_PTR(-ENOMEM);
343 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
346 return ERR_PTR(rval);
349 nvmem->wp_gpio = config->wp_gpio;
351 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
353 if (IS_ERR(nvmem->wp_gpio))
354 return ERR_CAST(nvmem->wp_gpio);
357 kref_init(&nvmem->refcnt);
358 INIT_LIST_HEAD(&nvmem->cells);
361 nvmem->owner = config->owner;
362 if (!nvmem->owner && config->dev->driver)
363 nvmem->owner = config->dev->driver->owner;
364 nvmem->stride = config->stride ?: 1;
365 nvmem->word_size = config->word_size ?: 1;
366 nvmem->size = config->size;
367 nvmem->dev.type = &nvmem_provider_type;
368 nvmem->dev.bus = &nvmem_bus_type;
369 nvmem->dev.parent = config->dev;
370 nvmem->priv = config->priv;
371 nvmem->type = config->type;
372 nvmem->reg_read = config->reg_read;
373 nvmem->reg_write = config->reg_write;
374 if (!config->no_of_node)
375 nvmem->dev.of_node = config->dev->of_node;
377 if (config->id == -1 && config->name) {
378 dev_set_name(&nvmem->dev, "%s", config->name);
380 dev_set_name(&nvmem->dev, "%s%d",
381 config->name ? : "nvmem",
382 config->name ? config->id : nvmem->id);
385 nvmem->read_only = device_property_present(config->dev, "read-only") ||
386 config->read_only || !nvmem->reg_write;
388 nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
390 device_initialize(&nvmem->dev);
392 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
394 rval = device_add(&nvmem->dev);
398 if (config->compat) {
399 rval = nvmem_sysfs_setup_compat(nvmem, config);
405 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
407 goto err_teardown_compat;
410 rval = nvmem_add_cells_from_table(nvmem);
412 goto err_remove_cells;
414 rval = nvmem_add_cells_from_of(nvmem);
416 goto err_remove_cells;
418 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
423 nvmem_device_remove_all_cells(nvmem);
426 nvmem_sysfs_remove_compat(nvmem, config);
428 device_del(&nvmem->dev);
430 put_device(&nvmem->dev);
432 return ERR_PTR(rval);
434 EXPORT_SYMBOL_GPL(nvmem_register);
436 static void nvmem_device_release(struct kref *kref)
438 struct nvmem_device *nvmem;
440 nvmem = container_of(kref, struct nvmem_device, refcnt);
442 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
444 if (nvmem->flags & FLAG_COMPAT)
445 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
447 nvmem_device_remove_all_cells(nvmem);
448 device_del(&nvmem->dev);
449 put_device(&nvmem->dev);
453 * nvmem_unregister() - Unregister previously registered nvmem device
455 * @nvmem: Pointer to previously registered nvmem device.
457 void nvmem_unregister(struct nvmem_device *nvmem)
459 kref_put(&nvmem->refcnt, nvmem_device_release);
461 EXPORT_SYMBOL_GPL(nvmem_unregister);
463 static void devm_nvmem_release(struct device *dev, void *res)
465 nvmem_unregister(*(struct nvmem_device **)res);
469 * devm_nvmem_register() - Register a managed nvmem device for given
471 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
473 * @dev: Device that uses the nvmem device.
474 * @config: nvmem device configuration with which nvmem device is created.
476 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
479 struct nvmem_device *devm_nvmem_register(struct device *dev,
480 const struct nvmem_config *config)
482 struct nvmem_device **ptr, *nvmem;
484 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
486 return ERR_PTR(-ENOMEM);
488 nvmem = nvmem_register(config);
490 if (!IS_ERR(nvmem)) {
492 devres_add(dev, ptr);
499 EXPORT_SYMBOL_GPL(devm_nvmem_register);
501 static int devm_nvmem_match(struct device *dev, void *res, void *data)
503 struct nvmem_device **r = res;
509 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
512 * @dev: Device that uses the nvmem device.
513 * @nvmem: Pointer to previously registered nvmem device.
515 * Return: Will be an negative on error or a zero on success.
517 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
519 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
521 EXPORT_SYMBOL(devm_nvmem_unregister);
523 static struct nvmem_device *__nvmem_device_get(void *data,
524 int (*match)(struct device *dev, const void *data))
526 struct nvmem_device *nvmem = NULL;
529 mutex_lock(&nvmem_mutex);
530 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
532 nvmem = to_nvmem_device(dev);
533 mutex_unlock(&nvmem_mutex);
535 return ERR_PTR(-EPROBE_DEFER);
537 if (!try_module_get(nvmem->owner)) {
539 "could not increase module refcount for cell %s\n",
540 nvmem_dev_name(nvmem));
542 put_device(&nvmem->dev);
543 return ERR_PTR(-EINVAL);
546 kref_get(&nvmem->refcnt);
551 static void __nvmem_device_put(struct nvmem_device *nvmem)
553 put_device(&nvmem->dev);
554 module_put(nvmem->owner);
555 kref_put(&nvmem->refcnt, nvmem_device_release);
558 #if IS_ENABLED(CONFIG_OF)
560 * of_nvmem_device_get() - Get nvmem device from a given id
562 * @np: Device tree node that uses the nvmem device.
563 * @id: nvmem name from nvmem-names property.
565 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
568 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
571 struct device_node *nvmem_np;
575 index = of_property_match_string(np, "nvmem-names", id);
577 nvmem_np = of_parse_phandle(np, "nvmem", index);
579 return ERR_PTR(-ENOENT);
581 return __nvmem_device_get(nvmem_np, device_match_of_node);
583 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
587 * nvmem_device_get() - Get nvmem device from a given id
589 * @dev: Device that uses the nvmem device.
590 * @dev_name: name of the requested nvmem device.
592 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
595 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
597 if (dev->of_node) { /* try dt first */
598 struct nvmem_device *nvmem;
600 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
602 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
607 return __nvmem_device_get((void *)dev_name, device_match_name);
609 EXPORT_SYMBOL_GPL(nvmem_device_get);
612 * nvmem_device_find() - Find nvmem device with matching function
614 * @data: Data to pass to match function
615 * @match: Callback function to check device
617 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
620 struct nvmem_device *nvmem_device_find(void *data,
621 int (*match)(struct device *dev, const void *data))
623 return __nvmem_device_get(data, match);
625 EXPORT_SYMBOL_GPL(nvmem_device_find);
627 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
629 struct nvmem_device **nvmem = res;
631 if (WARN_ON(!nvmem || !*nvmem))
634 return *nvmem == data;
637 static void devm_nvmem_device_release(struct device *dev, void *res)
639 nvmem_device_put(*(struct nvmem_device **)res);
643 * devm_nvmem_device_put() - put alredy got nvmem device
645 * @dev: Device that uses the nvmem device.
646 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
647 * that needs to be released.
649 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
653 ret = devres_release(dev, devm_nvmem_device_release,
654 devm_nvmem_device_match, nvmem);
658 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
661 * nvmem_device_put() - put alredy got nvmem device
663 * @nvmem: pointer to nvmem device that needs to be released.
665 void nvmem_device_put(struct nvmem_device *nvmem)
667 __nvmem_device_put(nvmem);
669 EXPORT_SYMBOL_GPL(nvmem_device_put);
672 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
674 * @dev: Device that requests the nvmem device.
675 * @id: name id for the requested nvmem device.
677 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
678 * on success. The nvmem_cell will be freed by the automatically once the
681 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
683 struct nvmem_device **ptr, *nvmem;
685 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
687 return ERR_PTR(-ENOMEM);
689 nvmem = nvmem_device_get(dev, id);
690 if (!IS_ERR(nvmem)) {
692 devres_add(dev, ptr);
699 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
701 static struct nvmem_cell *
702 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
704 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
705 struct nvmem_cell_lookup *lookup;
706 struct nvmem_device *nvmem;
710 return ERR_PTR(-EINVAL);
712 dev_id = dev_name(dev);
714 mutex_lock(&nvmem_lookup_mutex);
716 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
717 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
718 (strcmp(lookup->con_id, con_id) == 0)) {
719 /* This is the right entry. */
720 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
723 /* Provider may not be registered yet. */
724 cell = ERR_CAST(nvmem);
728 cell = nvmem_find_cell_by_name(nvmem,
731 __nvmem_device_put(nvmem);
732 cell = ERR_PTR(-ENOENT);
738 mutex_unlock(&nvmem_lookup_mutex);
742 #if IS_ENABLED(CONFIG_OF)
743 static struct nvmem_cell *
744 nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
746 struct nvmem_cell *iter, *cell = NULL;
748 mutex_lock(&nvmem_mutex);
749 list_for_each_entry(iter, &nvmem->cells, node) {
750 if (np == iter->np) {
755 mutex_unlock(&nvmem_mutex);
761 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
763 * @np: Device tree node that uses the nvmem cell.
764 * @id: nvmem cell name from nvmem-cell-names property, or NULL
765 * for the cell at index 0 (the lone cell with no accompanying
766 * nvmem-cell-names property).
768 * Return: Will be an ERR_PTR() on error or a valid pointer
769 * to a struct nvmem_cell. The nvmem_cell will be freed by the
772 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
774 struct device_node *cell_np, *nvmem_np;
775 struct nvmem_device *nvmem;
776 struct nvmem_cell *cell;
779 /* if cell name exists, find index to the name */
781 index = of_property_match_string(np, "nvmem-cell-names", id);
783 cell_np = of_parse_phandle(np, "nvmem-cells", index);
785 return ERR_PTR(-ENOENT);
787 nvmem_np = of_get_next_parent(cell_np);
789 return ERR_PTR(-EINVAL);
791 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
792 of_node_put(nvmem_np);
794 return ERR_CAST(nvmem);
796 cell = nvmem_find_cell_by_node(nvmem, cell_np);
798 __nvmem_device_put(nvmem);
799 return ERR_PTR(-ENOENT);
804 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
808 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
810 * @dev: Device that requests the nvmem cell.
811 * @id: nvmem cell name to get (this corresponds with the name from the
812 * nvmem-cell-names property for DT systems and with the con_id from
813 * the lookup entry for non-DT systems).
815 * Return: Will be an ERR_PTR() on error or a valid pointer
816 * to a struct nvmem_cell. The nvmem_cell will be freed by the
819 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
821 struct nvmem_cell *cell;
823 if (dev->of_node) { /* try dt first */
824 cell = of_nvmem_cell_get(dev->of_node, id);
825 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
829 /* NULL cell id only allowed for device tree; invalid otherwise */
831 return ERR_PTR(-EINVAL);
833 return nvmem_cell_get_from_lookup(dev, id);
835 EXPORT_SYMBOL_GPL(nvmem_cell_get);
837 static void devm_nvmem_cell_release(struct device *dev, void *res)
839 nvmem_cell_put(*(struct nvmem_cell **)res);
843 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
845 * @dev: Device that requests the nvmem cell.
846 * @id: nvmem cell name id to get.
848 * Return: Will be an ERR_PTR() on error or a valid pointer
849 * to a struct nvmem_cell. The nvmem_cell will be freed by the
850 * automatically once the device is freed.
852 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
854 struct nvmem_cell **ptr, *cell;
856 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
858 return ERR_PTR(-ENOMEM);
860 cell = nvmem_cell_get(dev, id);
863 devres_add(dev, ptr);
870 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
872 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
874 struct nvmem_cell **c = res;
876 if (WARN_ON(!c || !*c))
883 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
884 * from devm_nvmem_cell_get.
886 * @dev: Device that requests the nvmem cell.
887 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
889 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
893 ret = devres_release(dev, devm_nvmem_cell_release,
894 devm_nvmem_cell_match, cell);
898 EXPORT_SYMBOL(devm_nvmem_cell_put);
901 * nvmem_cell_put() - Release previously allocated nvmem cell.
903 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
905 void nvmem_cell_put(struct nvmem_cell *cell)
907 struct nvmem_device *nvmem = cell->nvmem;
909 __nvmem_device_put(nvmem);
911 EXPORT_SYMBOL_GPL(nvmem_cell_put);
913 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
916 int i, extra, bit_offset = cell->bit_offset;
923 /* setup rest of the bytes if any */
924 for (i = 1; i < cell->bytes; i++) {
925 /* Get bits from next byte and shift them towards msb */
926 *p |= *b << (BITS_PER_BYTE - bit_offset);
932 /* point to the msb */
933 p += cell->bytes - 1;
936 /* result fits in less bytes */
937 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
941 /* clear msb bits if any leftover in the last byte */
942 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
945 static int __nvmem_cell_read(struct nvmem_device *nvmem,
946 struct nvmem_cell *cell,
947 void *buf, size_t *len)
951 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
956 /* shift bits in-place */
957 if (cell->bit_offset || cell->nbits)
958 nvmem_shift_read_buffer_in_place(cell, buf);
967 * nvmem_cell_read() - Read a given nvmem cell
969 * @cell: nvmem cell to be read.
970 * @len: pointer to length of cell which will be populated on successful read;
973 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
974 * buffer should be freed by the consumer with a kfree().
976 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
978 struct nvmem_device *nvmem = cell->nvmem;
983 return ERR_PTR(-EINVAL);
985 buf = kzalloc(cell->bytes, GFP_KERNEL);
987 return ERR_PTR(-ENOMEM);
989 rc = __nvmem_cell_read(nvmem, cell, buf, len);
997 EXPORT_SYMBOL_GPL(nvmem_cell_read);
999 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1002 struct nvmem_device *nvmem = cell->nvmem;
1003 int i, rc, nbits, bit_offset = cell->bit_offset;
1004 u8 v, *p, *buf, *b, pbyte, pbits;
1006 nbits = cell->nbits;
1007 buf = kzalloc(cell->bytes, GFP_KERNEL);
1009 return ERR_PTR(-ENOMEM);
1011 memcpy(buf, _buf, len);
1018 /* setup the first byte with lsb bits from nvmem */
1019 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1022 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1024 /* setup rest of the byte if any */
1025 for (i = 1; i < cell->bytes; i++) {
1026 /* Get last byte bits and shift them towards lsb */
1027 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1035 /* if it's not end on byte boundary */
1036 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1037 /* setup the last byte with msb bits from nvmem */
1038 rc = nvmem_reg_read(nvmem,
1039 cell->offset + cell->bytes - 1, &v, 1);
1042 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1053 * nvmem_cell_write() - Write to a given nvmem cell
1055 * @cell: nvmem cell to be written.
1056 * @buf: Buffer to be written.
1057 * @len: length of buffer to be written to nvmem cell.
1059 * Return: length of bytes written or negative on failure.
1061 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1063 struct nvmem_device *nvmem = cell->nvmem;
1066 if (!nvmem || nvmem->read_only ||
1067 (cell->bit_offset == 0 && len != cell->bytes))
1070 if (cell->bit_offset || cell->nbits) {
1071 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1073 return PTR_ERR(buf);
1076 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1078 /* free the tmp buffer */
1079 if (cell->bit_offset || cell->nbits)
1087 EXPORT_SYMBOL_GPL(nvmem_cell_write);
1090 * nvmem_cell_read_u16() - Read a cell value as an u16
1092 * @dev: Device that requests the nvmem cell.
1093 * @cell_id: Name of nvmem cell to read.
1094 * @val: pointer to output value.
1096 * Return: 0 on success or negative errno.
1098 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1100 struct nvmem_cell *cell;
1104 cell = nvmem_cell_get(dev, cell_id);
1106 return PTR_ERR(cell);
1108 buf = nvmem_cell_read(cell, &len);
1110 nvmem_cell_put(cell);
1111 return PTR_ERR(buf);
1113 if (len != sizeof(*val)) {
1115 nvmem_cell_put(cell);
1118 memcpy(val, buf, sizeof(*val));
1120 nvmem_cell_put(cell);
1124 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1127 * nvmem_cell_read_u32() - Read a cell value as an u32
1129 * @dev: Device that requests the nvmem cell.
1130 * @cell_id: Name of nvmem cell to read.
1131 * @val: pointer to output value.
1133 * Return: 0 on success or negative errno.
1135 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1137 struct nvmem_cell *cell;
1141 cell = nvmem_cell_get(dev, cell_id);
1143 return PTR_ERR(cell);
1145 buf = nvmem_cell_read(cell, &len);
1147 nvmem_cell_put(cell);
1148 return PTR_ERR(buf);
1150 if (len != sizeof(*val)) {
1152 nvmem_cell_put(cell);
1155 memcpy(val, buf, sizeof(*val));
1158 nvmem_cell_put(cell);
1161 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1164 * nvmem_device_cell_read() - Read a given nvmem device and cell
1166 * @nvmem: nvmem device to read from.
1167 * @info: nvmem cell info to be read.
1168 * @buf: buffer pointer which will be populated on successful read.
1170 * Return: length of successful bytes read on success and negative
1171 * error code on error.
1173 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1174 struct nvmem_cell_info *info, void *buf)
1176 struct nvmem_cell cell;
1183 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1187 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1193 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1196 * nvmem_device_cell_write() - Write cell to a given nvmem device
1198 * @nvmem: nvmem device to be written to.
1199 * @info: nvmem cell info to be written.
1200 * @buf: buffer to be written to cell.
1202 * Return: length of bytes written or negative error code on failure.
1204 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1205 struct nvmem_cell_info *info, void *buf)
1207 struct nvmem_cell cell;
1213 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1217 return nvmem_cell_write(&cell, buf, cell.bytes);
1219 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1222 * nvmem_device_read() - Read from a given nvmem device
1224 * @nvmem: nvmem device to read from.
1225 * @offset: offset in nvmem device.
1226 * @bytes: number of bytes to read.
1227 * @buf: buffer pointer which will be populated on successful read.
1229 * Return: length of successful bytes read on success and negative
1230 * error code on error.
1232 int nvmem_device_read(struct nvmem_device *nvmem,
1233 unsigned int offset,
1234 size_t bytes, void *buf)
1241 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1248 EXPORT_SYMBOL_GPL(nvmem_device_read);
1251 * nvmem_device_write() - Write cell to a given nvmem device
1253 * @nvmem: nvmem device to be written to.
1254 * @offset: offset in nvmem device.
1255 * @bytes: number of bytes to write.
1256 * @buf: buffer to be written.
1258 * Return: length of bytes written or negative error code on failure.
1260 int nvmem_device_write(struct nvmem_device *nvmem,
1261 unsigned int offset,
1262 size_t bytes, void *buf)
1269 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1277 EXPORT_SYMBOL_GPL(nvmem_device_write);
1280 * nvmem_add_cell_table() - register a table of cell info entries
1282 * @table: table of cell info entries
1284 void nvmem_add_cell_table(struct nvmem_cell_table *table)
1286 mutex_lock(&nvmem_cell_mutex);
1287 list_add_tail(&table->node, &nvmem_cell_tables);
1288 mutex_unlock(&nvmem_cell_mutex);
1290 EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1293 * nvmem_del_cell_table() - remove a previously registered cell info table
1295 * @table: table of cell info entries
1297 void nvmem_del_cell_table(struct nvmem_cell_table *table)
1299 mutex_lock(&nvmem_cell_mutex);
1300 list_del(&table->node);
1301 mutex_unlock(&nvmem_cell_mutex);
1303 EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1306 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1308 * @entries: array of cell lookup entries
1309 * @nentries: number of cell lookup entries in the array
1311 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1315 mutex_lock(&nvmem_lookup_mutex);
1316 for (i = 0; i < nentries; i++)
1317 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1318 mutex_unlock(&nvmem_lookup_mutex);
1320 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1323 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1326 * @entries: array of cell lookup entries
1327 * @nentries: number of cell lookup entries in the array
1329 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1333 mutex_lock(&nvmem_lookup_mutex);
1334 for (i = 0; i < nentries; i++)
1335 list_del(&entries[i].node);
1336 mutex_unlock(&nvmem_lookup_mutex);
1338 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1341 * nvmem_dev_name() - Get the name of a given nvmem device.
1343 * @nvmem: nvmem device.
1345 * Return: name of the nvmem device.
1347 const char *nvmem_dev_name(struct nvmem_device *nvmem)
1349 return dev_name(&nvmem->dev);
1351 EXPORT_SYMBOL_GPL(nvmem_dev_name);
1353 static int __init nvmem_init(void)
1355 return bus_register(&nvmem_bus_type);
1358 static void __exit nvmem_exit(void)
1360 bus_unregister(&nvmem_bus_type);
1363 subsys_initcall(nvmem_init);
1364 module_exit(nvmem_exit);
1366 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1367 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1368 MODULE_DESCRIPTION("nvmem Driver Core");
1369 MODULE_LICENSE("GPL v2");