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>
34 struct bin_attribute eeprom
;
35 struct device
*base_dev
;
36 struct list_head cells
;
37 nvmem_reg_read_t reg_read
;
38 nvmem_reg_write_t reg_write
;
39 struct gpio_desc
*wp_gpio
;
43 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
45 #define FLAG_COMPAT BIT(0)
53 struct device_node
*np
;
54 struct nvmem_device
*nvmem
;
55 struct list_head node
;
58 static DEFINE_MUTEX(nvmem_mutex
);
59 static DEFINE_IDA(nvmem_ida
);
61 static DEFINE_MUTEX(nvmem_cell_mutex
);
62 static LIST_HEAD(nvmem_cell_tables
);
64 static DEFINE_MUTEX(nvmem_lookup_mutex
);
65 static LIST_HEAD(nvmem_lookup_list
);
67 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier
);
69 #ifdef CONFIG_NVMEM_SYSFS
70 static const char * const nvmem_type_str
[] = {
71 [NVMEM_TYPE_UNKNOWN
] = "Unknown",
72 [NVMEM_TYPE_EEPROM
] = "EEPROM",
73 [NVMEM_TYPE_OTP
] = "OTP",
74 [NVMEM_TYPE_BATTERY_BACKED
] = "Battery backed",
77 #ifdef CONFIG_DEBUG_LOCK_ALLOC
78 static struct lock_class_key eeprom_lock_key
;
81 static ssize_t
type_show(struct device
*dev
,
82 struct device_attribute
*attr
, char *buf
)
84 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
86 return sprintf(buf
, "%s\n", nvmem_type_str
[nvmem
->type
]);
89 static DEVICE_ATTR_RO(type
);
91 static struct attribute
*nvmem_attrs
[] = {
96 static ssize_t
bin_attr_nvmem_read(struct file
*filp
, struct kobject
*kobj
,
97 struct bin_attribute
*attr
, char *buf
,
98 loff_t pos
, size_t count
)
101 struct nvmem_device
*nvmem
;
107 dev
= container_of(kobj
, struct device
, kobj
);
108 nvmem
= to_nvmem_device(dev
);
110 /* Stop the user from reading */
111 if (pos
>= nvmem
->size
)
114 if (count
< nvmem
->word_size
)
117 if (pos
+ count
> nvmem
->size
)
118 count
= nvmem
->size
- pos
;
120 count
= round_down(count
, nvmem
->word_size
);
122 if (!nvmem
->reg_read
)
125 rc
= nvmem
->reg_read(nvmem
->priv
, pos
, buf
, count
);
133 static ssize_t
bin_attr_nvmem_write(struct file
*filp
, struct kobject
*kobj
,
134 struct bin_attribute
*attr
, char *buf
,
135 loff_t pos
, size_t count
)
138 struct nvmem_device
*nvmem
;
144 dev
= container_of(kobj
, struct device
, kobj
);
145 nvmem
= to_nvmem_device(dev
);
147 /* Stop the user from writing */
148 if (pos
>= nvmem
->size
)
151 if (count
< nvmem
->word_size
)
154 if (pos
+ count
> nvmem
->size
)
155 count
= nvmem
->size
- pos
;
157 count
= round_down(count
, nvmem
->word_size
);
159 if (!nvmem
->reg_write
)
162 rc
= nvmem
->reg_write(nvmem
->priv
, pos
, buf
, count
);
170 static umode_t
nvmem_bin_attr_is_visible(struct kobject
*kobj
,
171 struct bin_attribute
*attr
, int i
)
173 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
174 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
177 if (!nvmem
->root_only
)
180 if (!nvmem
->read_only
)
183 if (!nvmem
->reg_write
)
186 if (!nvmem
->reg_read
)
192 /* default read/write permissions */
193 static struct bin_attribute bin_attr_rw_nvmem
= {
198 .read
= bin_attr_nvmem_read
,
199 .write
= bin_attr_nvmem_write
,
202 static struct bin_attribute
*nvmem_bin_attributes
[] = {
207 static const struct attribute_group nvmem_bin_group
= {
208 .bin_attrs
= nvmem_bin_attributes
,
209 .attrs
= nvmem_attrs
,
210 .is_bin_visible
= nvmem_bin_attr_is_visible
,
213 static const struct attribute_group
*nvmem_dev_groups
[] = {
218 /* read only permission */
219 static struct bin_attribute bin_attr_ro_nvmem
= {
224 .read
= bin_attr_nvmem_read
,
227 /* default read/write permissions, root only */
228 static struct bin_attribute bin_attr_rw_root_nvmem
= {
233 .read
= bin_attr_nvmem_read
,
234 .write
= bin_attr_nvmem_write
,
237 /* read only permission, root only */
238 static struct bin_attribute bin_attr_ro_root_nvmem
= {
243 .read
= bin_attr_nvmem_read
,
247 * nvmem_setup_compat() - Create an additional binary entry in
248 * drivers sys directory, to be backwards compatible with the older
249 * drivers/misc/eeprom drivers.
251 static int nvmem_sysfs_setup_compat(struct nvmem_device
*nvmem
,
252 const struct nvmem_config
*config
)
259 if (!config
->base_dev
)
262 if (nvmem
->read_only
) {
263 if (config
->root_only
)
264 nvmem
->eeprom
= bin_attr_ro_root_nvmem
;
266 nvmem
->eeprom
= bin_attr_ro_nvmem
;
268 if (config
->root_only
)
269 nvmem
->eeprom
= bin_attr_rw_root_nvmem
;
271 nvmem
->eeprom
= bin_attr_rw_nvmem
;
273 nvmem
->eeprom
.attr
.name
= "eeprom";
274 nvmem
->eeprom
.size
= nvmem
->size
;
275 #ifdef CONFIG_DEBUG_LOCK_ALLOC
276 nvmem
->eeprom
.attr
.key
= &eeprom_lock_key
;
278 nvmem
->eeprom
.private = &nvmem
->dev
;
279 nvmem
->base_dev
= config
->base_dev
;
281 rval
= device_create_bin_file(nvmem
->base_dev
, &nvmem
->eeprom
);
284 "Failed to create eeprom binary file %d\n", rval
);
288 nvmem
->flags
|= FLAG_COMPAT
;
293 static void nvmem_sysfs_remove_compat(struct nvmem_device
*nvmem
,
294 const struct nvmem_config
*config
)
297 device_remove_bin_file(nvmem
->base_dev
, &nvmem
->eeprom
);
300 #else /* CONFIG_NVMEM_SYSFS */
302 static int nvmem_sysfs_setup_compat(struct nvmem_device
*nvmem
,
303 const struct nvmem_config
*config
)
307 static void nvmem_sysfs_remove_compat(struct nvmem_device
*nvmem
,
308 const struct nvmem_config
*config
)
312 #endif /* CONFIG_NVMEM_SYSFS */
314 static int nvmem_reg_read(struct nvmem_device
*nvmem
, unsigned int offset
,
315 void *val
, size_t bytes
)
318 return nvmem
->reg_read(nvmem
->priv
, offset
, val
, bytes
);
323 static int nvmem_reg_write(struct nvmem_device
*nvmem
, unsigned int offset
,
324 void *val
, size_t bytes
)
328 if (nvmem
->reg_write
) {
329 gpiod_set_value_cansleep(nvmem
->wp_gpio
, 0);
330 ret
= nvmem
->reg_write(nvmem
->priv
, offset
, val
, bytes
);
331 gpiod_set_value_cansleep(nvmem
->wp_gpio
, 1);
338 static void nvmem_release(struct device
*dev
)
340 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
342 ida_simple_remove(&nvmem_ida
, nvmem
->id
);
343 gpiod_put(nvmem
->wp_gpio
);
347 static const struct device_type nvmem_provider_type
= {
348 .release
= nvmem_release
,
351 static struct bus_type nvmem_bus_type
= {
355 static void nvmem_cell_drop(struct nvmem_cell
*cell
)
357 blocking_notifier_call_chain(&nvmem_notifier
, NVMEM_CELL_REMOVE
, cell
);
358 mutex_lock(&nvmem_mutex
);
359 list_del(&cell
->node
);
360 mutex_unlock(&nvmem_mutex
);
361 of_node_put(cell
->np
);
362 kfree_const(cell
->name
);
366 static void nvmem_device_remove_all_cells(const struct nvmem_device
*nvmem
)
368 struct nvmem_cell
*cell
, *p
;
370 list_for_each_entry_safe(cell
, p
, &nvmem
->cells
, node
)
371 nvmem_cell_drop(cell
);
374 static void nvmem_cell_add(struct nvmem_cell
*cell
)
376 mutex_lock(&nvmem_mutex
);
377 list_add_tail(&cell
->node
, &cell
->nvmem
->cells
);
378 mutex_unlock(&nvmem_mutex
);
379 blocking_notifier_call_chain(&nvmem_notifier
, NVMEM_CELL_ADD
, cell
);
382 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device
*nvmem
,
383 const struct nvmem_cell_info
*info
,
384 struct nvmem_cell
*cell
)
387 cell
->offset
= info
->offset
;
388 cell
->bytes
= info
->bytes
;
389 cell
->name
= kstrdup_const(info
->name
, GFP_KERNEL
);
393 cell
->bit_offset
= info
->bit_offset
;
394 cell
->nbits
= info
->nbits
;
397 cell
->bytes
= DIV_ROUND_UP(cell
->nbits
+ cell
->bit_offset
,
400 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
402 "cell %s unaligned to nvmem stride %d\n",
403 cell
->name
, nvmem
->stride
);
411 * nvmem_add_cells() - Add cell information to an nvmem device
413 * @nvmem: nvmem device to add cells to.
414 * @info: nvmem cell info to add to the device
415 * @ncells: number of cells in info
417 * Return: 0 or negative error code on failure.
419 static int nvmem_add_cells(struct nvmem_device
*nvmem
,
420 const struct nvmem_cell_info
*info
,
423 struct nvmem_cell
**cells
;
426 cells
= kcalloc(ncells
, sizeof(*cells
), GFP_KERNEL
);
430 for (i
= 0; i
< ncells
; i
++) {
431 cells
[i
] = kzalloc(sizeof(**cells
), GFP_KERNEL
);
437 rval
= nvmem_cell_info_to_nvmem_cell(nvmem
, &info
[i
], cells
[i
]);
443 nvmem_cell_add(cells
[i
]);
446 /* remove tmp array */
452 nvmem_cell_drop(cells
[i
]);
460 * nvmem_register_notifier() - Register a notifier block for nvmem events.
462 * @nb: notifier block to be called on nvmem events.
464 * Return: 0 on success, negative error number on failure.
466 int nvmem_register_notifier(struct notifier_block
*nb
)
468 return blocking_notifier_chain_register(&nvmem_notifier
, nb
);
470 EXPORT_SYMBOL_GPL(nvmem_register_notifier
);
473 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
475 * @nb: notifier block to be unregistered.
477 * Return: 0 on success, negative error number on failure.
479 int nvmem_unregister_notifier(struct notifier_block
*nb
)
481 return blocking_notifier_chain_unregister(&nvmem_notifier
, nb
);
483 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier
);
485 static int nvmem_add_cells_from_table(struct nvmem_device
*nvmem
)
487 const struct nvmem_cell_info
*info
;
488 struct nvmem_cell_table
*table
;
489 struct nvmem_cell
*cell
;
492 mutex_lock(&nvmem_cell_mutex
);
493 list_for_each_entry(table
, &nvmem_cell_tables
, node
) {
494 if (strcmp(nvmem_dev_name(nvmem
), table
->nvmem_name
) == 0) {
495 for (i
= 0; i
< table
->ncells
; i
++) {
496 info
= &table
->cells
[i
];
498 cell
= kzalloc(sizeof(*cell
), GFP_KERNEL
);
504 rval
= nvmem_cell_info_to_nvmem_cell(nvmem
,
512 nvmem_cell_add(cell
);
518 mutex_unlock(&nvmem_cell_mutex
);
522 static struct nvmem_cell
*
523 nvmem_find_cell_by_name(struct nvmem_device
*nvmem
, const char *cell_id
)
525 struct nvmem_cell
*iter
, *cell
= NULL
;
527 mutex_lock(&nvmem_mutex
);
528 list_for_each_entry(iter
, &nvmem
->cells
, node
) {
529 if (strcmp(cell_id
, iter
->name
) == 0) {
534 mutex_unlock(&nvmem_mutex
);
539 static int nvmem_add_cells_from_of(struct nvmem_device
*nvmem
)
541 struct device_node
*parent
, *child
;
542 struct device
*dev
= &nvmem
->dev
;
543 struct nvmem_cell
*cell
;
547 parent
= dev
->of_node
;
549 for_each_child_of_node(parent
, child
) {
550 addr
= of_get_property(child
, "reg", &len
);
551 if (!addr
|| (len
< 2 * sizeof(u32
))) {
552 dev_err(dev
, "nvmem: invalid reg on %pOF\n", child
);
556 cell
= kzalloc(sizeof(*cell
), GFP_KERNEL
);
561 cell
->np
= of_node_get(child
);
562 cell
->offset
= be32_to_cpup(addr
++);
563 cell
->bytes
= be32_to_cpup(addr
);
564 cell
->name
= kasprintf(GFP_KERNEL
, "%pOFn", child
);
566 addr
= of_get_property(child
, "bits", &len
);
567 if (addr
&& len
== (2 * sizeof(u32
))) {
568 cell
->bit_offset
= be32_to_cpup(addr
++);
569 cell
->nbits
= be32_to_cpup(addr
);
573 cell
->bytes
= DIV_ROUND_UP(
574 cell
->nbits
+ cell
->bit_offset
,
577 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
578 dev_err(dev
, "cell %s unaligned to nvmem stride %d\n",
579 cell
->name
, nvmem
->stride
);
580 /* Cells already added will be freed later. */
581 kfree_const(cell
->name
);
586 nvmem_cell_add(cell
);
593 * nvmem_register() - Register a nvmem device for given nvmem_config.
594 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
596 * @config: nvmem device configuration with which nvmem device is created.
598 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
602 struct nvmem_device
*nvmem_register(const struct nvmem_config
*config
)
604 struct nvmem_device
*nvmem
;
608 return ERR_PTR(-EINVAL
);
610 if (!config
->reg_read
&& !config
->reg_write
)
611 return ERR_PTR(-EINVAL
);
613 nvmem
= kzalloc(sizeof(*nvmem
), GFP_KERNEL
);
615 return ERR_PTR(-ENOMEM
);
617 rval
= ida_simple_get(&nvmem_ida
, 0, 0, GFP_KERNEL
);
620 return ERR_PTR(rval
);
624 nvmem
->wp_gpio
= config
->wp_gpio
;
626 nvmem
->wp_gpio
= gpiod_get_optional(config
->dev
, "wp",
628 if (IS_ERR(nvmem
->wp_gpio
)) {
629 ida_simple_remove(&nvmem_ida
, nvmem
->id
);
630 rval
= PTR_ERR(nvmem
->wp_gpio
);
632 return ERR_PTR(rval
);
635 kref_init(&nvmem
->refcnt
);
636 INIT_LIST_HEAD(&nvmem
->cells
);
639 nvmem
->owner
= config
->owner
;
640 if (!nvmem
->owner
&& config
->dev
->driver
)
641 nvmem
->owner
= config
->dev
->driver
->owner
;
642 nvmem
->stride
= config
->stride
?: 1;
643 nvmem
->word_size
= config
->word_size
?: 1;
644 nvmem
->size
= config
->size
;
645 nvmem
->dev
.type
= &nvmem_provider_type
;
646 nvmem
->dev
.bus
= &nvmem_bus_type
;
647 nvmem
->dev
.parent
= config
->dev
;
648 nvmem
->root_only
= config
->root_only
;
649 nvmem
->priv
= config
->priv
;
650 nvmem
->type
= config
->type
;
651 nvmem
->reg_read
= config
->reg_read
;
652 nvmem
->reg_write
= config
->reg_write
;
653 if (!config
->no_of_node
)
654 nvmem
->dev
.of_node
= config
->dev
->of_node
;
656 if (config
->id
== -1 && config
->name
) {
657 dev_set_name(&nvmem
->dev
, "%s", config
->name
);
659 dev_set_name(&nvmem
->dev
, "%s%d",
660 config
->name
? : "nvmem",
661 config
->name
? config
->id
: nvmem
->id
);
664 nvmem
->read_only
= device_property_present(config
->dev
, "read-only") ||
665 config
->read_only
|| !nvmem
->reg_write
;
667 #ifdef CONFIG_NVMEM_SYSFS
668 nvmem
->dev
.groups
= nvmem_dev_groups
;
671 dev_dbg(&nvmem
->dev
, "Registering nvmem device %s\n", config
->name
);
673 rval
= device_register(&nvmem
->dev
);
677 if (config
->compat
) {
678 rval
= nvmem_sysfs_setup_compat(nvmem
, config
);
684 rval
= nvmem_add_cells(nvmem
, config
->cells
, config
->ncells
);
686 goto err_teardown_compat
;
689 rval
= nvmem_add_cells_from_table(nvmem
);
691 goto err_remove_cells
;
693 rval
= nvmem_add_cells_from_of(nvmem
);
695 goto err_remove_cells
;
697 blocking_notifier_call_chain(&nvmem_notifier
, NVMEM_ADD
, nvmem
);
702 nvmem_device_remove_all_cells(nvmem
);
705 nvmem_sysfs_remove_compat(nvmem
, config
);
707 device_del(&nvmem
->dev
);
709 put_device(&nvmem
->dev
);
711 return ERR_PTR(rval
);
713 EXPORT_SYMBOL_GPL(nvmem_register
);
715 static void nvmem_device_release(struct kref
*kref
)
717 struct nvmem_device
*nvmem
;
719 nvmem
= container_of(kref
, struct nvmem_device
, refcnt
);
721 blocking_notifier_call_chain(&nvmem_notifier
, NVMEM_REMOVE
, nvmem
);
723 if (nvmem
->flags
& FLAG_COMPAT
)
724 device_remove_bin_file(nvmem
->base_dev
, &nvmem
->eeprom
);
726 nvmem_device_remove_all_cells(nvmem
);
727 device_unregister(&nvmem
->dev
);
731 * nvmem_unregister() - Unregister previously registered nvmem device
733 * @nvmem: Pointer to previously registered nvmem device.
735 void nvmem_unregister(struct nvmem_device
*nvmem
)
737 kref_put(&nvmem
->refcnt
, nvmem_device_release
);
739 EXPORT_SYMBOL_GPL(nvmem_unregister
);
741 static void devm_nvmem_release(struct device
*dev
, void *res
)
743 nvmem_unregister(*(struct nvmem_device
**)res
);
747 * devm_nvmem_register() - Register a managed nvmem device for given
749 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
751 * @dev: Device that uses the nvmem device.
752 * @config: nvmem device configuration with which nvmem device is created.
754 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
757 struct nvmem_device
*devm_nvmem_register(struct device
*dev
,
758 const struct nvmem_config
*config
)
760 struct nvmem_device
**ptr
, *nvmem
;
762 ptr
= devres_alloc(devm_nvmem_release
, sizeof(*ptr
), GFP_KERNEL
);
764 return ERR_PTR(-ENOMEM
);
766 nvmem
= nvmem_register(config
);
768 if (!IS_ERR(nvmem
)) {
770 devres_add(dev
, ptr
);
777 EXPORT_SYMBOL_GPL(devm_nvmem_register
);
779 static int devm_nvmem_match(struct device
*dev
, void *res
, void *data
)
781 struct nvmem_device
**r
= res
;
787 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
790 * @dev: Device that uses the nvmem device.
791 * @nvmem: Pointer to previously registered nvmem device.
793 * Return: Will be an negative on error or a zero on success.
795 int devm_nvmem_unregister(struct device
*dev
, struct nvmem_device
*nvmem
)
797 return devres_release(dev
, devm_nvmem_release
, devm_nvmem_match
, nvmem
);
799 EXPORT_SYMBOL(devm_nvmem_unregister
);
801 static struct nvmem_device
*__nvmem_device_get(void *data
,
802 int (*match
)(struct device
*dev
, const void *data
))
804 struct nvmem_device
*nvmem
= NULL
;
807 mutex_lock(&nvmem_mutex
);
808 dev
= bus_find_device(&nvmem_bus_type
, NULL
, data
, match
);
810 nvmem
= to_nvmem_device(dev
);
811 mutex_unlock(&nvmem_mutex
);
813 return ERR_PTR(-EPROBE_DEFER
);
815 if (!try_module_get(nvmem
->owner
)) {
817 "could not increase module refcount for cell %s\n",
818 nvmem_dev_name(nvmem
));
820 put_device(&nvmem
->dev
);
821 return ERR_PTR(-EINVAL
);
824 kref_get(&nvmem
->refcnt
);
829 static void __nvmem_device_put(struct nvmem_device
*nvmem
)
831 put_device(&nvmem
->dev
);
832 module_put(nvmem
->owner
);
833 kref_put(&nvmem
->refcnt
, nvmem_device_release
);
836 #if IS_ENABLED(CONFIG_OF)
838 * of_nvmem_device_get() - Get nvmem device from a given id
840 * @np: Device tree node that uses the nvmem device.
841 * @id: nvmem name from nvmem-names property.
843 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
846 struct nvmem_device
*of_nvmem_device_get(struct device_node
*np
, const char *id
)
849 struct device_node
*nvmem_np
;
853 index
= of_property_match_string(np
, "nvmem-names", id
);
855 nvmem_np
= of_parse_phandle(np
, "nvmem", index
);
857 return ERR_PTR(-ENOENT
);
859 return __nvmem_device_get(nvmem_np
, device_match_of_node
);
861 EXPORT_SYMBOL_GPL(of_nvmem_device_get
);
865 * nvmem_device_get() - Get nvmem device from a given id
867 * @dev: Device that uses the nvmem device.
868 * @dev_name: name of the requested nvmem device.
870 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
873 struct nvmem_device
*nvmem_device_get(struct device
*dev
, const char *dev_name
)
875 if (dev
->of_node
) { /* try dt first */
876 struct nvmem_device
*nvmem
;
878 nvmem
= of_nvmem_device_get(dev
->of_node
, dev_name
);
880 if (!IS_ERR(nvmem
) || PTR_ERR(nvmem
) == -EPROBE_DEFER
)
885 return __nvmem_device_get((void *)dev_name
, device_match_name
);
887 EXPORT_SYMBOL_GPL(nvmem_device_get
);
890 * nvmem_device_find() - Find nvmem device with matching function
892 * @data: Data to pass to match function
893 * @match: Callback function to check device
895 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
898 struct nvmem_device
*nvmem_device_find(void *data
,
899 int (*match
)(struct device
*dev
, const void *data
))
901 return __nvmem_device_get(data
, match
);
903 EXPORT_SYMBOL_GPL(nvmem_device_find
);
905 static int devm_nvmem_device_match(struct device
*dev
, void *res
, void *data
)
907 struct nvmem_device
**nvmem
= res
;
909 if (WARN_ON(!nvmem
|| !*nvmem
))
912 return *nvmem
== data
;
915 static void devm_nvmem_device_release(struct device
*dev
, void *res
)
917 nvmem_device_put(*(struct nvmem_device
**)res
);
921 * devm_nvmem_device_put() - put alredy got nvmem device
923 * @dev: Device that uses the nvmem device.
924 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
925 * that needs to be released.
927 void devm_nvmem_device_put(struct device
*dev
, struct nvmem_device
*nvmem
)
931 ret
= devres_release(dev
, devm_nvmem_device_release
,
932 devm_nvmem_device_match
, nvmem
);
936 EXPORT_SYMBOL_GPL(devm_nvmem_device_put
);
939 * nvmem_device_put() - put alredy got nvmem device
941 * @nvmem: pointer to nvmem device that needs to be released.
943 void nvmem_device_put(struct nvmem_device
*nvmem
)
945 __nvmem_device_put(nvmem
);
947 EXPORT_SYMBOL_GPL(nvmem_device_put
);
950 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
952 * @dev: Device that requests the nvmem device.
953 * @id: name id for the requested nvmem device.
955 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
956 * on success. The nvmem_cell will be freed by the automatically once the
959 struct nvmem_device
*devm_nvmem_device_get(struct device
*dev
, const char *id
)
961 struct nvmem_device
**ptr
, *nvmem
;
963 ptr
= devres_alloc(devm_nvmem_device_release
, sizeof(*ptr
), GFP_KERNEL
);
965 return ERR_PTR(-ENOMEM
);
967 nvmem
= nvmem_device_get(dev
, id
);
968 if (!IS_ERR(nvmem
)) {
970 devres_add(dev
, ptr
);
977 EXPORT_SYMBOL_GPL(devm_nvmem_device_get
);
979 static struct nvmem_cell
*
980 nvmem_cell_get_from_lookup(struct device
*dev
, const char *con_id
)
982 struct nvmem_cell
*cell
= ERR_PTR(-ENOENT
);
983 struct nvmem_cell_lookup
*lookup
;
984 struct nvmem_device
*nvmem
;
988 return ERR_PTR(-EINVAL
);
990 dev_id
= dev_name(dev
);
992 mutex_lock(&nvmem_lookup_mutex
);
994 list_for_each_entry(lookup
, &nvmem_lookup_list
, node
) {
995 if ((strcmp(lookup
->dev_id
, dev_id
) == 0) &&
996 (strcmp(lookup
->con_id
, con_id
) == 0)) {
997 /* This is the right entry. */
998 nvmem
= __nvmem_device_get((void *)lookup
->nvmem_name
,
1000 if (IS_ERR(nvmem
)) {
1001 /* Provider may not be registered yet. */
1002 cell
= ERR_CAST(nvmem
);
1006 cell
= nvmem_find_cell_by_name(nvmem
,
1009 __nvmem_device_put(nvmem
);
1010 cell
= ERR_PTR(-ENOENT
);
1016 mutex_unlock(&nvmem_lookup_mutex
);
1020 #if IS_ENABLED(CONFIG_OF)
1021 static struct nvmem_cell
*
1022 nvmem_find_cell_by_node(struct nvmem_device
*nvmem
, struct device_node
*np
)
1024 struct nvmem_cell
*iter
, *cell
= NULL
;
1026 mutex_lock(&nvmem_mutex
);
1027 list_for_each_entry(iter
, &nvmem
->cells
, node
) {
1028 if (np
== iter
->np
) {
1033 mutex_unlock(&nvmem_mutex
);
1039 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1041 * @np: Device tree node that uses the nvmem cell.
1042 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1043 * for the cell at index 0 (the lone cell with no accompanying
1044 * nvmem-cell-names property).
1046 * Return: Will be an ERR_PTR() on error or a valid pointer
1047 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1050 struct nvmem_cell
*of_nvmem_cell_get(struct device_node
*np
, const char *id
)
1052 struct device_node
*cell_np
, *nvmem_np
;
1053 struct nvmem_device
*nvmem
;
1054 struct nvmem_cell
*cell
;
1057 /* if cell name exists, find index to the name */
1059 index
= of_property_match_string(np
, "nvmem-cell-names", id
);
1061 cell_np
= of_parse_phandle(np
, "nvmem-cells", index
);
1063 return ERR_PTR(-ENOENT
);
1065 nvmem_np
= of_get_next_parent(cell_np
);
1067 return ERR_PTR(-EINVAL
);
1069 nvmem
= __nvmem_device_get(nvmem_np
, device_match_of_node
);
1070 of_node_put(nvmem_np
);
1072 return ERR_CAST(nvmem
);
1074 cell
= nvmem_find_cell_by_node(nvmem
, cell_np
);
1076 __nvmem_device_put(nvmem
);
1077 return ERR_PTR(-ENOENT
);
1082 EXPORT_SYMBOL_GPL(of_nvmem_cell_get
);
1086 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1088 * @dev: Device that requests the nvmem cell.
1089 * @id: nvmem cell name to get (this corresponds with the name from the
1090 * nvmem-cell-names property for DT systems and with the con_id from
1091 * the lookup entry for non-DT systems).
1093 * Return: Will be an ERR_PTR() on error or a valid pointer
1094 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1097 struct nvmem_cell
*nvmem_cell_get(struct device
*dev
, const char *id
)
1099 struct nvmem_cell
*cell
;
1101 if (dev
->of_node
) { /* try dt first */
1102 cell
= of_nvmem_cell_get(dev
->of_node
, id
);
1103 if (!IS_ERR(cell
) || PTR_ERR(cell
) == -EPROBE_DEFER
)
1107 /* NULL cell id only allowed for device tree; invalid otherwise */
1109 return ERR_PTR(-EINVAL
);
1111 return nvmem_cell_get_from_lookup(dev
, id
);
1113 EXPORT_SYMBOL_GPL(nvmem_cell_get
);
1115 static void devm_nvmem_cell_release(struct device
*dev
, void *res
)
1117 nvmem_cell_put(*(struct nvmem_cell
**)res
);
1121 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1123 * @dev: Device that requests the nvmem cell.
1124 * @id: nvmem cell name id to get.
1126 * Return: Will be an ERR_PTR() on error or a valid pointer
1127 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1128 * automatically once the device is freed.
1130 struct nvmem_cell
*devm_nvmem_cell_get(struct device
*dev
, const char *id
)
1132 struct nvmem_cell
**ptr
, *cell
;
1134 ptr
= devres_alloc(devm_nvmem_cell_release
, sizeof(*ptr
), GFP_KERNEL
);
1136 return ERR_PTR(-ENOMEM
);
1138 cell
= nvmem_cell_get(dev
, id
);
1139 if (!IS_ERR(cell
)) {
1141 devres_add(dev
, ptr
);
1148 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get
);
1150 static int devm_nvmem_cell_match(struct device
*dev
, void *res
, void *data
)
1152 struct nvmem_cell
**c
= res
;
1154 if (WARN_ON(!c
|| !*c
))
1161 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1162 * from devm_nvmem_cell_get.
1164 * @dev: Device that requests the nvmem cell.
1165 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1167 void devm_nvmem_cell_put(struct device
*dev
, struct nvmem_cell
*cell
)
1171 ret
= devres_release(dev
, devm_nvmem_cell_release
,
1172 devm_nvmem_cell_match
, cell
);
1176 EXPORT_SYMBOL(devm_nvmem_cell_put
);
1179 * nvmem_cell_put() - Release previously allocated nvmem cell.
1181 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1183 void nvmem_cell_put(struct nvmem_cell
*cell
)
1185 struct nvmem_device
*nvmem
= cell
->nvmem
;
1187 __nvmem_device_put(nvmem
);
1189 EXPORT_SYMBOL_GPL(nvmem_cell_put
);
1191 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell
*cell
, void *buf
)
1194 int i
, extra
, bit_offset
= cell
->bit_offset
;
1199 *b
++ >>= bit_offset
;
1201 /* setup rest of the bytes if any */
1202 for (i
= 1; i
< cell
->bytes
; i
++) {
1203 /* Get bits from next byte and shift them towards msb */
1204 *p
|= *b
<< (BITS_PER_BYTE
- bit_offset
);
1207 *b
++ >>= bit_offset
;
1210 /* point to the msb */
1211 p
+= cell
->bytes
- 1;
1214 /* result fits in less bytes */
1215 extra
= cell
->bytes
- DIV_ROUND_UP(cell
->nbits
, BITS_PER_BYTE
);
1216 while (--extra
>= 0)
1219 /* clear msb bits if any leftover in the last byte */
1220 *p
&= GENMASK((cell
->nbits
%BITS_PER_BYTE
) - 1, 0);
1223 static int __nvmem_cell_read(struct nvmem_device
*nvmem
,
1224 struct nvmem_cell
*cell
,
1225 void *buf
, size_t *len
)
1229 rc
= nvmem_reg_read(nvmem
, cell
->offset
, buf
, cell
->bytes
);
1234 /* shift bits in-place */
1235 if (cell
->bit_offset
|| cell
->nbits
)
1236 nvmem_shift_read_buffer_in_place(cell
, buf
);
1245 * nvmem_cell_read() - Read a given nvmem cell
1247 * @cell: nvmem cell to be read.
1248 * @len: pointer to length of cell which will be populated on successful read;
1251 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1252 * buffer should be freed by the consumer with a kfree().
1254 void *nvmem_cell_read(struct nvmem_cell
*cell
, size_t *len
)
1256 struct nvmem_device
*nvmem
= cell
->nvmem
;
1261 return ERR_PTR(-EINVAL
);
1263 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
1265 return ERR_PTR(-ENOMEM
);
1267 rc
= __nvmem_cell_read(nvmem
, cell
, buf
, len
);
1275 EXPORT_SYMBOL_GPL(nvmem_cell_read
);
1277 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell
*cell
,
1280 struct nvmem_device
*nvmem
= cell
->nvmem
;
1281 int i
, rc
, nbits
, bit_offset
= cell
->bit_offset
;
1282 u8 v
, *p
, *buf
, *b
, pbyte
, pbits
;
1284 nbits
= cell
->nbits
;
1285 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
1287 return ERR_PTR(-ENOMEM
);
1289 memcpy(buf
, _buf
, len
);
1296 /* setup the first byte with lsb bits from nvmem */
1297 rc
= nvmem_reg_read(nvmem
, cell
->offset
, &v
, 1);
1300 *b
++ |= GENMASK(bit_offset
- 1, 0) & v
;
1302 /* setup rest of the byte if any */
1303 for (i
= 1; i
< cell
->bytes
; i
++) {
1304 /* Get last byte bits and shift them towards lsb */
1305 pbits
= pbyte
>> (BITS_PER_BYTE
- 1 - bit_offset
);
1313 /* if it's not end on byte boundary */
1314 if ((nbits
+ bit_offset
) % BITS_PER_BYTE
) {
1315 /* setup the last byte with msb bits from nvmem */
1316 rc
= nvmem_reg_read(nvmem
,
1317 cell
->offset
+ cell
->bytes
- 1, &v
, 1);
1320 *p
|= GENMASK(7, (nbits
+ bit_offset
) % BITS_PER_BYTE
) & v
;
1331 * nvmem_cell_write() - Write to a given nvmem cell
1333 * @cell: nvmem cell to be written.
1334 * @buf: Buffer to be written.
1335 * @len: length of buffer to be written to nvmem cell.
1337 * Return: length of bytes written or negative on failure.
1339 int nvmem_cell_write(struct nvmem_cell
*cell
, void *buf
, size_t len
)
1341 struct nvmem_device
*nvmem
= cell
->nvmem
;
1344 if (!nvmem
|| nvmem
->read_only
||
1345 (cell
->bit_offset
== 0 && len
!= cell
->bytes
))
1348 if (cell
->bit_offset
|| cell
->nbits
) {
1349 buf
= nvmem_cell_prepare_write_buffer(cell
, buf
, len
);
1351 return PTR_ERR(buf
);
1354 rc
= nvmem_reg_write(nvmem
, cell
->offset
, buf
, cell
->bytes
);
1356 /* free the tmp buffer */
1357 if (cell
->bit_offset
|| cell
->nbits
)
1365 EXPORT_SYMBOL_GPL(nvmem_cell_write
);
1367 static int nvmem_cell_read_common(struct device
*dev
, const char *cell_id
,
1368 void *val
, size_t count
)
1370 struct nvmem_cell
*cell
;
1374 cell
= nvmem_cell_get(dev
, cell_id
);
1376 return PTR_ERR(cell
);
1378 buf
= nvmem_cell_read(cell
, &len
);
1380 nvmem_cell_put(cell
);
1381 return PTR_ERR(buf
);
1385 nvmem_cell_put(cell
);
1388 memcpy(val
, buf
, count
);
1390 nvmem_cell_put(cell
);
1396 * nvmem_cell_read_u16() - Read a cell value as an u16
1398 * @dev: Device that requests the nvmem cell.
1399 * @cell_id: Name of nvmem cell to read.
1400 * @val: pointer to output value.
1402 * Return: 0 on success or negative errno.
1404 int nvmem_cell_read_u16(struct device
*dev
, const char *cell_id
, u16
*val
)
1406 return nvmem_cell_read_common(dev
, cell_id
, val
, sizeof(*val
));
1408 EXPORT_SYMBOL_GPL(nvmem_cell_read_u16
);
1411 * nvmem_cell_read_u32() - Read a cell value as an u32
1413 * @dev: Device that requests the nvmem cell.
1414 * @cell_id: Name of nvmem cell to read.
1415 * @val: pointer to output value.
1417 * Return: 0 on success or negative errno.
1419 int nvmem_cell_read_u32(struct device
*dev
, const char *cell_id
, u32
*val
)
1421 return nvmem_cell_read_common(dev
, cell_id
, val
, sizeof(*val
));
1423 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32
);
1426 * nvmem_cell_read_u64() - Read a cell value as an u64
1428 * @dev: Device that requests the nvmem cell.
1429 * @cell_id: Name of nvmem cell to read.
1430 * @val: pointer to output value.
1432 * Return: 0 on success or negative errno.
1434 int nvmem_cell_read_u64(struct device
*dev
, const char *cell_id
, u64
*val
)
1436 return nvmem_cell_read_common(dev
, cell_id
, val
, sizeof(*val
));
1438 EXPORT_SYMBOL_GPL(nvmem_cell_read_u64
);
1441 * nvmem_device_cell_read() - Read a given nvmem device and cell
1443 * @nvmem: nvmem device to read from.
1444 * @info: nvmem cell info to be read.
1445 * @buf: buffer pointer which will be populated on successful read.
1447 * Return: length of successful bytes read on success and negative
1448 * error code on error.
1450 ssize_t
nvmem_device_cell_read(struct nvmem_device
*nvmem
,
1451 struct nvmem_cell_info
*info
, void *buf
)
1453 struct nvmem_cell cell
;
1460 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
1464 rc
= __nvmem_cell_read(nvmem
, &cell
, buf
, &len
);
1470 EXPORT_SYMBOL_GPL(nvmem_device_cell_read
);
1473 * nvmem_device_cell_write() - Write cell to a given nvmem device
1475 * @nvmem: nvmem device to be written to.
1476 * @info: nvmem cell info to be written.
1477 * @buf: buffer to be written to cell.
1479 * Return: length of bytes written or negative error code on failure.
1481 int nvmem_device_cell_write(struct nvmem_device
*nvmem
,
1482 struct nvmem_cell_info
*info
, void *buf
)
1484 struct nvmem_cell cell
;
1490 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
1494 return nvmem_cell_write(&cell
, buf
, cell
.bytes
);
1496 EXPORT_SYMBOL_GPL(nvmem_device_cell_write
);
1499 * nvmem_device_read() - Read from a given nvmem device
1501 * @nvmem: nvmem device to read from.
1502 * @offset: offset in nvmem device.
1503 * @bytes: number of bytes to read.
1504 * @buf: buffer pointer which will be populated on successful read.
1506 * Return: length of successful bytes read on success and negative
1507 * error code on error.
1509 int nvmem_device_read(struct nvmem_device
*nvmem
,
1510 unsigned int offset
,
1511 size_t bytes
, void *buf
)
1518 rc
= nvmem_reg_read(nvmem
, offset
, buf
, bytes
);
1525 EXPORT_SYMBOL_GPL(nvmem_device_read
);
1528 * nvmem_device_write() - Write cell to a given nvmem device
1530 * @nvmem: nvmem device to be written to.
1531 * @offset: offset in nvmem device.
1532 * @bytes: number of bytes to write.
1533 * @buf: buffer to be written.
1535 * Return: length of bytes written or negative error code on failure.
1537 int nvmem_device_write(struct nvmem_device
*nvmem
,
1538 unsigned int offset
,
1539 size_t bytes
, void *buf
)
1546 rc
= nvmem_reg_write(nvmem
, offset
, buf
, bytes
);
1554 EXPORT_SYMBOL_GPL(nvmem_device_write
);
1557 * nvmem_add_cell_table() - register a table of cell info entries
1559 * @table: table of cell info entries
1561 void nvmem_add_cell_table(struct nvmem_cell_table
*table
)
1563 mutex_lock(&nvmem_cell_mutex
);
1564 list_add_tail(&table
->node
, &nvmem_cell_tables
);
1565 mutex_unlock(&nvmem_cell_mutex
);
1567 EXPORT_SYMBOL_GPL(nvmem_add_cell_table
);
1570 * nvmem_del_cell_table() - remove a previously registered cell info table
1572 * @table: table of cell info entries
1574 void nvmem_del_cell_table(struct nvmem_cell_table
*table
)
1576 mutex_lock(&nvmem_cell_mutex
);
1577 list_del(&table
->node
);
1578 mutex_unlock(&nvmem_cell_mutex
);
1580 EXPORT_SYMBOL_GPL(nvmem_del_cell_table
);
1583 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1585 * @entries: array of cell lookup entries
1586 * @nentries: number of cell lookup entries in the array
1588 void nvmem_add_cell_lookups(struct nvmem_cell_lookup
*entries
, size_t nentries
)
1592 mutex_lock(&nvmem_lookup_mutex
);
1593 for (i
= 0; i
< nentries
; i
++)
1594 list_add_tail(&entries
[i
].node
, &nvmem_lookup_list
);
1595 mutex_unlock(&nvmem_lookup_mutex
);
1597 EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups
);
1600 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1603 * @entries: array of cell lookup entries
1604 * @nentries: number of cell lookup entries in the array
1606 void nvmem_del_cell_lookups(struct nvmem_cell_lookup
*entries
, size_t nentries
)
1610 mutex_lock(&nvmem_lookup_mutex
);
1611 for (i
= 0; i
< nentries
; i
++)
1612 list_del(&entries
[i
].node
);
1613 mutex_unlock(&nvmem_lookup_mutex
);
1615 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups
);
1618 * nvmem_dev_name() - Get the name of a given nvmem device.
1620 * @nvmem: nvmem device.
1622 * Return: name of the nvmem device.
1624 const char *nvmem_dev_name(struct nvmem_device
*nvmem
)
1626 return dev_name(&nvmem
->dev
);
1628 EXPORT_SYMBOL_GPL(nvmem_dev_name
);
1630 static int __init
nvmem_init(void)
1632 return bus_register(&nvmem_bus_type
);
1635 static void __exit
nvmem_exit(void)
1637 bus_unregister(&nvmem_bus_type
);
1640 subsys_initcall(nvmem_init
);
1641 module_exit(nvmem_exit
);
1643 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1644 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1645 MODULE_DESCRIPTION("nvmem Driver Core");
1646 MODULE_LICENSE("GPL v2");