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]);
306 * nvmem_register() - Register a nvmem device for given nvmem_config.
307 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
309 * @config: nvmem device configuration with which nvmem device is created.
311 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
315 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
317 struct nvmem_device *nvmem;
318 struct device_node *np;
323 return ERR_PTR(-EINVAL);
325 rm = dev_get_regmap(config->dev, NULL);
327 dev_err(config->dev, "Regmap not found\n");
328 return ERR_PTR(-EINVAL);
331 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
333 return ERR_PTR(-ENOMEM);
335 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
338 return ERR_PTR(rval);
343 nvmem->owner = config->owner;
344 nvmem->stride = regmap_get_reg_stride(rm);
345 nvmem->word_size = regmap_get_val_bytes(rm);
346 nvmem->size = regmap_get_max_register(rm) + nvmem->stride;
347 nvmem->dev.type = &nvmem_provider_type;
348 nvmem->dev.bus = &nvmem_bus_type;
349 nvmem->dev.parent = config->dev;
350 np = config->dev->of_node;
351 nvmem->dev.of_node = np;
352 dev_set_name(&nvmem->dev, "%s%d",
353 config->name ? : "nvmem", config->id);
355 nvmem->read_only = of_property_read_bool(np, "read-only") |
358 nvmem->dev.groups = nvmem->read_only ? nvmem_ro_dev_groups :
361 device_initialize(&nvmem->dev);
363 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
365 rval = device_add(&nvmem->dev);
367 ida_simple_remove(&nvmem_ida, nvmem->id);
369 return ERR_PTR(rval);
373 nvmem_add_cells(nvmem, config);
377 EXPORT_SYMBOL_GPL(nvmem_register);
380 * nvmem_unregister() - Unregister previously registered nvmem device
382 * @nvmem: Pointer to previously registered nvmem device.
384 * Return: Will be an negative on error or a zero on success.
386 int nvmem_unregister(struct nvmem_device *nvmem)
388 mutex_lock(&nvmem_mutex);
390 mutex_unlock(&nvmem_mutex);
393 mutex_unlock(&nvmem_mutex);
395 nvmem_device_remove_all_cells(nvmem);
396 device_del(&nvmem->dev);
400 EXPORT_SYMBOL_GPL(nvmem_unregister);
402 static struct nvmem_device *__nvmem_device_get(struct device_node *np,
403 struct nvmem_cell **cellp,
406 struct nvmem_device *nvmem = NULL;
408 mutex_lock(&nvmem_mutex);
411 nvmem = of_nvmem_find(np);
413 mutex_unlock(&nvmem_mutex);
414 return ERR_PTR(-EPROBE_DEFER);
417 struct nvmem_cell *cell = nvmem_find_cell(cell_id);
425 mutex_unlock(&nvmem_mutex);
426 return ERR_PTR(-ENOENT);
431 mutex_unlock(&nvmem_mutex);
433 if (!try_module_get(nvmem->owner)) {
435 "could not increase module refcount for cell %s\n",
438 mutex_lock(&nvmem_mutex);
440 mutex_unlock(&nvmem_mutex);
442 return ERR_PTR(-EINVAL);
448 static void __nvmem_device_put(struct nvmem_device *nvmem)
450 module_put(nvmem->owner);
451 mutex_lock(&nvmem_mutex);
453 mutex_unlock(&nvmem_mutex);
456 static int nvmem_match(struct device *dev, void *data)
458 return !strcmp(dev_name(dev), data);
461 static struct nvmem_device *nvmem_find(const char *name)
465 d = bus_find_device(&nvmem_bus_type, NULL, (void *)name, nvmem_match);
470 return to_nvmem_device(d);
473 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
475 * of_nvmem_device_get() - Get nvmem device from a given id
477 * @dev node: Device tree node that uses the nvmem device
478 * @id: nvmem name from nvmem-names property.
480 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
483 struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
486 struct device_node *nvmem_np;
489 index = of_property_match_string(np, "nvmem-names", id);
491 nvmem_np = of_parse_phandle(np, "nvmem", index);
493 return ERR_PTR(-EINVAL);
495 return __nvmem_device_get(nvmem_np, NULL, NULL);
497 EXPORT_SYMBOL_GPL(of_nvmem_device_get);
501 * nvmem_device_get() - Get nvmem device from a given id
503 * @dev : Device that uses the nvmem device
504 * @id: nvmem name from nvmem-names property.
506 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
509 struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
511 if (dev->of_node) { /* try dt first */
512 struct nvmem_device *nvmem;
514 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
516 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
521 return nvmem_find(dev_name);
523 EXPORT_SYMBOL_GPL(nvmem_device_get);
525 static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
527 struct nvmem_device **nvmem = res;
529 if (WARN_ON(!nvmem || !*nvmem))
532 return *nvmem == data;
535 static void devm_nvmem_device_release(struct device *dev, void *res)
537 nvmem_device_put(*(struct nvmem_device **)res);
541 * devm_nvmem_device_put() - put alredy got nvmem device
543 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
544 * that needs to be released.
546 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
550 ret = devres_release(dev, devm_nvmem_device_release,
551 devm_nvmem_device_match, nvmem);
555 EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
558 * nvmem_device_put() - put alredy got nvmem device
560 * @nvmem: pointer to nvmem device that needs to be released.
562 void nvmem_device_put(struct nvmem_device *nvmem)
564 __nvmem_device_put(nvmem);
566 EXPORT_SYMBOL_GPL(nvmem_device_put);
569 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
571 * @dev node: Device tree node that uses the nvmem cell
572 * @id: nvmem name in nvmems property.
574 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
575 * on success. The nvmem_cell will be freed by the automatically once the
578 struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
580 struct nvmem_device **ptr, *nvmem;
582 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
584 return ERR_PTR(-ENOMEM);
586 nvmem = nvmem_device_get(dev, id);
587 if (!IS_ERR(nvmem)) {
589 devres_add(dev, ptr);
596 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
598 static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id)
600 struct nvmem_cell *cell = NULL;
601 struct nvmem_device *nvmem;
603 nvmem = __nvmem_device_get(NULL, &cell, cell_id);
605 return ERR_CAST(nvmem);
610 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
612 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
614 * @dev node: Device tree node that uses the nvmem cell
615 * @id: nvmem cell name from nvmem-cell-names property.
617 * Return: Will be an ERR_PTR() on error or a valid pointer
618 * to a struct nvmem_cell. The nvmem_cell will be freed by the
621 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
624 struct device_node *cell_np, *nvmem_np;
625 struct nvmem_cell *cell;
626 struct nvmem_device *nvmem;
628 int rval, len, index;
630 index = of_property_match_string(np, "nvmem-cell-names", name);
632 cell_np = of_parse_phandle(np, "nvmem-cells", index);
634 return ERR_PTR(-EINVAL);
636 nvmem_np = of_get_next_parent(cell_np);
638 return ERR_PTR(-EINVAL);
640 nvmem = __nvmem_device_get(nvmem_np, NULL, NULL);
642 return ERR_CAST(nvmem);
644 addr = of_get_property(cell_np, "reg", &len);
645 if (!addr || (len < 2 * sizeof(u32))) {
646 dev_err(&nvmem->dev, "nvmem: invalid reg on %s\n",
652 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
659 cell->offset = be32_to_cpup(addr++);
660 cell->bytes = be32_to_cpup(addr);
661 cell->name = cell_np->name;
663 addr = of_get_property(cell_np, "bits", &len);
664 if (addr && len == (2 * sizeof(u32))) {
665 cell->bit_offset = be32_to_cpup(addr++);
666 cell->nbits = be32_to_cpup(addr);
670 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
673 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
675 "cell %s unaligned to nvmem stride %d\n",
676 cell->name, nvmem->stride);
681 nvmem_cell_add(cell);
689 __nvmem_device_put(nvmem);
691 return ERR_PTR(rval);
693 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
697 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
699 * @dev node: Device tree node that uses the nvmem cell
700 * @id: nvmem cell name to get.
702 * Return: Will be an ERR_PTR() on error or a valid pointer
703 * to a struct nvmem_cell. The nvmem_cell will be freed by the
706 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id)
708 struct nvmem_cell *cell;
710 if (dev->of_node) { /* try dt first */
711 cell = of_nvmem_cell_get(dev->of_node, cell_id);
712 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
716 return nvmem_cell_get_from_list(cell_id);
718 EXPORT_SYMBOL_GPL(nvmem_cell_get);
720 static void devm_nvmem_cell_release(struct device *dev, void *res)
722 nvmem_cell_put(*(struct nvmem_cell **)res);
726 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
728 * @dev node: Device tree node that uses the nvmem cell
729 * @id: nvmem id in nvmem-names property.
731 * Return: Will be an ERR_PTR() on error or a valid pointer
732 * to a struct nvmem_cell. The nvmem_cell will be freed by the
733 * automatically once the device is freed.
735 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
737 struct nvmem_cell **ptr, *cell;
739 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
741 return ERR_PTR(-ENOMEM);
743 cell = nvmem_cell_get(dev, id);
746 devres_add(dev, ptr);
753 EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
755 static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
757 struct nvmem_cell **c = res;
759 if (WARN_ON(!c || !*c))
766 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
767 * from devm_nvmem_cell_get.
769 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get()
771 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
775 ret = devres_release(dev, devm_nvmem_cell_release,
776 devm_nvmem_cell_match, cell);
780 EXPORT_SYMBOL(devm_nvmem_cell_put);
783 * nvmem_cell_put() - Release previously allocated nvmem cell.
785 * @cell: Previously allocated nvmem cell by nvmem_cell_get()
787 void nvmem_cell_put(struct nvmem_cell *cell)
789 struct nvmem_device *nvmem = cell->nvmem;
791 __nvmem_device_put(nvmem);
792 nvmem_cell_drop(cell);
794 EXPORT_SYMBOL_GPL(nvmem_cell_put);
796 static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell,
800 int i, bit_offset = cell->bit_offset;
807 /* setup rest of the bytes if any */
808 for (i = 1; i < cell->bytes; i++) {
809 /* Get bits from next byte and shift them towards msb */
810 *p |= *b << (BITS_PER_BYTE - bit_offset);
816 /* result fits in less bytes */
817 if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
820 /* clear msb bits if any leftover in the last byte */
821 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
824 static int __nvmem_cell_read(struct nvmem_device *nvmem,
825 struct nvmem_cell *cell,
826 void *buf, size_t *len)
830 rc = regmap_raw_read(nvmem->regmap, cell->offset, buf, cell->bytes);
832 if (IS_ERR_VALUE(rc))
835 /* shift bits in-place */
836 if (cell->bit_offset || cell->nbits)
837 nvmem_shift_read_buffer_in_place(cell, buf);
845 * nvmem_cell_read() - Read a given nvmem cell
847 * @cell: nvmem cell to be read.
848 * @len: pointer to length of cell which will be populated on successful read.
850 * Return: ERR_PTR() on error or a valid pointer to a char * buffer on success.
851 * The buffer should be freed by the consumer with a kfree().
853 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
855 struct nvmem_device *nvmem = cell->nvmem;
859 if (!nvmem || !nvmem->regmap)
860 return ERR_PTR(-EINVAL);
862 buf = kzalloc(cell->bytes, GFP_KERNEL);
864 return ERR_PTR(-ENOMEM);
866 rc = __nvmem_cell_read(nvmem, cell, buf, len);
867 if (IS_ERR_VALUE(rc)) {
874 EXPORT_SYMBOL_GPL(nvmem_cell_read);
876 static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
879 struct nvmem_device *nvmem = cell->nvmem;
880 int i, rc, nbits, bit_offset = cell->bit_offset;
881 u8 v, *p, *buf, *b, pbyte, pbits;
884 buf = kzalloc(cell->bytes, GFP_KERNEL);
886 return ERR_PTR(-ENOMEM);
888 memcpy(buf, _buf, len);
895 /* setup the first byte with lsb bits from nvmem */
896 rc = regmap_raw_read(nvmem->regmap, cell->offset, &v, 1);
897 *b++ |= GENMASK(bit_offset - 1, 0) & v;
899 /* setup rest of the byte if any */
900 for (i = 1; i < cell->bytes; i++) {
901 /* Get last byte bits and shift them towards lsb */
902 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
910 /* if it's not end on byte boundary */
911 if ((nbits + bit_offset) % BITS_PER_BYTE) {
912 /* setup the last byte with msb bits from nvmem */
913 rc = regmap_raw_read(nvmem->regmap,
914 cell->offset + cell->bytes - 1, &v, 1);
915 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
923 * nvmem_cell_write() - Write to a given nvmem cell
925 * @cell: nvmem cell to be written.
926 * @buf: Buffer to be written.
927 * @len: length of buffer to be written to nvmem cell.
929 * Return: length of bytes written or negative on failure.
931 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
933 struct nvmem_device *nvmem = cell->nvmem;
936 if (!nvmem || !nvmem->regmap || nvmem->read_only ||
937 (cell->bit_offset == 0 && len != cell->bytes))
940 if (cell->bit_offset || cell->nbits) {
941 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
946 rc = regmap_raw_write(nvmem->regmap, cell->offset, buf, cell->bytes);
948 /* free the tmp buffer */
949 if (cell->bit_offset || cell->nbits)
952 if (IS_ERR_VALUE(rc))
957 EXPORT_SYMBOL_GPL(nvmem_cell_write);
960 * nvmem_device_cell_read() - Read a given nvmem device and cell
962 * @nvmem: nvmem device to read from.
963 * @info: nvmem cell info to be read.
964 * @buf: buffer pointer which will be populated on successful read.
966 * Return: length of successful bytes read on success and negative
967 * error code on error.
969 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
970 struct nvmem_cell_info *info, void *buf)
972 struct nvmem_cell cell;
976 if (!nvmem || !nvmem->regmap)
979 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
980 if (IS_ERR_VALUE(rc))
983 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
984 if (IS_ERR_VALUE(rc))
989 EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
992 * nvmem_device_cell_write() - Write cell to a given nvmem device
994 * @nvmem: nvmem device to be written to.
995 * @info: nvmem cell info to be written
996 * @buf: buffer to be written to cell.
998 * Return: length of bytes written or negative error code on failure.
1000 int nvmem_device_cell_write(struct nvmem_device *nvmem,
1001 struct nvmem_cell_info *info, void *buf)
1003 struct nvmem_cell cell;
1006 if (!nvmem || !nvmem->regmap)
1009 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1010 if (IS_ERR_VALUE(rc))
1013 return nvmem_cell_write(&cell, buf, cell.bytes);
1015 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1018 * nvmem_device_read() - Read from a given nvmem device
1020 * @nvmem: nvmem device to read from.
1021 * @offset: offset in nvmem device.
1022 * @bytes: number of bytes to read.
1023 * @buf: buffer pointer which will be populated on successful read.
1025 * Return: length of successful bytes read on success and negative
1026 * error code on error.
1028 int nvmem_device_read(struct nvmem_device *nvmem,
1029 unsigned int offset,
1030 size_t bytes, void *buf)
1034 if (!nvmem || !nvmem->regmap)
1037 rc = regmap_raw_read(nvmem->regmap, offset, buf, bytes);
1039 if (IS_ERR_VALUE(rc))
1044 EXPORT_SYMBOL_GPL(nvmem_device_read);
1047 * nvmem_device_write() - Write cell to a given nvmem device
1049 * @nvmem: nvmem device to be written to.
1050 * @offset: offset in nvmem device.
1051 * @bytes: number of bytes to write.
1052 * @buf: buffer to be written.
1054 * Return: length of bytes written or negative error code on failure.
1056 int nvmem_device_write(struct nvmem_device *nvmem,
1057 unsigned int offset,
1058 size_t bytes, void *buf)
1062 if (!nvmem || !nvmem->regmap)
1065 rc = regmap_raw_write(nvmem->regmap, offset, buf, bytes);
1067 if (IS_ERR_VALUE(rc))
1073 EXPORT_SYMBOL_GPL(nvmem_device_write);
1075 static int __init nvmem_init(void)
1077 return bus_register(&nvmem_bus_type);
1080 static void __exit nvmem_exit(void)
1082 bus_unregister(&nvmem_bus_type);
1085 subsys_initcall(nvmem_init);
1086 module_exit(nvmem_exit);
1088 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1089 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1090 MODULE_DESCRIPTION("nvmem Driver Core");
1091 MODULE_LICENSE("GPL v2");