2 * nvmem framework core.
4 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
5 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/device.h>
18 #include <linux/export.h>
20 #include <linux/idr.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/nvmem-consumer.h>
24 #include <linux/nvmem-provider.h>
26 #include <linux/slab.h>
39 struct bin_attribute eeprom
;
40 struct device
*base_dev
;
41 nvmem_reg_read_t reg_read
;
42 nvmem_reg_write_t reg_write
;
46 #define FLAG_COMPAT BIT(0)
54 struct nvmem_device
*nvmem
;
55 struct list_head node
;
58 static DEFINE_MUTEX(nvmem_mutex
);
59 static DEFINE_IDA(nvmem_ida
);
61 static LIST_HEAD(nvmem_cells
);
62 static DEFINE_MUTEX(nvmem_cells_mutex
);
64 #ifdef CONFIG_DEBUG_LOCK_ALLOC
65 static struct lock_class_key eeprom_lock_key
;
68 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
69 static int nvmem_reg_read(struct nvmem_device
*nvmem
, unsigned int offset
,
70 void *val
, size_t bytes
)
73 return nvmem
->reg_read(nvmem
->priv
, offset
, val
, bytes
);
78 static int nvmem_reg_write(struct nvmem_device
*nvmem
, unsigned int offset
,
79 void *val
, size_t bytes
)
82 return nvmem
->reg_write(nvmem
->priv
, offset
, val
, bytes
);
87 static ssize_t
bin_attr_nvmem_read(struct file
*filp
, struct kobject
*kobj
,
88 struct bin_attribute
*attr
,
89 char *buf
, loff_t pos
, size_t count
)
92 struct nvmem_device
*nvmem
;
98 dev
= container_of(kobj
, struct device
, kobj
);
99 nvmem
= to_nvmem_device(dev
);
101 /* Stop the user from reading */
102 if (pos
>= nvmem
->size
)
105 if (count
< nvmem
->word_size
)
108 if (pos
+ count
> nvmem
->size
)
109 count
= nvmem
->size
- pos
;
111 count
= round_down(count
, nvmem
->word_size
);
113 rc
= nvmem_reg_read(nvmem
, pos
, buf
, count
);
121 static ssize_t
bin_attr_nvmem_write(struct file
*filp
, struct kobject
*kobj
,
122 struct bin_attribute
*attr
,
123 char *buf
, loff_t pos
, size_t count
)
126 struct nvmem_device
*nvmem
;
132 dev
= container_of(kobj
, struct device
, kobj
);
133 nvmem
= to_nvmem_device(dev
);
135 /* Stop the user from writing */
136 if (pos
>= nvmem
->size
)
139 if (count
< nvmem
->word_size
)
142 if (pos
+ count
> nvmem
->size
)
143 count
= nvmem
->size
- pos
;
145 count
= round_down(count
, nvmem
->word_size
);
147 rc
= nvmem_reg_write(nvmem
, pos
, buf
, count
);
155 /* default read/write permissions */
156 static struct bin_attribute bin_attr_rw_nvmem
= {
159 .mode
= S_IWUSR
| S_IRUGO
,
161 .read
= bin_attr_nvmem_read
,
162 .write
= bin_attr_nvmem_write
,
165 static struct bin_attribute
*nvmem_bin_rw_attributes
[] = {
170 static const struct attribute_group nvmem_bin_rw_group
= {
171 .bin_attrs
= nvmem_bin_rw_attributes
,
174 static const struct attribute_group
*nvmem_rw_dev_groups
[] = {
179 /* read only permission */
180 static struct bin_attribute bin_attr_ro_nvmem
= {
185 .read
= bin_attr_nvmem_read
,
188 static struct bin_attribute
*nvmem_bin_ro_attributes
[] = {
193 static const struct attribute_group nvmem_bin_ro_group
= {
194 .bin_attrs
= nvmem_bin_ro_attributes
,
197 static const struct attribute_group
*nvmem_ro_dev_groups
[] = {
202 /* default read/write permissions, root only */
203 static struct bin_attribute bin_attr_rw_root_nvmem
= {
206 .mode
= S_IWUSR
| S_IRUSR
,
208 .read
= bin_attr_nvmem_read
,
209 .write
= bin_attr_nvmem_write
,
212 static struct bin_attribute
*nvmem_bin_rw_root_attributes
[] = {
213 &bin_attr_rw_root_nvmem
,
217 static const struct attribute_group nvmem_bin_rw_root_group
= {
218 .bin_attrs
= nvmem_bin_rw_root_attributes
,
221 static const struct attribute_group
*nvmem_rw_root_dev_groups
[] = {
222 &nvmem_bin_rw_root_group
,
226 /* read only permission, root only */
227 static struct bin_attribute bin_attr_ro_root_nvmem
= {
232 .read
= bin_attr_nvmem_read
,
235 static struct bin_attribute
*nvmem_bin_ro_root_attributes
[] = {
236 &bin_attr_ro_root_nvmem
,
240 static const struct attribute_group nvmem_bin_ro_root_group
= {
241 .bin_attrs
= nvmem_bin_ro_root_attributes
,
244 static const struct attribute_group
*nvmem_ro_root_dev_groups
[] = {
245 &nvmem_bin_ro_root_group
,
249 static void nvmem_release(struct device
*dev
)
251 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
253 ida_simple_remove(&nvmem_ida
, nvmem
->id
);
257 static const struct device_type nvmem_provider_type
= {
258 .release
= nvmem_release
,
261 static struct bus_type nvmem_bus_type
= {
265 static int of_nvmem_match(struct device
*dev
, void *nvmem_np
)
267 return dev
->of_node
== nvmem_np
;
270 static struct nvmem_device
*of_nvmem_find(struct device_node
*nvmem_np
)
277 d
= bus_find_device(&nvmem_bus_type
, NULL
, nvmem_np
, of_nvmem_match
);
282 return to_nvmem_device(d
);
285 static struct nvmem_cell
*nvmem_find_cell(const char *cell_id
)
287 struct nvmem_cell
*p
;
289 mutex_lock(&nvmem_cells_mutex
);
291 list_for_each_entry(p
, &nvmem_cells
, node
)
292 if (!strcmp(p
->name
, cell_id
)) {
293 mutex_unlock(&nvmem_cells_mutex
);
297 mutex_unlock(&nvmem_cells_mutex
);
302 static void nvmem_cell_drop(struct nvmem_cell
*cell
)
304 mutex_lock(&nvmem_cells_mutex
);
305 list_del(&cell
->node
);
306 mutex_unlock(&nvmem_cells_mutex
);
310 static void nvmem_device_remove_all_cells(const struct nvmem_device
*nvmem
)
312 struct nvmem_cell
*cell
;
313 struct list_head
*p
, *n
;
315 list_for_each_safe(p
, n
, &nvmem_cells
) {
316 cell
= list_entry(p
, struct nvmem_cell
, node
);
317 if (cell
->nvmem
== nvmem
)
318 nvmem_cell_drop(cell
);
322 static void nvmem_cell_add(struct nvmem_cell
*cell
)
324 mutex_lock(&nvmem_cells_mutex
);
325 list_add_tail(&cell
->node
, &nvmem_cells
);
326 mutex_unlock(&nvmem_cells_mutex
);
329 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device
*nvmem
,
330 const struct nvmem_cell_info
*info
,
331 struct nvmem_cell
*cell
)
334 cell
->offset
= info
->offset
;
335 cell
->bytes
= info
->bytes
;
336 cell
->name
= info
->name
;
338 cell
->bit_offset
= info
->bit_offset
;
339 cell
->nbits
= info
->nbits
;
342 cell
->bytes
= DIV_ROUND_UP(cell
->nbits
+ cell
->bit_offset
,
345 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
347 "cell %s unaligned to nvmem stride %d\n",
348 cell
->name
, nvmem
->stride
);
356 * nvmem_add_cells() - Add cell information to an nvmem device
358 * @nvmem: nvmem device to add cells to.
359 * @info: nvmem cell info to add to the device
360 * @ncells: number of cells in info
362 * Return: 0 or negative error code on failure.
364 int nvmem_add_cells(struct nvmem_device
*nvmem
,
365 const struct nvmem_cell_info
*info
,
368 struct nvmem_cell
**cells
;
371 cells
= kcalloc(ncells
, sizeof(*cells
), GFP_KERNEL
);
375 for (i
= 0; i
< ncells
; i
++) {
376 cells
[i
] = kzalloc(sizeof(**cells
), GFP_KERNEL
);
382 rval
= nvmem_cell_info_to_nvmem_cell(nvmem
, &info
[i
], cells
[i
]);
388 nvmem_cell_add(cells
[i
]);
391 /* remove tmp array */
397 nvmem_cell_drop(cells
[i
]);
403 EXPORT_SYMBOL_GPL(nvmem_add_cells
);
406 * nvmem_setup_compat() - Create an additional binary entry in
407 * drivers sys directory, to be backwards compatible with the older
408 * drivers/misc/eeprom drivers.
410 static int nvmem_setup_compat(struct nvmem_device
*nvmem
,
411 const struct nvmem_config
*config
)
415 if (!config
->base_dev
)
418 if (nvmem
->read_only
)
419 nvmem
->eeprom
= bin_attr_ro_root_nvmem
;
421 nvmem
->eeprom
= bin_attr_rw_root_nvmem
;
422 nvmem
->eeprom
.attr
.name
= "eeprom";
423 nvmem
->eeprom
.size
= nvmem
->size
;
424 #ifdef CONFIG_DEBUG_LOCK_ALLOC
425 nvmem
->eeprom
.attr
.key
= &eeprom_lock_key
;
427 nvmem
->eeprom
.private = &nvmem
->dev
;
428 nvmem
->base_dev
= config
->base_dev
;
430 rval
= device_create_bin_file(nvmem
->base_dev
, &nvmem
->eeprom
);
433 "Failed to create eeprom binary file %d\n", rval
);
437 nvmem
->flags
|= FLAG_COMPAT
;
443 * nvmem_register() - Register a nvmem device for given nvmem_config.
444 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
446 * @config: nvmem device configuration with which nvmem device is created.
448 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
452 struct nvmem_device
*nvmem_register(const struct nvmem_config
*config
)
454 struct nvmem_device
*nvmem
;
458 return ERR_PTR(-EINVAL
);
460 nvmem
= kzalloc(sizeof(*nvmem
), GFP_KERNEL
);
462 return ERR_PTR(-ENOMEM
);
464 rval
= ida_simple_get(&nvmem_ida
, 0, 0, GFP_KERNEL
);
467 return ERR_PTR(rval
);
471 nvmem
->owner
= config
->owner
;
472 if (!nvmem
->owner
&& config
->dev
->driver
)
473 nvmem
->owner
= config
->dev
->driver
->owner
;
474 nvmem
->stride
= config
->stride
?: 1;
475 nvmem
->word_size
= config
->word_size
?: 1;
476 nvmem
->size
= config
->size
;
477 nvmem
->dev
.type
= &nvmem_provider_type
;
478 nvmem
->dev
.bus
= &nvmem_bus_type
;
479 nvmem
->dev
.parent
= config
->dev
;
480 nvmem
->priv
= config
->priv
;
481 nvmem
->reg_read
= config
->reg_read
;
482 nvmem
->reg_write
= config
->reg_write
;
483 nvmem
->dev
.of_node
= config
->dev
->of_node
;
485 if (config
->id
== -1 && config
->name
) {
486 dev_set_name(&nvmem
->dev
, "%s", config
->name
);
488 dev_set_name(&nvmem
->dev
, "%s%d",
489 config
->name
? : "nvmem",
490 config
->name
? config
->id
: nvmem
->id
);
493 nvmem
->read_only
= device_property_present(config
->dev
, "read-only") |
496 if (config
->root_only
)
497 nvmem
->dev
.groups
= nvmem
->read_only
?
498 nvmem_ro_root_dev_groups
:
499 nvmem_rw_root_dev_groups
;
501 nvmem
->dev
.groups
= nvmem
->read_only
?
502 nvmem_ro_dev_groups
:
505 device_initialize(&nvmem
->dev
);
507 dev_dbg(&nvmem
->dev
, "Registering nvmem device %s\n", config
->name
);
509 rval
= device_add(&nvmem
->dev
);
513 if (config
->compat
) {
514 rval
= nvmem_setup_compat(nvmem
, config
);
520 nvmem_add_cells(nvmem
, config
->cells
, config
->ncells
);
525 device_del(&nvmem
->dev
);
527 put_device(&nvmem
->dev
);
529 return ERR_PTR(rval
);
531 EXPORT_SYMBOL_GPL(nvmem_register
);
534 * nvmem_unregister() - Unregister previously registered nvmem device
536 * @nvmem: Pointer to previously registered nvmem device.
538 * Return: Will be an negative on error or a zero on success.
540 int nvmem_unregister(struct nvmem_device
*nvmem
)
542 mutex_lock(&nvmem_mutex
);
544 mutex_unlock(&nvmem_mutex
);
547 mutex_unlock(&nvmem_mutex
);
549 if (nvmem
->flags
& FLAG_COMPAT
)
550 device_remove_bin_file(nvmem
->base_dev
, &nvmem
->eeprom
);
552 nvmem_device_remove_all_cells(nvmem
);
553 device_del(&nvmem
->dev
);
554 put_device(&nvmem
->dev
);
558 EXPORT_SYMBOL_GPL(nvmem_unregister
);
560 static void devm_nvmem_release(struct device
*dev
, void *res
)
562 WARN_ON(nvmem_unregister(*(struct nvmem_device
**)res
));
566 * devm_nvmem_register() - Register a managed nvmem device for given
568 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
570 * @dev: Device that uses the nvmem device.
571 * @config: nvmem device configuration with which nvmem device is created.
573 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
576 struct nvmem_device
*devm_nvmem_register(struct device
*dev
,
577 const struct nvmem_config
*config
)
579 struct nvmem_device
**ptr
, *nvmem
;
581 ptr
= devres_alloc(devm_nvmem_release
, sizeof(*ptr
), GFP_KERNEL
);
583 return ERR_PTR(-ENOMEM
);
585 nvmem
= nvmem_register(config
);
587 if (!IS_ERR(nvmem
)) {
589 devres_add(dev
, ptr
);
596 EXPORT_SYMBOL_GPL(devm_nvmem_register
);
598 static int devm_nvmem_match(struct device
*dev
, void *res
, void *data
)
600 struct nvmem_device
**r
= res
;
606 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
609 * @dev: Device that uses the nvmem device.
610 * @nvmem: Pointer to previously registered nvmem device.
612 * Return: Will be an negative on error or a zero on success.
614 int devm_nvmem_unregister(struct device
*dev
, struct nvmem_device
*nvmem
)
616 return devres_release(dev
, devm_nvmem_release
, devm_nvmem_match
, nvmem
);
618 EXPORT_SYMBOL(devm_nvmem_unregister
);
621 static struct nvmem_device
*__nvmem_device_get(struct device_node
*np
,
622 struct nvmem_cell
**cellp
,
625 struct nvmem_device
*nvmem
= NULL
;
627 mutex_lock(&nvmem_mutex
);
630 nvmem
= of_nvmem_find(np
);
632 mutex_unlock(&nvmem_mutex
);
633 return ERR_PTR(-EPROBE_DEFER
);
636 struct nvmem_cell
*cell
= nvmem_find_cell(cell_id
);
644 mutex_unlock(&nvmem_mutex
);
645 return ERR_PTR(-ENOENT
);
650 mutex_unlock(&nvmem_mutex
);
652 if (!try_module_get(nvmem
->owner
)) {
654 "could not increase module refcount for cell %s\n",
657 mutex_lock(&nvmem_mutex
);
659 mutex_unlock(&nvmem_mutex
);
661 return ERR_PTR(-EINVAL
);
667 static void __nvmem_device_put(struct nvmem_device
*nvmem
)
669 module_put(nvmem
->owner
);
670 mutex_lock(&nvmem_mutex
);
672 mutex_unlock(&nvmem_mutex
);
675 static struct nvmem_device
*nvmem_find(const char *name
)
679 d
= bus_find_device_by_name(&nvmem_bus_type
, NULL
, name
);
684 return to_nvmem_device(d
);
687 #if IS_ENABLED(CONFIG_OF)
689 * of_nvmem_device_get() - Get nvmem device from a given id
691 * @np: Device tree node that uses the nvmem device.
692 * @id: nvmem name from nvmem-names property.
694 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
697 struct nvmem_device
*of_nvmem_device_get(struct device_node
*np
, const char *id
)
700 struct device_node
*nvmem_np
;
703 index
= of_property_match_string(np
, "nvmem-names", id
);
705 nvmem_np
= of_parse_phandle(np
, "nvmem", index
);
707 return ERR_PTR(-EINVAL
);
709 return __nvmem_device_get(nvmem_np
, NULL
, NULL
);
711 EXPORT_SYMBOL_GPL(of_nvmem_device_get
);
715 * nvmem_device_get() - Get nvmem device from a given id
717 * @dev: Device that uses the nvmem device.
718 * @dev_name: name of the requested nvmem device.
720 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
723 struct nvmem_device
*nvmem_device_get(struct device
*dev
, const char *dev_name
)
725 if (dev
->of_node
) { /* try dt first */
726 struct nvmem_device
*nvmem
;
728 nvmem
= of_nvmem_device_get(dev
->of_node
, dev_name
);
730 if (!IS_ERR(nvmem
) || PTR_ERR(nvmem
) == -EPROBE_DEFER
)
735 return nvmem_find(dev_name
);
737 EXPORT_SYMBOL_GPL(nvmem_device_get
);
739 static int devm_nvmem_device_match(struct device
*dev
, void *res
, void *data
)
741 struct nvmem_device
**nvmem
= res
;
743 if (WARN_ON(!nvmem
|| !*nvmem
))
746 return *nvmem
== data
;
749 static void devm_nvmem_device_release(struct device
*dev
, void *res
)
751 nvmem_device_put(*(struct nvmem_device
**)res
);
755 * devm_nvmem_device_put() - put alredy got nvmem device
757 * @dev: Device that uses the nvmem device.
758 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
759 * that needs to be released.
761 void devm_nvmem_device_put(struct device
*dev
, struct nvmem_device
*nvmem
)
765 ret
= devres_release(dev
, devm_nvmem_device_release
,
766 devm_nvmem_device_match
, nvmem
);
770 EXPORT_SYMBOL_GPL(devm_nvmem_device_put
);
773 * nvmem_device_put() - put alredy got nvmem device
775 * @nvmem: pointer to nvmem device that needs to be released.
777 void nvmem_device_put(struct nvmem_device
*nvmem
)
779 __nvmem_device_put(nvmem
);
781 EXPORT_SYMBOL_GPL(nvmem_device_put
);
784 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
786 * @dev: Device that requests the nvmem device.
787 * @id: name id for the requested nvmem device.
789 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
790 * on success. The nvmem_cell will be freed by the automatically once the
793 struct nvmem_device
*devm_nvmem_device_get(struct device
*dev
, const char *id
)
795 struct nvmem_device
**ptr
, *nvmem
;
797 ptr
= devres_alloc(devm_nvmem_device_release
, sizeof(*ptr
), GFP_KERNEL
);
799 return ERR_PTR(-ENOMEM
);
801 nvmem
= nvmem_device_get(dev
, id
);
802 if (!IS_ERR(nvmem
)) {
804 devres_add(dev
, ptr
);
811 EXPORT_SYMBOL_GPL(devm_nvmem_device_get
);
813 static struct nvmem_cell
*nvmem_cell_get_from_list(const char *cell_id
)
815 struct nvmem_cell
*cell
= NULL
;
816 struct nvmem_device
*nvmem
;
818 nvmem
= __nvmem_device_get(NULL
, &cell
, cell_id
);
820 return ERR_CAST(nvmem
);
825 #if IS_ENABLED(CONFIG_OF)
827 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
829 * @np: Device tree node that uses the nvmem cell.
830 * @name: nvmem cell name from nvmem-cell-names property, or NULL
831 * for the cell at index 0 (the lone cell with no accompanying
832 * nvmem-cell-names property).
834 * Return: Will be an ERR_PTR() on error or a valid pointer
835 * to a struct nvmem_cell. The nvmem_cell will be freed by the
838 struct nvmem_cell
*of_nvmem_cell_get(struct device_node
*np
,
841 struct device_node
*cell_np
, *nvmem_np
;
842 struct nvmem_cell
*cell
;
843 struct nvmem_device
*nvmem
;
848 /* if cell name exists, find index to the name */
850 index
= of_property_match_string(np
, "nvmem-cell-names", name
);
852 cell_np
= of_parse_phandle(np
, "nvmem-cells", index
);
854 return ERR_PTR(-EINVAL
);
856 nvmem_np
= of_get_next_parent(cell_np
);
858 return ERR_PTR(-EINVAL
);
860 nvmem
= __nvmem_device_get(nvmem_np
, NULL
, NULL
);
861 of_node_put(nvmem_np
);
863 return ERR_CAST(nvmem
);
865 addr
= of_get_property(cell_np
, "reg", &len
);
866 if (!addr
|| (len
< 2 * sizeof(u32
))) {
867 dev_err(&nvmem
->dev
, "nvmem: invalid reg on %pOF\n",
873 cell
= kzalloc(sizeof(*cell
), GFP_KERNEL
);
880 cell
->offset
= be32_to_cpup(addr
++);
881 cell
->bytes
= be32_to_cpup(addr
);
882 cell
->name
= cell_np
->name
;
884 addr
= of_get_property(cell_np
, "bits", &len
);
885 if (addr
&& len
== (2 * sizeof(u32
))) {
886 cell
->bit_offset
= be32_to_cpup(addr
++);
887 cell
->nbits
= be32_to_cpup(addr
);
891 cell
->bytes
= DIV_ROUND_UP(cell
->nbits
+ cell
->bit_offset
,
894 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
896 "cell %s unaligned to nvmem stride %d\n",
897 cell
->name
, nvmem
->stride
);
902 nvmem_cell_add(cell
);
910 __nvmem_device_put(nvmem
);
912 return ERR_PTR(rval
);
914 EXPORT_SYMBOL_GPL(of_nvmem_cell_get
);
918 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
920 * @dev: Device that requests the nvmem cell.
921 * @cell_id: nvmem cell name to get.
923 * Return: Will be an ERR_PTR() on error or a valid pointer
924 * to a struct nvmem_cell. The nvmem_cell will be freed by the
927 struct nvmem_cell
*nvmem_cell_get(struct device
*dev
, const char *cell_id
)
929 struct nvmem_cell
*cell
;
931 if (dev
->of_node
) { /* try dt first */
932 cell
= of_nvmem_cell_get(dev
->of_node
, cell_id
);
933 if (!IS_ERR(cell
) || PTR_ERR(cell
) == -EPROBE_DEFER
)
937 /* NULL cell_id only allowed for device tree; invalid otherwise */
939 return ERR_PTR(-EINVAL
);
941 return nvmem_cell_get_from_list(cell_id
);
943 EXPORT_SYMBOL_GPL(nvmem_cell_get
);
945 static void devm_nvmem_cell_release(struct device
*dev
, void *res
)
947 nvmem_cell_put(*(struct nvmem_cell
**)res
);
951 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
953 * @dev: Device that requests the nvmem cell.
954 * @id: nvmem cell name id to get.
956 * Return: Will be an ERR_PTR() on error or a valid pointer
957 * to a struct nvmem_cell. The nvmem_cell will be freed by the
958 * automatically once the device is freed.
960 struct nvmem_cell
*devm_nvmem_cell_get(struct device
*dev
, const char *id
)
962 struct nvmem_cell
**ptr
, *cell
;
964 ptr
= devres_alloc(devm_nvmem_cell_release
, sizeof(*ptr
), GFP_KERNEL
);
966 return ERR_PTR(-ENOMEM
);
968 cell
= nvmem_cell_get(dev
, id
);
971 devres_add(dev
, ptr
);
978 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get
);
980 static int devm_nvmem_cell_match(struct device
*dev
, void *res
, void *data
)
982 struct nvmem_cell
**c
= res
;
984 if (WARN_ON(!c
|| !*c
))
991 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
992 * from devm_nvmem_cell_get.
994 * @dev: Device that requests the nvmem cell.
995 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
997 void devm_nvmem_cell_put(struct device
*dev
, struct nvmem_cell
*cell
)
1001 ret
= devres_release(dev
, devm_nvmem_cell_release
,
1002 devm_nvmem_cell_match
, cell
);
1006 EXPORT_SYMBOL(devm_nvmem_cell_put
);
1009 * nvmem_cell_put() - Release previously allocated nvmem cell.
1011 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1013 void nvmem_cell_put(struct nvmem_cell
*cell
)
1015 struct nvmem_device
*nvmem
= cell
->nvmem
;
1017 __nvmem_device_put(nvmem
);
1018 nvmem_cell_drop(cell
);
1020 EXPORT_SYMBOL_GPL(nvmem_cell_put
);
1022 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell
*cell
, void *buf
)
1025 int i
, bit_offset
= cell
->bit_offset
;
1030 *b
++ >>= bit_offset
;
1032 /* setup rest of the bytes if any */
1033 for (i
= 1; i
< cell
->bytes
; i
++) {
1034 /* Get bits from next byte and shift them towards msb */
1035 *p
|= *b
<< (BITS_PER_BYTE
- bit_offset
);
1038 *b
++ >>= bit_offset
;
1041 /* result fits in less bytes */
1042 if (cell
->bytes
!= DIV_ROUND_UP(cell
->nbits
, BITS_PER_BYTE
))
1045 /* clear msb bits if any leftover in the last byte */
1046 *p
&= GENMASK((cell
->nbits
%BITS_PER_BYTE
) - 1, 0);
1049 static int __nvmem_cell_read(struct nvmem_device
*nvmem
,
1050 struct nvmem_cell
*cell
,
1051 void *buf
, size_t *len
)
1055 rc
= nvmem_reg_read(nvmem
, cell
->offset
, buf
, cell
->bytes
);
1060 /* shift bits in-place */
1061 if (cell
->bit_offset
|| cell
->nbits
)
1062 nvmem_shift_read_buffer_in_place(cell
, buf
);
1071 * nvmem_cell_read() - Read a given nvmem cell
1073 * @cell: nvmem cell to be read.
1074 * @len: pointer to length of cell which will be populated on successful read;
1077 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1078 * buffer should be freed by the consumer with a kfree().
1080 void *nvmem_cell_read(struct nvmem_cell
*cell
, size_t *len
)
1082 struct nvmem_device
*nvmem
= cell
->nvmem
;
1087 return ERR_PTR(-EINVAL
);
1089 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
1091 return ERR_PTR(-ENOMEM
);
1093 rc
= __nvmem_cell_read(nvmem
, cell
, buf
, len
);
1101 EXPORT_SYMBOL_GPL(nvmem_cell_read
);
1103 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell
*cell
,
1106 struct nvmem_device
*nvmem
= cell
->nvmem
;
1107 int i
, rc
, nbits
, bit_offset
= cell
->bit_offset
;
1108 u8 v
, *p
, *buf
, *b
, pbyte
, pbits
;
1110 nbits
= cell
->nbits
;
1111 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
1113 return ERR_PTR(-ENOMEM
);
1115 memcpy(buf
, _buf
, len
);
1122 /* setup the first byte with lsb bits from nvmem */
1123 rc
= nvmem_reg_read(nvmem
, cell
->offset
, &v
, 1);
1126 *b
++ |= GENMASK(bit_offset
- 1, 0) & v
;
1128 /* setup rest of the byte if any */
1129 for (i
= 1; i
< cell
->bytes
; i
++) {
1130 /* Get last byte bits and shift them towards lsb */
1131 pbits
= pbyte
>> (BITS_PER_BYTE
- 1 - bit_offset
);
1139 /* if it's not end on byte boundary */
1140 if ((nbits
+ bit_offset
) % BITS_PER_BYTE
) {
1141 /* setup the last byte with msb bits from nvmem */
1142 rc
= nvmem_reg_read(nvmem
,
1143 cell
->offset
+ cell
->bytes
- 1, &v
, 1);
1146 *p
|= GENMASK(7, (nbits
+ bit_offset
) % BITS_PER_BYTE
) & v
;
1157 * nvmem_cell_write() - Write to a given nvmem cell
1159 * @cell: nvmem cell to be written.
1160 * @buf: Buffer to be written.
1161 * @len: length of buffer to be written to nvmem cell.
1163 * Return: length of bytes written or negative on failure.
1165 int nvmem_cell_write(struct nvmem_cell
*cell
, void *buf
, size_t len
)
1167 struct nvmem_device
*nvmem
= cell
->nvmem
;
1170 if (!nvmem
|| nvmem
->read_only
||
1171 (cell
->bit_offset
== 0 && len
!= cell
->bytes
))
1174 if (cell
->bit_offset
|| cell
->nbits
) {
1175 buf
= nvmem_cell_prepare_write_buffer(cell
, buf
, len
);
1177 return PTR_ERR(buf
);
1180 rc
= nvmem_reg_write(nvmem
, cell
->offset
, buf
, cell
->bytes
);
1182 /* free the tmp buffer */
1183 if (cell
->bit_offset
|| cell
->nbits
)
1191 EXPORT_SYMBOL_GPL(nvmem_cell_write
);
1194 * nvmem_cell_read_u32() - Read a cell value as an u32
1196 * @dev: Device that requests the nvmem cell.
1197 * @cell_id: Name of nvmem cell to read.
1198 * @val: pointer to output value.
1200 * Return: 0 on success or negative errno.
1202 int nvmem_cell_read_u32(struct device
*dev
, const char *cell_id
, u32
*val
)
1204 struct nvmem_cell
*cell
;
1208 cell
= nvmem_cell_get(dev
, cell_id
);
1210 return PTR_ERR(cell
);
1212 buf
= nvmem_cell_read(cell
, &len
);
1214 nvmem_cell_put(cell
);
1215 return PTR_ERR(buf
);
1217 if (len
!= sizeof(*val
)) {
1219 nvmem_cell_put(cell
);
1222 memcpy(val
, buf
, sizeof(*val
));
1225 nvmem_cell_put(cell
);
1228 EXPORT_SYMBOL_GPL(nvmem_cell_read_u32
);
1231 * nvmem_device_cell_read() - Read a given nvmem device and cell
1233 * @nvmem: nvmem device to read from.
1234 * @info: nvmem cell info to be read.
1235 * @buf: buffer pointer which will be populated on successful read.
1237 * Return: length of successful bytes read on success and negative
1238 * error code on error.
1240 ssize_t
nvmem_device_cell_read(struct nvmem_device
*nvmem
,
1241 struct nvmem_cell_info
*info
, void *buf
)
1243 struct nvmem_cell cell
;
1250 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
1254 rc
= __nvmem_cell_read(nvmem
, &cell
, buf
, &len
);
1260 EXPORT_SYMBOL_GPL(nvmem_device_cell_read
);
1263 * nvmem_device_cell_write() - Write cell to a given nvmem device
1265 * @nvmem: nvmem device to be written to.
1266 * @info: nvmem cell info to be written.
1267 * @buf: buffer to be written to cell.
1269 * Return: length of bytes written or negative error code on failure.
1271 int nvmem_device_cell_write(struct nvmem_device
*nvmem
,
1272 struct nvmem_cell_info
*info
, void *buf
)
1274 struct nvmem_cell cell
;
1280 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
1284 return nvmem_cell_write(&cell
, buf
, cell
.bytes
);
1286 EXPORT_SYMBOL_GPL(nvmem_device_cell_write
);
1289 * nvmem_device_read() - Read from a given nvmem device
1291 * @nvmem: nvmem device to read from.
1292 * @offset: offset in nvmem device.
1293 * @bytes: number of bytes to read.
1294 * @buf: buffer pointer which will be populated on successful read.
1296 * Return: length of successful bytes read on success and negative
1297 * error code on error.
1299 int nvmem_device_read(struct nvmem_device
*nvmem
,
1300 unsigned int offset
,
1301 size_t bytes
, void *buf
)
1308 rc
= nvmem_reg_read(nvmem
, offset
, buf
, bytes
);
1315 EXPORT_SYMBOL_GPL(nvmem_device_read
);
1318 * nvmem_device_write() - Write cell to a given nvmem device
1320 * @nvmem: nvmem device to be written to.
1321 * @offset: offset in nvmem device.
1322 * @bytes: number of bytes to write.
1323 * @buf: buffer to be written.
1325 * Return: length of bytes written or negative error code on failure.
1327 int nvmem_device_write(struct nvmem_device
*nvmem
,
1328 unsigned int offset
,
1329 size_t bytes
, void *buf
)
1336 rc
= nvmem_reg_write(nvmem
, offset
, buf
, bytes
);
1344 EXPORT_SYMBOL_GPL(nvmem_device_write
);
1346 static int __init
nvmem_init(void)
1348 return bus_register(&nvmem_bus_type
);
1351 static void __exit
nvmem_exit(void)
1353 bus_unregister(&nvmem_bus_type
);
1356 subsys_initcall(nvmem_init
);
1357 module_exit(nvmem_exit
);
1359 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1360 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1361 MODULE_DESCRIPTION("nvmem Driver Core");
1362 MODULE_LICENSE("GPL v2");