Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * nvmem framework core.
4 *
5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7 */
8
9#include <linux/device.h>
10#include <linux/export.h>
11#include <linux/fs.h>
12#include <linux/idr.h>
13#include <linux/init.h>
14#include <linux/kref.h>
15#include <linux/module.h>
16#include <linux/nvmem-consumer.h>
17#include <linux/nvmem-provider.h>
18#include <linux/gpio/consumer.h>
19#include <linux/of.h>
20#include <linux/slab.h>
21
22struct nvmem_device {
23 struct module *owner;
24 struct device dev;
25 int stride;
26 int word_size;
27 int id;
28 struct kref refcnt;
29 size_t size;
30 bool read_only;
31 bool root_only;
32 int flags;
33 enum nvmem_type type;
34 struct bin_attribute eeprom;
35 struct device *base_dev;
36 struct list_head cells;
37 nvmem_reg_read_t reg_read;
38 nvmem_reg_write_t reg_write;
39 struct gpio_desc *wp_gpio;
40 void *priv;
41};
42
43#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
44
45#define FLAG_COMPAT BIT(0)
46
47struct nvmem_cell {
48 const char *name;
49 int offset;
50 int bytes;
51 int bit_offset;
52 int nbits;
53 struct device_node *np;
54 struct nvmem_device *nvmem;
55 struct list_head node;
56};
57
58static DEFINE_MUTEX(nvmem_mutex);
59static DEFINE_IDA(nvmem_ida);
60
61static DEFINE_MUTEX(nvmem_cell_mutex);
62static LIST_HEAD(nvmem_cell_tables);
63
64static DEFINE_MUTEX(nvmem_lookup_mutex);
65static LIST_HEAD(nvmem_lookup_list);
66
67static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
68
69#ifdef CONFIG_NVMEM_SYSFS
70static const char * const nvmem_type_str[] = {
71 [NVMEM_TYPE_UNKNOWN] = "Unknown",
72 [NVMEM_TYPE_EEPROM] = "EEPROM",
73 [NVMEM_TYPE_OTP] = "OTP",
74 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
75};
76
77#ifdef CONFIG_DEBUG_LOCK_ALLOC
78static struct lock_class_key eeprom_lock_key;
79#endif
80
81static ssize_t type_show(struct device *dev,
82 struct device_attribute *attr, char *buf)
83{
84 struct nvmem_device *nvmem = to_nvmem_device(dev);
85
86 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
87}
88
89static DEVICE_ATTR_RO(type);
90
91static struct attribute *nvmem_attrs[] = {
92 &dev_attr_type.attr,
93 NULL,
94};
95
96static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
97 struct bin_attribute *attr, char *buf,
98 loff_t pos, size_t count)
99{
100 struct device *dev;
101 struct nvmem_device *nvmem;
102 int rc;
103
104 if (attr->private)
105 dev = attr->private;
106 else
107 dev = container_of(kobj, struct device, kobj);
108 nvmem = to_nvmem_device(dev);
109
110 /* Stop the user from reading */
111 if (pos >= nvmem->size)
112 return 0;
113
114 if (count < nvmem->word_size)
115 return -EINVAL;
116
117 if (pos + count > nvmem->size)
118 count = nvmem->size - pos;
119
120 count = round_down(count, nvmem->word_size);
121
122 if (!nvmem->reg_read)
123 return -EPERM;
124
125 rc = nvmem->reg_read(nvmem->priv, pos, buf, count);
126
127 if (rc)
128 return rc;
129
130 return count;
131}
132
133static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
134 struct bin_attribute *attr, char *buf,
135 loff_t pos, size_t count)
136{
137 struct device *dev;
138 struct nvmem_device *nvmem;
139 int rc;
140
141 if (attr->private)
142 dev = attr->private;
143 else
144 dev = container_of(kobj, struct device, kobj);
145 nvmem = to_nvmem_device(dev);
146
147 /* Stop the user from writing */
148 if (pos >= nvmem->size)
149 return -EFBIG;
150
151 if (count < nvmem->word_size)
152 return -EINVAL;
153
154 if (pos + count > nvmem->size)
155 count = nvmem->size - pos;
156
157 count = round_down(count, nvmem->word_size);
158
159 if (!nvmem->reg_write)
160 return -EPERM;
161
162 rc = nvmem->reg_write(nvmem->priv, pos, buf, count);
163
164 if (rc)
165 return rc;
166
167 return count;
168}
169
170static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
171 struct bin_attribute *attr, int i)
172{
173 struct device *dev = container_of(kobj, struct device, kobj);
174 struct nvmem_device *nvmem = to_nvmem_device(dev);
175 umode_t mode = 0400;
176
177 if (!nvmem->root_only)
178 mode |= 0044;
179
180 if (!nvmem->read_only)
181 mode |= 0200;
182
183 if (!nvmem->reg_write)
184 mode &= ~0200;
185
186 if (!nvmem->reg_read)
187 mode &= ~0444;
188
189 return mode;
190}
191
192/* default read/write permissions */
193static struct bin_attribute bin_attr_rw_nvmem = {
194 .attr = {
195 .name = "nvmem",
196 .mode = 0644,
197 },
198 .read = bin_attr_nvmem_read,
199 .write = bin_attr_nvmem_write,
200};
201
202static struct bin_attribute *nvmem_bin_attributes[] = {
203 &bin_attr_rw_nvmem,
204 NULL,
205};
206
207static const struct attribute_group nvmem_bin_group = {
208 .bin_attrs = nvmem_bin_attributes,
209 .attrs = nvmem_attrs,
210 .is_bin_visible = nvmem_bin_attr_is_visible,
211};
212
213static const struct attribute_group *nvmem_dev_groups[] = {
214 &nvmem_bin_group,
215 NULL,
216};
217
218/* read only permission */
219static struct bin_attribute bin_attr_ro_nvmem = {
220 .attr = {
221 .name = "nvmem",
222 .mode = 0444,
223 },
224 .read = bin_attr_nvmem_read,
225};
226
227/* default read/write permissions, root only */
228static struct bin_attribute bin_attr_rw_root_nvmem = {
229 .attr = {
230 .name = "nvmem",
231 .mode = 0600,
232 },
233 .read = bin_attr_nvmem_read,
234 .write = bin_attr_nvmem_write,
235};
236
237/* read only permission, root only */
238static struct bin_attribute bin_attr_ro_root_nvmem = {
239 .attr = {
240 .name = "nvmem",
241 .mode = 0400,
242 },
243 .read = bin_attr_nvmem_read,
244};
245
246/*
247 * nvmem_setup_compat() - Create an additional binary entry in
248 * drivers sys directory, to be backwards compatible with the older
249 * drivers/misc/eeprom drivers.
250 */
251static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
252 const struct nvmem_config *config)
253{
254 int rval;
255
256 if (!config->compat)
257 return 0;
258
259 if (!config->base_dev)
260 return -EINVAL;
261
262 if (nvmem->read_only) {
263 if (config->root_only)
264 nvmem->eeprom = bin_attr_ro_root_nvmem;
265 else
266 nvmem->eeprom = bin_attr_ro_nvmem;
267 } else {
268 if (config->root_only)
269 nvmem->eeprom = bin_attr_rw_root_nvmem;
270 else
271 nvmem->eeprom = bin_attr_rw_nvmem;
272 }
273 nvmem->eeprom.attr.name = "eeprom";
274 nvmem->eeprom.size = nvmem->size;
275#ifdef CONFIG_DEBUG_LOCK_ALLOC
276 nvmem->eeprom.attr.key = &eeprom_lock_key;
277#endif
278 nvmem->eeprom.private = &nvmem->dev;
279 nvmem->base_dev = config->base_dev;
280
281 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
282 if (rval) {
283 dev_err(&nvmem->dev,
284 "Failed to create eeprom binary file %d\n", rval);
285 return rval;
286 }
287
288 nvmem->flags |= FLAG_COMPAT;
289
290 return 0;
291}
292
293static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
294 const struct nvmem_config *config)
295{
296 if (config->compat)
297 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
298}
299
300#else /* CONFIG_NVMEM_SYSFS */
301
302static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
303 const struct nvmem_config *config)
304{
305 return -ENOSYS;
306}
307static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
308 const struct nvmem_config *config)
309{
310}
311
312#endif /* CONFIG_NVMEM_SYSFS */
313
314static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
315 void *val, size_t bytes)
316{
317 if (nvmem->reg_read)
318 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
319
320 return -EINVAL;
321}
322
323static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
324 void *val, size_t bytes)
325{
326 int ret;
327
328 if (nvmem->reg_write) {
329 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
330 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
331 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
332 return ret;
333 }
334
335 return -EINVAL;
336}
337
338static void nvmem_release(struct device *dev)
339{
340 struct nvmem_device *nvmem = to_nvmem_device(dev);
341
342 ida_simple_remove(&nvmem_ida, nvmem->id);
343 gpiod_put(nvmem->wp_gpio);
344 kfree(nvmem);
345}
346
347static const struct device_type nvmem_provider_type = {
348 .release = nvmem_release,
349};
350
351static struct bus_type nvmem_bus_type = {
352 .name = "nvmem",
353};
354
355static void nvmem_cell_drop(struct nvmem_cell *cell)
356{
357 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
358 mutex_lock(&nvmem_mutex);
359 list_del(&cell->node);
360 mutex_unlock(&nvmem_mutex);
361 of_node_put(cell->np);
362 kfree_const(cell->name);
363 kfree(cell);
364}
365
366static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
367{
368 struct nvmem_cell *cell, *p;
369
370 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
371 nvmem_cell_drop(cell);
372}
373
374static void nvmem_cell_add(struct nvmem_cell *cell)
375{
376 mutex_lock(&nvmem_mutex);
377 list_add_tail(&cell->node, &cell->nvmem->cells);
378 mutex_unlock(&nvmem_mutex);
379 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
380}
381
382static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
383 const struct nvmem_cell_info *info,
384 struct nvmem_cell *cell)
385{
386 cell->nvmem = nvmem;
387 cell->offset = info->offset;
388 cell->bytes = info->bytes;
389 cell->name = kstrdup_const(info->name, GFP_KERNEL);
390 if (!cell->name)
391 return -ENOMEM;
392
393 cell->bit_offset = info->bit_offset;
394 cell->nbits = info->nbits;
395
396 if (cell->nbits)
397 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
398 BITS_PER_BYTE);
399
400 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
401 dev_err(&nvmem->dev,
402 "cell %s unaligned to nvmem stride %d\n",
403 cell->name, nvmem->stride);
404 return -EINVAL;
405 }
406
407 return 0;
408}
409
410/**
411 * nvmem_add_cells() - Add cell information to an nvmem device
412 *
413 * @nvmem: nvmem device to add cells to.
414 * @info: nvmem cell info to add to the device
415 * @ncells: number of cells in info
416 *
417 * Return: 0 or negative error code on failure.
418 */
419static int nvmem_add_cells(struct nvmem_device *nvmem,
420 const struct nvmem_cell_info *info,
421 int ncells)
422{
423 struct nvmem_cell **cells;
424 int i, rval;
425
426 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
427 if (!cells)
428 return -ENOMEM;
429
430 for (i = 0; i < ncells; i++) {
431 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
432 if (!cells[i]) {
433 rval = -ENOMEM;
434 goto err;
435 }
436
437 rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
438 if (rval) {
439 kfree(cells[i]);
440 goto err;
441 }
442
443 nvmem_cell_add(cells[i]);
444 }
445
446 /* remove tmp array */
447 kfree(cells);
448
449 return 0;
450err:
451 while (i--)
452 nvmem_cell_drop(cells[i]);
453
454 kfree(cells);
455
456 return rval;
457}
458
459/**
460 * nvmem_register_notifier() - Register a notifier block for nvmem events.
461 *
462 * @nb: notifier block to be called on nvmem events.
463 *
464 * Return: 0 on success, negative error number on failure.
465 */
466int nvmem_register_notifier(struct notifier_block *nb)
467{
468 return blocking_notifier_chain_register(&nvmem_notifier, nb);
469}
470EXPORT_SYMBOL_GPL(nvmem_register_notifier);
471
472/**
473 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
474 *
475 * @nb: notifier block to be unregistered.
476 *
477 * Return: 0 on success, negative error number on failure.
478 */
479int nvmem_unregister_notifier(struct notifier_block *nb)
480{
481 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
482}
483EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
484
485static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
486{
487 const struct nvmem_cell_info *info;
488 struct nvmem_cell_table *table;
489 struct nvmem_cell *cell;
490 int rval = 0, i;
491
492 mutex_lock(&nvmem_cell_mutex);
493 list_for_each_entry(table, &nvmem_cell_tables, node) {
494 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
495 for (i = 0; i < table->ncells; i++) {
496 info = &table->cells[i];
497
498 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
499 if (!cell) {
500 rval = -ENOMEM;
501 goto out;
502 }
503
504 rval = nvmem_cell_info_to_nvmem_cell(nvmem,
505 info,
506 cell);
507 if (rval) {
508 kfree(cell);
509 goto out;
510 }
511
512 nvmem_cell_add(cell);
513 }
514 }
515 }
516
517out:
518 mutex_unlock(&nvmem_cell_mutex);
519 return rval;
520}
521
522static struct nvmem_cell *
523nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
524{
525 struct nvmem_cell *iter, *cell = NULL;
526
527 mutex_lock(&nvmem_mutex);
528 list_for_each_entry(iter, &nvmem->cells, node) {
529 if (strcmp(cell_id, iter->name) == 0) {
530 cell = iter;
531 break;
532 }
533 }
534 mutex_unlock(&nvmem_mutex);
535
536 return cell;
537}
538
539static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
540{
541 struct device_node *parent, *child;
542 struct device *dev = &nvmem->dev;
543 struct nvmem_cell *cell;
544 const __be32 *addr;
545 int len;
546
547 parent = dev->of_node;
548
549 for_each_child_of_node(parent, child) {
550 addr = of_get_property(child, "reg", &len);
551 if (!addr || (len < 2 * sizeof(u32))) {
552 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
553 return -EINVAL;
554 }
555
556 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
557 if (!cell)
558 return -ENOMEM;
559
560 cell->nvmem = nvmem;
561 cell->np = of_node_get(child);
562 cell->offset = be32_to_cpup(addr++);
563 cell->bytes = be32_to_cpup(addr);
564 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
565
566 addr = of_get_property(child, "bits", &len);
567 if (addr && len == (2 * sizeof(u32))) {
568 cell->bit_offset = be32_to_cpup(addr++);
569 cell->nbits = be32_to_cpup(addr);
570 }
571
572 if (cell->nbits)
573 cell->bytes = DIV_ROUND_UP(
574 cell->nbits + cell->bit_offset,
575 BITS_PER_BYTE);
576
577 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
578 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
579 cell->name, nvmem->stride);
580 /* Cells already added will be freed later. */
581 kfree_const(cell->name);
582 kfree(cell);
583 return -EINVAL;
584 }
585
586 nvmem_cell_add(cell);
587 }
588
589 return 0;
590}
591
592/**
593 * nvmem_register() - Register a nvmem device for given nvmem_config.
594 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
595 *
596 * @config: nvmem device configuration with which nvmem device is created.
597 *
598 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
599 * on success.
600 */
601
602struct nvmem_device *nvmem_register(const struct nvmem_config *config)
603{
604 struct nvmem_device *nvmem;
605 int rval;
606
607 if (!config->dev)
608 return ERR_PTR(-EINVAL);
609
610 if (!config->reg_read && !config->reg_write)
611 return ERR_PTR(-EINVAL);
612
613 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
614 if (!nvmem)
615 return ERR_PTR(-ENOMEM);
616
617 rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
618 if (rval < 0) {
619 kfree(nvmem);
620 return ERR_PTR(rval);
621 }
622
623 if (config->wp_gpio)
624 nvmem->wp_gpio = config->wp_gpio;
625 else
626 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
627 GPIOD_OUT_HIGH);
628 if (IS_ERR(nvmem->wp_gpio)) {
629 ida_simple_remove(&nvmem_ida, nvmem->id);
630 rval = PTR_ERR(nvmem->wp_gpio);
631 kfree(nvmem);
632 return ERR_PTR(rval);
633 }
634
635 kref_init(&nvmem->refcnt);
636 INIT_LIST_HEAD(&nvmem->cells);
637
638 nvmem->id = rval;
639 nvmem->owner = config->owner;
640 if (!nvmem->owner && config->dev->driver)
641 nvmem->owner = config->dev->driver->owner;
642 nvmem->stride = config->stride ?: 1;
643 nvmem->word_size = config->word_size ?: 1;
644 nvmem->size = config->size;
645 nvmem->dev.type = &nvmem_provider_type;
646 nvmem->dev.bus = &nvmem_bus_type;
647 nvmem->dev.parent = config->dev;
648 nvmem->root_only = config->root_only;
649 nvmem->priv = config->priv;
650 nvmem->type = config->type;
651 nvmem->reg_read = config->reg_read;
652 nvmem->reg_write = config->reg_write;
653 if (!config->no_of_node)
654 nvmem->dev.of_node = config->dev->of_node;
655
656 if (config->id == -1 && config->name) {
657 dev_set_name(&nvmem->dev, "%s", config->name);
658 } else {
659 dev_set_name(&nvmem->dev, "%s%d",
660 config->name ? : "nvmem",
661 config->name ? config->id : nvmem->id);
662 }
663
664 nvmem->read_only = device_property_present(config->dev, "read-only") ||
665 config->read_only || !nvmem->reg_write;
666
667#ifdef CONFIG_NVMEM_SYSFS
668 nvmem->dev.groups = nvmem_dev_groups;
669#endif
670
671 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
672
673 rval = device_register(&nvmem->dev);
674 if (rval)
675 goto err_put_device;
676
677 if (config->compat) {
678 rval = nvmem_sysfs_setup_compat(nvmem, config);
679 if (rval)
680 goto err_device_del;
681 }
682
683 if (config->cells) {
684 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
685 if (rval)
686 goto err_teardown_compat;
687 }
688
689 rval = nvmem_add_cells_from_table(nvmem);
690 if (rval)
691 goto err_remove_cells;
692
693 rval = nvmem_add_cells_from_of(nvmem);
694 if (rval)
695 goto err_remove_cells;
696
697 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
698
699 return nvmem;
700
701err_remove_cells:
702 nvmem_device_remove_all_cells(nvmem);
703err_teardown_compat:
704 if (config->compat)
705 nvmem_sysfs_remove_compat(nvmem, config);
706err_device_del:
707 device_del(&nvmem->dev);
708err_put_device:
709 put_device(&nvmem->dev);
710
711 return ERR_PTR(rval);
712}
713EXPORT_SYMBOL_GPL(nvmem_register);
714
715static void nvmem_device_release(struct kref *kref)
716{
717 struct nvmem_device *nvmem;
718
719 nvmem = container_of(kref, struct nvmem_device, refcnt);
720
721 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
722
723 if (nvmem->flags & FLAG_COMPAT)
724 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
725
726 nvmem_device_remove_all_cells(nvmem);
727 device_unregister(&nvmem->dev);
728}
729
730/**
731 * nvmem_unregister() - Unregister previously registered nvmem device
732 *
733 * @nvmem: Pointer to previously registered nvmem device.
734 */
735void nvmem_unregister(struct nvmem_device *nvmem)
736{
737 kref_put(&nvmem->refcnt, nvmem_device_release);
738}
739EXPORT_SYMBOL_GPL(nvmem_unregister);
740
741static void devm_nvmem_release(struct device *dev, void *res)
742{
743 nvmem_unregister(*(struct nvmem_device **)res);
744}
745
746/**
747 * devm_nvmem_register() - Register a managed nvmem device for given
748 * nvmem_config.
749 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
750 *
751 * @dev: Device that uses the nvmem device.
752 * @config: nvmem device configuration with which nvmem device is created.
753 *
754 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
755 * on success.
756 */
757struct nvmem_device *devm_nvmem_register(struct device *dev,
758 const struct nvmem_config *config)
759{
760 struct nvmem_device **ptr, *nvmem;
761
762 ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
763 if (!ptr)
764 return ERR_PTR(-ENOMEM);
765
766 nvmem = nvmem_register(config);
767
768 if (!IS_ERR(nvmem)) {
769 *ptr = nvmem;
770 devres_add(dev, ptr);
771 } else {
772 devres_free(ptr);
773 }
774
775 return nvmem;
776}
777EXPORT_SYMBOL_GPL(devm_nvmem_register);
778
779static int devm_nvmem_match(struct device *dev, void *res, void *data)
780{
781 struct nvmem_device **r = res;
782
783 return *r == data;
784}
785
786/**
787 * devm_nvmem_unregister() - Unregister previously registered managed nvmem
788 * device.
789 *
790 * @dev: Device that uses the nvmem device.
791 * @nvmem: Pointer to previously registered nvmem device.
792 *
793 * Return: Will be an negative on error or a zero on success.
794 */
795int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
796{
797 return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
798}
799EXPORT_SYMBOL(devm_nvmem_unregister);
800
801static struct nvmem_device *__nvmem_device_get(void *data,
802 int (*match)(struct device *dev, const void *data))
803{
804 struct nvmem_device *nvmem = NULL;
805 struct device *dev;
806
807 mutex_lock(&nvmem_mutex);
808 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
809 if (dev)
810 nvmem = to_nvmem_device(dev);
811 mutex_unlock(&nvmem_mutex);
812 if (!nvmem)
813 return ERR_PTR(-EPROBE_DEFER);
814
815 if (!try_module_get(nvmem->owner)) {
816 dev_err(&nvmem->dev,
817 "could not increase module refcount for cell %s\n",
818 nvmem_dev_name(nvmem));
819
820 put_device(&nvmem->dev);
821 return ERR_PTR(-EINVAL);
822 }
823
824 kref_get(&nvmem->refcnt);
825
826 return nvmem;
827}
828
829static void __nvmem_device_put(struct nvmem_device *nvmem)
830{
831 put_device(&nvmem->dev);
832 module_put(nvmem->owner);
833 kref_put(&nvmem->refcnt, nvmem_device_release);
834}
835
836#if IS_ENABLED(CONFIG_OF)
837/**
838 * of_nvmem_device_get() - Get nvmem device from a given id
839 *
840 * @np: Device tree node that uses the nvmem device.
841 * @id: nvmem name from nvmem-names property.
842 *
843 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
844 * on success.
845 */
846struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
847{
848
849 struct device_node *nvmem_np;
850 int index = 0;
851
852 if (id)
853 index = of_property_match_string(np, "nvmem-names", id);
854
855 nvmem_np = of_parse_phandle(np, "nvmem", index);
856 if (!nvmem_np)
857 return ERR_PTR(-ENOENT);
858
859 return __nvmem_device_get(nvmem_np, device_match_of_node);
860}
861EXPORT_SYMBOL_GPL(of_nvmem_device_get);
862#endif
863
864/**
865 * nvmem_device_get() - Get nvmem device from a given id
866 *
867 * @dev: Device that uses the nvmem device.
868 * @dev_name: name of the requested nvmem device.
869 *
870 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
871 * on success.
872 */
873struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
874{
875 if (dev->of_node) { /* try dt first */
876 struct nvmem_device *nvmem;
877
878 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
879
880 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
881 return nvmem;
882
883 }
884
885 return __nvmem_device_get((void *)dev_name, device_match_name);
886}
887EXPORT_SYMBOL_GPL(nvmem_device_get);
888
889/**
890 * nvmem_device_find() - Find nvmem device with matching function
891 *
892 * @data: Data to pass to match function
893 * @match: Callback function to check device
894 *
895 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
896 * on success.
897 */
898struct nvmem_device *nvmem_device_find(void *data,
899 int (*match)(struct device *dev, const void *data))
900{
901 return __nvmem_device_get(data, match);
902}
903EXPORT_SYMBOL_GPL(nvmem_device_find);
904
905static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
906{
907 struct nvmem_device **nvmem = res;
908
909 if (WARN_ON(!nvmem || !*nvmem))
910 return 0;
911
912 return *nvmem == data;
913}
914
915static void devm_nvmem_device_release(struct device *dev, void *res)
916{
917 nvmem_device_put(*(struct nvmem_device **)res);
918}
919
920/**
921 * devm_nvmem_device_put() - put alredy got nvmem device
922 *
923 * @dev: Device that uses the nvmem device.
924 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
925 * that needs to be released.
926 */
927void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
928{
929 int ret;
930
931 ret = devres_release(dev, devm_nvmem_device_release,
932 devm_nvmem_device_match, nvmem);
933
934 WARN_ON(ret);
935}
936EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
937
938/**
939 * nvmem_device_put() - put alredy got nvmem device
940 *
941 * @nvmem: pointer to nvmem device that needs to be released.
942 */
943void nvmem_device_put(struct nvmem_device *nvmem)
944{
945 __nvmem_device_put(nvmem);
946}
947EXPORT_SYMBOL_GPL(nvmem_device_put);
948
949/**
950 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
951 *
952 * @dev: Device that requests the nvmem device.
953 * @id: name id for the requested nvmem device.
954 *
955 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
956 * on success. The nvmem_cell will be freed by the automatically once the
957 * device is freed.
958 */
959struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
960{
961 struct nvmem_device **ptr, *nvmem;
962
963 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
964 if (!ptr)
965 return ERR_PTR(-ENOMEM);
966
967 nvmem = nvmem_device_get(dev, id);
968 if (!IS_ERR(nvmem)) {
969 *ptr = nvmem;
970 devres_add(dev, ptr);
971 } else {
972 devres_free(ptr);
973 }
974
975 return nvmem;
976}
977EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
978
979static struct nvmem_cell *
980nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
981{
982 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
983 struct nvmem_cell_lookup *lookup;
984 struct nvmem_device *nvmem;
985 const char *dev_id;
986
987 if (!dev)
988 return ERR_PTR(-EINVAL);
989
990 dev_id = dev_name(dev);
991
992 mutex_lock(&nvmem_lookup_mutex);
993
994 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
995 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
996 (strcmp(lookup->con_id, con_id) == 0)) {
997 /* This is the right entry. */
998 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
999 device_match_name);
1000 if (IS_ERR(nvmem)) {
1001 /* Provider may not be registered yet. */
1002 cell = ERR_CAST(nvmem);
1003 break;
1004 }
1005
1006 cell = nvmem_find_cell_by_name(nvmem,
1007 lookup->cell_name);
1008 if (!cell) {
1009 __nvmem_device_put(nvmem);
1010 cell = ERR_PTR(-ENOENT);
1011 }
1012 break;
1013 }
1014 }
1015
1016 mutex_unlock(&nvmem_lookup_mutex);
1017 return cell;
1018}
1019
1020#if IS_ENABLED(CONFIG_OF)
1021static struct nvmem_cell *
1022nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
1023{
1024 struct nvmem_cell *iter, *cell = NULL;
1025
1026 mutex_lock(&nvmem_mutex);
1027 list_for_each_entry(iter, &nvmem->cells, node) {
1028 if (np == iter->np) {
1029 cell = iter;
1030 break;
1031 }
1032 }
1033 mutex_unlock(&nvmem_mutex);
1034
1035 return cell;
1036}
1037
1038/**
1039 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1040 *
1041 * @np: Device tree node that uses the nvmem cell.
1042 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1043 * for the cell at index 0 (the lone cell with no accompanying
1044 * nvmem-cell-names property).
1045 *
1046 * Return: Will be an ERR_PTR() on error or a valid pointer
1047 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1048 * nvmem_cell_put().
1049 */
1050struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1051{
1052 struct device_node *cell_np, *nvmem_np;
1053 struct nvmem_device *nvmem;
1054 struct nvmem_cell *cell;
1055 int index = 0;
1056
1057 /* if cell name exists, find index to the name */
1058 if (id)
1059 index = of_property_match_string(np, "nvmem-cell-names", id);
1060
1061 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1062 if (!cell_np)
1063 return ERR_PTR(-ENOENT);
1064
1065 nvmem_np = of_get_next_parent(cell_np);
1066 if (!nvmem_np)
1067 return ERR_PTR(-EINVAL);
1068
1069 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1070 of_node_put(nvmem_np);
1071 if (IS_ERR(nvmem))
1072 return ERR_CAST(nvmem);
1073
1074 cell = nvmem_find_cell_by_node(nvmem, cell_np);
1075 if (!cell) {
1076 __nvmem_device_put(nvmem);
1077 return ERR_PTR(-ENOENT);
1078 }
1079
1080 return cell;
1081}
1082EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1083#endif
1084
1085/**
1086 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1087 *
1088 * @dev: Device that requests the nvmem cell.
1089 * @id: nvmem cell name to get (this corresponds with the name from the
1090 * nvmem-cell-names property for DT systems and with the con_id from
1091 * the lookup entry for non-DT systems).
1092 *
1093 * Return: Will be an ERR_PTR() on error or a valid pointer
1094 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1095 * nvmem_cell_put().
1096 */
1097struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1098{
1099 struct nvmem_cell *cell;
1100
1101 if (dev->of_node) { /* try dt first */
1102 cell = of_nvmem_cell_get(dev->of_node, id);
1103 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1104 return cell;
1105 }
1106
1107 /* NULL cell id only allowed for device tree; invalid otherwise */
1108 if (!id)
1109 return ERR_PTR(-EINVAL);
1110
1111 return nvmem_cell_get_from_lookup(dev, id);
1112}
1113EXPORT_SYMBOL_GPL(nvmem_cell_get);
1114
1115static void devm_nvmem_cell_release(struct device *dev, void *res)
1116{
1117 nvmem_cell_put(*(struct nvmem_cell **)res);
1118}
1119
1120/**
1121 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1122 *
1123 * @dev: Device that requests the nvmem cell.
1124 * @id: nvmem cell name id to get.
1125 *
1126 * Return: Will be an ERR_PTR() on error or a valid pointer
1127 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1128 * automatically once the device is freed.
1129 */
1130struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1131{
1132 struct nvmem_cell **ptr, *cell;
1133
1134 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1135 if (!ptr)
1136 return ERR_PTR(-ENOMEM);
1137
1138 cell = nvmem_cell_get(dev, id);
1139 if (!IS_ERR(cell)) {
1140 *ptr = cell;
1141 devres_add(dev, ptr);
1142 } else {
1143 devres_free(ptr);
1144 }
1145
1146 return cell;
1147}
1148EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1149
1150static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1151{
1152 struct nvmem_cell **c = res;
1153
1154 if (WARN_ON(!c || !*c))
1155 return 0;
1156
1157 return *c == data;
1158}
1159
1160/**
1161 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1162 * from devm_nvmem_cell_get.
1163 *
1164 * @dev: Device that requests the nvmem cell.
1165 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1166 */
1167void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1168{
1169 int ret;
1170
1171 ret = devres_release(dev, devm_nvmem_cell_release,
1172 devm_nvmem_cell_match, cell);
1173
1174 WARN_ON(ret);
1175}
1176EXPORT_SYMBOL(devm_nvmem_cell_put);
1177
1178/**
1179 * nvmem_cell_put() - Release previously allocated nvmem cell.
1180 *
1181 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1182 */
1183void nvmem_cell_put(struct nvmem_cell *cell)
1184{
1185 struct nvmem_device *nvmem = cell->nvmem;
1186
1187 __nvmem_device_put(nvmem);
1188}
1189EXPORT_SYMBOL_GPL(nvmem_cell_put);
1190
1191static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
1192{
1193 u8 *p, *b;
1194 int i, extra, bit_offset = cell->bit_offset;
1195
1196 p = b = buf;
1197 if (bit_offset) {
1198 /* First shift */
1199 *b++ >>= bit_offset;
1200
1201 /* setup rest of the bytes if any */
1202 for (i = 1; i < cell->bytes; i++) {
1203 /* Get bits from next byte and shift them towards msb */
1204 *p |= *b << (BITS_PER_BYTE - bit_offset);
1205
1206 p = b;
1207 *b++ >>= bit_offset;
1208 }
1209 } else {
1210 /* point to the msb */
1211 p += cell->bytes - 1;
1212 }
1213
1214 /* result fits in less bytes */
1215 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1216 while (--extra >= 0)
1217 *p-- = 0;
1218
1219 /* clear msb bits if any leftover in the last byte */
1220 *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
1221}
1222
1223static int __nvmem_cell_read(struct nvmem_device *nvmem,
1224 struct nvmem_cell *cell,
1225 void *buf, size_t *len)
1226{
1227 int rc;
1228
1229 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1230
1231 if (rc)
1232 return rc;
1233
1234 /* shift bits in-place */
1235 if (cell->bit_offset || cell->nbits)
1236 nvmem_shift_read_buffer_in_place(cell, buf);
1237
1238 if (len)
1239 *len = cell->bytes;
1240
1241 return 0;
1242}
1243
1244/**
1245 * nvmem_cell_read() - Read a given nvmem cell
1246 *
1247 * @cell: nvmem cell to be read.
1248 * @len: pointer to length of cell which will be populated on successful read;
1249 * can be NULL.
1250 *
1251 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1252 * buffer should be freed by the consumer with a kfree().
1253 */
1254void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1255{
1256 struct nvmem_device *nvmem = cell->nvmem;
1257 u8 *buf;
1258 int rc;
1259
1260 if (!nvmem)
1261 return ERR_PTR(-EINVAL);
1262
1263 buf = kzalloc(cell->bytes, GFP_KERNEL);
1264 if (!buf)
1265 return ERR_PTR(-ENOMEM);
1266
1267 rc = __nvmem_cell_read(nvmem, cell, buf, len);
1268 if (rc) {
1269 kfree(buf);
1270 return ERR_PTR(rc);
1271 }
1272
1273 return buf;
1274}
1275EXPORT_SYMBOL_GPL(nvmem_cell_read);
1276
1277static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
1278 u8 *_buf, int len)
1279{
1280 struct nvmem_device *nvmem = cell->nvmem;
1281 int i, rc, nbits, bit_offset = cell->bit_offset;
1282 u8 v, *p, *buf, *b, pbyte, pbits;
1283
1284 nbits = cell->nbits;
1285 buf = kzalloc(cell->bytes, GFP_KERNEL);
1286 if (!buf)
1287 return ERR_PTR(-ENOMEM);
1288
1289 memcpy(buf, _buf, len);
1290 p = b = buf;
1291
1292 if (bit_offset) {
1293 pbyte = *b;
1294 *b <<= bit_offset;
1295
1296 /* setup the first byte with lsb bits from nvmem */
1297 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1298 if (rc)
1299 goto err;
1300 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1301
1302 /* setup rest of the byte if any */
1303 for (i = 1; i < cell->bytes; i++) {
1304 /* Get last byte bits and shift them towards lsb */
1305 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1306 pbyte = *b;
1307 p = b;
1308 *b <<= bit_offset;
1309 *b++ |= pbits;
1310 }
1311 }
1312
1313 /* if it's not end on byte boundary */
1314 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1315 /* setup the last byte with msb bits from nvmem */
1316 rc = nvmem_reg_read(nvmem,
1317 cell->offset + cell->bytes - 1, &v, 1);
1318 if (rc)
1319 goto err;
1320 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1321
1322 }
1323
1324 return buf;
1325err:
1326 kfree(buf);
1327 return ERR_PTR(rc);
1328}
1329
1330/**
1331 * nvmem_cell_write() - Write to a given nvmem cell
1332 *
1333 * @cell: nvmem cell to be written.
1334 * @buf: Buffer to be written.
1335 * @len: length of buffer to be written to nvmem cell.
1336 *
1337 * Return: length of bytes written or negative on failure.
1338 */
1339int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1340{
1341 struct nvmem_device *nvmem = cell->nvmem;
1342 int rc;
1343
1344 if (!nvmem || nvmem->read_only ||
1345 (cell->bit_offset == 0 && len != cell->bytes))
1346 return -EINVAL;
1347
1348 if (cell->bit_offset || cell->nbits) {
1349 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1350 if (IS_ERR(buf))
1351 return PTR_ERR(buf);
1352 }
1353
1354 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1355
1356 /* free the tmp buffer */
1357 if (cell->bit_offset || cell->nbits)
1358 kfree(buf);
1359
1360 if (rc)
1361 return rc;
1362
1363 return len;
1364}
1365EXPORT_SYMBOL_GPL(nvmem_cell_write);
1366
1367static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1368 void *val, size_t count)
1369{
1370 struct nvmem_cell *cell;
1371 void *buf;
1372 size_t len;
1373
1374 cell = nvmem_cell_get(dev, cell_id);
1375 if (IS_ERR(cell))
1376 return PTR_ERR(cell);
1377
1378 buf = nvmem_cell_read(cell, &len);
1379 if (IS_ERR(buf)) {
1380 nvmem_cell_put(cell);
1381 return PTR_ERR(buf);
1382 }
1383 if (len != count) {
1384 kfree(buf);
1385 nvmem_cell_put(cell);
1386 return -EINVAL;
1387 }
1388 memcpy(val, buf, count);
1389 kfree(buf);
1390 nvmem_cell_put(cell);
1391
1392 return 0;
1393}
1394
1395/**
1396 * nvmem_cell_read_u16() - Read a cell value as an u16
1397 *
1398 * @dev: Device that requests the nvmem cell.
1399 * @cell_id: Name of nvmem cell to read.
1400 * @val: pointer to output value.
1401 *
1402 * Return: 0 on success or negative errno.
1403 */
1404int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1405{
1406 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1407}
1408EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1409
1410/**
1411 * nvmem_cell_read_u32() - Read a cell value as an u32
1412 *
1413 * @dev: Device that requests the nvmem cell.
1414 * @cell_id: Name of nvmem cell to read.
1415 * @val: pointer to output value.
1416 *
1417 * Return: 0 on success or negative errno.
1418 */
1419int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1420{
1421 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1422}
1423EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1424
1425/**
1426 * nvmem_cell_read_u64() - Read a cell value as an u64
1427 *
1428 * @dev: Device that requests the nvmem cell.
1429 * @cell_id: Name of nvmem cell to read.
1430 * @val: pointer to output value.
1431 *
1432 * Return: 0 on success or negative errno.
1433 */
1434int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1435{
1436 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1437}
1438EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1439
1440/**
1441 * nvmem_device_cell_read() - Read a given nvmem device and cell
1442 *
1443 * @nvmem: nvmem device to read from.
1444 * @info: nvmem cell info to be read.
1445 * @buf: buffer pointer which will be populated on successful read.
1446 *
1447 * Return: length of successful bytes read on success and negative
1448 * error code on error.
1449 */
1450ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1451 struct nvmem_cell_info *info, void *buf)
1452{
1453 struct nvmem_cell cell;
1454 int rc;
1455 ssize_t len;
1456
1457 if (!nvmem)
1458 return -EINVAL;
1459
1460 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1461 if (rc)
1462 return rc;
1463
1464 rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
1465 if (rc)
1466 return rc;
1467
1468 return len;
1469}
1470EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1471
1472/**
1473 * nvmem_device_cell_write() - Write cell to a given nvmem device
1474 *
1475 * @nvmem: nvmem device to be written to.
1476 * @info: nvmem cell info to be written.
1477 * @buf: buffer to be written to cell.
1478 *
1479 * Return: length of bytes written or negative error code on failure.
1480 */
1481int nvmem_device_cell_write(struct nvmem_device *nvmem,
1482 struct nvmem_cell_info *info, void *buf)
1483{
1484 struct nvmem_cell cell;
1485 int rc;
1486
1487 if (!nvmem)
1488 return -EINVAL;
1489
1490 rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
1491 if (rc)
1492 return rc;
1493
1494 return nvmem_cell_write(&cell, buf, cell.bytes);
1495}
1496EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1497
1498/**
1499 * nvmem_device_read() - Read from a given nvmem device
1500 *
1501 * @nvmem: nvmem device to read from.
1502 * @offset: offset in nvmem device.
1503 * @bytes: number of bytes to read.
1504 * @buf: buffer pointer which will be populated on successful read.
1505 *
1506 * Return: length of successful bytes read on success and negative
1507 * error code on error.
1508 */
1509int nvmem_device_read(struct nvmem_device *nvmem,
1510 unsigned int offset,
1511 size_t bytes, void *buf)
1512{
1513 int rc;
1514
1515 if (!nvmem)
1516 return -EINVAL;
1517
1518 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1519
1520 if (rc)
1521 return rc;
1522
1523 return bytes;
1524}
1525EXPORT_SYMBOL_GPL(nvmem_device_read);
1526
1527/**
1528 * nvmem_device_write() - Write cell to a given nvmem device
1529 *
1530 * @nvmem: nvmem device to be written to.
1531 * @offset: offset in nvmem device.
1532 * @bytes: number of bytes to write.
1533 * @buf: buffer to be written.
1534 *
1535 * Return: length of bytes written or negative error code on failure.
1536 */
1537int nvmem_device_write(struct nvmem_device *nvmem,
1538 unsigned int offset,
1539 size_t bytes, void *buf)
1540{
1541 int rc;
1542
1543 if (!nvmem)
1544 return -EINVAL;
1545
1546 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1547
1548 if (rc)
1549 return rc;
1550
1551
1552 return bytes;
1553}
1554EXPORT_SYMBOL_GPL(nvmem_device_write);
1555
1556/**
1557 * nvmem_add_cell_table() - register a table of cell info entries
1558 *
1559 * @table: table of cell info entries
1560 */
1561void nvmem_add_cell_table(struct nvmem_cell_table *table)
1562{
1563 mutex_lock(&nvmem_cell_mutex);
1564 list_add_tail(&table->node, &nvmem_cell_tables);
1565 mutex_unlock(&nvmem_cell_mutex);
1566}
1567EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1568
1569/**
1570 * nvmem_del_cell_table() - remove a previously registered cell info table
1571 *
1572 * @table: table of cell info entries
1573 */
1574void nvmem_del_cell_table(struct nvmem_cell_table *table)
1575{
1576 mutex_lock(&nvmem_cell_mutex);
1577 list_del(&table->node);
1578 mutex_unlock(&nvmem_cell_mutex);
1579}
1580EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1581
1582/**
1583 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1584 *
1585 * @entries: array of cell lookup entries
1586 * @nentries: number of cell lookup entries in the array
1587 */
1588void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1589{
1590 int i;
1591
1592 mutex_lock(&nvmem_lookup_mutex);
1593 for (i = 0; i < nentries; i++)
1594 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1595 mutex_unlock(&nvmem_lookup_mutex);
1596}
1597EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1598
1599/**
1600 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1601 * entries
1602 *
1603 * @entries: array of cell lookup entries
1604 * @nentries: number of cell lookup entries in the array
1605 */
1606void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1607{
1608 int i;
1609
1610 mutex_lock(&nvmem_lookup_mutex);
1611 for (i = 0; i < nentries; i++)
1612 list_del(&entries[i].node);
1613 mutex_unlock(&nvmem_lookup_mutex);
1614}
1615EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1616
1617/**
1618 * nvmem_dev_name() - Get the name of a given nvmem device.
1619 *
1620 * @nvmem: nvmem device.
1621 *
1622 * Return: name of the nvmem device.
1623 */
1624const char *nvmem_dev_name(struct nvmem_device *nvmem)
1625{
1626 return dev_name(&nvmem->dev);
1627}
1628EXPORT_SYMBOL_GPL(nvmem_dev_name);
1629
1630static int __init nvmem_init(void)
1631{
1632 return bus_register(&nvmem_bus_type);
1633}
1634
1635static void __exit nvmem_exit(void)
1636{
1637 bus_unregister(&nvmem_bus_type);
1638}
1639
1640subsys_initcall(nvmem_init);
1641module_exit(nvmem_exit);
1642
1643MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1644MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1645MODULE_DESCRIPTION("nvmem Driver Core");
1646MODULE_LICENSE("GPL v2");