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/regmap.h>
27 #include <linux/slab.h>
31 struct regmap
*regmap
;
49 struct nvmem_device
*nvmem
;
50 struct list_head node
;
53 static DEFINE_MUTEX(nvmem_mutex
);
54 static DEFINE_IDA(nvmem_ida
);
56 static LIST_HEAD(nvmem_cells
);
57 static DEFINE_MUTEX(nvmem_cells_mutex
);
59 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
61 static ssize_t
bin_attr_nvmem_read(struct file
*filp
, struct kobject
*kobj
,
62 struct bin_attribute
*attr
,
63 char *buf
, loff_t pos
, size_t count
)
65 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
66 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
69 /* Stop the user from reading */
70 if (pos
> nvmem
->size
)
73 if (pos
+ count
> nvmem
->size
)
74 count
= nvmem
->size
- pos
;
76 count
= round_down(count
, nvmem
->word_size
);
78 rc
= regmap_raw_read(nvmem
->regmap
, pos
, buf
, count
);
86 static ssize_t
bin_attr_nvmem_write(struct file
*filp
, struct kobject
*kobj
,
87 struct bin_attribute
*attr
,
88 char *buf
, loff_t pos
, size_t count
)
90 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
91 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
94 /* Stop the user from writing */
95 if (pos
> nvmem
->size
)
98 if (pos
+ count
> nvmem
->size
)
99 count
= nvmem
->size
- pos
;
101 count
= round_down(count
, nvmem
->word_size
);
103 rc
= regmap_raw_write(nvmem
->regmap
, pos
, buf
, count
);
105 if (IS_ERR_VALUE(rc
))
111 /* default read/write permissions */
112 static struct bin_attribute bin_attr_rw_nvmem
= {
115 .mode
= S_IWUSR
| S_IRUGO
,
117 .read
= bin_attr_nvmem_read
,
118 .write
= bin_attr_nvmem_write
,
121 static struct bin_attribute
*nvmem_bin_rw_attributes
[] = {
126 static const struct attribute_group nvmem_bin_rw_group
= {
127 .bin_attrs
= nvmem_bin_rw_attributes
,
130 static const struct attribute_group
*nvmem_rw_dev_groups
[] = {
135 /* read only permission */
136 static struct bin_attribute bin_attr_ro_nvmem
= {
141 .read
= bin_attr_nvmem_read
,
144 static struct bin_attribute
*nvmem_bin_ro_attributes
[] = {
149 static const struct attribute_group nvmem_bin_ro_group
= {
150 .bin_attrs
= nvmem_bin_ro_attributes
,
153 static const struct attribute_group
*nvmem_ro_dev_groups
[] = {
158 static void nvmem_release(struct device
*dev
)
160 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
162 ida_simple_remove(&nvmem_ida
, nvmem
->id
);
166 static const struct device_type nvmem_provider_type
= {
167 .release
= nvmem_release
,
170 static struct bus_type nvmem_bus_type
= {
174 static int of_nvmem_match(struct device
*dev
, void *nvmem_np
)
176 return dev
->of_node
== nvmem_np
;
179 static struct nvmem_device
*of_nvmem_find(struct device_node
*nvmem_np
)
186 d
= bus_find_device(&nvmem_bus_type
, NULL
, nvmem_np
, of_nvmem_match
);
191 return to_nvmem_device(d
);
194 static struct nvmem_cell
*nvmem_find_cell(const char *cell_id
)
196 struct nvmem_cell
*p
;
198 list_for_each_entry(p
, &nvmem_cells
, node
)
199 if (p
&& !strcmp(p
->name
, cell_id
))
205 static void nvmem_cell_drop(struct nvmem_cell
*cell
)
207 mutex_lock(&nvmem_cells_mutex
);
208 list_del(&cell
->node
);
209 mutex_unlock(&nvmem_cells_mutex
);
213 static void nvmem_device_remove_all_cells(const struct nvmem_device
*nvmem
)
215 struct nvmem_cell
*cell
;
216 struct list_head
*p
, *n
;
218 list_for_each_safe(p
, n
, &nvmem_cells
) {
219 cell
= list_entry(p
, struct nvmem_cell
, node
);
220 if (cell
->nvmem
== nvmem
)
221 nvmem_cell_drop(cell
);
225 static void nvmem_cell_add(struct nvmem_cell
*cell
)
227 mutex_lock(&nvmem_cells_mutex
);
228 list_add_tail(&cell
->node
, &nvmem_cells
);
229 mutex_unlock(&nvmem_cells_mutex
);
232 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device
*nvmem
,
233 const struct nvmem_cell_info
*info
,
234 struct nvmem_cell
*cell
)
237 cell
->offset
= info
->offset
;
238 cell
->bytes
= info
->bytes
;
239 cell
->name
= info
->name
;
241 cell
->bit_offset
= info
->bit_offset
;
242 cell
->nbits
= info
->nbits
;
245 cell
->bytes
= DIV_ROUND_UP(cell
->nbits
+ cell
->bit_offset
,
248 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
250 "cell %s unaligned to nvmem stride %d\n",
251 cell
->name
, nvmem
->stride
);
258 static int nvmem_add_cells(struct nvmem_device
*nvmem
,
259 const struct nvmem_config
*cfg
)
261 struct nvmem_cell
**cells
;
262 const struct nvmem_cell_info
*info
= cfg
->cells
;
265 cells
= kcalloc(cfg
->ncells
, sizeof(*cells
), GFP_KERNEL
);
269 for (i
= 0; i
< cfg
->ncells
; i
++) {
270 cells
[i
] = kzalloc(sizeof(**cells
), GFP_KERNEL
);
276 rval
= nvmem_cell_info_to_nvmem_cell(nvmem
, &info
[i
], cells
[i
]);
277 if (IS_ERR_VALUE(rval
)) {
282 nvmem_cell_add(cells
[i
]);
285 nvmem
->ncells
= cfg
->ncells
;
286 /* remove tmp array */
292 nvmem_cell_drop(cells
[i
]);
298 * nvmem_register() - Register a nvmem device for given nvmem_config.
299 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
301 * @config: nvmem device configuration with which nvmem device is created.
303 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
307 struct nvmem_device
*nvmem_register(const struct nvmem_config
*config
)
309 struct nvmem_device
*nvmem
;
310 struct device_node
*np
;
315 return ERR_PTR(-EINVAL
);
317 rm
= dev_get_regmap(config
->dev
, NULL
);
319 dev_err(config
->dev
, "Regmap not found\n");
320 return ERR_PTR(-EINVAL
);
323 nvmem
= kzalloc(sizeof(*nvmem
), GFP_KERNEL
);
325 return ERR_PTR(-ENOMEM
);
327 rval
= ida_simple_get(&nvmem_ida
, 0, 0, GFP_KERNEL
);
330 return ERR_PTR(rval
);
335 nvmem
->owner
= config
->owner
;
336 nvmem
->stride
= regmap_get_reg_stride(rm
);
337 nvmem
->word_size
= regmap_get_val_bytes(rm
);
338 nvmem
->size
= regmap_get_max_register(rm
) + nvmem
->stride
;
339 nvmem
->dev
.type
= &nvmem_provider_type
;
340 nvmem
->dev
.bus
= &nvmem_bus_type
;
341 nvmem
->dev
.parent
= config
->dev
;
342 np
= config
->dev
->of_node
;
343 nvmem
->dev
.of_node
= np
;
344 dev_set_name(&nvmem
->dev
, "%s%d",
345 config
->name
? : "nvmem", config
->id
);
347 nvmem
->read_only
= of_property_read_bool(np
, "read-only") |
350 nvmem
->dev
.groups
= nvmem
->read_only
? nvmem_ro_dev_groups
:
353 device_initialize(&nvmem
->dev
);
355 dev_dbg(&nvmem
->dev
, "Registering nvmem device %s\n", config
->name
);
357 rval
= device_add(&nvmem
->dev
);
359 ida_simple_remove(&nvmem_ida
, nvmem
->id
);
361 return ERR_PTR(rval
);
365 nvmem_add_cells(nvmem
, config
);
369 EXPORT_SYMBOL_GPL(nvmem_register
);
372 * nvmem_unregister() - Unregister previously registered nvmem device
374 * @nvmem: Pointer to previously registered nvmem device.
376 * Return: Will be an negative on error or a zero on success.
378 int nvmem_unregister(struct nvmem_device
*nvmem
)
380 mutex_lock(&nvmem_mutex
);
382 mutex_unlock(&nvmem_mutex
);
385 mutex_unlock(&nvmem_mutex
);
387 nvmem_device_remove_all_cells(nvmem
);
388 device_del(&nvmem
->dev
);
392 EXPORT_SYMBOL_GPL(nvmem_unregister
);
394 static struct nvmem_device
*__nvmem_device_get(struct device_node
*np
,
395 struct nvmem_cell
**cellp
,
398 struct nvmem_device
*nvmem
= NULL
;
400 mutex_lock(&nvmem_mutex
);
403 nvmem
= of_nvmem_find(np
);
405 mutex_unlock(&nvmem_mutex
);
406 return ERR_PTR(-EPROBE_DEFER
);
409 struct nvmem_cell
*cell
= nvmem_find_cell(cell_id
);
417 mutex_unlock(&nvmem_mutex
);
418 return ERR_PTR(-ENOENT
);
423 mutex_unlock(&nvmem_mutex
);
425 if (!try_module_get(nvmem
->owner
)) {
427 "could not increase module refcount for cell %s\n",
430 mutex_lock(&nvmem_mutex
);
432 mutex_unlock(&nvmem_mutex
);
434 return ERR_PTR(-EINVAL
);
440 static void __nvmem_device_put(struct nvmem_device
*nvmem
)
442 module_put(nvmem
->owner
);
443 mutex_lock(&nvmem_mutex
);
445 mutex_unlock(&nvmem_mutex
);
448 static int nvmem_match(struct device
*dev
, void *data
)
450 return !strcmp(dev_name(dev
), data
);
453 static struct nvmem_device
*nvmem_find(const char *name
)
457 d
= bus_find_device(&nvmem_bus_type
, NULL
, (void *)name
, nvmem_match
);
462 return to_nvmem_device(d
);
465 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
467 * of_nvmem_device_get() - Get nvmem device from a given id
469 * @dev node: Device tree node that uses the nvmem device
470 * @id: nvmem name from nvmem-names property.
472 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
475 struct nvmem_device
*of_nvmem_device_get(struct device_node
*np
, const char *id
)
478 struct device_node
*nvmem_np
;
481 index
= of_property_match_string(np
, "nvmem-names", id
);
483 nvmem_np
= of_parse_phandle(np
, "nvmem", index
);
485 return ERR_PTR(-EINVAL
);
487 return __nvmem_device_get(nvmem_np
, NULL
, NULL
);
489 EXPORT_SYMBOL_GPL(of_nvmem_device_get
);
493 * nvmem_device_get() - Get nvmem device from a given id
495 * @dev : Device that uses the nvmem device
496 * @id: nvmem name from nvmem-names property.
498 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
501 struct nvmem_device
*nvmem_device_get(struct device
*dev
, const char *dev_name
)
503 if (dev
->of_node
) { /* try dt first */
504 struct nvmem_device
*nvmem
;
506 nvmem
= of_nvmem_device_get(dev
->of_node
, dev_name
);
508 if (!IS_ERR(nvmem
) || PTR_ERR(nvmem
) == -EPROBE_DEFER
)
513 return nvmem_find(dev_name
);
515 EXPORT_SYMBOL_GPL(nvmem_device_get
);
517 static int devm_nvmem_device_match(struct device
*dev
, void *res
, void *data
)
519 struct nvmem_device
**nvmem
= res
;
521 if (WARN_ON(!nvmem
|| !*nvmem
))
524 return *nvmem
== data
;
527 static void devm_nvmem_device_release(struct device
*dev
, void *res
)
529 nvmem_device_put(*(struct nvmem_device
**)res
);
533 * devm_nvmem_device_put() - put alredy got nvmem device
535 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
536 * that needs to be released.
538 void devm_nvmem_device_put(struct device
*dev
, struct nvmem_device
*nvmem
)
542 ret
= devres_release(dev
, devm_nvmem_device_release
,
543 devm_nvmem_device_match
, nvmem
);
547 EXPORT_SYMBOL_GPL(devm_nvmem_device_put
);
550 * nvmem_device_put() - put alredy got nvmem device
552 * @nvmem: pointer to nvmem device that needs to be released.
554 void nvmem_device_put(struct nvmem_device
*nvmem
)
556 __nvmem_device_put(nvmem
);
558 EXPORT_SYMBOL_GPL(nvmem_device_put
);
561 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
563 * @dev node: Device tree node that uses the nvmem cell
564 * @id: nvmem name in nvmems property.
566 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
567 * on success. The nvmem_cell will be freed by the automatically once the
570 struct nvmem_device
*devm_nvmem_device_get(struct device
*dev
, const char *id
)
572 struct nvmem_device
**ptr
, *nvmem
;
574 ptr
= devres_alloc(devm_nvmem_device_release
, sizeof(*ptr
), GFP_KERNEL
);
576 return ERR_PTR(-ENOMEM
);
578 nvmem
= nvmem_device_get(dev
, id
);
579 if (!IS_ERR(nvmem
)) {
581 devres_add(dev
, ptr
);
588 EXPORT_SYMBOL_GPL(devm_nvmem_device_get
);
590 static struct nvmem_cell
*nvmem_cell_get_from_list(const char *cell_id
)
592 struct nvmem_cell
*cell
= NULL
;
593 struct nvmem_device
*nvmem
;
595 nvmem
= __nvmem_device_get(NULL
, &cell
, cell_id
);
597 return ERR_CAST(nvmem
);
602 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
604 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
606 * @dev node: Device tree node that uses the nvmem cell
607 * @id: nvmem cell name from nvmem-cell-names property.
609 * Return: Will be an ERR_PTR() on error or a valid pointer
610 * to a struct nvmem_cell. The nvmem_cell will be freed by the
613 struct nvmem_cell
*of_nvmem_cell_get(struct device_node
*np
,
616 struct device_node
*cell_np
, *nvmem_np
;
617 struct nvmem_cell
*cell
;
618 struct nvmem_device
*nvmem
;
620 int rval
, len
, index
;
622 index
= of_property_match_string(np
, "nvmem-cell-names", name
);
624 cell_np
= of_parse_phandle(np
, "nvmem-cells", index
);
626 return ERR_PTR(-EINVAL
);
628 nvmem_np
= of_get_next_parent(cell_np
);
630 return ERR_PTR(-EINVAL
);
632 nvmem
= __nvmem_device_get(nvmem_np
, NULL
, NULL
);
634 return ERR_CAST(nvmem
);
636 addr
= of_get_property(cell_np
, "reg", &len
);
637 if (!addr
|| (len
< 2 * sizeof(u32
))) {
638 dev_err(&nvmem
->dev
, "nvmem: invalid reg on %s\n",
644 cell
= kzalloc(sizeof(*cell
), GFP_KERNEL
);
651 cell
->offset
= be32_to_cpup(addr
++);
652 cell
->bytes
= be32_to_cpup(addr
);
653 cell
->name
= cell_np
->name
;
655 addr
= of_get_property(cell_np
, "bits", &len
);
656 if (addr
&& len
== (2 * sizeof(u32
))) {
657 cell
->bit_offset
= be32_to_cpup(addr
++);
658 cell
->nbits
= be32_to_cpup(addr
);
662 cell
->bytes
= DIV_ROUND_UP(cell
->nbits
+ cell
->bit_offset
,
665 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
667 "cell %s unaligned to nvmem stride %d\n",
668 cell
->name
, nvmem
->stride
);
673 nvmem_cell_add(cell
);
681 __nvmem_device_put(nvmem
);
683 return ERR_PTR(rval
);
685 EXPORT_SYMBOL_GPL(of_nvmem_cell_get
);
689 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
691 * @dev node: Device tree node that uses the nvmem cell
692 * @id: nvmem cell name to get.
694 * Return: Will be an ERR_PTR() on error or a valid pointer
695 * to a struct nvmem_cell. The nvmem_cell will be freed by the
698 struct nvmem_cell
*nvmem_cell_get(struct device
*dev
, const char *cell_id
)
700 struct nvmem_cell
*cell
;
702 if (dev
->of_node
) { /* try dt first */
703 cell
= of_nvmem_cell_get(dev
->of_node
, cell_id
);
704 if (!IS_ERR(cell
) || PTR_ERR(cell
) == -EPROBE_DEFER
)
708 return nvmem_cell_get_from_list(cell_id
);
710 EXPORT_SYMBOL_GPL(nvmem_cell_get
);
712 static void devm_nvmem_cell_release(struct device
*dev
, void *res
)
714 nvmem_cell_put(*(struct nvmem_cell
**)res
);
718 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
720 * @dev node: Device tree node that uses the nvmem cell
721 * @id: nvmem id in nvmem-names property.
723 * Return: Will be an ERR_PTR() on error or a valid pointer
724 * to a struct nvmem_cell. The nvmem_cell will be freed by the
725 * automatically once the device is freed.
727 struct nvmem_cell
*devm_nvmem_cell_get(struct device
*dev
, const char *id
)
729 struct nvmem_cell
**ptr
, *cell
;
731 ptr
= devres_alloc(devm_nvmem_cell_release
, sizeof(*ptr
), GFP_KERNEL
);
733 return ERR_PTR(-ENOMEM
);
735 cell
= nvmem_cell_get(dev
, id
);
738 devres_add(dev
, ptr
);
745 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get
);
747 static int devm_nvmem_cell_match(struct device
*dev
, void *res
, void *data
)
749 struct nvmem_cell
**c
= res
;
751 if (WARN_ON(!c
|| !*c
))
758 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
759 * from devm_nvmem_cell_get.
761 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get()
763 void devm_nvmem_cell_put(struct device
*dev
, struct nvmem_cell
*cell
)
767 ret
= devres_release(dev
, devm_nvmem_cell_release
,
768 devm_nvmem_cell_match
, cell
);
772 EXPORT_SYMBOL(devm_nvmem_cell_put
);
775 * nvmem_cell_put() - Release previously allocated nvmem cell.
777 * @cell: Previously allocated nvmem cell by nvmem_cell_get()
779 void nvmem_cell_put(struct nvmem_cell
*cell
)
781 struct nvmem_device
*nvmem
= cell
->nvmem
;
783 __nvmem_device_put(nvmem
);
784 nvmem_cell_drop(cell
);
786 EXPORT_SYMBOL_GPL(nvmem_cell_put
);
788 static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell
*cell
,
792 int i
, bit_offset
= cell
->bit_offset
;
799 /* setup rest of the bytes if any */
800 for (i
= 1; i
< cell
->bytes
; i
++) {
801 /* Get bits from next byte and shift them towards msb */
802 *p
|= *b
<< (BITS_PER_BYTE
- bit_offset
);
808 /* result fits in less bytes */
809 if (cell
->bytes
!= DIV_ROUND_UP(cell
->nbits
, BITS_PER_BYTE
))
812 /* clear msb bits if any leftover in the last byte */
813 *p
&= GENMASK((cell
->nbits
%BITS_PER_BYTE
) - 1, 0);
816 static int __nvmem_cell_read(struct nvmem_device
*nvmem
,
817 struct nvmem_cell
*cell
,
818 void *buf
, size_t *len
)
822 rc
= regmap_raw_read(nvmem
->regmap
, cell
->offset
, buf
, cell
->bytes
);
824 if (IS_ERR_VALUE(rc
))
827 /* shift bits in-place */
828 if (cell
->bit_offset
|| cell
->bit_offset
)
829 nvmem_shift_read_buffer_in_place(cell
, buf
);
837 * nvmem_cell_read() - Read a given nvmem cell
839 * @cell: nvmem cell to be read.
840 * @len: pointer to length of cell which will be populated on successful read.
842 * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success.
843 * The buffer should be freed by the consumer with a kfree().
845 void *nvmem_cell_read(struct nvmem_cell
*cell
, size_t *len
)
847 struct nvmem_device
*nvmem
= cell
->nvmem
;
851 if (!nvmem
|| !nvmem
->regmap
)
852 return ERR_PTR(-EINVAL
);
854 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
856 return ERR_PTR(-ENOMEM
);
858 rc
= __nvmem_cell_read(nvmem
, cell
, buf
, len
);
859 if (IS_ERR_VALUE(rc
)) {
866 EXPORT_SYMBOL_GPL(nvmem_cell_read
);
868 static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell
*cell
,
871 struct nvmem_device
*nvmem
= cell
->nvmem
;
872 int i
, rc
, nbits
, bit_offset
= cell
->bit_offset
;
873 u8 v
, *p
, *buf
, *b
, pbyte
, pbits
;
876 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
878 return ERR_PTR(-ENOMEM
);
880 memcpy(buf
, _buf
, len
);
887 /* setup the first byte with lsb bits from nvmem */
888 rc
= regmap_raw_read(nvmem
->regmap
, cell
->offset
, &v
, 1);
889 *b
++ |= GENMASK(bit_offset
- 1, 0) & v
;
891 /* setup rest of the byte if any */
892 for (i
= 1; i
< cell
->bytes
; i
++) {
893 /* Get last byte bits and shift them towards lsb */
894 pbits
= pbyte
>> (BITS_PER_BYTE
- 1 - bit_offset
);
902 /* if it's not end on byte boundary */
903 if ((nbits
+ bit_offset
) % BITS_PER_BYTE
) {
904 /* setup the last byte with msb bits from nvmem */
905 rc
= regmap_raw_read(nvmem
->regmap
,
906 cell
->offset
+ cell
->bytes
- 1, &v
, 1);
907 *p
|= GENMASK(7, (nbits
+ bit_offset
) % BITS_PER_BYTE
) & v
;
915 * nvmem_cell_write() - Write to a given nvmem cell
917 * @cell: nvmem cell to be written.
918 * @buf: Buffer to be written.
919 * @len: length of buffer to be written to nvmem cell.
921 * Return: length of bytes written or negative on failure.
923 int nvmem_cell_write(struct nvmem_cell
*cell
, void *buf
, size_t len
)
925 struct nvmem_device
*nvmem
= cell
->nvmem
;
928 if (!nvmem
|| !nvmem
->regmap
|| nvmem
->read_only
||
929 (cell
->bit_offset
== 0 && len
!= cell
->bytes
))
932 if (cell
->bit_offset
|| cell
->nbits
) {
933 buf
= nvmem_cell_prepare_write_buffer(cell
, buf
, len
);
938 rc
= regmap_raw_write(nvmem
->regmap
, cell
->offset
, buf
, cell
->bytes
);
940 /* free the tmp buffer */
941 if (cell
->bit_offset
)
944 if (IS_ERR_VALUE(rc
))
949 EXPORT_SYMBOL_GPL(nvmem_cell_write
);
952 * nvmem_device_cell_read() - Read a given nvmem device and cell
954 * @nvmem: nvmem device to read from.
955 * @info: nvmem cell info to be read.
956 * @buf: buffer pointer which will be populated on successful read.
958 * Return: length of successful bytes read on success and negative
959 * error code on error.
961 ssize_t
nvmem_device_cell_read(struct nvmem_device
*nvmem
,
962 struct nvmem_cell_info
*info
, void *buf
)
964 struct nvmem_cell cell
;
968 if (!nvmem
|| !nvmem
->regmap
)
971 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
972 if (IS_ERR_VALUE(rc
))
975 rc
= __nvmem_cell_read(nvmem
, &cell
, buf
, &len
);
976 if (IS_ERR_VALUE(rc
))
981 EXPORT_SYMBOL_GPL(nvmem_device_cell_read
);
984 * nvmem_device_cell_write() - Write cell to a given nvmem device
986 * @nvmem: nvmem device to be written to.
987 * @info: nvmem cell info to be written
988 * @buf: buffer to be written to cell.
990 * Return: length of bytes written or negative error code on failure.
992 int nvmem_device_cell_write(struct nvmem_device
*nvmem
,
993 struct nvmem_cell_info
*info
, void *buf
)
995 struct nvmem_cell cell
;
998 if (!nvmem
|| !nvmem
->regmap
)
1001 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
1002 if (IS_ERR_VALUE(rc
))
1005 return nvmem_cell_write(&cell
, buf
, cell
.bytes
);
1007 EXPORT_SYMBOL_GPL(nvmem_device_cell_write
);
1010 * nvmem_device_read() - Read from a given nvmem device
1012 * @nvmem: nvmem device to read from.
1013 * @offset: offset in nvmem device.
1014 * @bytes: number of bytes to read.
1015 * @buf: buffer pointer which will be populated on successful read.
1017 * Return: length of successful bytes read on success and negative
1018 * error code on error.
1020 int nvmem_device_read(struct nvmem_device
*nvmem
,
1021 unsigned int offset
,
1022 size_t bytes
, void *buf
)
1026 if (!nvmem
|| !nvmem
->regmap
)
1029 rc
= regmap_raw_read(nvmem
->regmap
, offset
, buf
, bytes
);
1031 if (IS_ERR_VALUE(rc
))
1036 EXPORT_SYMBOL_GPL(nvmem_device_read
);
1039 * nvmem_device_write() - Write cell to a given nvmem device
1041 * @nvmem: nvmem device to be written to.
1042 * @offset: offset in nvmem device.
1043 * @bytes: number of bytes to write.
1044 * @buf: buffer to be written.
1046 * Return: length of bytes written or negative error code on failure.
1048 int nvmem_device_write(struct nvmem_device
*nvmem
,
1049 unsigned int offset
,
1050 size_t bytes
, void *buf
)
1054 if (!nvmem
|| !nvmem
->regmap
)
1057 rc
= regmap_raw_write(nvmem
->regmap
, offset
, buf
, bytes
);
1059 if (IS_ERR_VALUE(rc
))
1065 EXPORT_SYMBOL_GPL(nvmem_device_write
);
1067 static int __init
nvmem_init(void)
1069 return bus_register(&nvmem_bus_type
);
1072 static void __exit
nvmem_exit(void)
1074 bus_unregister(&nvmem_bus_type
);
1077 subsys_initcall(nvmem_init
);
1078 module_exit(nvmem_exit
);
1080 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1081 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1082 MODULE_DESCRIPTION("nvmem Driver Core");
1083 MODULE_LICENSE("GPL v2");