Loading...
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 const struct nvmem_keepout *keepout;
38 unsigned int nkeepout;
39 nvmem_reg_read_t reg_read;
40 nvmem_reg_write_t reg_write;
41 nvmem_cell_post_process_t cell_post_process;
42 struct gpio_desc *wp_gpio;
43 void *priv;
44};
45
46#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
47
48#define FLAG_COMPAT BIT(0)
49struct nvmem_cell_entry {
50 const char *name;
51 int offset;
52 int bytes;
53 int bit_offset;
54 int nbits;
55 struct device_node *np;
56 struct nvmem_device *nvmem;
57 struct list_head node;
58};
59
60struct nvmem_cell {
61 struct nvmem_cell_entry *entry;
62 const char *id;
63};
64
65static DEFINE_MUTEX(nvmem_mutex);
66static DEFINE_IDA(nvmem_ida);
67
68static DEFINE_MUTEX(nvmem_cell_mutex);
69static LIST_HEAD(nvmem_cell_tables);
70
71static DEFINE_MUTEX(nvmem_lookup_mutex);
72static LIST_HEAD(nvmem_lookup_list);
73
74static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
75
76static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
77 void *val, size_t bytes)
78{
79 if (nvmem->reg_read)
80 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
81
82 return -EINVAL;
83}
84
85static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
86 void *val, size_t bytes)
87{
88 int ret;
89
90 if (nvmem->reg_write) {
91 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
92 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
93 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
94 return ret;
95 }
96
97 return -EINVAL;
98}
99
100static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
101 unsigned int offset, void *val,
102 size_t bytes, int write)
103{
104
105 unsigned int end = offset + bytes;
106 unsigned int kend, ksize;
107 const struct nvmem_keepout *keepout = nvmem->keepout;
108 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
109 int rc;
110
111 /*
112 * Skip all keepouts before the range being accessed.
113 * Keepouts are sorted.
114 */
115 while ((keepout < keepoutend) && (keepout->end <= offset))
116 keepout++;
117
118 while ((offset < end) && (keepout < keepoutend)) {
119 /* Access the valid portion before the keepout. */
120 if (offset < keepout->start) {
121 kend = min(end, keepout->start);
122 ksize = kend - offset;
123 if (write)
124 rc = __nvmem_reg_write(nvmem, offset, val, ksize);
125 else
126 rc = __nvmem_reg_read(nvmem, offset, val, ksize);
127
128 if (rc)
129 return rc;
130
131 offset += ksize;
132 val += ksize;
133 }
134
135 /*
136 * Now we're aligned to the start of this keepout zone. Go
137 * through it.
138 */
139 kend = min(end, keepout->end);
140 ksize = kend - offset;
141 if (!write)
142 memset(val, keepout->value, ksize);
143
144 val += ksize;
145 offset += ksize;
146 keepout++;
147 }
148
149 /*
150 * If we ran out of keepouts but there's still stuff to do, send it
151 * down directly
152 */
153 if (offset < end) {
154 ksize = end - offset;
155 if (write)
156 return __nvmem_reg_write(nvmem, offset, val, ksize);
157 else
158 return __nvmem_reg_read(nvmem, offset, val, ksize);
159 }
160
161 return 0;
162}
163
164static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
165 void *val, size_t bytes)
166{
167 if (!nvmem->nkeepout)
168 return __nvmem_reg_read(nvmem, offset, val, bytes);
169
170 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
171}
172
173static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
174 void *val, size_t bytes)
175{
176 if (!nvmem->nkeepout)
177 return __nvmem_reg_write(nvmem, offset, val, bytes);
178
179 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
180}
181
182#ifdef CONFIG_NVMEM_SYSFS
183static const char * const nvmem_type_str[] = {
184 [NVMEM_TYPE_UNKNOWN] = "Unknown",
185 [NVMEM_TYPE_EEPROM] = "EEPROM",
186 [NVMEM_TYPE_OTP] = "OTP",
187 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
188 [NVMEM_TYPE_FRAM] = "FRAM",
189};
190
191#ifdef CONFIG_DEBUG_LOCK_ALLOC
192static struct lock_class_key eeprom_lock_key;
193#endif
194
195static ssize_t type_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
197{
198 struct nvmem_device *nvmem = to_nvmem_device(dev);
199
200 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
201}
202
203static DEVICE_ATTR_RO(type);
204
205static struct attribute *nvmem_attrs[] = {
206 &dev_attr_type.attr,
207 NULL,
208};
209
210static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
211 struct bin_attribute *attr, char *buf,
212 loff_t pos, size_t count)
213{
214 struct device *dev;
215 struct nvmem_device *nvmem;
216 int rc;
217
218 if (attr->private)
219 dev = attr->private;
220 else
221 dev = kobj_to_dev(kobj);
222 nvmem = to_nvmem_device(dev);
223
224 /* Stop the user from reading */
225 if (pos >= nvmem->size)
226 return 0;
227
228 if (!IS_ALIGNED(pos, nvmem->stride))
229 return -EINVAL;
230
231 if (count < nvmem->word_size)
232 return -EINVAL;
233
234 if (pos + count > nvmem->size)
235 count = nvmem->size - pos;
236
237 count = round_down(count, nvmem->word_size);
238
239 if (!nvmem->reg_read)
240 return -EPERM;
241
242 rc = nvmem_reg_read(nvmem, pos, buf, count);
243
244 if (rc)
245 return rc;
246
247 return count;
248}
249
250static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
251 struct bin_attribute *attr, char *buf,
252 loff_t pos, size_t count)
253{
254 struct device *dev;
255 struct nvmem_device *nvmem;
256 int rc;
257
258 if (attr->private)
259 dev = attr->private;
260 else
261 dev = kobj_to_dev(kobj);
262 nvmem = to_nvmem_device(dev);
263
264 /* Stop the user from writing */
265 if (pos >= nvmem->size)
266 return -EFBIG;
267
268 if (!IS_ALIGNED(pos, nvmem->stride))
269 return -EINVAL;
270
271 if (count < nvmem->word_size)
272 return -EINVAL;
273
274 if (pos + count > nvmem->size)
275 count = nvmem->size - pos;
276
277 count = round_down(count, nvmem->word_size);
278
279 if (!nvmem->reg_write)
280 return -EPERM;
281
282 rc = nvmem_reg_write(nvmem, pos, buf, count);
283
284 if (rc)
285 return rc;
286
287 return count;
288}
289
290static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
291{
292 umode_t mode = 0400;
293
294 if (!nvmem->root_only)
295 mode |= 0044;
296
297 if (!nvmem->read_only)
298 mode |= 0200;
299
300 if (!nvmem->reg_write)
301 mode &= ~0200;
302
303 if (!nvmem->reg_read)
304 mode &= ~0444;
305
306 return mode;
307}
308
309static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
310 struct bin_attribute *attr, int i)
311{
312 struct device *dev = kobj_to_dev(kobj);
313 struct nvmem_device *nvmem = to_nvmem_device(dev);
314
315 attr->size = nvmem->size;
316
317 return nvmem_bin_attr_get_umode(nvmem);
318}
319
320/* default read/write permissions */
321static struct bin_attribute bin_attr_rw_nvmem = {
322 .attr = {
323 .name = "nvmem",
324 .mode = 0644,
325 },
326 .read = bin_attr_nvmem_read,
327 .write = bin_attr_nvmem_write,
328};
329
330static struct bin_attribute *nvmem_bin_attributes[] = {
331 &bin_attr_rw_nvmem,
332 NULL,
333};
334
335static const struct attribute_group nvmem_bin_group = {
336 .bin_attrs = nvmem_bin_attributes,
337 .attrs = nvmem_attrs,
338 .is_bin_visible = nvmem_bin_attr_is_visible,
339};
340
341static const struct attribute_group *nvmem_dev_groups[] = {
342 &nvmem_bin_group,
343 NULL,
344};
345
346static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
347 .attr = {
348 .name = "eeprom",
349 },
350 .read = bin_attr_nvmem_read,
351 .write = bin_attr_nvmem_write,
352};
353
354/*
355 * nvmem_setup_compat() - Create an additional binary entry in
356 * drivers sys directory, to be backwards compatible with the older
357 * drivers/misc/eeprom drivers.
358 */
359static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
360 const struct nvmem_config *config)
361{
362 int rval;
363
364 if (!config->compat)
365 return 0;
366
367 if (!config->base_dev)
368 return -EINVAL;
369
370 if (config->type == NVMEM_TYPE_FRAM)
371 bin_attr_nvmem_eeprom_compat.attr.name = "fram";
372
373 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
374 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
375 nvmem->eeprom.size = nvmem->size;
376#ifdef CONFIG_DEBUG_LOCK_ALLOC
377 nvmem->eeprom.attr.key = &eeprom_lock_key;
378#endif
379 nvmem->eeprom.private = &nvmem->dev;
380 nvmem->base_dev = config->base_dev;
381
382 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
383 if (rval) {
384 dev_err(&nvmem->dev,
385 "Failed to create eeprom binary file %d\n", rval);
386 return rval;
387 }
388
389 nvmem->flags |= FLAG_COMPAT;
390
391 return 0;
392}
393
394static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
395 const struct nvmem_config *config)
396{
397 if (config->compat)
398 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
399}
400
401#else /* CONFIG_NVMEM_SYSFS */
402
403static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
404 const struct nvmem_config *config)
405{
406 return -ENOSYS;
407}
408static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
409 const struct nvmem_config *config)
410{
411}
412
413#endif /* CONFIG_NVMEM_SYSFS */
414
415static void nvmem_release(struct device *dev)
416{
417 struct nvmem_device *nvmem = to_nvmem_device(dev);
418
419 ida_free(&nvmem_ida, nvmem->id);
420 gpiod_put(nvmem->wp_gpio);
421 kfree(nvmem);
422}
423
424static const struct device_type nvmem_provider_type = {
425 .release = nvmem_release,
426};
427
428static struct bus_type nvmem_bus_type = {
429 .name = "nvmem",
430};
431
432static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
433{
434 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
435 mutex_lock(&nvmem_mutex);
436 list_del(&cell->node);
437 mutex_unlock(&nvmem_mutex);
438 of_node_put(cell->np);
439 kfree_const(cell->name);
440 kfree(cell);
441}
442
443static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
444{
445 struct nvmem_cell_entry *cell, *p;
446
447 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
448 nvmem_cell_entry_drop(cell);
449}
450
451static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
452{
453 mutex_lock(&nvmem_mutex);
454 list_add_tail(&cell->node, &cell->nvmem->cells);
455 mutex_unlock(&nvmem_mutex);
456 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
457}
458
459static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
460 const struct nvmem_cell_info *info,
461 struct nvmem_cell_entry *cell)
462{
463 cell->nvmem = nvmem;
464 cell->offset = info->offset;
465 cell->bytes = info->bytes;
466 cell->name = info->name;
467
468 cell->bit_offset = info->bit_offset;
469 cell->nbits = info->nbits;
470 cell->np = info->np;
471
472 if (cell->nbits)
473 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
474 BITS_PER_BYTE);
475
476 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
477 dev_err(&nvmem->dev,
478 "cell %s unaligned to nvmem stride %d\n",
479 cell->name ?: "<unknown>", nvmem->stride);
480 return -EINVAL;
481 }
482
483 return 0;
484}
485
486static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
487 const struct nvmem_cell_info *info,
488 struct nvmem_cell_entry *cell)
489{
490 int err;
491
492 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
493 if (err)
494 return err;
495
496 cell->name = kstrdup_const(info->name, GFP_KERNEL);
497 if (!cell->name)
498 return -ENOMEM;
499
500 return 0;
501}
502
503/**
504 * nvmem_add_cells() - Add cell information to an nvmem device
505 *
506 * @nvmem: nvmem device to add cells to.
507 * @info: nvmem cell info to add to the device
508 * @ncells: number of cells in info
509 *
510 * Return: 0 or negative error code on failure.
511 */
512static int nvmem_add_cells(struct nvmem_device *nvmem,
513 const struct nvmem_cell_info *info,
514 int ncells)
515{
516 struct nvmem_cell_entry **cells;
517 int i, rval;
518
519 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
520 if (!cells)
521 return -ENOMEM;
522
523 for (i = 0; i < ncells; i++) {
524 cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
525 if (!cells[i]) {
526 rval = -ENOMEM;
527 goto err;
528 }
529
530 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
531 if (rval) {
532 kfree(cells[i]);
533 goto err;
534 }
535
536 nvmem_cell_entry_add(cells[i]);
537 }
538
539 /* remove tmp array */
540 kfree(cells);
541
542 return 0;
543err:
544 while (i--)
545 nvmem_cell_entry_drop(cells[i]);
546
547 kfree(cells);
548
549 return rval;
550}
551
552/**
553 * nvmem_register_notifier() - Register a notifier block for nvmem events.
554 *
555 * @nb: notifier block to be called on nvmem events.
556 *
557 * Return: 0 on success, negative error number on failure.
558 */
559int nvmem_register_notifier(struct notifier_block *nb)
560{
561 return blocking_notifier_chain_register(&nvmem_notifier, nb);
562}
563EXPORT_SYMBOL_GPL(nvmem_register_notifier);
564
565/**
566 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
567 *
568 * @nb: notifier block to be unregistered.
569 *
570 * Return: 0 on success, negative error number on failure.
571 */
572int nvmem_unregister_notifier(struct notifier_block *nb)
573{
574 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
575}
576EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
577
578static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
579{
580 const struct nvmem_cell_info *info;
581 struct nvmem_cell_table *table;
582 struct nvmem_cell_entry *cell;
583 int rval = 0, i;
584
585 mutex_lock(&nvmem_cell_mutex);
586 list_for_each_entry(table, &nvmem_cell_tables, node) {
587 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
588 for (i = 0; i < table->ncells; i++) {
589 info = &table->cells[i];
590
591 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
592 if (!cell) {
593 rval = -ENOMEM;
594 goto out;
595 }
596
597 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
598 if (rval) {
599 kfree(cell);
600 goto out;
601 }
602
603 nvmem_cell_entry_add(cell);
604 }
605 }
606 }
607
608out:
609 mutex_unlock(&nvmem_cell_mutex);
610 return rval;
611}
612
613static struct nvmem_cell_entry *
614nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
615{
616 struct nvmem_cell_entry *iter, *cell = NULL;
617
618 mutex_lock(&nvmem_mutex);
619 list_for_each_entry(iter, &nvmem->cells, node) {
620 if (strcmp(cell_id, iter->name) == 0) {
621 cell = iter;
622 break;
623 }
624 }
625 mutex_unlock(&nvmem_mutex);
626
627 return cell;
628}
629
630static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
631{
632 unsigned int cur = 0;
633 const struct nvmem_keepout *keepout = nvmem->keepout;
634 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
635
636 while (keepout < keepoutend) {
637 /* Ensure keepouts are sorted and don't overlap. */
638 if (keepout->start < cur) {
639 dev_err(&nvmem->dev,
640 "Keepout regions aren't sorted or overlap.\n");
641
642 return -ERANGE;
643 }
644
645 if (keepout->end < keepout->start) {
646 dev_err(&nvmem->dev,
647 "Invalid keepout region.\n");
648
649 return -EINVAL;
650 }
651
652 /*
653 * Validate keepouts (and holes between) don't violate
654 * word_size constraints.
655 */
656 if ((keepout->end - keepout->start < nvmem->word_size) ||
657 ((keepout->start != cur) &&
658 (keepout->start - cur < nvmem->word_size))) {
659
660 dev_err(&nvmem->dev,
661 "Keepout regions violate word_size constraints.\n");
662
663 return -ERANGE;
664 }
665
666 /* Validate keepouts don't violate stride (alignment). */
667 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
668 !IS_ALIGNED(keepout->end, nvmem->stride)) {
669
670 dev_err(&nvmem->dev,
671 "Keepout regions violate stride.\n");
672
673 return -EINVAL;
674 }
675
676 cur = keepout->end;
677 keepout++;
678 }
679
680 return 0;
681}
682
683static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
684{
685 struct device_node *parent, *child;
686 struct device *dev = &nvmem->dev;
687 struct nvmem_cell_entry *cell;
688 const __be32 *addr;
689 int len;
690
691 parent = dev->of_node;
692
693 for_each_child_of_node(parent, child) {
694 addr = of_get_property(child, "reg", &len);
695 if (!addr)
696 continue;
697 if (len < 2 * sizeof(u32)) {
698 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
699 of_node_put(child);
700 return -EINVAL;
701 }
702
703 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
704 if (!cell) {
705 of_node_put(child);
706 return -ENOMEM;
707 }
708
709 cell->nvmem = nvmem;
710 cell->offset = be32_to_cpup(addr++);
711 cell->bytes = be32_to_cpup(addr);
712 cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
713
714 addr = of_get_property(child, "bits", &len);
715 if (addr && len == (2 * sizeof(u32))) {
716 cell->bit_offset = be32_to_cpup(addr++);
717 cell->nbits = be32_to_cpup(addr);
718 }
719
720 if (cell->nbits)
721 cell->bytes = DIV_ROUND_UP(
722 cell->nbits + cell->bit_offset,
723 BITS_PER_BYTE);
724
725 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
726 dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
727 cell->name, nvmem->stride);
728 /* Cells already added will be freed later. */
729 kfree_const(cell->name);
730 kfree(cell);
731 of_node_put(child);
732 return -EINVAL;
733 }
734
735 cell->np = of_node_get(child);
736 nvmem_cell_entry_add(cell);
737 }
738
739 return 0;
740}
741
742/**
743 * nvmem_register() - Register a nvmem device for given nvmem_config.
744 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
745 *
746 * @config: nvmem device configuration with which nvmem device is created.
747 *
748 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
749 * on success.
750 */
751
752struct nvmem_device *nvmem_register(const struct nvmem_config *config)
753{
754 struct nvmem_device *nvmem;
755 int rval;
756
757 if (!config->dev)
758 return ERR_PTR(-EINVAL);
759
760 if (!config->reg_read && !config->reg_write)
761 return ERR_PTR(-EINVAL);
762
763 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
764 if (!nvmem)
765 return ERR_PTR(-ENOMEM);
766
767 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
768 if (rval < 0) {
769 kfree(nvmem);
770 return ERR_PTR(rval);
771 }
772
773 nvmem->id = rval;
774
775 nvmem->dev.type = &nvmem_provider_type;
776 nvmem->dev.bus = &nvmem_bus_type;
777 nvmem->dev.parent = config->dev;
778
779 device_initialize(&nvmem->dev);
780
781 if (!config->ignore_wp)
782 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
783 GPIOD_OUT_HIGH);
784 if (IS_ERR(nvmem->wp_gpio)) {
785 rval = PTR_ERR(nvmem->wp_gpio);
786 nvmem->wp_gpio = NULL;
787 goto err_put_device;
788 }
789
790 kref_init(&nvmem->refcnt);
791 INIT_LIST_HEAD(&nvmem->cells);
792
793 nvmem->owner = config->owner;
794 if (!nvmem->owner && config->dev->driver)
795 nvmem->owner = config->dev->driver->owner;
796 nvmem->stride = config->stride ?: 1;
797 nvmem->word_size = config->word_size ?: 1;
798 nvmem->size = config->size;
799 nvmem->root_only = config->root_only;
800 nvmem->priv = config->priv;
801 nvmem->type = config->type;
802 nvmem->reg_read = config->reg_read;
803 nvmem->reg_write = config->reg_write;
804 nvmem->cell_post_process = config->cell_post_process;
805 nvmem->keepout = config->keepout;
806 nvmem->nkeepout = config->nkeepout;
807 if (config->of_node)
808 nvmem->dev.of_node = config->of_node;
809 else if (!config->no_of_node)
810 nvmem->dev.of_node = config->dev->of_node;
811
812 switch (config->id) {
813 case NVMEM_DEVID_NONE:
814 rval = dev_set_name(&nvmem->dev, "%s", config->name);
815 break;
816 case NVMEM_DEVID_AUTO:
817 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
818 break;
819 default:
820 rval = dev_set_name(&nvmem->dev, "%s%d",
821 config->name ? : "nvmem",
822 config->name ? config->id : nvmem->id);
823 break;
824 }
825
826 if (rval)
827 goto err_put_device;
828
829 nvmem->read_only = device_property_present(config->dev, "read-only") ||
830 config->read_only || !nvmem->reg_write;
831
832#ifdef CONFIG_NVMEM_SYSFS
833 nvmem->dev.groups = nvmem_dev_groups;
834#endif
835
836 if (nvmem->nkeepout) {
837 rval = nvmem_validate_keepouts(nvmem);
838 if (rval)
839 goto err_put_device;
840 }
841
842 if (config->compat) {
843 rval = nvmem_sysfs_setup_compat(nvmem, config);
844 if (rval)
845 goto err_put_device;
846 }
847
848 if (config->cells) {
849 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
850 if (rval)
851 goto err_remove_cells;
852 }
853
854 rval = nvmem_add_cells_from_table(nvmem);
855 if (rval)
856 goto err_remove_cells;
857
858 rval = nvmem_add_cells_from_of(nvmem);
859 if (rval)
860 goto err_remove_cells;
861
862 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
863
864 rval = device_add(&nvmem->dev);
865 if (rval)
866 goto err_remove_cells;
867
868 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
869
870 return nvmem;
871
872err_remove_cells:
873 nvmem_device_remove_all_cells(nvmem);
874 if (config->compat)
875 nvmem_sysfs_remove_compat(nvmem, config);
876err_put_device:
877 put_device(&nvmem->dev);
878
879 return ERR_PTR(rval);
880}
881EXPORT_SYMBOL_GPL(nvmem_register);
882
883static void nvmem_device_release(struct kref *kref)
884{
885 struct nvmem_device *nvmem;
886
887 nvmem = container_of(kref, struct nvmem_device, refcnt);
888
889 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
890
891 if (nvmem->flags & FLAG_COMPAT)
892 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
893
894 nvmem_device_remove_all_cells(nvmem);
895 device_unregister(&nvmem->dev);
896}
897
898/**
899 * nvmem_unregister() - Unregister previously registered nvmem device
900 *
901 * @nvmem: Pointer to previously registered nvmem device.
902 */
903void nvmem_unregister(struct nvmem_device *nvmem)
904{
905 if (nvmem)
906 kref_put(&nvmem->refcnt, nvmem_device_release);
907}
908EXPORT_SYMBOL_GPL(nvmem_unregister);
909
910static void devm_nvmem_unregister(void *nvmem)
911{
912 nvmem_unregister(nvmem);
913}
914
915/**
916 * devm_nvmem_register() - Register a managed nvmem device for given
917 * nvmem_config.
918 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
919 *
920 * @dev: Device that uses the nvmem device.
921 * @config: nvmem device configuration with which nvmem device is created.
922 *
923 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
924 * on success.
925 */
926struct nvmem_device *devm_nvmem_register(struct device *dev,
927 const struct nvmem_config *config)
928{
929 struct nvmem_device *nvmem;
930 int ret;
931
932 nvmem = nvmem_register(config);
933 if (IS_ERR(nvmem))
934 return nvmem;
935
936 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
937 if (ret)
938 return ERR_PTR(ret);
939
940 return nvmem;
941}
942EXPORT_SYMBOL_GPL(devm_nvmem_register);
943
944static struct nvmem_device *__nvmem_device_get(void *data,
945 int (*match)(struct device *dev, const void *data))
946{
947 struct nvmem_device *nvmem = NULL;
948 struct device *dev;
949
950 mutex_lock(&nvmem_mutex);
951 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
952 if (dev)
953 nvmem = to_nvmem_device(dev);
954 mutex_unlock(&nvmem_mutex);
955 if (!nvmem)
956 return ERR_PTR(-EPROBE_DEFER);
957
958 if (!try_module_get(nvmem->owner)) {
959 dev_err(&nvmem->dev,
960 "could not increase module refcount for cell %s\n",
961 nvmem_dev_name(nvmem));
962
963 put_device(&nvmem->dev);
964 return ERR_PTR(-EINVAL);
965 }
966
967 kref_get(&nvmem->refcnt);
968
969 return nvmem;
970}
971
972static void __nvmem_device_put(struct nvmem_device *nvmem)
973{
974 put_device(&nvmem->dev);
975 module_put(nvmem->owner);
976 kref_put(&nvmem->refcnt, nvmem_device_release);
977}
978
979#if IS_ENABLED(CONFIG_OF)
980/**
981 * of_nvmem_device_get() - Get nvmem device from a given id
982 *
983 * @np: Device tree node that uses the nvmem device.
984 * @id: nvmem name from nvmem-names property.
985 *
986 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
987 * on success.
988 */
989struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
990{
991
992 struct device_node *nvmem_np;
993 struct nvmem_device *nvmem;
994 int index = 0;
995
996 if (id)
997 index = of_property_match_string(np, "nvmem-names", id);
998
999 nvmem_np = of_parse_phandle(np, "nvmem", index);
1000 if (!nvmem_np)
1001 return ERR_PTR(-ENOENT);
1002
1003 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1004 of_node_put(nvmem_np);
1005 return nvmem;
1006}
1007EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1008#endif
1009
1010/**
1011 * nvmem_device_get() - Get nvmem device from a given id
1012 *
1013 * @dev: Device that uses the nvmem device.
1014 * @dev_name: name of the requested nvmem device.
1015 *
1016 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1017 * on success.
1018 */
1019struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1020{
1021 if (dev->of_node) { /* try dt first */
1022 struct nvmem_device *nvmem;
1023
1024 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1025
1026 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1027 return nvmem;
1028
1029 }
1030
1031 return __nvmem_device_get((void *)dev_name, device_match_name);
1032}
1033EXPORT_SYMBOL_GPL(nvmem_device_get);
1034
1035/**
1036 * nvmem_device_find() - Find nvmem device with matching function
1037 *
1038 * @data: Data to pass to match function
1039 * @match: Callback function to check device
1040 *
1041 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1042 * on success.
1043 */
1044struct nvmem_device *nvmem_device_find(void *data,
1045 int (*match)(struct device *dev, const void *data))
1046{
1047 return __nvmem_device_get(data, match);
1048}
1049EXPORT_SYMBOL_GPL(nvmem_device_find);
1050
1051static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1052{
1053 struct nvmem_device **nvmem = res;
1054
1055 if (WARN_ON(!nvmem || !*nvmem))
1056 return 0;
1057
1058 return *nvmem == data;
1059}
1060
1061static void devm_nvmem_device_release(struct device *dev, void *res)
1062{
1063 nvmem_device_put(*(struct nvmem_device **)res);
1064}
1065
1066/**
1067 * devm_nvmem_device_put() - put alredy got nvmem device
1068 *
1069 * @dev: Device that uses the nvmem device.
1070 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1071 * that needs to be released.
1072 */
1073void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1074{
1075 int ret;
1076
1077 ret = devres_release(dev, devm_nvmem_device_release,
1078 devm_nvmem_device_match, nvmem);
1079
1080 WARN_ON(ret);
1081}
1082EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1083
1084/**
1085 * nvmem_device_put() - put alredy got nvmem device
1086 *
1087 * @nvmem: pointer to nvmem device that needs to be released.
1088 */
1089void nvmem_device_put(struct nvmem_device *nvmem)
1090{
1091 __nvmem_device_put(nvmem);
1092}
1093EXPORT_SYMBOL_GPL(nvmem_device_put);
1094
1095/**
1096 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1097 *
1098 * @dev: Device that requests the nvmem device.
1099 * @id: name id for the requested nvmem device.
1100 *
1101 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1102 * on success. The nvmem_cell will be freed by the automatically once the
1103 * device is freed.
1104 */
1105struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1106{
1107 struct nvmem_device **ptr, *nvmem;
1108
1109 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1110 if (!ptr)
1111 return ERR_PTR(-ENOMEM);
1112
1113 nvmem = nvmem_device_get(dev, id);
1114 if (!IS_ERR(nvmem)) {
1115 *ptr = nvmem;
1116 devres_add(dev, ptr);
1117 } else {
1118 devres_free(ptr);
1119 }
1120
1121 return nvmem;
1122}
1123EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1124
1125static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, const char *id)
1126{
1127 struct nvmem_cell *cell;
1128 const char *name = NULL;
1129
1130 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1131 if (!cell)
1132 return ERR_PTR(-ENOMEM);
1133
1134 if (id) {
1135 name = kstrdup_const(id, GFP_KERNEL);
1136 if (!name) {
1137 kfree(cell);
1138 return ERR_PTR(-ENOMEM);
1139 }
1140 }
1141
1142 cell->id = name;
1143 cell->entry = entry;
1144
1145 return cell;
1146}
1147
1148static struct nvmem_cell *
1149nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1150{
1151 struct nvmem_cell_entry *cell_entry;
1152 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1153 struct nvmem_cell_lookup *lookup;
1154 struct nvmem_device *nvmem;
1155 const char *dev_id;
1156
1157 if (!dev)
1158 return ERR_PTR(-EINVAL);
1159
1160 dev_id = dev_name(dev);
1161
1162 mutex_lock(&nvmem_lookup_mutex);
1163
1164 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1165 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1166 (strcmp(lookup->con_id, con_id) == 0)) {
1167 /* This is the right entry. */
1168 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1169 device_match_name);
1170 if (IS_ERR(nvmem)) {
1171 /* Provider may not be registered yet. */
1172 cell = ERR_CAST(nvmem);
1173 break;
1174 }
1175
1176 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1177 lookup->cell_name);
1178 if (!cell_entry) {
1179 __nvmem_device_put(nvmem);
1180 cell = ERR_PTR(-ENOENT);
1181 } else {
1182 cell = nvmem_create_cell(cell_entry, con_id);
1183 if (IS_ERR(cell))
1184 __nvmem_device_put(nvmem);
1185 }
1186 break;
1187 }
1188 }
1189
1190 mutex_unlock(&nvmem_lookup_mutex);
1191 return cell;
1192}
1193
1194#if IS_ENABLED(CONFIG_OF)
1195static struct nvmem_cell_entry *
1196nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1197{
1198 struct nvmem_cell_entry *iter, *cell = NULL;
1199
1200 mutex_lock(&nvmem_mutex);
1201 list_for_each_entry(iter, &nvmem->cells, node) {
1202 if (np == iter->np) {
1203 cell = iter;
1204 break;
1205 }
1206 }
1207 mutex_unlock(&nvmem_mutex);
1208
1209 return cell;
1210}
1211
1212/**
1213 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1214 *
1215 * @np: Device tree node that uses the nvmem cell.
1216 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1217 * for the cell at index 0 (the lone cell with no accompanying
1218 * nvmem-cell-names property).
1219 *
1220 * Return: Will be an ERR_PTR() on error or a valid pointer
1221 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1222 * nvmem_cell_put().
1223 */
1224struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1225{
1226 struct device_node *cell_np, *nvmem_np;
1227 struct nvmem_device *nvmem;
1228 struct nvmem_cell_entry *cell_entry;
1229 struct nvmem_cell *cell;
1230 int index = 0;
1231
1232 /* if cell name exists, find index to the name */
1233 if (id)
1234 index = of_property_match_string(np, "nvmem-cell-names", id);
1235
1236 cell_np = of_parse_phandle(np, "nvmem-cells", index);
1237 if (!cell_np)
1238 return ERR_PTR(-ENOENT);
1239
1240 nvmem_np = of_get_parent(cell_np);
1241 if (!nvmem_np) {
1242 of_node_put(cell_np);
1243 return ERR_PTR(-EINVAL);
1244 }
1245
1246 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1247 of_node_put(nvmem_np);
1248 if (IS_ERR(nvmem)) {
1249 of_node_put(cell_np);
1250 return ERR_CAST(nvmem);
1251 }
1252
1253 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1254 of_node_put(cell_np);
1255 if (!cell_entry) {
1256 __nvmem_device_put(nvmem);
1257 return ERR_PTR(-ENOENT);
1258 }
1259
1260 cell = nvmem_create_cell(cell_entry, id);
1261 if (IS_ERR(cell))
1262 __nvmem_device_put(nvmem);
1263
1264 return cell;
1265}
1266EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1267#endif
1268
1269/**
1270 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1271 *
1272 * @dev: Device that requests the nvmem cell.
1273 * @id: nvmem cell name to get (this corresponds with the name from the
1274 * nvmem-cell-names property for DT systems and with the con_id from
1275 * the lookup entry for non-DT systems).
1276 *
1277 * Return: Will be an ERR_PTR() on error or a valid pointer
1278 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1279 * nvmem_cell_put().
1280 */
1281struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1282{
1283 struct nvmem_cell *cell;
1284
1285 if (dev->of_node) { /* try dt first */
1286 cell = of_nvmem_cell_get(dev->of_node, id);
1287 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1288 return cell;
1289 }
1290
1291 /* NULL cell id only allowed for device tree; invalid otherwise */
1292 if (!id)
1293 return ERR_PTR(-EINVAL);
1294
1295 return nvmem_cell_get_from_lookup(dev, id);
1296}
1297EXPORT_SYMBOL_GPL(nvmem_cell_get);
1298
1299static void devm_nvmem_cell_release(struct device *dev, void *res)
1300{
1301 nvmem_cell_put(*(struct nvmem_cell **)res);
1302}
1303
1304/**
1305 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1306 *
1307 * @dev: Device that requests the nvmem cell.
1308 * @id: nvmem cell name id to get.
1309 *
1310 * Return: Will be an ERR_PTR() on error or a valid pointer
1311 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1312 * automatically once the device is freed.
1313 */
1314struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1315{
1316 struct nvmem_cell **ptr, *cell;
1317
1318 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1319 if (!ptr)
1320 return ERR_PTR(-ENOMEM);
1321
1322 cell = nvmem_cell_get(dev, id);
1323 if (!IS_ERR(cell)) {
1324 *ptr = cell;
1325 devres_add(dev, ptr);
1326 } else {
1327 devres_free(ptr);
1328 }
1329
1330 return cell;
1331}
1332EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1333
1334static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1335{
1336 struct nvmem_cell **c = res;
1337
1338 if (WARN_ON(!c || !*c))
1339 return 0;
1340
1341 return *c == data;
1342}
1343
1344/**
1345 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1346 * from devm_nvmem_cell_get.
1347 *
1348 * @dev: Device that requests the nvmem cell.
1349 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1350 */
1351void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1352{
1353 int ret;
1354
1355 ret = devres_release(dev, devm_nvmem_cell_release,
1356 devm_nvmem_cell_match, cell);
1357
1358 WARN_ON(ret);
1359}
1360EXPORT_SYMBOL(devm_nvmem_cell_put);
1361
1362/**
1363 * nvmem_cell_put() - Release previously allocated nvmem cell.
1364 *
1365 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1366 */
1367void nvmem_cell_put(struct nvmem_cell *cell)
1368{
1369 struct nvmem_device *nvmem = cell->entry->nvmem;
1370
1371 if (cell->id)
1372 kfree_const(cell->id);
1373
1374 kfree(cell);
1375 __nvmem_device_put(nvmem);
1376}
1377EXPORT_SYMBOL_GPL(nvmem_cell_put);
1378
1379static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1380{
1381 u8 *p, *b;
1382 int i, extra, bit_offset = cell->bit_offset;
1383
1384 p = b = buf;
1385 if (bit_offset) {
1386 /* First shift */
1387 *b++ >>= bit_offset;
1388
1389 /* setup rest of the bytes if any */
1390 for (i = 1; i < cell->bytes; i++) {
1391 /* Get bits from next byte and shift them towards msb */
1392 *p |= *b << (BITS_PER_BYTE - bit_offset);
1393
1394 p = b;
1395 *b++ >>= bit_offset;
1396 }
1397 } else {
1398 /* point to the msb */
1399 p += cell->bytes - 1;
1400 }
1401
1402 /* result fits in less bytes */
1403 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1404 while (--extra >= 0)
1405 *p-- = 0;
1406
1407 /* clear msb bits if any leftover in the last byte */
1408 if (cell->nbits % BITS_PER_BYTE)
1409 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1410}
1411
1412static int __nvmem_cell_read(struct nvmem_device *nvmem,
1413 struct nvmem_cell_entry *cell,
1414 void *buf, size_t *len, const char *id)
1415{
1416 int rc;
1417
1418 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
1419
1420 if (rc)
1421 return rc;
1422
1423 /* shift bits in-place */
1424 if (cell->bit_offset || cell->nbits)
1425 nvmem_shift_read_buffer_in_place(cell, buf);
1426
1427 if (nvmem->cell_post_process) {
1428 rc = nvmem->cell_post_process(nvmem->priv, id,
1429 cell->offset, buf, cell->bytes);
1430 if (rc)
1431 return rc;
1432 }
1433
1434 if (len)
1435 *len = cell->bytes;
1436
1437 return 0;
1438}
1439
1440/**
1441 * nvmem_cell_read() - Read a given nvmem cell
1442 *
1443 * @cell: nvmem cell to be read.
1444 * @len: pointer to length of cell which will be populated on successful read;
1445 * can be NULL.
1446 *
1447 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1448 * buffer should be freed by the consumer with a kfree().
1449 */
1450void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1451{
1452 struct nvmem_device *nvmem = cell->entry->nvmem;
1453 u8 *buf;
1454 int rc;
1455
1456 if (!nvmem)
1457 return ERR_PTR(-EINVAL);
1458
1459 buf = kzalloc(cell->entry->bytes, GFP_KERNEL);
1460 if (!buf)
1461 return ERR_PTR(-ENOMEM);
1462
1463 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id);
1464 if (rc) {
1465 kfree(buf);
1466 return ERR_PTR(rc);
1467 }
1468
1469 return buf;
1470}
1471EXPORT_SYMBOL_GPL(nvmem_cell_read);
1472
1473static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1474 u8 *_buf, int len)
1475{
1476 struct nvmem_device *nvmem = cell->nvmem;
1477 int i, rc, nbits, bit_offset = cell->bit_offset;
1478 u8 v, *p, *buf, *b, pbyte, pbits;
1479
1480 nbits = cell->nbits;
1481 buf = kzalloc(cell->bytes, GFP_KERNEL);
1482 if (!buf)
1483 return ERR_PTR(-ENOMEM);
1484
1485 memcpy(buf, _buf, len);
1486 p = b = buf;
1487
1488 if (bit_offset) {
1489 pbyte = *b;
1490 *b <<= bit_offset;
1491
1492 /* setup the first byte with lsb bits from nvmem */
1493 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1494 if (rc)
1495 goto err;
1496 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1497
1498 /* setup rest of the byte if any */
1499 for (i = 1; i < cell->bytes; i++) {
1500 /* Get last byte bits and shift them towards lsb */
1501 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1502 pbyte = *b;
1503 p = b;
1504 *b <<= bit_offset;
1505 *b++ |= pbits;
1506 }
1507 }
1508
1509 /* if it's not end on byte boundary */
1510 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1511 /* setup the last byte with msb bits from nvmem */
1512 rc = nvmem_reg_read(nvmem,
1513 cell->offset + cell->bytes - 1, &v, 1);
1514 if (rc)
1515 goto err;
1516 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1517
1518 }
1519
1520 return buf;
1521err:
1522 kfree(buf);
1523 return ERR_PTR(rc);
1524}
1525
1526static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1527{
1528 struct nvmem_device *nvmem = cell->nvmem;
1529 int rc;
1530
1531 if (!nvmem || nvmem->read_only ||
1532 (cell->bit_offset == 0 && len != cell->bytes))
1533 return -EINVAL;
1534
1535 if (cell->bit_offset || cell->nbits) {
1536 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1537 if (IS_ERR(buf))
1538 return PTR_ERR(buf);
1539 }
1540
1541 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1542
1543 /* free the tmp buffer */
1544 if (cell->bit_offset || cell->nbits)
1545 kfree(buf);
1546
1547 if (rc)
1548 return rc;
1549
1550 return len;
1551}
1552
1553/**
1554 * nvmem_cell_write() - Write to a given nvmem cell
1555 *
1556 * @cell: nvmem cell to be written.
1557 * @buf: Buffer to be written.
1558 * @len: length of buffer to be written to nvmem cell.
1559 *
1560 * Return: length of bytes written or negative on failure.
1561 */
1562int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1563{
1564 return __nvmem_cell_entry_write(cell->entry, buf, len);
1565}
1566
1567EXPORT_SYMBOL_GPL(nvmem_cell_write);
1568
1569static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1570 void *val, size_t count)
1571{
1572 struct nvmem_cell *cell;
1573 void *buf;
1574 size_t len;
1575
1576 cell = nvmem_cell_get(dev, cell_id);
1577 if (IS_ERR(cell))
1578 return PTR_ERR(cell);
1579
1580 buf = nvmem_cell_read(cell, &len);
1581 if (IS_ERR(buf)) {
1582 nvmem_cell_put(cell);
1583 return PTR_ERR(buf);
1584 }
1585 if (len != count) {
1586 kfree(buf);
1587 nvmem_cell_put(cell);
1588 return -EINVAL;
1589 }
1590 memcpy(val, buf, count);
1591 kfree(buf);
1592 nvmem_cell_put(cell);
1593
1594 return 0;
1595}
1596
1597/**
1598 * nvmem_cell_read_u8() - Read a cell value as a u8
1599 *
1600 * @dev: Device that requests the nvmem cell.
1601 * @cell_id: Name of nvmem cell to read.
1602 * @val: pointer to output value.
1603 *
1604 * Return: 0 on success or negative errno.
1605 */
1606int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1607{
1608 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1609}
1610EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1611
1612/**
1613 * nvmem_cell_read_u16() - Read a cell value as a u16
1614 *
1615 * @dev: Device that requests the nvmem cell.
1616 * @cell_id: Name of nvmem cell to read.
1617 * @val: pointer to output value.
1618 *
1619 * Return: 0 on success or negative errno.
1620 */
1621int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1622{
1623 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1624}
1625EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1626
1627/**
1628 * nvmem_cell_read_u32() - Read a cell value as a u32
1629 *
1630 * @dev: Device that requests the nvmem cell.
1631 * @cell_id: Name of nvmem cell to read.
1632 * @val: pointer to output value.
1633 *
1634 * Return: 0 on success or negative errno.
1635 */
1636int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1637{
1638 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1639}
1640EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1641
1642/**
1643 * nvmem_cell_read_u64() - Read a cell value as a u64
1644 *
1645 * @dev: Device that requests the nvmem cell.
1646 * @cell_id: Name of nvmem cell to read.
1647 * @val: pointer to output value.
1648 *
1649 * Return: 0 on success or negative errno.
1650 */
1651int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1652{
1653 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1654}
1655EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1656
1657static const void *nvmem_cell_read_variable_common(struct device *dev,
1658 const char *cell_id,
1659 size_t max_len, size_t *len)
1660{
1661 struct nvmem_cell *cell;
1662 int nbits;
1663 void *buf;
1664
1665 cell = nvmem_cell_get(dev, cell_id);
1666 if (IS_ERR(cell))
1667 return cell;
1668
1669 nbits = cell->entry->nbits;
1670 buf = nvmem_cell_read(cell, len);
1671 nvmem_cell_put(cell);
1672 if (IS_ERR(buf))
1673 return buf;
1674
1675 /*
1676 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1677 * the length of the real data. Throw away the extra junk.
1678 */
1679 if (nbits)
1680 *len = DIV_ROUND_UP(nbits, 8);
1681
1682 if (*len > max_len) {
1683 kfree(buf);
1684 return ERR_PTR(-ERANGE);
1685 }
1686
1687 return buf;
1688}
1689
1690/**
1691 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1692 *
1693 * @dev: Device that requests the nvmem cell.
1694 * @cell_id: Name of nvmem cell to read.
1695 * @val: pointer to output value.
1696 *
1697 * Return: 0 on success or negative errno.
1698 */
1699int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1700 u32 *val)
1701{
1702 size_t len;
1703 const u8 *buf;
1704 int i;
1705
1706 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1707 if (IS_ERR(buf))
1708 return PTR_ERR(buf);
1709
1710 /* Copy w/ implicit endian conversion */
1711 *val = 0;
1712 for (i = 0; i < len; i++)
1713 *val |= buf[i] << (8 * i);
1714
1715 kfree(buf);
1716
1717 return 0;
1718}
1719EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1720
1721/**
1722 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1723 *
1724 * @dev: Device that requests the nvmem cell.
1725 * @cell_id: Name of nvmem cell to read.
1726 * @val: pointer to output value.
1727 *
1728 * Return: 0 on success or negative errno.
1729 */
1730int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1731 u64 *val)
1732{
1733 size_t len;
1734 const u8 *buf;
1735 int i;
1736
1737 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1738 if (IS_ERR(buf))
1739 return PTR_ERR(buf);
1740
1741 /* Copy w/ implicit endian conversion */
1742 *val = 0;
1743 for (i = 0; i < len; i++)
1744 *val |= (uint64_t)buf[i] << (8 * i);
1745
1746 kfree(buf);
1747
1748 return 0;
1749}
1750EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1751
1752/**
1753 * nvmem_device_cell_read() - Read a given nvmem device and cell
1754 *
1755 * @nvmem: nvmem device to read from.
1756 * @info: nvmem cell info to be read.
1757 * @buf: buffer pointer which will be populated on successful read.
1758 *
1759 * Return: length of successful bytes read on success and negative
1760 * error code on error.
1761 */
1762ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1763 struct nvmem_cell_info *info, void *buf)
1764{
1765 struct nvmem_cell_entry cell;
1766 int rc;
1767 ssize_t len;
1768
1769 if (!nvmem)
1770 return -EINVAL;
1771
1772 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1773 if (rc)
1774 return rc;
1775
1776 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL);
1777 if (rc)
1778 return rc;
1779
1780 return len;
1781}
1782EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
1783
1784/**
1785 * nvmem_device_cell_write() - Write cell to a given nvmem device
1786 *
1787 * @nvmem: nvmem device to be written to.
1788 * @info: nvmem cell info to be written.
1789 * @buf: buffer to be written to cell.
1790 *
1791 * Return: length of bytes written or negative error code on failure.
1792 */
1793int nvmem_device_cell_write(struct nvmem_device *nvmem,
1794 struct nvmem_cell_info *info, void *buf)
1795{
1796 struct nvmem_cell_entry cell;
1797 int rc;
1798
1799 if (!nvmem)
1800 return -EINVAL;
1801
1802 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1803 if (rc)
1804 return rc;
1805
1806 return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
1807}
1808EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
1809
1810/**
1811 * nvmem_device_read() - Read from a given nvmem device
1812 *
1813 * @nvmem: nvmem device to read from.
1814 * @offset: offset in nvmem device.
1815 * @bytes: number of bytes to read.
1816 * @buf: buffer pointer which will be populated on successful read.
1817 *
1818 * Return: length of successful bytes read on success and negative
1819 * error code on error.
1820 */
1821int nvmem_device_read(struct nvmem_device *nvmem,
1822 unsigned int offset,
1823 size_t bytes, void *buf)
1824{
1825 int rc;
1826
1827 if (!nvmem)
1828 return -EINVAL;
1829
1830 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
1831
1832 if (rc)
1833 return rc;
1834
1835 return bytes;
1836}
1837EXPORT_SYMBOL_GPL(nvmem_device_read);
1838
1839/**
1840 * nvmem_device_write() - Write cell to a given nvmem device
1841 *
1842 * @nvmem: nvmem device to be written to.
1843 * @offset: offset in nvmem device.
1844 * @bytes: number of bytes to write.
1845 * @buf: buffer to be written.
1846 *
1847 * Return: length of bytes written or negative error code on failure.
1848 */
1849int nvmem_device_write(struct nvmem_device *nvmem,
1850 unsigned int offset,
1851 size_t bytes, void *buf)
1852{
1853 int rc;
1854
1855 if (!nvmem)
1856 return -EINVAL;
1857
1858 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
1859
1860 if (rc)
1861 return rc;
1862
1863
1864 return bytes;
1865}
1866EXPORT_SYMBOL_GPL(nvmem_device_write);
1867
1868/**
1869 * nvmem_add_cell_table() - register a table of cell info entries
1870 *
1871 * @table: table of cell info entries
1872 */
1873void nvmem_add_cell_table(struct nvmem_cell_table *table)
1874{
1875 mutex_lock(&nvmem_cell_mutex);
1876 list_add_tail(&table->node, &nvmem_cell_tables);
1877 mutex_unlock(&nvmem_cell_mutex);
1878}
1879EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
1880
1881/**
1882 * nvmem_del_cell_table() - remove a previously registered cell info table
1883 *
1884 * @table: table of cell info entries
1885 */
1886void nvmem_del_cell_table(struct nvmem_cell_table *table)
1887{
1888 mutex_lock(&nvmem_cell_mutex);
1889 list_del(&table->node);
1890 mutex_unlock(&nvmem_cell_mutex);
1891}
1892EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
1893
1894/**
1895 * nvmem_add_cell_lookups() - register a list of cell lookup entries
1896 *
1897 * @entries: array of cell lookup entries
1898 * @nentries: number of cell lookup entries in the array
1899 */
1900void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1901{
1902 int i;
1903
1904 mutex_lock(&nvmem_lookup_mutex);
1905 for (i = 0; i < nentries; i++)
1906 list_add_tail(&entries[i].node, &nvmem_lookup_list);
1907 mutex_unlock(&nvmem_lookup_mutex);
1908}
1909EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
1910
1911/**
1912 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
1913 * entries
1914 *
1915 * @entries: array of cell lookup entries
1916 * @nentries: number of cell lookup entries in the array
1917 */
1918void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
1919{
1920 int i;
1921
1922 mutex_lock(&nvmem_lookup_mutex);
1923 for (i = 0; i < nentries; i++)
1924 list_del(&entries[i].node);
1925 mutex_unlock(&nvmem_lookup_mutex);
1926}
1927EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
1928
1929/**
1930 * nvmem_dev_name() - Get the name of a given nvmem device.
1931 *
1932 * @nvmem: nvmem device.
1933 *
1934 * Return: name of the nvmem device.
1935 */
1936const char *nvmem_dev_name(struct nvmem_device *nvmem)
1937{
1938 return dev_name(&nvmem->dev);
1939}
1940EXPORT_SYMBOL_GPL(nvmem_dev_name);
1941
1942static int __init nvmem_init(void)
1943{
1944 return bus_register(&nvmem_bus_type);
1945}
1946
1947static void __exit nvmem_exit(void)
1948{
1949 bus_unregister(&nvmem_bus_type);
1950}
1951
1952subsys_initcall(nvmem_init);
1953module_exit(nvmem_exit);
1954
1955MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
1956MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
1957MODULE_DESCRIPTION("nvmem Driver Core");
1958MODULE_LICENSE("GPL v2");
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
22#include "internals.h"
23
24#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
25
26#define FLAG_COMPAT BIT(0)
27struct nvmem_cell_entry {
28 const char *name;
29 int offset;
30 size_t raw_len;
31 int bytes;
32 int bit_offset;
33 int nbits;
34 nvmem_cell_post_process_t read_post_process;
35 void *priv;
36 struct device_node *np;
37 struct nvmem_device *nvmem;
38 struct list_head node;
39};
40
41struct nvmem_cell {
42 struct nvmem_cell_entry *entry;
43 const char *id;
44 int index;
45};
46
47static DEFINE_MUTEX(nvmem_mutex);
48static DEFINE_IDA(nvmem_ida);
49
50static DEFINE_MUTEX(nvmem_cell_mutex);
51static LIST_HEAD(nvmem_cell_tables);
52
53static DEFINE_MUTEX(nvmem_lookup_mutex);
54static LIST_HEAD(nvmem_lookup_list);
55
56static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
57
58static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
59 void *val, size_t bytes)
60{
61 if (nvmem->reg_read)
62 return nvmem->reg_read(nvmem->priv, offset, val, bytes);
63
64 return -EINVAL;
65}
66
67static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
68 void *val, size_t bytes)
69{
70 int ret;
71
72 if (nvmem->reg_write) {
73 gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
74 ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
75 gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
76 return ret;
77 }
78
79 return -EINVAL;
80}
81
82static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
83 unsigned int offset, void *val,
84 size_t bytes, int write)
85{
86
87 unsigned int end = offset + bytes;
88 unsigned int kend, ksize;
89 const struct nvmem_keepout *keepout = nvmem->keepout;
90 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
91 int rc;
92
93 /*
94 * Skip all keepouts before the range being accessed.
95 * Keepouts are sorted.
96 */
97 while ((keepout < keepoutend) && (keepout->end <= offset))
98 keepout++;
99
100 while ((offset < end) && (keepout < keepoutend)) {
101 /* Access the valid portion before the keepout. */
102 if (offset < keepout->start) {
103 kend = min(end, keepout->start);
104 ksize = kend - offset;
105 if (write)
106 rc = __nvmem_reg_write(nvmem, offset, val, ksize);
107 else
108 rc = __nvmem_reg_read(nvmem, offset, val, ksize);
109
110 if (rc)
111 return rc;
112
113 offset += ksize;
114 val += ksize;
115 }
116
117 /*
118 * Now we're aligned to the start of this keepout zone. Go
119 * through it.
120 */
121 kend = min(end, keepout->end);
122 ksize = kend - offset;
123 if (!write)
124 memset(val, keepout->value, ksize);
125
126 val += ksize;
127 offset += ksize;
128 keepout++;
129 }
130
131 /*
132 * If we ran out of keepouts but there's still stuff to do, send it
133 * down directly
134 */
135 if (offset < end) {
136 ksize = end - offset;
137 if (write)
138 return __nvmem_reg_write(nvmem, offset, val, ksize);
139 else
140 return __nvmem_reg_read(nvmem, offset, val, ksize);
141 }
142
143 return 0;
144}
145
146static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
147 void *val, size_t bytes)
148{
149 if (!nvmem->nkeepout)
150 return __nvmem_reg_read(nvmem, offset, val, bytes);
151
152 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
153}
154
155static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
156 void *val, size_t bytes)
157{
158 if (!nvmem->nkeepout)
159 return __nvmem_reg_write(nvmem, offset, val, bytes);
160
161 return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
162}
163
164#ifdef CONFIG_NVMEM_SYSFS
165static const char * const nvmem_type_str[] = {
166 [NVMEM_TYPE_UNKNOWN] = "Unknown",
167 [NVMEM_TYPE_EEPROM] = "EEPROM",
168 [NVMEM_TYPE_OTP] = "OTP",
169 [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
170 [NVMEM_TYPE_FRAM] = "FRAM",
171};
172
173#ifdef CONFIG_DEBUG_LOCK_ALLOC
174static struct lock_class_key eeprom_lock_key;
175#endif
176
177static ssize_t type_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
180 struct nvmem_device *nvmem = to_nvmem_device(dev);
181
182 return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
183}
184
185static DEVICE_ATTR_RO(type);
186
187static struct attribute *nvmem_attrs[] = {
188 &dev_attr_type.attr,
189 NULL,
190};
191
192static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
193 struct bin_attribute *attr, char *buf,
194 loff_t pos, size_t count)
195{
196 struct device *dev;
197 struct nvmem_device *nvmem;
198 int rc;
199
200 if (attr->private)
201 dev = attr->private;
202 else
203 dev = kobj_to_dev(kobj);
204 nvmem = to_nvmem_device(dev);
205
206 /* Stop the user from reading */
207 if (pos >= nvmem->size)
208 return 0;
209
210 if (!IS_ALIGNED(pos, nvmem->stride))
211 return -EINVAL;
212
213 if (count < nvmem->word_size)
214 return -EINVAL;
215
216 if (pos + count > nvmem->size)
217 count = nvmem->size - pos;
218
219 count = round_down(count, nvmem->word_size);
220
221 if (!nvmem->reg_read)
222 return -EPERM;
223
224 rc = nvmem_reg_read(nvmem, pos, buf, count);
225
226 if (rc)
227 return rc;
228
229 return count;
230}
231
232static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
233 struct bin_attribute *attr, char *buf,
234 loff_t pos, size_t count)
235{
236 struct device *dev;
237 struct nvmem_device *nvmem;
238 int rc;
239
240 if (attr->private)
241 dev = attr->private;
242 else
243 dev = kobj_to_dev(kobj);
244 nvmem = to_nvmem_device(dev);
245
246 /* Stop the user from writing */
247 if (pos >= nvmem->size)
248 return -EFBIG;
249
250 if (!IS_ALIGNED(pos, nvmem->stride))
251 return -EINVAL;
252
253 if (count < nvmem->word_size)
254 return -EINVAL;
255
256 if (pos + count > nvmem->size)
257 count = nvmem->size - pos;
258
259 count = round_down(count, nvmem->word_size);
260
261 if (!nvmem->reg_write)
262 return -EPERM;
263
264 rc = nvmem_reg_write(nvmem, pos, buf, count);
265
266 if (rc)
267 return rc;
268
269 return count;
270}
271
272static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
273{
274 umode_t mode = 0400;
275
276 if (!nvmem->root_only)
277 mode |= 0044;
278
279 if (!nvmem->read_only)
280 mode |= 0200;
281
282 if (!nvmem->reg_write)
283 mode &= ~0200;
284
285 if (!nvmem->reg_read)
286 mode &= ~0444;
287
288 return mode;
289}
290
291static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
292 struct bin_attribute *attr, int i)
293{
294 struct device *dev = kobj_to_dev(kobj);
295 struct nvmem_device *nvmem = to_nvmem_device(dev);
296
297 attr->size = nvmem->size;
298
299 return nvmem_bin_attr_get_umode(nvmem);
300}
301
302static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
303 const char *id, int index);
304
305static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
306 struct bin_attribute *attr, char *buf,
307 loff_t pos, size_t count)
308{
309 struct nvmem_cell_entry *entry;
310 struct nvmem_cell *cell = NULL;
311 size_t cell_sz, read_len;
312 void *content;
313
314 entry = attr->private;
315 cell = nvmem_create_cell(entry, entry->name, 0);
316 if (IS_ERR(cell))
317 return PTR_ERR(cell);
318
319 if (!cell)
320 return -EINVAL;
321
322 content = nvmem_cell_read(cell, &cell_sz);
323 if (IS_ERR(content)) {
324 read_len = PTR_ERR(content);
325 goto destroy_cell;
326 }
327
328 read_len = min_t(unsigned int, cell_sz - pos, count);
329 memcpy(buf, content + pos, read_len);
330 kfree(content);
331
332destroy_cell:
333 kfree_const(cell->id);
334 kfree(cell);
335
336 return read_len;
337}
338
339/* default read/write permissions */
340static struct bin_attribute bin_attr_rw_nvmem = {
341 .attr = {
342 .name = "nvmem",
343 .mode = 0644,
344 },
345 .read = bin_attr_nvmem_read,
346 .write = bin_attr_nvmem_write,
347};
348
349static struct bin_attribute *nvmem_bin_attributes[] = {
350 &bin_attr_rw_nvmem,
351 NULL,
352};
353
354static const struct attribute_group nvmem_bin_group = {
355 .bin_attrs = nvmem_bin_attributes,
356 .attrs = nvmem_attrs,
357 .is_bin_visible = nvmem_bin_attr_is_visible,
358};
359
360/* Cell attributes will be dynamically allocated */
361static struct attribute_group nvmem_cells_group = {
362 .name = "cells",
363};
364
365static const struct attribute_group *nvmem_dev_groups[] = {
366 &nvmem_bin_group,
367 NULL,
368};
369
370static const struct attribute_group *nvmem_cells_groups[] = {
371 &nvmem_cells_group,
372 NULL,
373};
374
375static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
376 .attr = {
377 .name = "eeprom",
378 },
379 .read = bin_attr_nvmem_read,
380 .write = bin_attr_nvmem_write,
381};
382
383/*
384 * nvmem_setup_compat() - Create an additional binary entry in
385 * drivers sys directory, to be backwards compatible with the older
386 * drivers/misc/eeprom drivers.
387 */
388static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
389 const struct nvmem_config *config)
390{
391 int rval;
392
393 if (!config->compat)
394 return 0;
395
396 if (!config->base_dev)
397 return -EINVAL;
398
399 if (config->type == NVMEM_TYPE_FRAM)
400 bin_attr_nvmem_eeprom_compat.attr.name = "fram";
401
402 nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
403 nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
404 nvmem->eeprom.size = nvmem->size;
405#ifdef CONFIG_DEBUG_LOCK_ALLOC
406 nvmem->eeprom.attr.key = &eeprom_lock_key;
407#endif
408 nvmem->eeprom.private = &nvmem->dev;
409 nvmem->base_dev = config->base_dev;
410
411 rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
412 if (rval) {
413 dev_err(&nvmem->dev,
414 "Failed to create eeprom binary file %d\n", rval);
415 return rval;
416 }
417
418 nvmem->flags |= FLAG_COMPAT;
419
420 return 0;
421}
422
423static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
424 const struct nvmem_config *config)
425{
426 if (config->compat)
427 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
428}
429
430static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
431{
432 struct bin_attribute **cells_attrs, *attrs;
433 struct nvmem_cell_entry *entry;
434 unsigned int ncells = 0, i = 0;
435 int ret = 0;
436
437 mutex_lock(&nvmem_mutex);
438
439 if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) {
440 nvmem_cells_group.bin_attrs = NULL;
441 goto unlock_mutex;
442 }
443
444 /* Allocate an array of attributes with a sentinel */
445 ncells = list_count_nodes(&nvmem->cells);
446 cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1,
447 sizeof(struct bin_attribute *), GFP_KERNEL);
448 if (!cells_attrs) {
449 ret = -ENOMEM;
450 goto unlock_mutex;
451 }
452
453 attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
454 if (!attrs) {
455 ret = -ENOMEM;
456 goto unlock_mutex;
457 }
458
459 /* Initialize each attribute to take the name and size of the cell */
460 list_for_each_entry(entry, &nvmem->cells, node) {
461 sysfs_bin_attr_init(&attrs[i]);
462 attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
463 "%s@%x,%x", entry->name,
464 entry->offset,
465 entry->bit_offset);
466 attrs[i].attr.mode = 0444;
467 attrs[i].size = entry->bytes;
468 attrs[i].read = &nvmem_cell_attr_read;
469 attrs[i].private = entry;
470 if (!attrs[i].attr.name) {
471 ret = -ENOMEM;
472 goto unlock_mutex;
473 }
474
475 cells_attrs[i] = &attrs[i];
476 i++;
477 }
478
479 nvmem_cells_group.bin_attrs = cells_attrs;
480
481 ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups);
482 if (ret)
483 goto unlock_mutex;
484
485 nvmem->sysfs_cells_populated = true;
486
487unlock_mutex:
488 mutex_unlock(&nvmem_mutex);
489
490 return ret;
491}
492
493#else /* CONFIG_NVMEM_SYSFS */
494
495static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
496 const struct nvmem_config *config)
497{
498 return -ENOSYS;
499}
500static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
501 const struct nvmem_config *config)
502{
503}
504
505#endif /* CONFIG_NVMEM_SYSFS */
506
507static void nvmem_release(struct device *dev)
508{
509 struct nvmem_device *nvmem = to_nvmem_device(dev);
510
511 ida_free(&nvmem_ida, nvmem->id);
512 gpiod_put(nvmem->wp_gpio);
513 kfree(nvmem);
514}
515
516static const struct device_type nvmem_provider_type = {
517 .release = nvmem_release,
518};
519
520static struct bus_type nvmem_bus_type = {
521 .name = "nvmem",
522};
523
524static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
525{
526 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
527 mutex_lock(&nvmem_mutex);
528 list_del(&cell->node);
529 mutex_unlock(&nvmem_mutex);
530 of_node_put(cell->np);
531 kfree_const(cell->name);
532 kfree(cell);
533}
534
535static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
536{
537 struct nvmem_cell_entry *cell, *p;
538
539 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
540 nvmem_cell_entry_drop(cell);
541}
542
543static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
544{
545 mutex_lock(&nvmem_mutex);
546 list_add_tail(&cell->node, &cell->nvmem->cells);
547 mutex_unlock(&nvmem_mutex);
548 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
549}
550
551static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
552 const struct nvmem_cell_info *info,
553 struct nvmem_cell_entry *cell)
554{
555 cell->nvmem = nvmem;
556 cell->offset = info->offset;
557 cell->raw_len = info->raw_len ?: info->bytes;
558 cell->bytes = info->bytes;
559 cell->name = info->name;
560 cell->read_post_process = info->read_post_process;
561 cell->priv = info->priv;
562
563 cell->bit_offset = info->bit_offset;
564 cell->nbits = info->nbits;
565 cell->np = info->np;
566
567 if (cell->nbits)
568 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
569 BITS_PER_BYTE);
570
571 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
572 dev_err(&nvmem->dev,
573 "cell %s unaligned to nvmem stride %d\n",
574 cell->name ?: "<unknown>", nvmem->stride);
575 return -EINVAL;
576 }
577
578 return 0;
579}
580
581static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
582 const struct nvmem_cell_info *info,
583 struct nvmem_cell_entry *cell)
584{
585 int err;
586
587 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
588 if (err)
589 return err;
590
591 cell->name = kstrdup_const(info->name, GFP_KERNEL);
592 if (!cell->name)
593 return -ENOMEM;
594
595 return 0;
596}
597
598/**
599 * nvmem_add_one_cell() - Add one cell information to an nvmem device
600 *
601 * @nvmem: nvmem device to add cells to.
602 * @info: nvmem cell info to add to the device
603 *
604 * Return: 0 or negative error code on failure.
605 */
606int nvmem_add_one_cell(struct nvmem_device *nvmem,
607 const struct nvmem_cell_info *info)
608{
609 struct nvmem_cell_entry *cell;
610 int rval;
611
612 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
613 if (!cell)
614 return -ENOMEM;
615
616 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
617 if (rval) {
618 kfree(cell);
619 return rval;
620 }
621
622 nvmem_cell_entry_add(cell);
623
624 return 0;
625}
626EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
627
628/**
629 * nvmem_add_cells() - Add cell information to an nvmem device
630 *
631 * @nvmem: nvmem device to add cells to.
632 * @info: nvmem cell info to add to the device
633 * @ncells: number of cells in info
634 *
635 * Return: 0 or negative error code on failure.
636 */
637static int nvmem_add_cells(struct nvmem_device *nvmem,
638 const struct nvmem_cell_info *info,
639 int ncells)
640{
641 int i, rval;
642
643 for (i = 0; i < ncells; i++) {
644 rval = nvmem_add_one_cell(nvmem, &info[i]);
645 if (rval)
646 return rval;
647 }
648
649 return 0;
650}
651
652/**
653 * nvmem_register_notifier() - Register a notifier block for nvmem events.
654 *
655 * @nb: notifier block to be called on nvmem events.
656 *
657 * Return: 0 on success, negative error number on failure.
658 */
659int nvmem_register_notifier(struct notifier_block *nb)
660{
661 return blocking_notifier_chain_register(&nvmem_notifier, nb);
662}
663EXPORT_SYMBOL_GPL(nvmem_register_notifier);
664
665/**
666 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
667 *
668 * @nb: notifier block to be unregistered.
669 *
670 * Return: 0 on success, negative error number on failure.
671 */
672int nvmem_unregister_notifier(struct notifier_block *nb)
673{
674 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
675}
676EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
677
678static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
679{
680 const struct nvmem_cell_info *info;
681 struct nvmem_cell_table *table;
682 struct nvmem_cell_entry *cell;
683 int rval = 0, i;
684
685 mutex_lock(&nvmem_cell_mutex);
686 list_for_each_entry(table, &nvmem_cell_tables, node) {
687 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
688 for (i = 0; i < table->ncells; i++) {
689 info = &table->cells[i];
690
691 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
692 if (!cell) {
693 rval = -ENOMEM;
694 goto out;
695 }
696
697 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
698 if (rval) {
699 kfree(cell);
700 goto out;
701 }
702
703 nvmem_cell_entry_add(cell);
704 }
705 }
706 }
707
708out:
709 mutex_unlock(&nvmem_cell_mutex);
710 return rval;
711}
712
713static struct nvmem_cell_entry *
714nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
715{
716 struct nvmem_cell_entry *iter, *cell = NULL;
717
718 mutex_lock(&nvmem_mutex);
719 list_for_each_entry(iter, &nvmem->cells, node) {
720 if (strcmp(cell_id, iter->name) == 0) {
721 cell = iter;
722 break;
723 }
724 }
725 mutex_unlock(&nvmem_mutex);
726
727 return cell;
728}
729
730static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
731{
732 unsigned int cur = 0;
733 const struct nvmem_keepout *keepout = nvmem->keepout;
734 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
735
736 while (keepout < keepoutend) {
737 /* Ensure keepouts are sorted and don't overlap. */
738 if (keepout->start < cur) {
739 dev_err(&nvmem->dev,
740 "Keepout regions aren't sorted or overlap.\n");
741
742 return -ERANGE;
743 }
744
745 if (keepout->end < keepout->start) {
746 dev_err(&nvmem->dev,
747 "Invalid keepout region.\n");
748
749 return -EINVAL;
750 }
751
752 /*
753 * Validate keepouts (and holes between) don't violate
754 * word_size constraints.
755 */
756 if ((keepout->end - keepout->start < nvmem->word_size) ||
757 ((keepout->start != cur) &&
758 (keepout->start - cur < nvmem->word_size))) {
759
760 dev_err(&nvmem->dev,
761 "Keepout regions violate word_size constraints.\n");
762
763 return -ERANGE;
764 }
765
766 /* Validate keepouts don't violate stride (alignment). */
767 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
768 !IS_ALIGNED(keepout->end, nvmem->stride)) {
769
770 dev_err(&nvmem->dev,
771 "Keepout regions violate stride.\n");
772
773 return -EINVAL;
774 }
775
776 cur = keepout->end;
777 keepout++;
778 }
779
780 return 0;
781}
782
783static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
784{
785 struct device *dev = &nvmem->dev;
786 struct device_node *child;
787 const __be32 *addr;
788 int len, ret;
789
790 for_each_child_of_node(np, child) {
791 struct nvmem_cell_info info = {0};
792
793 addr = of_get_property(child, "reg", &len);
794 if (!addr)
795 continue;
796 if (len < 2 * sizeof(u32)) {
797 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
798 of_node_put(child);
799 return -EINVAL;
800 }
801
802 info.offset = be32_to_cpup(addr++);
803 info.bytes = be32_to_cpup(addr);
804 info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
805
806 addr = of_get_property(child, "bits", &len);
807 if (addr && len == (2 * sizeof(u32))) {
808 info.bit_offset = be32_to_cpup(addr++);
809 info.nbits = be32_to_cpup(addr);
810 if (info.bit_offset >= BITS_PER_BYTE || info.nbits < 1) {
811 dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
812 of_node_put(child);
813 return -EINVAL;
814 }
815 }
816
817 info.np = of_node_get(child);
818
819 if (nvmem->fixup_dt_cell_info)
820 nvmem->fixup_dt_cell_info(nvmem, &info);
821
822 ret = nvmem_add_one_cell(nvmem, &info);
823 kfree(info.name);
824 if (ret) {
825 of_node_put(child);
826 return ret;
827 }
828 }
829
830 return 0;
831}
832
833static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
834{
835 return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
836}
837
838static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
839{
840 struct device_node *layout_np;
841 int err = 0;
842
843 layout_np = of_nvmem_layout_get_container(nvmem);
844 if (!layout_np)
845 return 0;
846
847 if (of_device_is_compatible(layout_np, "fixed-layout"))
848 err = nvmem_add_cells_from_dt(nvmem, layout_np);
849
850 of_node_put(layout_np);
851
852 return err;
853}
854
855int nvmem_layout_register(struct nvmem_layout *layout)
856{
857 int ret;
858
859 if (!layout->add_cells)
860 return -EINVAL;
861
862 /* Populate the cells */
863 ret = layout->add_cells(layout);
864 if (ret)
865 return ret;
866
867#ifdef CONFIG_NVMEM_SYSFS
868 ret = nvmem_populate_sysfs_cells(layout->nvmem);
869 if (ret) {
870 nvmem_device_remove_all_cells(layout->nvmem);
871 return ret;
872 }
873#endif
874
875 return 0;
876}
877EXPORT_SYMBOL_GPL(nvmem_layout_register);
878
879void nvmem_layout_unregister(struct nvmem_layout *layout)
880{
881 /* Keep the API even with an empty stub in case we need it later */
882}
883EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
884
885/**
886 * nvmem_register() - Register a nvmem device for given nvmem_config.
887 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
888 *
889 * @config: nvmem device configuration with which nvmem device is created.
890 *
891 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
892 * on success.
893 */
894
895struct nvmem_device *nvmem_register(const struct nvmem_config *config)
896{
897 struct nvmem_device *nvmem;
898 int rval;
899
900 if (!config->dev)
901 return ERR_PTR(-EINVAL);
902
903 if (!config->reg_read && !config->reg_write)
904 return ERR_PTR(-EINVAL);
905
906 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
907 if (!nvmem)
908 return ERR_PTR(-ENOMEM);
909
910 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
911 if (rval < 0) {
912 kfree(nvmem);
913 return ERR_PTR(rval);
914 }
915
916 nvmem->id = rval;
917
918 nvmem->dev.type = &nvmem_provider_type;
919 nvmem->dev.bus = &nvmem_bus_type;
920 nvmem->dev.parent = config->dev;
921
922 device_initialize(&nvmem->dev);
923
924 if (!config->ignore_wp)
925 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
926 GPIOD_OUT_HIGH);
927 if (IS_ERR(nvmem->wp_gpio)) {
928 rval = PTR_ERR(nvmem->wp_gpio);
929 nvmem->wp_gpio = NULL;
930 goto err_put_device;
931 }
932
933 kref_init(&nvmem->refcnt);
934 INIT_LIST_HEAD(&nvmem->cells);
935 nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
936
937 nvmem->owner = config->owner;
938 if (!nvmem->owner && config->dev->driver)
939 nvmem->owner = config->dev->driver->owner;
940 nvmem->stride = config->stride ?: 1;
941 nvmem->word_size = config->word_size ?: 1;
942 nvmem->size = config->size;
943 nvmem->root_only = config->root_only;
944 nvmem->priv = config->priv;
945 nvmem->type = config->type;
946 nvmem->reg_read = config->reg_read;
947 nvmem->reg_write = config->reg_write;
948 nvmem->keepout = config->keepout;
949 nvmem->nkeepout = config->nkeepout;
950 if (config->of_node)
951 nvmem->dev.of_node = config->of_node;
952 else
953 nvmem->dev.of_node = config->dev->of_node;
954
955 switch (config->id) {
956 case NVMEM_DEVID_NONE:
957 rval = dev_set_name(&nvmem->dev, "%s", config->name);
958 break;
959 case NVMEM_DEVID_AUTO:
960 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
961 break;
962 default:
963 rval = dev_set_name(&nvmem->dev, "%s%d",
964 config->name ? : "nvmem",
965 config->name ? config->id : nvmem->id);
966 break;
967 }
968
969 if (rval)
970 goto err_put_device;
971
972 nvmem->read_only = device_property_present(config->dev, "read-only") ||
973 config->read_only || !nvmem->reg_write;
974
975#ifdef CONFIG_NVMEM_SYSFS
976 nvmem->dev.groups = nvmem_dev_groups;
977#endif
978
979 if (nvmem->nkeepout) {
980 rval = nvmem_validate_keepouts(nvmem);
981 if (rval)
982 goto err_put_device;
983 }
984
985 if (config->compat) {
986 rval = nvmem_sysfs_setup_compat(nvmem, config);
987 if (rval)
988 goto err_put_device;
989 }
990
991 if (config->cells) {
992 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
993 if (rval)
994 goto err_remove_cells;
995 }
996
997 rval = nvmem_add_cells_from_table(nvmem);
998 if (rval)
999 goto err_remove_cells;
1000
1001 if (config->add_legacy_fixed_of_cells) {
1002 rval = nvmem_add_cells_from_legacy_of(nvmem);
1003 if (rval)
1004 goto err_remove_cells;
1005 }
1006
1007 rval = nvmem_add_cells_from_fixed_layout(nvmem);
1008 if (rval)
1009 goto err_remove_cells;
1010
1011 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
1012
1013 rval = device_add(&nvmem->dev);
1014 if (rval)
1015 goto err_remove_cells;
1016
1017 rval = nvmem_populate_layout(nvmem);
1018 if (rval)
1019 goto err_remove_dev;
1020
1021#ifdef CONFIG_NVMEM_SYSFS
1022 rval = nvmem_populate_sysfs_cells(nvmem);
1023 if (rval)
1024 goto err_destroy_layout;
1025#endif
1026
1027 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
1028
1029 return nvmem;
1030
1031#ifdef CONFIG_NVMEM_SYSFS
1032err_destroy_layout:
1033 nvmem_destroy_layout(nvmem);
1034#endif
1035err_remove_dev:
1036 device_del(&nvmem->dev);
1037err_remove_cells:
1038 nvmem_device_remove_all_cells(nvmem);
1039 if (config->compat)
1040 nvmem_sysfs_remove_compat(nvmem, config);
1041err_put_device:
1042 put_device(&nvmem->dev);
1043
1044 return ERR_PTR(rval);
1045}
1046EXPORT_SYMBOL_GPL(nvmem_register);
1047
1048static void nvmem_device_release(struct kref *kref)
1049{
1050 struct nvmem_device *nvmem;
1051
1052 nvmem = container_of(kref, struct nvmem_device, refcnt);
1053
1054 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1055
1056 if (nvmem->flags & FLAG_COMPAT)
1057 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1058
1059 nvmem_device_remove_all_cells(nvmem);
1060 nvmem_destroy_layout(nvmem);
1061 device_unregister(&nvmem->dev);
1062}
1063
1064/**
1065 * nvmem_unregister() - Unregister previously registered nvmem device
1066 *
1067 * @nvmem: Pointer to previously registered nvmem device.
1068 */
1069void nvmem_unregister(struct nvmem_device *nvmem)
1070{
1071 if (nvmem)
1072 kref_put(&nvmem->refcnt, nvmem_device_release);
1073}
1074EXPORT_SYMBOL_GPL(nvmem_unregister);
1075
1076static void devm_nvmem_unregister(void *nvmem)
1077{
1078 nvmem_unregister(nvmem);
1079}
1080
1081/**
1082 * devm_nvmem_register() - Register a managed nvmem device for given
1083 * nvmem_config.
1084 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1085 *
1086 * @dev: Device that uses the nvmem device.
1087 * @config: nvmem device configuration with which nvmem device is created.
1088 *
1089 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1090 * on success.
1091 */
1092struct nvmem_device *devm_nvmem_register(struct device *dev,
1093 const struct nvmem_config *config)
1094{
1095 struct nvmem_device *nvmem;
1096 int ret;
1097
1098 nvmem = nvmem_register(config);
1099 if (IS_ERR(nvmem))
1100 return nvmem;
1101
1102 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1103 if (ret)
1104 return ERR_PTR(ret);
1105
1106 return nvmem;
1107}
1108EXPORT_SYMBOL_GPL(devm_nvmem_register);
1109
1110static struct nvmem_device *__nvmem_device_get(void *data,
1111 int (*match)(struct device *dev, const void *data))
1112{
1113 struct nvmem_device *nvmem = NULL;
1114 struct device *dev;
1115
1116 mutex_lock(&nvmem_mutex);
1117 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1118 if (dev)
1119 nvmem = to_nvmem_device(dev);
1120 mutex_unlock(&nvmem_mutex);
1121 if (!nvmem)
1122 return ERR_PTR(-EPROBE_DEFER);
1123
1124 if (!try_module_get(nvmem->owner)) {
1125 dev_err(&nvmem->dev,
1126 "could not increase module refcount for cell %s\n",
1127 nvmem_dev_name(nvmem));
1128
1129 put_device(&nvmem->dev);
1130 return ERR_PTR(-EINVAL);
1131 }
1132
1133 kref_get(&nvmem->refcnt);
1134
1135 return nvmem;
1136}
1137
1138static void __nvmem_device_put(struct nvmem_device *nvmem)
1139{
1140 put_device(&nvmem->dev);
1141 module_put(nvmem->owner);
1142 kref_put(&nvmem->refcnt, nvmem_device_release);
1143}
1144
1145#if IS_ENABLED(CONFIG_OF)
1146/**
1147 * of_nvmem_device_get() - Get nvmem device from a given id
1148 *
1149 * @np: Device tree node that uses the nvmem device.
1150 * @id: nvmem name from nvmem-names property.
1151 *
1152 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1153 * on success.
1154 */
1155struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1156{
1157
1158 struct device_node *nvmem_np;
1159 struct nvmem_device *nvmem;
1160 int index = 0;
1161
1162 if (id)
1163 index = of_property_match_string(np, "nvmem-names", id);
1164
1165 nvmem_np = of_parse_phandle(np, "nvmem", index);
1166 if (!nvmem_np)
1167 return ERR_PTR(-ENOENT);
1168
1169 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1170 of_node_put(nvmem_np);
1171 return nvmem;
1172}
1173EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1174#endif
1175
1176/**
1177 * nvmem_device_get() - Get nvmem device from a given id
1178 *
1179 * @dev: Device that uses the nvmem device.
1180 * @dev_name: name of the requested nvmem device.
1181 *
1182 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1183 * on success.
1184 */
1185struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1186{
1187 if (dev->of_node) { /* try dt first */
1188 struct nvmem_device *nvmem;
1189
1190 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1191
1192 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1193 return nvmem;
1194
1195 }
1196
1197 return __nvmem_device_get((void *)dev_name, device_match_name);
1198}
1199EXPORT_SYMBOL_GPL(nvmem_device_get);
1200
1201/**
1202 * nvmem_device_find() - Find nvmem device with matching function
1203 *
1204 * @data: Data to pass to match function
1205 * @match: Callback function to check device
1206 *
1207 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1208 * on success.
1209 */
1210struct nvmem_device *nvmem_device_find(void *data,
1211 int (*match)(struct device *dev, const void *data))
1212{
1213 return __nvmem_device_get(data, match);
1214}
1215EXPORT_SYMBOL_GPL(nvmem_device_find);
1216
1217static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1218{
1219 struct nvmem_device **nvmem = res;
1220
1221 if (WARN_ON(!nvmem || !*nvmem))
1222 return 0;
1223
1224 return *nvmem == data;
1225}
1226
1227static void devm_nvmem_device_release(struct device *dev, void *res)
1228{
1229 nvmem_device_put(*(struct nvmem_device **)res);
1230}
1231
1232/**
1233 * devm_nvmem_device_put() - put alredy got nvmem device
1234 *
1235 * @dev: Device that uses the nvmem device.
1236 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1237 * that needs to be released.
1238 */
1239void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1240{
1241 int ret;
1242
1243 ret = devres_release(dev, devm_nvmem_device_release,
1244 devm_nvmem_device_match, nvmem);
1245
1246 WARN_ON(ret);
1247}
1248EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1249
1250/**
1251 * nvmem_device_put() - put alredy got nvmem device
1252 *
1253 * @nvmem: pointer to nvmem device that needs to be released.
1254 */
1255void nvmem_device_put(struct nvmem_device *nvmem)
1256{
1257 __nvmem_device_put(nvmem);
1258}
1259EXPORT_SYMBOL_GPL(nvmem_device_put);
1260
1261/**
1262 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1263 *
1264 * @dev: Device that requests the nvmem device.
1265 * @id: name id for the requested nvmem device.
1266 *
1267 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1268 * on success. The nvmem_cell will be freed by the automatically once the
1269 * device is freed.
1270 */
1271struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1272{
1273 struct nvmem_device **ptr, *nvmem;
1274
1275 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1276 if (!ptr)
1277 return ERR_PTR(-ENOMEM);
1278
1279 nvmem = nvmem_device_get(dev, id);
1280 if (!IS_ERR(nvmem)) {
1281 *ptr = nvmem;
1282 devres_add(dev, ptr);
1283 } else {
1284 devres_free(ptr);
1285 }
1286
1287 return nvmem;
1288}
1289EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1290
1291static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1292 const char *id, int index)
1293{
1294 struct nvmem_cell *cell;
1295 const char *name = NULL;
1296
1297 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1298 if (!cell)
1299 return ERR_PTR(-ENOMEM);
1300
1301 if (id) {
1302 name = kstrdup_const(id, GFP_KERNEL);
1303 if (!name) {
1304 kfree(cell);
1305 return ERR_PTR(-ENOMEM);
1306 }
1307 }
1308
1309 cell->id = name;
1310 cell->entry = entry;
1311 cell->index = index;
1312
1313 return cell;
1314}
1315
1316static struct nvmem_cell *
1317nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1318{
1319 struct nvmem_cell_entry *cell_entry;
1320 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1321 struct nvmem_cell_lookup *lookup;
1322 struct nvmem_device *nvmem;
1323 const char *dev_id;
1324
1325 if (!dev)
1326 return ERR_PTR(-EINVAL);
1327
1328 dev_id = dev_name(dev);
1329
1330 mutex_lock(&nvmem_lookup_mutex);
1331
1332 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1333 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1334 (strcmp(lookup->con_id, con_id) == 0)) {
1335 /* This is the right entry. */
1336 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1337 device_match_name);
1338 if (IS_ERR(nvmem)) {
1339 /* Provider may not be registered yet. */
1340 cell = ERR_CAST(nvmem);
1341 break;
1342 }
1343
1344 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1345 lookup->cell_name);
1346 if (!cell_entry) {
1347 __nvmem_device_put(nvmem);
1348 cell = ERR_PTR(-ENOENT);
1349 } else {
1350 cell = nvmem_create_cell(cell_entry, con_id, 0);
1351 if (IS_ERR(cell))
1352 __nvmem_device_put(nvmem);
1353 }
1354 break;
1355 }
1356 }
1357
1358 mutex_unlock(&nvmem_lookup_mutex);
1359 return cell;
1360}
1361
1362static void nvmem_layout_module_put(struct nvmem_device *nvmem)
1363{
1364 if (nvmem->layout && nvmem->layout->dev.driver)
1365 module_put(nvmem->layout->dev.driver->owner);
1366}
1367
1368#if IS_ENABLED(CONFIG_OF)
1369static struct nvmem_cell_entry *
1370nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1371{
1372 struct nvmem_cell_entry *iter, *cell = NULL;
1373
1374 mutex_lock(&nvmem_mutex);
1375 list_for_each_entry(iter, &nvmem->cells, node) {
1376 if (np == iter->np) {
1377 cell = iter;
1378 break;
1379 }
1380 }
1381 mutex_unlock(&nvmem_mutex);
1382
1383 return cell;
1384}
1385
1386static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
1387{
1388 if (!nvmem->layout)
1389 return 0;
1390
1391 if (!nvmem->layout->dev.driver ||
1392 !try_module_get(nvmem->layout->dev.driver->owner))
1393 return -EPROBE_DEFER;
1394
1395 return 0;
1396}
1397
1398/**
1399 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1400 *
1401 * @np: Device tree node that uses the nvmem cell.
1402 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1403 * for the cell at index 0 (the lone cell with no accompanying
1404 * nvmem-cell-names property).
1405 *
1406 * Return: Will be an ERR_PTR() on error or a valid pointer
1407 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1408 * nvmem_cell_put().
1409 */
1410struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1411{
1412 struct device_node *cell_np, *nvmem_np;
1413 struct nvmem_device *nvmem;
1414 struct nvmem_cell_entry *cell_entry;
1415 struct nvmem_cell *cell;
1416 struct of_phandle_args cell_spec;
1417 int index = 0;
1418 int cell_index = 0;
1419 int ret;
1420
1421 /* if cell name exists, find index to the name */
1422 if (id)
1423 index = of_property_match_string(np, "nvmem-cell-names", id);
1424
1425 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1426 "#nvmem-cell-cells",
1427 index, &cell_spec);
1428 if (ret)
1429 return ERR_PTR(-ENOENT);
1430
1431 if (cell_spec.args_count > 1)
1432 return ERR_PTR(-EINVAL);
1433
1434 cell_np = cell_spec.np;
1435 if (cell_spec.args_count)
1436 cell_index = cell_spec.args[0];
1437
1438 nvmem_np = of_get_parent(cell_np);
1439 if (!nvmem_np) {
1440 of_node_put(cell_np);
1441 return ERR_PTR(-EINVAL);
1442 }
1443
1444 /* nvmem layouts produce cells within the nvmem-layout container */
1445 if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1446 nvmem_np = of_get_next_parent(nvmem_np);
1447 if (!nvmem_np) {
1448 of_node_put(cell_np);
1449 return ERR_PTR(-EINVAL);
1450 }
1451 }
1452
1453 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1454 of_node_put(nvmem_np);
1455 if (IS_ERR(nvmem)) {
1456 of_node_put(cell_np);
1457 return ERR_CAST(nvmem);
1458 }
1459
1460 ret = nvmem_layout_module_get_optional(nvmem);
1461 if (ret) {
1462 of_node_put(cell_np);
1463 __nvmem_device_put(nvmem);
1464 return ERR_PTR(ret);
1465 }
1466
1467 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1468 of_node_put(cell_np);
1469 if (!cell_entry) {
1470 __nvmem_device_put(nvmem);
1471 nvmem_layout_module_put(nvmem);
1472 if (nvmem->layout)
1473 return ERR_PTR(-EPROBE_DEFER);
1474 else
1475 return ERR_PTR(-ENOENT);
1476 }
1477
1478 cell = nvmem_create_cell(cell_entry, id, cell_index);
1479 if (IS_ERR(cell)) {
1480 __nvmem_device_put(nvmem);
1481 nvmem_layout_module_put(nvmem);
1482 }
1483
1484 return cell;
1485}
1486EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1487#endif
1488
1489/**
1490 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1491 *
1492 * @dev: Device that requests the nvmem cell.
1493 * @id: nvmem cell name to get (this corresponds with the name from the
1494 * nvmem-cell-names property for DT systems and with the con_id from
1495 * the lookup entry for non-DT systems).
1496 *
1497 * Return: Will be an ERR_PTR() on error or a valid pointer
1498 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1499 * nvmem_cell_put().
1500 */
1501struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1502{
1503 struct nvmem_cell *cell;
1504
1505 if (dev->of_node) { /* try dt first */
1506 cell = of_nvmem_cell_get(dev->of_node, id);
1507 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1508 return cell;
1509 }
1510
1511 /* NULL cell id only allowed for device tree; invalid otherwise */
1512 if (!id)
1513 return ERR_PTR(-EINVAL);
1514
1515 return nvmem_cell_get_from_lookup(dev, id);
1516}
1517EXPORT_SYMBOL_GPL(nvmem_cell_get);
1518
1519static void devm_nvmem_cell_release(struct device *dev, void *res)
1520{
1521 nvmem_cell_put(*(struct nvmem_cell **)res);
1522}
1523
1524/**
1525 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1526 *
1527 * @dev: Device that requests the nvmem cell.
1528 * @id: nvmem cell name id to get.
1529 *
1530 * Return: Will be an ERR_PTR() on error or a valid pointer
1531 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1532 * automatically once the device is freed.
1533 */
1534struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1535{
1536 struct nvmem_cell **ptr, *cell;
1537
1538 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1539 if (!ptr)
1540 return ERR_PTR(-ENOMEM);
1541
1542 cell = nvmem_cell_get(dev, id);
1543 if (!IS_ERR(cell)) {
1544 *ptr = cell;
1545 devres_add(dev, ptr);
1546 } else {
1547 devres_free(ptr);
1548 }
1549
1550 return cell;
1551}
1552EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1553
1554static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1555{
1556 struct nvmem_cell **c = res;
1557
1558 if (WARN_ON(!c || !*c))
1559 return 0;
1560
1561 return *c == data;
1562}
1563
1564/**
1565 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1566 * from devm_nvmem_cell_get.
1567 *
1568 * @dev: Device that requests the nvmem cell.
1569 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1570 */
1571void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1572{
1573 int ret;
1574
1575 ret = devres_release(dev, devm_nvmem_cell_release,
1576 devm_nvmem_cell_match, cell);
1577
1578 WARN_ON(ret);
1579}
1580EXPORT_SYMBOL(devm_nvmem_cell_put);
1581
1582/**
1583 * nvmem_cell_put() - Release previously allocated nvmem cell.
1584 *
1585 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1586 */
1587void nvmem_cell_put(struct nvmem_cell *cell)
1588{
1589 struct nvmem_device *nvmem = cell->entry->nvmem;
1590
1591 if (cell->id)
1592 kfree_const(cell->id);
1593
1594 kfree(cell);
1595 __nvmem_device_put(nvmem);
1596 nvmem_layout_module_put(nvmem);
1597}
1598EXPORT_SYMBOL_GPL(nvmem_cell_put);
1599
1600static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1601{
1602 u8 *p, *b;
1603 int i, extra, bit_offset = cell->bit_offset;
1604
1605 p = b = buf;
1606 if (bit_offset) {
1607 /* First shift */
1608 *b++ >>= bit_offset;
1609
1610 /* setup rest of the bytes if any */
1611 for (i = 1; i < cell->bytes; i++) {
1612 /* Get bits from next byte and shift them towards msb */
1613 *p |= *b << (BITS_PER_BYTE - bit_offset);
1614
1615 p = b;
1616 *b++ >>= bit_offset;
1617 }
1618 } else {
1619 /* point to the msb */
1620 p += cell->bytes - 1;
1621 }
1622
1623 /* result fits in less bytes */
1624 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1625 while (--extra >= 0)
1626 *p-- = 0;
1627
1628 /* clear msb bits if any leftover in the last byte */
1629 if (cell->nbits % BITS_PER_BYTE)
1630 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1631}
1632
1633static int __nvmem_cell_read(struct nvmem_device *nvmem,
1634 struct nvmem_cell_entry *cell,
1635 void *buf, size_t *len, const char *id, int index)
1636{
1637 int rc;
1638
1639 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
1640
1641 if (rc)
1642 return rc;
1643
1644 /* shift bits in-place */
1645 if (cell->bit_offset || cell->nbits)
1646 nvmem_shift_read_buffer_in_place(cell, buf);
1647
1648 if (cell->read_post_process) {
1649 rc = cell->read_post_process(cell->priv, id, index,
1650 cell->offset, buf, cell->raw_len);
1651 if (rc)
1652 return rc;
1653 }
1654
1655 if (len)
1656 *len = cell->bytes;
1657
1658 return 0;
1659}
1660
1661/**
1662 * nvmem_cell_read() - Read a given nvmem cell
1663 *
1664 * @cell: nvmem cell to be read.
1665 * @len: pointer to length of cell which will be populated on successful read;
1666 * can be NULL.
1667 *
1668 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1669 * buffer should be freed by the consumer with a kfree().
1670 */
1671void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1672{
1673 struct nvmem_cell_entry *entry = cell->entry;
1674 struct nvmem_device *nvmem = entry->nvmem;
1675 u8 *buf;
1676 int rc;
1677
1678 if (!nvmem)
1679 return ERR_PTR(-EINVAL);
1680
1681 buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
1682 if (!buf)
1683 return ERR_PTR(-ENOMEM);
1684
1685 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1686 if (rc) {
1687 kfree(buf);
1688 return ERR_PTR(rc);
1689 }
1690
1691 return buf;
1692}
1693EXPORT_SYMBOL_GPL(nvmem_cell_read);
1694
1695static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1696 u8 *_buf, int len)
1697{
1698 struct nvmem_device *nvmem = cell->nvmem;
1699 int i, rc, nbits, bit_offset = cell->bit_offset;
1700 u8 v, *p, *buf, *b, pbyte, pbits;
1701
1702 nbits = cell->nbits;
1703 buf = kzalloc(cell->bytes, GFP_KERNEL);
1704 if (!buf)
1705 return ERR_PTR(-ENOMEM);
1706
1707 memcpy(buf, _buf, len);
1708 p = b = buf;
1709
1710 if (bit_offset) {
1711 pbyte = *b;
1712 *b <<= bit_offset;
1713
1714 /* setup the first byte with lsb bits from nvmem */
1715 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1716 if (rc)
1717 goto err;
1718 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1719
1720 /* setup rest of the byte if any */
1721 for (i = 1; i < cell->bytes; i++) {
1722 /* Get last byte bits and shift them towards lsb */
1723 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1724 pbyte = *b;
1725 p = b;
1726 *b <<= bit_offset;
1727 *b++ |= pbits;
1728 }
1729 }
1730
1731 /* if it's not end on byte boundary */
1732 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1733 /* setup the last byte with msb bits from nvmem */
1734 rc = nvmem_reg_read(nvmem,
1735 cell->offset + cell->bytes - 1, &v, 1);
1736 if (rc)
1737 goto err;
1738 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1739
1740 }
1741
1742 return buf;
1743err:
1744 kfree(buf);
1745 return ERR_PTR(rc);
1746}
1747
1748static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1749{
1750 struct nvmem_device *nvmem = cell->nvmem;
1751 int rc;
1752
1753 if (!nvmem || nvmem->read_only ||
1754 (cell->bit_offset == 0 && len != cell->bytes))
1755 return -EINVAL;
1756
1757 /*
1758 * Any cells which have a read_post_process hook are read-only because
1759 * we cannot reverse the operation and it might affect other cells,
1760 * too.
1761 */
1762 if (cell->read_post_process)
1763 return -EINVAL;
1764
1765 if (cell->bit_offset || cell->nbits) {
1766 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1767 if (IS_ERR(buf))
1768 return PTR_ERR(buf);
1769 }
1770
1771 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1772
1773 /* free the tmp buffer */
1774 if (cell->bit_offset || cell->nbits)
1775 kfree(buf);
1776
1777 if (rc)
1778 return rc;
1779
1780 return len;
1781}
1782
1783/**
1784 * nvmem_cell_write() - Write to a given nvmem cell
1785 *
1786 * @cell: nvmem cell to be written.
1787 * @buf: Buffer to be written.
1788 * @len: length of buffer to be written to nvmem cell.
1789 *
1790 * Return: length of bytes written or negative on failure.
1791 */
1792int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1793{
1794 return __nvmem_cell_entry_write(cell->entry, buf, len);
1795}
1796
1797EXPORT_SYMBOL_GPL(nvmem_cell_write);
1798
1799static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1800 void *val, size_t count)
1801{
1802 struct nvmem_cell *cell;
1803 void *buf;
1804 size_t len;
1805
1806 cell = nvmem_cell_get(dev, cell_id);
1807 if (IS_ERR(cell))
1808 return PTR_ERR(cell);
1809
1810 buf = nvmem_cell_read(cell, &len);
1811 if (IS_ERR(buf)) {
1812 nvmem_cell_put(cell);
1813 return PTR_ERR(buf);
1814 }
1815 if (len != count) {
1816 kfree(buf);
1817 nvmem_cell_put(cell);
1818 return -EINVAL;
1819 }
1820 memcpy(val, buf, count);
1821 kfree(buf);
1822 nvmem_cell_put(cell);
1823
1824 return 0;
1825}
1826
1827/**
1828 * nvmem_cell_read_u8() - Read a cell value as a u8
1829 *
1830 * @dev: Device that requests the nvmem cell.
1831 * @cell_id: Name of nvmem cell to read.
1832 * @val: pointer to output value.
1833 *
1834 * Return: 0 on success or negative errno.
1835 */
1836int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1837{
1838 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1839}
1840EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1841
1842/**
1843 * nvmem_cell_read_u16() - Read a cell value as a u16
1844 *
1845 * @dev: Device that requests the nvmem cell.
1846 * @cell_id: Name of nvmem cell to read.
1847 * @val: pointer to output value.
1848 *
1849 * Return: 0 on success or negative errno.
1850 */
1851int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1852{
1853 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1854}
1855EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1856
1857/**
1858 * nvmem_cell_read_u32() - Read a cell value as a u32
1859 *
1860 * @dev: Device that requests the nvmem cell.
1861 * @cell_id: Name of nvmem cell to read.
1862 * @val: pointer to output value.
1863 *
1864 * Return: 0 on success or negative errno.
1865 */
1866int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1867{
1868 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1869}
1870EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1871
1872/**
1873 * nvmem_cell_read_u64() - Read a cell value as a u64
1874 *
1875 * @dev: Device that requests the nvmem cell.
1876 * @cell_id: Name of nvmem cell to read.
1877 * @val: pointer to output value.
1878 *
1879 * Return: 0 on success or negative errno.
1880 */
1881int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1882{
1883 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1884}
1885EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1886
1887static const void *nvmem_cell_read_variable_common(struct device *dev,
1888 const char *cell_id,
1889 size_t max_len, size_t *len)
1890{
1891 struct nvmem_cell *cell;
1892 int nbits;
1893 void *buf;
1894
1895 cell = nvmem_cell_get(dev, cell_id);
1896 if (IS_ERR(cell))
1897 return cell;
1898
1899 nbits = cell->entry->nbits;
1900 buf = nvmem_cell_read(cell, len);
1901 nvmem_cell_put(cell);
1902 if (IS_ERR(buf))
1903 return buf;
1904
1905 /*
1906 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1907 * the length of the real data. Throw away the extra junk.
1908 */
1909 if (nbits)
1910 *len = DIV_ROUND_UP(nbits, 8);
1911
1912 if (*len > max_len) {
1913 kfree(buf);
1914 return ERR_PTR(-ERANGE);
1915 }
1916
1917 return buf;
1918}
1919
1920/**
1921 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1922 *
1923 * @dev: Device that requests the nvmem cell.
1924 * @cell_id: Name of nvmem cell to read.
1925 * @val: pointer to output value.
1926 *
1927 * Return: 0 on success or negative errno.
1928 */
1929int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1930 u32 *val)
1931{
1932 size_t len;
1933 const u8 *buf;
1934 int i;
1935
1936 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1937 if (IS_ERR(buf))
1938 return PTR_ERR(buf);
1939
1940 /* Copy w/ implicit endian conversion */
1941 *val = 0;
1942 for (i = 0; i < len; i++)
1943 *val |= buf[i] << (8 * i);
1944
1945 kfree(buf);
1946
1947 return 0;
1948}
1949EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1950
1951/**
1952 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1953 *
1954 * @dev: Device that requests the nvmem cell.
1955 * @cell_id: Name of nvmem cell to read.
1956 * @val: pointer to output value.
1957 *
1958 * Return: 0 on success or negative errno.
1959 */
1960int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1961 u64 *val)
1962{
1963 size_t len;
1964 const u8 *buf;
1965 int i;
1966
1967 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1968 if (IS_ERR(buf))
1969 return PTR_ERR(buf);
1970
1971 /* Copy w/ implicit endian conversion */
1972 *val = 0;
1973 for (i = 0; i < len; i++)
1974 *val |= (uint64_t)buf[i] << (8 * i);
1975
1976 kfree(buf);
1977
1978 return 0;
1979}
1980EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1981
1982/**
1983 * nvmem_device_cell_read() - Read a given nvmem device and cell
1984 *
1985 * @nvmem: nvmem device to read from.
1986 * @info: nvmem cell info to be read.
1987 * @buf: buffer pointer which will be populated on successful read.
1988 *
1989 * Return: length of successful bytes read on success and negative
1990 * error code on error.
1991 */
1992ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1993 struct nvmem_cell_info *info, void *buf)
1994{
1995 struct nvmem_cell_entry cell;
1996 int rc;
1997 ssize_t len;
1998
1999 if (!nvmem)
2000 return -EINVAL;
2001
2002 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
2003 if (rc)
2004 return rc;
2005
2006 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
2007 if (rc)
2008 return rc;
2009
2010 return len;
2011}
2012EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
2013
2014/**
2015 * nvmem_device_cell_write() - Write cell to a given nvmem device
2016 *
2017 * @nvmem: nvmem device to be written to.
2018 * @info: nvmem cell info to be written.
2019 * @buf: buffer to be written to cell.
2020 *
2021 * Return: length of bytes written or negative error code on failure.
2022 */
2023int nvmem_device_cell_write(struct nvmem_device *nvmem,
2024 struct nvmem_cell_info *info, void *buf)
2025{
2026 struct nvmem_cell_entry cell;
2027 int rc;
2028
2029 if (!nvmem)
2030 return -EINVAL;
2031
2032 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
2033 if (rc)
2034 return rc;
2035
2036 return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
2037}
2038EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
2039
2040/**
2041 * nvmem_device_read() - Read from a given nvmem device
2042 *
2043 * @nvmem: nvmem device to read from.
2044 * @offset: offset in nvmem device.
2045 * @bytes: number of bytes to read.
2046 * @buf: buffer pointer which will be populated on successful read.
2047 *
2048 * Return: length of successful bytes read on success and negative
2049 * error code on error.
2050 */
2051int nvmem_device_read(struct nvmem_device *nvmem,
2052 unsigned int offset,
2053 size_t bytes, void *buf)
2054{
2055 int rc;
2056
2057 if (!nvmem)
2058 return -EINVAL;
2059
2060 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
2061
2062 if (rc)
2063 return rc;
2064
2065 return bytes;
2066}
2067EXPORT_SYMBOL_GPL(nvmem_device_read);
2068
2069/**
2070 * nvmem_device_write() - Write cell to a given nvmem device
2071 *
2072 * @nvmem: nvmem device to be written to.
2073 * @offset: offset in nvmem device.
2074 * @bytes: number of bytes to write.
2075 * @buf: buffer to be written.
2076 *
2077 * Return: length of bytes written or negative error code on failure.
2078 */
2079int nvmem_device_write(struct nvmem_device *nvmem,
2080 unsigned int offset,
2081 size_t bytes, void *buf)
2082{
2083 int rc;
2084
2085 if (!nvmem)
2086 return -EINVAL;
2087
2088 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2089
2090 if (rc)
2091 return rc;
2092
2093
2094 return bytes;
2095}
2096EXPORT_SYMBOL_GPL(nvmem_device_write);
2097
2098/**
2099 * nvmem_add_cell_table() - register a table of cell info entries
2100 *
2101 * @table: table of cell info entries
2102 */
2103void nvmem_add_cell_table(struct nvmem_cell_table *table)
2104{
2105 mutex_lock(&nvmem_cell_mutex);
2106 list_add_tail(&table->node, &nvmem_cell_tables);
2107 mutex_unlock(&nvmem_cell_mutex);
2108}
2109EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
2110
2111/**
2112 * nvmem_del_cell_table() - remove a previously registered cell info table
2113 *
2114 * @table: table of cell info entries
2115 */
2116void nvmem_del_cell_table(struct nvmem_cell_table *table)
2117{
2118 mutex_lock(&nvmem_cell_mutex);
2119 list_del(&table->node);
2120 mutex_unlock(&nvmem_cell_mutex);
2121}
2122EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
2123
2124/**
2125 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2126 *
2127 * @entries: array of cell lookup entries
2128 * @nentries: number of cell lookup entries in the array
2129 */
2130void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2131{
2132 int i;
2133
2134 mutex_lock(&nvmem_lookup_mutex);
2135 for (i = 0; i < nentries; i++)
2136 list_add_tail(&entries[i].node, &nvmem_lookup_list);
2137 mutex_unlock(&nvmem_lookup_mutex);
2138}
2139EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2140
2141/**
2142 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2143 * entries
2144 *
2145 * @entries: array of cell lookup entries
2146 * @nentries: number of cell lookup entries in the array
2147 */
2148void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2149{
2150 int i;
2151
2152 mutex_lock(&nvmem_lookup_mutex);
2153 for (i = 0; i < nentries; i++)
2154 list_del(&entries[i].node);
2155 mutex_unlock(&nvmem_lookup_mutex);
2156}
2157EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2158
2159/**
2160 * nvmem_dev_name() - Get the name of a given nvmem device.
2161 *
2162 * @nvmem: nvmem device.
2163 *
2164 * Return: name of the nvmem device.
2165 */
2166const char *nvmem_dev_name(struct nvmem_device *nvmem)
2167{
2168 return dev_name(&nvmem->dev);
2169}
2170EXPORT_SYMBOL_GPL(nvmem_dev_name);
2171
2172/**
2173 * nvmem_dev_size() - Get the size of a given nvmem device.
2174 *
2175 * @nvmem: nvmem device.
2176 *
2177 * Return: size of the nvmem device.
2178 */
2179size_t nvmem_dev_size(struct nvmem_device *nvmem)
2180{
2181 return nvmem->size;
2182}
2183EXPORT_SYMBOL_GPL(nvmem_dev_size);
2184
2185static int __init nvmem_init(void)
2186{
2187 int ret;
2188
2189 ret = bus_register(&nvmem_bus_type);
2190 if (ret)
2191 return ret;
2192
2193 ret = nvmem_layout_bus_register();
2194 if (ret)
2195 bus_unregister(&nvmem_bus_type);
2196
2197 return ret;
2198}
2199
2200static void __exit nvmem_exit(void)
2201{
2202 nvmem_layout_bus_unregister();
2203 bus_unregister(&nvmem_bus_type);
2204}
2205
2206subsys_initcall(nvmem_init);
2207module_exit(nvmem_exit);
2208
2209MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
2210MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
2211MODULE_DESCRIPTION("nvmem Driver Core");