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 (count
< nvmem
->word_size
)
76 if (pos
+ count
> nvmem
->size
)
77 count
= nvmem
->size
- pos
;
79 count
= round_down(count
, nvmem
->word_size
);
81 rc
= regmap_raw_read(nvmem
->regmap
, pos
, buf
, count
);
89 static ssize_t
bin_attr_nvmem_write(struct file
*filp
, struct kobject
*kobj
,
90 struct bin_attribute
*attr
,
91 char *buf
, loff_t pos
, size_t count
)
93 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
94 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
97 /* Stop the user from writing */
98 if (pos
>= nvmem
->size
)
101 if (count
< nvmem
->word_size
)
104 if (pos
+ count
> nvmem
->size
)
105 count
= nvmem
->size
- pos
;
107 count
= round_down(count
, nvmem
->word_size
);
109 rc
= regmap_raw_write(nvmem
->regmap
, pos
, buf
, count
);
111 if (IS_ERR_VALUE(rc
))
117 /* default read/write permissions */
118 static struct bin_attribute bin_attr_rw_nvmem
= {
121 .mode
= S_IWUSR
| S_IRUGO
,
123 .read
= bin_attr_nvmem_read
,
124 .write
= bin_attr_nvmem_write
,
127 static struct bin_attribute
*nvmem_bin_rw_attributes
[] = {
132 static const struct attribute_group nvmem_bin_rw_group
= {
133 .bin_attrs
= nvmem_bin_rw_attributes
,
136 static const struct attribute_group
*nvmem_rw_dev_groups
[] = {
141 /* read only permission */
142 static struct bin_attribute bin_attr_ro_nvmem
= {
147 .read
= bin_attr_nvmem_read
,
150 static struct bin_attribute
*nvmem_bin_ro_attributes
[] = {
155 static const struct attribute_group nvmem_bin_ro_group
= {
156 .bin_attrs
= nvmem_bin_ro_attributes
,
159 static const struct attribute_group
*nvmem_ro_dev_groups
[] = {
164 static void nvmem_release(struct device
*dev
)
166 struct nvmem_device
*nvmem
= to_nvmem_device(dev
);
168 ida_simple_remove(&nvmem_ida
, nvmem
->id
);
172 static const struct device_type nvmem_provider_type
= {
173 .release
= nvmem_release
,
176 static struct bus_type nvmem_bus_type
= {
180 static int of_nvmem_match(struct device
*dev
, void *nvmem_np
)
182 return dev
->of_node
== nvmem_np
;
185 static struct nvmem_device
*of_nvmem_find(struct device_node
*nvmem_np
)
192 d
= bus_find_device(&nvmem_bus_type
, NULL
, nvmem_np
, of_nvmem_match
);
197 return to_nvmem_device(d
);
200 static struct nvmem_cell
*nvmem_find_cell(const char *cell_id
)
202 struct nvmem_cell
*p
;
204 list_for_each_entry(p
, &nvmem_cells
, node
)
205 if (p
&& !strcmp(p
->name
, cell_id
))
211 static void nvmem_cell_drop(struct nvmem_cell
*cell
)
213 mutex_lock(&nvmem_cells_mutex
);
214 list_del(&cell
->node
);
215 mutex_unlock(&nvmem_cells_mutex
);
219 static void nvmem_device_remove_all_cells(const struct nvmem_device
*nvmem
)
221 struct nvmem_cell
*cell
;
222 struct list_head
*p
, *n
;
224 list_for_each_safe(p
, n
, &nvmem_cells
) {
225 cell
= list_entry(p
, struct nvmem_cell
, node
);
226 if (cell
->nvmem
== nvmem
)
227 nvmem_cell_drop(cell
);
231 static void nvmem_cell_add(struct nvmem_cell
*cell
)
233 mutex_lock(&nvmem_cells_mutex
);
234 list_add_tail(&cell
->node
, &nvmem_cells
);
235 mutex_unlock(&nvmem_cells_mutex
);
238 static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device
*nvmem
,
239 const struct nvmem_cell_info
*info
,
240 struct nvmem_cell
*cell
)
243 cell
->offset
= info
->offset
;
244 cell
->bytes
= info
->bytes
;
245 cell
->name
= info
->name
;
247 cell
->bit_offset
= info
->bit_offset
;
248 cell
->nbits
= info
->nbits
;
251 cell
->bytes
= DIV_ROUND_UP(cell
->nbits
+ cell
->bit_offset
,
254 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
256 "cell %s unaligned to nvmem stride %d\n",
257 cell
->name
, nvmem
->stride
);
264 static int nvmem_add_cells(struct nvmem_device
*nvmem
,
265 const struct nvmem_config
*cfg
)
267 struct nvmem_cell
**cells
;
268 const struct nvmem_cell_info
*info
= cfg
->cells
;
271 cells
= kcalloc(cfg
->ncells
, sizeof(*cells
), GFP_KERNEL
);
275 for (i
= 0; i
< cfg
->ncells
; i
++) {
276 cells
[i
] = kzalloc(sizeof(**cells
), GFP_KERNEL
);
282 rval
= nvmem_cell_info_to_nvmem_cell(nvmem
, &info
[i
], cells
[i
]);
283 if (IS_ERR_VALUE(rval
)) {
288 nvmem_cell_add(cells
[i
]);
291 nvmem
->ncells
= cfg
->ncells
;
292 /* remove tmp array */
298 nvmem_cell_drop(cells
[i
]);
304 * nvmem_register() - Register a nvmem device for given nvmem_config.
305 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
307 * @config: nvmem device configuration with which nvmem device is created.
309 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
313 struct nvmem_device
*nvmem_register(const struct nvmem_config
*config
)
315 struct nvmem_device
*nvmem
;
316 struct device_node
*np
;
321 return ERR_PTR(-EINVAL
);
323 rm
= dev_get_regmap(config
->dev
, NULL
);
325 dev_err(config
->dev
, "Regmap not found\n");
326 return ERR_PTR(-EINVAL
);
329 nvmem
= kzalloc(sizeof(*nvmem
), GFP_KERNEL
);
331 return ERR_PTR(-ENOMEM
);
333 rval
= ida_simple_get(&nvmem_ida
, 0, 0, GFP_KERNEL
);
336 return ERR_PTR(rval
);
341 nvmem
->owner
= config
->owner
;
342 nvmem
->stride
= regmap_get_reg_stride(rm
);
343 nvmem
->word_size
= regmap_get_val_bytes(rm
);
344 nvmem
->size
= regmap_get_max_register(rm
) + nvmem
->stride
;
345 nvmem
->dev
.type
= &nvmem_provider_type
;
346 nvmem
->dev
.bus
= &nvmem_bus_type
;
347 nvmem
->dev
.parent
= config
->dev
;
348 np
= config
->dev
->of_node
;
349 nvmem
->dev
.of_node
= np
;
350 dev_set_name(&nvmem
->dev
, "%s%d",
351 config
->name
? : "nvmem", config
->id
);
353 nvmem
->read_only
= of_property_read_bool(np
, "read-only") |
356 nvmem
->dev
.groups
= nvmem
->read_only
? nvmem_ro_dev_groups
:
359 device_initialize(&nvmem
->dev
);
361 dev_dbg(&nvmem
->dev
, "Registering nvmem device %s\n", config
->name
);
363 rval
= device_add(&nvmem
->dev
);
365 ida_simple_remove(&nvmem_ida
, nvmem
->id
);
367 return ERR_PTR(rval
);
371 nvmem_add_cells(nvmem
, config
);
375 EXPORT_SYMBOL_GPL(nvmem_register
);
378 * nvmem_unregister() - Unregister previously registered nvmem device
380 * @nvmem: Pointer to previously registered nvmem device.
382 * Return: Will be an negative on error or a zero on success.
384 int nvmem_unregister(struct nvmem_device
*nvmem
)
386 mutex_lock(&nvmem_mutex
);
388 mutex_unlock(&nvmem_mutex
);
391 mutex_unlock(&nvmem_mutex
);
393 nvmem_device_remove_all_cells(nvmem
);
394 device_del(&nvmem
->dev
);
398 EXPORT_SYMBOL_GPL(nvmem_unregister
);
400 static struct nvmem_device
*__nvmem_device_get(struct device_node
*np
,
401 struct nvmem_cell
**cellp
,
404 struct nvmem_device
*nvmem
= NULL
;
406 mutex_lock(&nvmem_mutex
);
409 nvmem
= of_nvmem_find(np
);
411 mutex_unlock(&nvmem_mutex
);
412 return ERR_PTR(-EPROBE_DEFER
);
415 struct nvmem_cell
*cell
= nvmem_find_cell(cell_id
);
423 mutex_unlock(&nvmem_mutex
);
424 return ERR_PTR(-ENOENT
);
429 mutex_unlock(&nvmem_mutex
);
431 if (!try_module_get(nvmem
->owner
)) {
433 "could not increase module refcount for cell %s\n",
436 mutex_lock(&nvmem_mutex
);
438 mutex_unlock(&nvmem_mutex
);
440 return ERR_PTR(-EINVAL
);
446 static void __nvmem_device_put(struct nvmem_device
*nvmem
)
448 module_put(nvmem
->owner
);
449 mutex_lock(&nvmem_mutex
);
451 mutex_unlock(&nvmem_mutex
);
454 static int nvmem_match(struct device
*dev
, void *data
)
456 return !strcmp(dev_name(dev
), data
);
459 static struct nvmem_device
*nvmem_find(const char *name
)
463 d
= bus_find_device(&nvmem_bus_type
, NULL
, (void *)name
, nvmem_match
);
468 return to_nvmem_device(d
);
471 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
473 * of_nvmem_device_get() - Get nvmem device from a given id
475 * @dev node: Device tree node that uses the nvmem device
476 * @id: nvmem name from nvmem-names property.
478 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
481 struct nvmem_device
*of_nvmem_device_get(struct device_node
*np
, const char *id
)
484 struct device_node
*nvmem_np
;
487 index
= of_property_match_string(np
, "nvmem-names", id
);
489 nvmem_np
= of_parse_phandle(np
, "nvmem", index
);
491 return ERR_PTR(-EINVAL
);
493 return __nvmem_device_get(nvmem_np
, NULL
, NULL
);
495 EXPORT_SYMBOL_GPL(of_nvmem_device_get
);
499 * nvmem_device_get() - Get nvmem device from a given id
501 * @dev : Device that uses the nvmem device
502 * @id: nvmem name from nvmem-names property.
504 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
507 struct nvmem_device
*nvmem_device_get(struct device
*dev
, const char *dev_name
)
509 if (dev
->of_node
) { /* try dt first */
510 struct nvmem_device
*nvmem
;
512 nvmem
= of_nvmem_device_get(dev
->of_node
, dev_name
);
514 if (!IS_ERR(nvmem
) || PTR_ERR(nvmem
) == -EPROBE_DEFER
)
519 return nvmem_find(dev_name
);
521 EXPORT_SYMBOL_GPL(nvmem_device_get
);
523 static int devm_nvmem_device_match(struct device
*dev
, void *res
, void *data
)
525 struct nvmem_device
**nvmem
= res
;
527 if (WARN_ON(!nvmem
|| !*nvmem
))
530 return *nvmem
== data
;
533 static void devm_nvmem_device_release(struct device
*dev
, void *res
)
535 nvmem_device_put(*(struct nvmem_device
**)res
);
539 * devm_nvmem_device_put() - put alredy got nvmem device
541 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
542 * that needs to be released.
544 void devm_nvmem_device_put(struct device
*dev
, struct nvmem_device
*nvmem
)
548 ret
= devres_release(dev
, devm_nvmem_device_release
,
549 devm_nvmem_device_match
, nvmem
);
553 EXPORT_SYMBOL_GPL(devm_nvmem_device_put
);
556 * nvmem_device_put() - put alredy got nvmem device
558 * @nvmem: pointer to nvmem device that needs to be released.
560 void nvmem_device_put(struct nvmem_device
*nvmem
)
562 __nvmem_device_put(nvmem
);
564 EXPORT_SYMBOL_GPL(nvmem_device_put
);
567 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
569 * @dev node: Device tree node that uses the nvmem cell
570 * @id: nvmem name in nvmems property.
572 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
573 * on success. The nvmem_cell will be freed by the automatically once the
576 struct nvmem_device
*devm_nvmem_device_get(struct device
*dev
, const char *id
)
578 struct nvmem_device
**ptr
, *nvmem
;
580 ptr
= devres_alloc(devm_nvmem_device_release
, sizeof(*ptr
), GFP_KERNEL
);
582 return ERR_PTR(-ENOMEM
);
584 nvmem
= nvmem_device_get(dev
, id
);
585 if (!IS_ERR(nvmem
)) {
587 devres_add(dev
, ptr
);
594 EXPORT_SYMBOL_GPL(devm_nvmem_device_get
);
596 static struct nvmem_cell
*nvmem_cell_get_from_list(const char *cell_id
)
598 struct nvmem_cell
*cell
= NULL
;
599 struct nvmem_device
*nvmem
;
601 nvmem
= __nvmem_device_get(NULL
, &cell
, cell_id
);
603 return ERR_CAST(nvmem
);
608 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
610 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
612 * @dev node: Device tree node that uses the nvmem cell
613 * @id: nvmem cell name from nvmem-cell-names property.
615 * Return: Will be an ERR_PTR() on error or a valid pointer
616 * to a struct nvmem_cell. The nvmem_cell will be freed by the
619 struct nvmem_cell
*of_nvmem_cell_get(struct device_node
*np
,
622 struct device_node
*cell_np
, *nvmem_np
;
623 struct nvmem_cell
*cell
;
624 struct nvmem_device
*nvmem
;
626 int rval
, len
, index
;
628 index
= of_property_match_string(np
, "nvmem-cell-names", name
);
630 cell_np
= of_parse_phandle(np
, "nvmem-cells", index
);
632 return ERR_PTR(-EINVAL
);
634 nvmem_np
= of_get_next_parent(cell_np
);
636 return ERR_PTR(-EINVAL
);
638 nvmem
= __nvmem_device_get(nvmem_np
, NULL
, NULL
);
640 return ERR_CAST(nvmem
);
642 addr
= of_get_property(cell_np
, "reg", &len
);
643 if (!addr
|| (len
< 2 * sizeof(u32
))) {
644 dev_err(&nvmem
->dev
, "nvmem: invalid reg on %s\n",
650 cell
= kzalloc(sizeof(*cell
), GFP_KERNEL
);
657 cell
->offset
= be32_to_cpup(addr
++);
658 cell
->bytes
= be32_to_cpup(addr
);
659 cell
->name
= cell_np
->name
;
661 addr
= of_get_property(cell_np
, "bits", &len
);
662 if (addr
&& len
== (2 * sizeof(u32
))) {
663 cell
->bit_offset
= be32_to_cpup(addr
++);
664 cell
->nbits
= be32_to_cpup(addr
);
668 cell
->bytes
= DIV_ROUND_UP(cell
->nbits
+ cell
->bit_offset
,
671 if (!IS_ALIGNED(cell
->offset
, nvmem
->stride
)) {
673 "cell %s unaligned to nvmem stride %d\n",
674 cell
->name
, nvmem
->stride
);
679 nvmem_cell_add(cell
);
687 __nvmem_device_put(nvmem
);
689 return ERR_PTR(rval
);
691 EXPORT_SYMBOL_GPL(of_nvmem_cell_get
);
695 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
697 * @dev node: Device tree node that uses the nvmem cell
698 * @id: nvmem cell name to get.
700 * Return: Will be an ERR_PTR() on error or a valid pointer
701 * to a struct nvmem_cell. The nvmem_cell will be freed by the
704 struct nvmem_cell
*nvmem_cell_get(struct device
*dev
, const char *cell_id
)
706 struct nvmem_cell
*cell
;
708 if (dev
->of_node
) { /* try dt first */
709 cell
= of_nvmem_cell_get(dev
->of_node
, cell_id
);
710 if (!IS_ERR(cell
) || PTR_ERR(cell
) == -EPROBE_DEFER
)
714 return nvmem_cell_get_from_list(cell_id
);
716 EXPORT_SYMBOL_GPL(nvmem_cell_get
);
718 static void devm_nvmem_cell_release(struct device
*dev
, void *res
)
720 nvmem_cell_put(*(struct nvmem_cell
**)res
);
724 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
726 * @dev node: Device tree node that uses the nvmem cell
727 * @id: nvmem id in nvmem-names property.
729 * Return: Will be an ERR_PTR() on error or a valid pointer
730 * to a struct nvmem_cell. The nvmem_cell will be freed by the
731 * automatically once the device is freed.
733 struct nvmem_cell
*devm_nvmem_cell_get(struct device
*dev
, const char *id
)
735 struct nvmem_cell
**ptr
, *cell
;
737 ptr
= devres_alloc(devm_nvmem_cell_release
, sizeof(*ptr
), GFP_KERNEL
);
739 return ERR_PTR(-ENOMEM
);
741 cell
= nvmem_cell_get(dev
, id
);
744 devres_add(dev
, ptr
);
751 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get
);
753 static int devm_nvmem_cell_match(struct device
*dev
, void *res
, void *data
)
755 struct nvmem_cell
**c
= res
;
757 if (WARN_ON(!c
|| !*c
))
764 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
765 * from devm_nvmem_cell_get.
767 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get()
769 void devm_nvmem_cell_put(struct device
*dev
, struct nvmem_cell
*cell
)
773 ret
= devres_release(dev
, devm_nvmem_cell_release
,
774 devm_nvmem_cell_match
, cell
);
778 EXPORT_SYMBOL(devm_nvmem_cell_put
);
781 * nvmem_cell_put() - Release previously allocated nvmem cell.
783 * @cell: Previously allocated nvmem cell by nvmem_cell_get()
785 void nvmem_cell_put(struct nvmem_cell
*cell
)
787 struct nvmem_device
*nvmem
= cell
->nvmem
;
789 __nvmem_device_put(nvmem
);
790 nvmem_cell_drop(cell
);
792 EXPORT_SYMBOL_GPL(nvmem_cell_put
);
794 static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell
*cell
,
798 int i
, bit_offset
= cell
->bit_offset
;
805 /* setup rest of the bytes if any */
806 for (i
= 1; i
< cell
->bytes
; i
++) {
807 /* Get bits from next byte and shift them towards msb */
808 *p
|= *b
<< (BITS_PER_BYTE
- bit_offset
);
814 /* result fits in less bytes */
815 if (cell
->bytes
!= DIV_ROUND_UP(cell
->nbits
, BITS_PER_BYTE
))
818 /* clear msb bits if any leftover in the last byte */
819 *p
&= GENMASK((cell
->nbits
%BITS_PER_BYTE
) - 1, 0);
822 static int __nvmem_cell_read(struct nvmem_device
*nvmem
,
823 struct nvmem_cell
*cell
,
824 void *buf
, size_t *len
)
828 rc
= regmap_raw_read(nvmem
->regmap
, cell
->offset
, buf
, cell
->bytes
);
830 if (IS_ERR_VALUE(rc
))
833 /* shift bits in-place */
834 if (cell
->bit_offset
|| cell
->nbits
)
835 nvmem_shift_read_buffer_in_place(cell
, buf
);
843 * nvmem_cell_read() - Read a given nvmem cell
845 * @cell: nvmem cell to be read.
846 * @len: pointer to length of cell which will be populated on successful read.
848 * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success.
849 * The buffer should be freed by the consumer with a kfree().
851 void *nvmem_cell_read(struct nvmem_cell
*cell
, size_t *len
)
853 struct nvmem_device
*nvmem
= cell
->nvmem
;
857 if (!nvmem
|| !nvmem
->regmap
)
858 return ERR_PTR(-EINVAL
);
860 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
862 return ERR_PTR(-ENOMEM
);
864 rc
= __nvmem_cell_read(nvmem
, cell
, buf
, len
);
865 if (IS_ERR_VALUE(rc
)) {
872 EXPORT_SYMBOL_GPL(nvmem_cell_read
);
874 static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell
*cell
,
877 struct nvmem_device
*nvmem
= cell
->nvmem
;
878 int i
, rc
, nbits
, bit_offset
= cell
->bit_offset
;
879 u8 v
, *p
, *buf
, *b
, pbyte
, pbits
;
882 buf
= kzalloc(cell
->bytes
, GFP_KERNEL
);
884 return ERR_PTR(-ENOMEM
);
886 memcpy(buf
, _buf
, len
);
893 /* setup the first byte with lsb bits from nvmem */
894 rc
= regmap_raw_read(nvmem
->regmap
, cell
->offset
, &v
, 1);
895 *b
++ |= GENMASK(bit_offset
- 1, 0) & v
;
897 /* setup rest of the byte if any */
898 for (i
= 1; i
< cell
->bytes
; i
++) {
899 /* Get last byte bits and shift them towards lsb */
900 pbits
= pbyte
>> (BITS_PER_BYTE
- 1 - bit_offset
);
908 /* if it's not end on byte boundary */
909 if ((nbits
+ bit_offset
) % BITS_PER_BYTE
) {
910 /* setup the last byte with msb bits from nvmem */
911 rc
= regmap_raw_read(nvmem
->regmap
,
912 cell
->offset
+ cell
->bytes
- 1, &v
, 1);
913 *p
|= GENMASK(7, (nbits
+ bit_offset
) % BITS_PER_BYTE
) & v
;
921 * nvmem_cell_write() - Write to a given nvmem cell
923 * @cell: nvmem cell to be written.
924 * @buf: Buffer to be written.
925 * @len: length of buffer to be written to nvmem cell.
927 * Return: length of bytes written or negative on failure.
929 int nvmem_cell_write(struct nvmem_cell
*cell
, void *buf
, size_t len
)
931 struct nvmem_device
*nvmem
= cell
->nvmem
;
934 if (!nvmem
|| !nvmem
->regmap
|| nvmem
->read_only
||
935 (cell
->bit_offset
== 0 && len
!= cell
->bytes
))
938 if (cell
->bit_offset
|| cell
->nbits
) {
939 buf
= nvmem_cell_prepare_write_buffer(cell
, buf
, len
);
944 rc
= regmap_raw_write(nvmem
->regmap
, cell
->offset
, buf
, cell
->bytes
);
946 /* free the tmp buffer */
947 if (cell
->bit_offset
|| cell
->nbits
)
950 if (IS_ERR_VALUE(rc
))
955 EXPORT_SYMBOL_GPL(nvmem_cell_write
);
958 * nvmem_device_cell_read() - Read a given nvmem device and cell
960 * @nvmem: nvmem device to read from.
961 * @info: nvmem cell info to be read.
962 * @buf: buffer pointer which will be populated on successful read.
964 * Return: length of successful bytes read on success and negative
965 * error code on error.
967 ssize_t
nvmem_device_cell_read(struct nvmem_device
*nvmem
,
968 struct nvmem_cell_info
*info
, void *buf
)
970 struct nvmem_cell cell
;
974 if (!nvmem
|| !nvmem
->regmap
)
977 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
978 if (IS_ERR_VALUE(rc
))
981 rc
= __nvmem_cell_read(nvmem
, &cell
, buf
, &len
);
982 if (IS_ERR_VALUE(rc
))
987 EXPORT_SYMBOL_GPL(nvmem_device_cell_read
);
990 * nvmem_device_cell_write() - Write cell to a given nvmem device
992 * @nvmem: nvmem device to be written to.
993 * @info: nvmem cell info to be written
994 * @buf: buffer to be written to cell.
996 * Return: length of bytes written or negative error code on failure.
998 int nvmem_device_cell_write(struct nvmem_device
*nvmem
,
999 struct nvmem_cell_info
*info
, void *buf
)
1001 struct nvmem_cell cell
;
1004 if (!nvmem
|| !nvmem
->regmap
)
1007 rc
= nvmem_cell_info_to_nvmem_cell(nvmem
, info
, &cell
);
1008 if (IS_ERR_VALUE(rc
))
1011 return nvmem_cell_write(&cell
, buf
, cell
.bytes
);
1013 EXPORT_SYMBOL_GPL(nvmem_device_cell_write
);
1016 * nvmem_device_read() - Read from a given nvmem device
1018 * @nvmem: nvmem device to read from.
1019 * @offset: offset in nvmem device.
1020 * @bytes: number of bytes to read.
1021 * @buf: buffer pointer which will be populated on successful read.
1023 * Return: length of successful bytes read on success and negative
1024 * error code on error.
1026 int nvmem_device_read(struct nvmem_device
*nvmem
,
1027 unsigned int offset
,
1028 size_t bytes
, void *buf
)
1032 if (!nvmem
|| !nvmem
->regmap
)
1035 rc
= regmap_raw_read(nvmem
->regmap
, offset
, buf
, bytes
);
1037 if (IS_ERR_VALUE(rc
))
1042 EXPORT_SYMBOL_GPL(nvmem_device_read
);
1045 * nvmem_device_write() - Write cell to a given nvmem device
1047 * @nvmem: nvmem device to be written to.
1048 * @offset: offset in nvmem device.
1049 * @bytes: number of bytes to write.
1050 * @buf: buffer to be written.
1052 * Return: length of bytes written or negative error code on failure.
1054 int nvmem_device_write(struct nvmem_device
*nvmem
,
1055 unsigned int offset
,
1056 size_t bytes
, void *buf
)
1060 if (!nvmem
|| !nvmem
->regmap
)
1063 rc
= regmap_raw_write(nvmem
->regmap
, offset
, buf
, bytes
);
1065 if (IS_ERR_VALUE(rc
))
1071 EXPORT_SYMBOL_GPL(nvmem_device_write
);
1073 static int __init
nvmem_init(void)
1075 return bus_register(&nvmem_bus_type
);
1078 static void __exit
nvmem_exit(void)
1080 bus_unregister(&nvmem_bus_type
);
1083 subsys_initcall(nvmem_init
);
1084 module_exit(nvmem_exit
);
1086 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1087 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1088 MODULE_DESCRIPTION("nvmem Driver Core");
1089 MODULE_LICENSE("GPL v2");