Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * nvmem framework core.
4 *
5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7 */
8
9#include <linux/device.h>
10#include <linux/export.h>
11#include <linux/fs.h>
12#include <linux/idr.h>
13#include <linux/init.h>
14#include <linux/kref.h>
15#include <linux/module.h>
16#include <linux/nvmem-consumer.h>
17#include <linux/nvmem-provider.h>
18#include <linux/gpio/consumer.h>
19#include <linux/of.h>
20#include <linux/slab.h>
21
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", entry->name,
464 entry->offset);
465 attrs[i].attr.mode = 0444;
466 attrs[i].size = entry->bytes;
467 attrs[i].read = &nvmem_cell_attr_read;
468 attrs[i].private = entry;
469 if (!attrs[i].attr.name) {
470 ret = -ENOMEM;
471 goto unlock_mutex;
472 }
473
474 cells_attrs[i] = &attrs[i];
475 i++;
476 }
477
478 nvmem_cells_group.bin_attrs = cells_attrs;
479
480 ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups);
481 if (ret)
482 goto unlock_mutex;
483
484 nvmem->sysfs_cells_populated = true;
485
486unlock_mutex:
487 mutex_unlock(&nvmem_mutex);
488
489 return ret;
490}
491
492#else /* CONFIG_NVMEM_SYSFS */
493
494static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
495 const struct nvmem_config *config)
496{
497 return -ENOSYS;
498}
499static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
500 const struct nvmem_config *config)
501{
502}
503
504#endif /* CONFIG_NVMEM_SYSFS */
505
506static void nvmem_release(struct device *dev)
507{
508 struct nvmem_device *nvmem = to_nvmem_device(dev);
509
510 ida_free(&nvmem_ida, nvmem->id);
511 gpiod_put(nvmem->wp_gpio);
512 kfree(nvmem);
513}
514
515static const struct device_type nvmem_provider_type = {
516 .release = nvmem_release,
517};
518
519static struct bus_type nvmem_bus_type = {
520 .name = "nvmem",
521};
522
523static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
524{
525 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
526 mutex_lock(&nvmem_mutex);
527 list_del(&cell->node);
528 mutex_unlock(&nvmem_mutex);
529 of_node_put(cell->np);
530 kfree_const(cell->name);
531 kfree(cell);
532}
533
534static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
535{
536 struct nvmem_cell_entry *cell, *p;
537
538 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
539 nvmem_cell_entry_drop(cell);
540}
541
542static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
543{
544 mutex_lock(&nvmem_mutex);
545 list_add_tail(&cell->node, &cell->nvmem->cells);
546 mutex_unlock(&nvmem_mutex);
547 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
548}
549
550static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
551 const struct nvmem_cell_info *info,
552 struct nvmem_cell_entry *cell)
553{
554 cell->nvmem = nvmem;
555 cell->offset = info->offset;
556 cell->raw_len = info->raw_len ?: info->bytes;
557 cell->bytes = info->bytes;
558 cell->name = info->name;
559 cell->read_post_process = info->read_post_process;
560 cell->priv = info->priv;
561
562 cell->bit_offset = info->bit_offset;
563 cell->nbits = info->nbits;
564 cell->np = info->np;
565
566 if (cell->nbits)
567 cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
568 BITS_PER_BYTE);
569
570 if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
571 dev_err(&nvmem->dev,
572 "cell %s unaligned to nvmem stride %d\n",
573 cell->name ?: "<unknown>", nvmem->stride);
574 return -EINVAL;
575 }
576
577 return 0;
578}
579
580static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
581 const struct nvmem_cell_info *info,
582 struct nvmem_cell_entry *cell)
583{
584 int err;
585
586 err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
587 if (err)
588 return err;
589
590 cell->name = kstrdup_const(info->name, GFP_KERNEL);
591 if (!cell->name)
592 return -ENOMEM;
593
594 return 0;
595}
596
597/**
598 * nvmem_add_one_cell() - Add one cell information to an nvmem device
599 *
600 * @nvmem: nvmem device to add cells to.
601 * @info: nvmem cell info to add to the device
602 *
603 * Return: 0 or negative error code on failure.
604 */
605int nvmem_add_one_cell(struct nvmem_device *nvmem,
606 const struct nvmem_cell_info *info)
607{
608 struct nvmem_cell_entry *cell;
609 int rval;
610
611 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
612 if (!cell)
613 return -ENOMEM;
614
615 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
616 if (rval) {
617 kfree(cell);
618 return rval;
619 }
620
621 nvmem_cell_entry_add(cell);
622
623 return 0;
624}
625EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
626
627/**
628 * nvmem_add_cells() - Add cell information to an nvmem device
629 *
630 * @nvmem: nvmem device to add cells to.
631 * @info: nvmem cell info to add to the device
632 * @ncells: number of cells in info
633 *
634 * Return: 0 or negative error code on failure.
635 */
636static int nvmem_add_cells(struct nvmem_device *nvmem,
637 const struct nvmem_cell_info *info,
638 int ncells)
639{
640 int i, rval;
641
642 for (i = 0; i < ncells; i++) {
643 rval = nvmem_add_one_cell(nvmem, &info[i]);
644 if (rval)
645 return rval;
646 }
647
648 return 0;
649}
650
651/**
652 * nvmem_register_notifier() - Register a notifier block for nvmem events.
653 *
654 * @nb: notifier block to be called on nvmem events.
655 *
656 * Return: 0 on success, negative error number on failure.
657 */
658int nvmem_register_notifier(struct notifier_block *nb)
659{
660 return blocking_notifier_chain_register(&nvmem_notifier, nb);
661}
662EXPORT_SYMBOL_GPL(nvmem_register_notifier);
663
664/**
665 * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
666 *
667 * @nb: notifier block to be unregistered.
668 *
669 * Return: 0 on success, negative error number on failure.
670 */
671int nvmem_unregister_notifier(struct notifier_block *nb)
672{
673 return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
674}
675EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
676
677static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
678{
679 const struct nvmem_cell_info *info;
680 struct nvmem_cell_table *table;
681 struct nvmem_cell_entry *cell;
682 int rval = 0, i;
683
684 mutex_lock(&nvmem_cell_mutex);
685 list_for_each_entry(table, &nvmem_cell_tables, node) {
686 if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
687 for (i = 0; i < table->ncells; i++) {
688 info = &table->cells[i];
689
690 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
691 if (!cell) {
692 rval = -ENOMEM;
693 goto out;
694 }
695
696 rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
697 if (rval) {
698 kfree(cell);
699 goto out;
700 }
701
702 nvmem_cell_entry_add(cell);
703 }
704 }
705 }
706
707out:
708 mutex_unlock(&nvmem_cell_mutex);
709 return rval;
710}
711
712static struct nvmem_cell_entry *
713nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
714{
715 struct nvmem_cell_entry *iter, *cell = NULL;
716
717 mutex_lock(&nvmem_mutex);
718 list_for_each_entry(iter, &nvmem->cells, node) {
719 if (strcmp(cell_id, iter->name) == 0) {
720 cell = iter;
721 break;
722 }
723 }
724 mutex_unlock(&nvmem_mutex);
725
726 return cell;
727}
728
729static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
730{
731 unsigned int cur = 0;
732 const struct nvmem_keepout *keepout = nvmem->keepout;
733 const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
734
735 while (keepout < keepoutend) {
736 /* Ensure keepouts are sorted and don't overlap. */
737 if (keepout->start < cur) {
738 dev_err(&nvmem->dev,
739 "Keepout regions aren't sorted or overlap.\n");
740
741 return -ERANGE;
742 }
743
744 if (keepout->end < keepout->start) {
745 dev_err(&nvmem->dev,
746 "Invalid keepout region.\n");
747
748 return -EINVAL;
749 }
750
751 /*
752 * Validate keepouts (and holes between) don't violate
753 * word_size constraints.
754 */
755 if ((keepout->end - keepout->start < nvmem->word_size) ||
756 ((keepout->start != cur) &&
757 (keepout->start - cur < nvmem->word_size))) {
758
759 dev_err(&nvmem->dev,
760 "Keepout regions violate word_size constraints.\n");
761
762 return -ERANGE;
763 }
764
765 /* Validate keepouts don't violate stride (alignment). */
766 if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
767 !IS_ALIGNED(keepout->end, nvmem->stride)) {
768
769 dev_err(&nvmem->dev,
770 "Keepout regions violate stride.\n");
771
772 return -EINVAL;
773 }
774
775 cur = keepout->end;
776 keepout++;
777 }
778
779 return 0;
780}
781
782static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
783{
784 struct device *dev = &nvmem->dev;
785 struct device_node *child;
786 const __be32 *addr;
787 int len, ret;
788
789 for_each_child_of_node(np, child) {
790 struct nvmem_cell_info info = {0};
791
792 addr = of_get_property(child, "reg", &len);
793 if (!addr)
794 continue;
795 if (len < 2 * sizeof(u32)) {
796 dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
797 of_node_put(child);
798 return -EINVAL;
799 }
800
801 info.offset = be32_to_cpup(addr++);
802 info.bytes = be32_to_cpup(addr);
803 info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
804
805 addr = of_get_property(child, "bits", &len);
806 if (addr && len == (2 * sizeof(u32))) {
807 info.bit_offset = be32_to_cpup(addr++);
808 info.nbits = be32_to_cpup(addr);
809 }
810
811 info.np = of_node_get(child);
812
813 if (nvmem->fixup_dt_cell_info)
814 nvmem->fixup_dt_cell_info(nvmem, &info);
815
816 ret = nvmem_add_one_cell(nvmem, &info);
817 kfree(info.name);
818 if (ret) {
819 of_node_put(child);
820 return ret;
821 }
822 }
823
824 return 0;
825}
826
827static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
828{
829 return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
830}
831
832static int nvmem_add_cells_from_fixed_layout(struct nvmem_device *nvmem)
833{
834 struct device_node *layout_np;
835 int err = 0;
836
837 layout_np = of_nvmem_layout_get_container(nvmem);
838 if (!layout_np)
839 return 0;
840
841 if (of_device_is_compatible(layout_np, "fixed-layout"))
842 err = nvmem_add_cells_from_dt(nvmem, layout_np);
843
844 of_node_put(layout_np);
845
846 return err;
847}
848
849int nvmem_layout_register(struct nvmem_layout *layout)
850{
851 int ret;
852
853 if (!layout->add_cells)
854 return -EINVAL;
855
856 /* Populate the cells */
857 ret = layout->add_cells(layout);
858 if (ret)
859 return ret;
860
861#ifdef CONFIG_NVMEM_SYSFS
862 ret = nvmem_populate_sysfs_cells(layout->nvmem);
863 if (ret) {
864 nvmem_device_remove_all_cells(layout->nvmem);
865 return ret;
866 }
867#endif
868
869 return 0;
870}
871EXPORT_SYMBOL_GPL(nvmem_layout_register);
872
873void nvmem_layout_unregister(struct nvmem_layout *layout)
874{
875 /* Keep the API even with an empty stub in case we need it later */
876}
877EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
878
879/**
880 * nvmem_register() - Register a nvmem device for given nvmem_config.
881 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
882 *
883 * @config: nvmem device configuration with which nvmem device is created.
884 *
885 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
886 * on success.
887 */
888
889struct nvmem_device *nvmem_register(const struct nvmem_config *config)
890{
891 struct nvmem_device *nvmem;
892 int rval;
893
894 if (!config->dev)
895 return ERR_PTR(-EINVAL);
896
897 if (!config->reg_read && !config->reg_write)
898 return ERR_PTR(-EINVAL);
899
900 nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
901 if (!nvmem)
902 return ERR_PTR(-ENOMEM);
903
904 rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
905 if (rval < 0) {
906 kfree(nvmem);
907 return ERR_PTR(rval);
908 }
909
910 nvmem->id = rval;
911
912 nvmem->dev.type = &nvmem_provider_type;
913 nvmem->dev.bus = &nvmem_bus_type;
914 nvmem->dev.parent = config->dev;
915
916 device_initialize(&nvmem->dev);
917
918 if (!config->ignore_wp)
919 nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
920 GPIOD_OUT_HIGH);
921 if (IS_ERR(nvmem->wp_gpio)) {
922 rval = PTR_ERR(nvmem->wp_gpio);
923 nvmem->wp_gpio = NULL;
924 goto err_put_device;
925 }
926
927 kref_init(&nvmem->refcnt);
928 INIT_LIST_HEAD(&nvmem->cells);
929 nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
930
931 nvmem->owner = config->owner;
932 if (!nvmem->owner && config->dev->driver)
933 nvmem->owner = config->dev->driver->owner;
934 nvmem->stride = config->stride ?: 1;
935 nvmem->word_size = config->word_size ?: 1;
936 nvmem->size = config->size;
937 nvmem->root_only = config->root_only;
938 nvmem->priv = config->priv;
939 nvmem->type = config->type;
940 nvmem->reg_read = config->reg_read;
941 nvmem->reg_write = config->reg_write;
942 nvmem->keepout = config->keepout;
943 nvmem->nkeepout = config->nkeepout;
944 if (config->of_node)
945 nvmem->dev.of_node = config->of_node;
946 else
947 nvmem->dev.of_node = config->dev->of_node;
948
949 switch (config->id) {
950 case NVMEM_DEVID_NONE:
951 rval = dev_set_name(&nvmem->dev, "%s", config->name);
952 break;
953 case NVMEM_DEVID_AUTO:
954 rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
955 break;
956 default:
957 rval = dev_set_name(&nvmem->dev, "%s%d",
958 config->name ? : "nvmem",
959 config->name ? config->id : nvmem->id);
960 break;
961 }
962
963 if (rval)
964 goto err_put_device;
965
966 nvmem->read_only = device_property_present(config->dev, "read-only") ||
967 config->read_only || !nvmem->reg_write;
968
969#ifdef CONFIG_NVMEM_SYSFS
970 nvmem->dev.groups = nvmem_dev_groups;
971#endif
972
973 if (nvmem->nkeepout) {
974 rval = nvmem_validate_keepouts(nvmem);
975 if (rval)
976 goto err_put_device;
977 }
978
979 if (config->compat) {
980 rval = nvmem_sysfs_setup_compat(nvmem, config);
981 if (rval)
982 goto err_put_device;
983 }
984
985 if (config->cells) {
986 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
987 if (rval)
988 goto err_remove_cells;
989 }
990
991 rval = nvmem_add_cells_from_table(nvmem);
992 if (rval)
993 goto err_remove_cells;
994
995 if (config->add_legacy_fixed_of_cells) {
996 rval = nvmem_add_cells_from_legacy_of(nvmem);
997 if (rval)
998 goto err_remove_cells;
999 }
1000
1001 rval = nvmem_add_cells_from_fixed_layout(nvmem);
1002 if (rval)
1003 goto err_remove_cells;
1004
1005 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
1006
1007 rval = device_add(&nvmem->dev);
1008 if (rval)
1009 goto err_remove_cells;
1010
1011 rval = nvmem_populate_layout(nvmem);
1012 if (rval)
1013 goto err_remove_dev;
1014
1015#ifdef CONFIG_NVMEM_SYSFS
1016 rval = nvmem_populate_sysfs_cells(nvmem);
1017 if (rval)
1018 goto err_destroy_layout;
1019#endif
1020
1021 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
1022
1023 return nvmem;
1024
1025#ifdef CONFIG_NVMEM_SYSFS
1026err_destroy_layout:
1027 nvmem_destroy_layout(nvmem);
1028#endif
1029err_remove_dev:
1030 device_del(&nvmem->dev);
1031err_remove_cells:
1032 nvmem_device_remove_all_cells(nvmem);
1033 if (config->compat)
1034 nvmem_sysfs_remove_compat(nvmem, config);
1035err_put_device:
1036 put_device(&nvmem->dev);
1037
1038 return ERR_PTR(rval);
1039}
1040EXPORT_SYMBOL_GPL(nvmem_register);
1041
1042static void nvmem_device_release(struct kref *kref)
1043{
1044 struct nvmem_device *nvmem;
1045
1046 nvmem = container_of(kref, struct nvmem_device, refcnt);
1047
1048 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
1049
1050 if (nvmem->flags & FLAG_COMPAT)
1051 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
1052
1053 nvmem_device_remove_all_cells(nvmem);
1054 nvmem_destroy_layout(nvmem);
1055 device_unregister(&nvmem->dev);
1056}
1057
1058/**
1059 * nvmem_unregister() - Unregister previously registered nvmem device
1060 *
1061 * @nvmem: Pointer to previously registered nvmem device.
1062 */
1063void nvmem_unregister(struct nvmem_device *nvmem)
1064{
1065 if (nvmem)
1066 kref_put(&nvmem->refcnt, nvmem_device_release);
1067}
1068EXPORT_SYMBOL_GPL(nvmem_unregister);
1069
1070static void devm_nvmem_unregister(void *nvmem)
1071{
1072 nvmem_unregister(nvmem);
1073}
1074
1075/**
1076 * devm_nvmem_register() - Register a managed nvmem device for given
1077 * nvmem_config.
1078 * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
1079 *
1080 * @dev: Device that uses the nvmem device.
1081 * @config: nvmem device configuration with which nvmem device is created.
1082 *
1083 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
1084 * on success.
1085 */
1086struct nvmem_device *devm_nvmem_register(struct device *dev,
1087 const struct nvmem_config *config)
1088{
1089 struct nvmem_device *nvmem;
1090 int ret;
1091
1092 nvmem = nvmem_register(config);
1093 if (IS_ERR(nvmem))
1094 return nvmem;
1095
1096 ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
1097 if (ret)
1098 return ERR_PTR(ret);
1099
1100 return nvmem;
1101}
1102EXPORT_SYMBOL_GPL(devm_nvmem_register);
1103
1104static struct nvmem_device *__nvmem_device_get(void *data,
1105 int (*match)(struct device *dev, const void *data))
1106{
1107 struct nvmem_device *nvmem = NULL;
1108 struct device *dev;
1109
1110 mutex_lock(&nvmem_mutex);
1111 dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
1112 if (dev)
1113 nvmem = to_nvmem_device(dev);
1114 mutex_unlock(&nvmem_mutex);
1115 if (!nvmem)
1116 return ERR_PTR(-EPROBE_DEFER);
1117
1118 if (!try_module_get(nvmem->owner)) {
1119 dev_err(&nvmem->dev,
1120 "could not increase module refcount for cell %s\n",
1121 nvmem_dev_name(nvmem));
1122
1123 put_device(&nvmem->dev);
1124 return ERR_PTR(-EINVAL);
1125 }
1126
1127 kref_get(&nvmem->refcnt);
1128
1129 return nvmem;
1130}
1131
1132static void __nvmem_device_put(struct nvmem_device *nvmem)
1133{
1134 put_device(&nvmem->dev);
1135 module_put(nvmem->owner);
1136 kref_put(&nvmem->refcnt, nvmem_device_release);
1137}
1138
1139#if IS_ENABLED(CONFIG_OF)
1140/**
1141 * of_nvmem_device_get() - Get nvmem device from a given id
1142 *
1143 * @np: Device tree node that uses the nvmem device.
1144 * @id: nvmem name from nvmem-names property.
1145 *
1146 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1147 * on success.
1148 */
1149struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
1150{
1151
1152 struct device_node *nvmem_np;
1153 struct nvmem_device *nvmem;
1154 int index = 0;
1155
1156 if (id)
1157 index = of_property_match_string(np, "nvmem-names", id);
1158
1159 nvmem_np = of_parse_phandle(np, "nvmem", index);
1160 if (!nvmem_np)
1161 return ERR_PTR(-ENOENT);
1162
1163 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1164 of_node_put(nvmem_np);
1165 return nvmem;
1166}
1167EXPORT_SYMBOL_GPL(of_nvmem_device_get);
1168#endif
1169
1170/**
1171 * nvmem_device_get() - Get nvmem device from a given id
1172 *
1173 * @dev: Device that uses the nvmem device.
1174 * @dev_name: name of the requested nvmem device.
1175 *
1176 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1177 * on success.
1178 */
1179struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
1180{
1181 if (dev->of_node) { /* try dt first */
1182 struct nvmem_device *nvmem;
1183
1184 nvmem = of_nvmem_device_get(dev->of_node, dev_name);
1185
1186 if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
1187 return nvmem;
1188
1189 }
1190
1191 return __nvmem_device_get((void *)dev_name, device_match_name);
1192}
1193EXPORT_SYMBOL_GPL(nvmem_device_get);
1194
1195/**
1196 * nvmem_device_find() - Find nvmem device with matching function
1197 *
1198 * @data: Data to pass to match function
1199 * @match: Callback function to check device
1200 *
1201 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
1202 * on success.
1203 */
1204struct nvmem_device *nvmem_device_find(void *data,
1205 int (*match)(struct device *dev, const void *data))
1206{
1207 return __nvmem_device_get(data, match);
1208}
1209EXPORT_SYMBOL_GPL(nvmem_device_find);
1210
1211static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
1212{
1213 struct nvmem_device **nvmem = res;
1214
1215 if (WARN_ON(!nvmem || !*nvmem))
1216 return 0;
1217
1218 return *nvmem == data;
1219}
1220
1221static void devm_nvmem_device_release(struct device *dev, void *res)
1222{
1223 nvmem_device_put(*(struct nvmem_device **)res);
1224}
1225
1226/**
1227 * devm_nvmem_device_put() - put alredy got nvmem device
1228 *
1229 * @dev: Device that uses the nvmem device.
1230 * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
1231 * that needs to be released.
1232 */
1233void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
1234{
1235 int ret;
1236
1237 ret = devres_release(dev, devm_nvmem_device_release,
1238 devm_nvmem_device_match, nvmem);
1239
1240 WARN_ON(ret);
1241}
1242EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
1243
1244/**
1245 * nvmem_device_put() - put alredy got nvmem device
1246 *
1247 * @nvmem: pointer to nvmem device that needs to be released.
1248 */
1249void nvmem_device_put(struct nvmem_device *nvmem)
1250{
1251 __nvmem_device_put(nvmem);
1252}
1253EXPORT_SYMBOL_GPL(nvmem_device_put);
1254
1255/**
1256 * devm_nvmem_device_get() - Get nvmem cell of device form a given id
1257 *
1258 * @dev: Device that requests the nvmem device.
1259 * @id: name id for the requested nvmem device.
1260 *
1261 * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
1262 * on success. The nvmem_cell will be freed by the automatically once the
1263 * device is freed.
1264 */
1265struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
1266{
1267 struct nvmem_device **ptr, *nvmem;
1268
1269 ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
1270 if (!ptr)
1271 return ERR_PTR(-ENOMEM);
1272
1273 nvmem = nvmem_device_get(dev, id);
1274 if (!IS_ERR(nvmem)) {
1275 *ptr = nvmem;
1276 devres_add(dev, ptr);
1277 } else {
1278 devres_free(ptr);
1279 }
1280
1281 return nvmem;
1282}
1283EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
1284
1285static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
1286 const char *id, int index)
1287{
1288 struct nvmem_cell *cell;
1289 const char *name = NULL;
1290
1291 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
1292 if (!cell)
1293 return ERR_PTR(-ENOMEM);
1294
1295 if (id) {
1296 name = kstrdup_const(id, GFP_KERNEL);
1297 if (!name) {
1298 kfree(cell);
1299 return ERR_PTR(-ENOMEM);
1300 }
1301 }
1302
1303 cell->id = name;
1304 cell->entry = entry;
1305 cell->index = index;
1306
1307 return cell;
1308}
1309
1310static struct nvmem_cell *
1311nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
1312{
1313 struct nvmem_cell_entry *cell_entry;
1314 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
1315 struct nvmem_cell_lookup *lookup;
1316 struct nvmem_device *nvmem;
1317 const char *dev_id;
1318
1319 if (!dev)
1320 return ERR_PTR(-EINVAL);
1321
1322 dev_id = dev_name(dev);
1323
1324 mutex_lock(&nvmem_lookup_mutex);
1325
1326 list_for_each_entry(lookup, &nvmem_lookup_list, node) {
1327 if ((strcmp(lookup->dev_id, dev_id) == 0) &&
1328 (strcmp(lookup->con_id, con_id) == 0)) {
1329 /* This is the right entry. */
1330 nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
1331 device_match_name);
1332 if (IS_ERR(nvmem)) {
1333 /* Provider may not be registered yet. */
1334 cell = ERR_CAST(nvmem);
1335 break;
1336 }
1337
1338 cell_entry = nvmem_find_cell_entry_by_name(nvmem,
1339 lookup->cell_name);
1340 if (!cell_entry) {
1341 __nvmem_device_put(nvmem);
1342 cell = ERR_PTR(-ENOENT);
1343 } else {
1344 cell = nvmem_create_cell(cell_entry, con_id, 0);
1345 if (IS_ERR(cell))
1346 __nvmem_device_put(nvmem);
1347 }
1348 break;
1349 }
1350 }
1351
1352 mutex_unlock(&nvmem_lookup_mutex);
1353 return cell;
1354}
1355
1356static void nvmem_layout_module_put(struct nvmem_device *nvmem)
1357{
1358 if (nvmem->layout && nvmem->layout->dev.driver)
1359 module_put(nvmem->layout->dev.driver->owner);
1360}
1361
1362#if IS_ENABLED(CONFIG_OF)
1363static struct nvmem_cell_entry *
1364nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
1365{
1366 struct nvmem_cell_entry *iter, *cell = NULL;
1367
1368 mutex_lock(&nvmem_mutex);
1369 list_for_each_entry(iter, &nvmem->cells, node) {
1370 if (np == iter->np) {
1371 cell = iter;
1372 break;
1373 }
1374 }
1375 mutex_unlock(&nvmem_mutex);
1376
1377 return cell;
1378}
1379
1380static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
1381{
1382 if (!nvmem->layout)
1383 return 0;
1384
1385 if (!nvmem->layout->dev.driver ||
1386 !try_module_get(nvmem->layout->dev.driver->owner))
1387 return -EPROBE_DEFER;
1388
1389 return 0;
1390}
1391
1392/**
1393 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
1394 *
1395 * @np: Device tree node that uses the nvmem cell.
1396 * @id: nvmem cell name from nvmem-cell-names property, or NULL
1397 * for the cell at index 0 (the lone cell with no accompanying
1398 * nvmem-cell-names property).
1399 *
1400 * Return: Will be an ERR_PTR() on error or a valid pointer
1401 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1402 * nvmem_cell_put().
1403 */
1404struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
1405{
1406 struct device_node *cell_np, *nvmem_np;
1407 struct nvmem_device *nvmem;
1408 struct nvmem_cell_entry *cell_entry;
1409 struct nvmem_cell *cell;
1410 struct of_phandle_args cell_spec;
1411 int index = 0;
1412 int cell_index = 0;
1413 int ret;
1414
1415 /* if cell name exists, find index to the name */
1416 if (id)
1417 index = of_property_match_string(np, "nvmem-cell-names", id);
1418
1419 ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
1420 "#nvmem-cell-cells",
1421 index, &cell_spec);
1422 if (ret)
1423 return ERR_PTR(-ENOENT);
1424
1425 if (cell_spec.args_count > 1)
1426 return ERR_PTR(-EINVAL);
1427
1428 cell_np = cell_spec.np;
1429 if (cell_spec.args_count)
1430 cell_index = cell_spec.args[0];
1431
1432 nvmem_np = of_get_parent(cell_np);
1433 if (!nvmem_np) {
1434 of_node_put(cell_np);
1435 return ERR_PTR(-EINVAL);
1436 }
1437
1438 /* nvmem layouts produce cells within the nvmem-layout container */
1439 if (of_node_name_eq(nvmem_np, "nvmem-layout")) {
1440 nvmem_np = of_get_next_parent(nvmem_np);
1441 if (!nvmem_np) {
1442 of_node_put(cell_np);
1443 return ERR_PTR(-EINVAL);
1444 }
1445 }
1446
1447 nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
1448 of_node_put(nvmem_np);
1449 if (IS_ERR(nvmem)) {
1450 of_node_put(cell_np);
1451 return ERR_CAST(nvmem);
1452 }
1453
1454 ret = nvmem_layout_module_get_optional(nvmem);
1455 if (ret) {
1456 of_node_put(cell_np);
1457 __nvmem_device_put(nvmem);
1458 return ERR_PTR(ret);
1459 }
1460
1461 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
1462 of_node_put(cell_np);
1463 if (!cell_entry) {
1464 __nvmem_device_put(nvmem);
1465 nvmem_layout_module_put(nvmem);
1466 if (nvmem->layout)
1467 return ERR_PTR(-EPROBE_DEFER);
1468 else
1469 return ERR_PTR(-ENOENT);
1470 }
1471
1472 cell = nvmem_create_cell(cell_entry, id, cell_index);
1473 if (IS_ERR(cell)) {
1474 __nvmem_device_put(nvmem);
1475 nvmem_layout_module_put(nvmem);
1476 }
1477
1478 return cell;
1479}
1480EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
1481#endif
1482
1483/**
1484 * nvmem_cell_get() - Get nvmem cell of device form a given cell name
1485 *
1486 * @dev: Device that requests the nvmem cell.
1487 * @id: nvmem cell name to get (this corresponds with the name from the
1488 * nvmem-cell-names property for DT systems and with the con_id from
1489 * the lookup entry for non-DT systems).
1490 *
1491 * Return: Will be an ERR_PTR() on error or a valid pointer
1492 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1493 * nvmem_cell_put().
1494 */
1495struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
1496{
1497 struct nvmem_cell *cell;
1498
1499 if (dev->of_node) { /* try dt first */
1500 cell = of_nvmem_cell_get(dev->of_node, id);
1501 if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
1502 return cell;
1503 }
1504
1505 /* NULL cell id only allowed for device tree; invalid otherwise */
1506 if (!id)
1507 return ERR_PTR(-EINVAL);
1508
1509 return nvmem_cell_get_from_lookup(dev, id);
1510}
1511EXPORT_SYMBOL_GPL(nvmem_cell_get);
1512
1513static void devm_nvmem_cell_release(struct device *dev, void *res)
1514{
1515 nvmem_cell_put(*(struct nvmem_cell **)res);
1516}
1517
1518/**
1519 * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
1520 *
1521 * @dev: Device that requests the nvmem cell.
1522 * @id: nvmem cell name id to get.
1523 *
1524 * Return: Will be an ERR_PTR() on error or a valid pointer
1525 * to a struct nvmem_cell. The nvmem_cell will be freed by the
1526 * automatically once the device is freed.
1527 */
1528struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
1529{
1530 struct nvmem_cell **ptr, *cell;
1531
1532 ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
1533 if (!ptr)
1534 return ERR_PTR(-ENOMEM);
1535
1536 cell = nvmem_cell_get(dev, id);
1537 if (!IS_ERR(cell)) {
1538 *ptr = cell;
1539 devres_add(dev, ptr);
1540 } else {
1541 devres_free(ptr);
1542 }
1543
1544 return cell;
1545}
1546EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
1547
1548static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
1549{
1550 struct nvmem_cell **c = res;
1551
1552 if (WARN_ON(!c || !*c))
1553 return 0;
1554
1555 return *c == data;
1556}
1557
1558/**
1559 * devm_nvmem_cell_put() - Release previously allocated nvmem cell
1560 * from devm_nvmem_cell_get.
1561 *
1562 * @dev: Device that requests the nvmem cell.
1563 * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
1564 */
1565void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
1566{
1567 int ret;
1568
1569 ret = devres_release(dev, devm_nvmem_cell_release,
1570 devm_nvmem_cell_match, cell);
1571
1572 WARN_ON(ret);
1573}
1574EXPORT_SYMBOL(devm_nvmem_cell_put);
1575
1576/**
1577 * nvmem_cell_put() - Release previously allocated nvmem cell.
1578 *
1579 * @cell: Previously allocated nvmem cell by nvmem_cell_get().
1580 */
1581void nvmem_cell_put(struct nvmem_cell *cell)
1582{
1583 struct nvmem_device *nvmem = cell->entry->nvmem;
1584
1585 if (cell->id)
1586 kfree_const(cell->id);
1587
1588 kfree(cell);
1589 __nvmem_device_put(nvmem);
1590 nvmem_layout_module_put(nvmem);
1591}
1592EXPORT_SYMBOL_GPL(nvmem_cell_put);
1593
1594static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
1595{
1596 u8 *p, *b;
1597 int i, extra, bit_offset = cell->bit_offset;
1598
1599 p = b = buf;
1600 if (bit_offset) {
1601 /* First shift */
1602 *b++ >>= bit_offset;
1603
1604 /* setup rest of the bytes if any */
1605 for (i = 1; i < cell->bytes; i++) {
1606 /* Get bits from next byte and shift them towards msb */
1607 *p |= *b << (BITS_PER_BYTE - bit_offset);
1608
1609 p = b;
1610 *b++ >>= bit_offset;
1611 }
1612 } else {
1613 /* point to the msb */
1614 p += cell->bytes - 1;
1615 }
1616
1617 /* result fits in less bytes */
1618 extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
1619 while (--extra >= 0)
1620 *p-- = 0;
1621
1622 /* clear msb bits if any leftover in the last byte */
1623 if (cell->nbits % BITS_PER_BYTE)
1624 *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
1625}
1626
1627static int __nvmem_cell_read(struct nvmem_device *nvmem,
1628 struct nvmem_cell_entry *cell,
1629 void *buf, size_t *len, const char *id, int index)
1630{
1631 int rc;
1632
1633 rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
1634
1635 if (rc)
1636 return rc;
1637
1638 /* shift bits in-place */
1639 if (cell->bit_offset || cell->nbits)
1640 nvmem_shift_read_buffer_in_place(cell, buf);
1641
1642 if (cell->read_post_process) {
1643 rc = cell->read_post_process(cell->priv, id, index,
1644 cell->offset, buf, cell->raw_len);
1645 if (rc)
1646 return rc;
1647 }
1648
1649 if (len)
1650 *len = cell->bytes;
1651
1652 return 0;
1653}
1654
1655/**
1656 * nvmem_cell_read() - Read a given nvmem cell
1657 *
1658 * @cell: nvmem cell to be read.
1659 * @len: pointer to length of cell which will be populated on successful read;
1660 * can be NULL.
1661 *
1662 * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
1663 * buffer should be freed by the consumer with a kfree().
1664 */
1665void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
1666{
1667 struct nvmem_cell_entry *entry = cell->entry;
1668 struct nvmem_device *nvmem = entry->nvmem;
1669 u8 *buf;
1670 int rc;
1671
1672 if (!nvmem)
1673 return ERR_PTR(-EINVAL);
1674
1675 buf = kzalloc(max_t(size_t, entry->raw_len, entry->bytes), GFP_KERNEL);
1676 if (!buf)
1677 return ERR_PTR(-ENOMEM);
1678
1679 rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
1680 if (rc) {
1681 kfree(buf);
1682 return ERR_PTR(rc);
1683 }
1684
1685 return buf;
1686}
1687EXPORT_SYMBOL_GPL(nvmem_cell_read);
1688
1689static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
1690 u8 *_buf, int len)
1691{
1692 struct nvmem_device *nvmem = cell->nvmem;
1693 int i, rc, nbits, bit_offset = cell->bit_offset;
1694 u8 v, *p, *buf, *b, pbyte, pbits;
1695
1696 nbits = cell->nbits;
1697 buf = kzalloc(cell->bytes, GFP_KERNEL);
1698 if (!buf)
1699 return ERR_PTR(-ENOMEM);
1700
1701 memcpy(buf, _buf, len);
1702 p = b = buf;
1703
1704 if (bit_offset) {
1705 pbyte = *b;
1706 *b <<= bit_offset;
1707
1708 /* setup the first byte with lsb bits from nvmem */
1709 rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
1710 if (rc)
1711 goto err;
1712 *b++ |= GENMASK(bit_offset - 1, 0) & v;
1713
1714 /* setup rest of the byte if any */
1715 for (i = 1; i < cell->bytes; i++) {
1716 /* Get last byte bits and shift them towards lsb */
1717 pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
1718 pbyte = *b;
1719 p = b;
1720 *b <<= bit_offset;
1721 *b++ |= pbits;
1722 }
1723 }
1724
1725 /* if it's not end on byte boundary */
1726 if ((nbits + bit_offset) % BITS_PER_BYTE) {
1727 /* setup the last byte with msb bits from nvmem */
1728 rc = nvmem_reg_read(nvmem,
1729 cell->offset + cell->bytes - 1, &v, 1);
1730 if (rc)
1731 goto err;
1732 *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
1733
1734 }
1735
1736 return buf;
1737err:
1738 kfree(buf);
1739 return ERR_PTR(rc);
1740}
1741
1742static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
1743{
1744 struct nvmem_device *nvmem = cell->nvmem;
1745 int rc;
1746
1747 if (!nvmem || nvmem->read_only ||
1748 (cell->bit_offset == 0 && len != cell->bytes))
1749 return -EINVAL;
1750
1751 /*
1752 * Any cells which have a read_post_process hook are read-only because
1753 * we cannot reverse the operation and it might affect other cells,
1754 * too.
1755 */
1756 if (cell->read_post_process)
1757 return -EINVAL;
1758
1759 if (cell->bit_offset || cell->nbits) {
1760 buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
1761 if (IS_ERR(buf))
1762 return PTR_ERR(buf);
1763 }
1764
1765 rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
1766
1767 /* free the tmp buffer */
1768 if (cell->bit_offset || cell->nbits)
1769 kfree(buf);
1770
1771 if (rc)
1772 return rc;
1773
1774 return len;
1775}
1776
1777/**
1778 * nvmem_cell_write() - Write to a given nvmem cell
1779 *
1780 * @cell: nvmem cell to be written.
1781 * @buf: Buffer to be written.
1782 * @len: length of buffer to be written to nvmem cell.
1783 *
1784 * Return: length of bytes written or negative on failure.
1785 */
1786int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
1787{
1788 return __nvmem_cell_entry_write(cell->entry, buf, len);
1789}
1790
1791EXPORT_SYMBOL_GPL(nvmem_cell_write);
1792
1793static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
1794 void *val, size_t count)
1795{
1796 struct nvmem_cell *cell;
1797 void *buf;
1798 size_t len;
1799
1800 cell = nvmem_cell_get(dev, cell_id);
1801 if (IS_ERR(cell))
1802 return PTR_ERR(cell);
1803
1804 buf = nvmem_cell_read(cell, &len);
1805 if (IS_ERR(buf)) {
1806 nvmem_cell_put(cell);
1807 return PTR_ERR(buf);
1808 }
1809 if (len != count) {
1810 kfree(buf);
1811 nvmem_cell_put(cell);
1812 return -EINVAL;
1813 }
1814 memcpy(val, buf, count);
1815 kfree(buf);
1816 nvmem_cell_put(cell);
1817
1818 return 0;
1819}
1820
1821/**
1822 * nvmem_cell_read_u8() - Read a cell value as a u8
1823 *
1824 * @dev: Device that requests the nvmem cell.
1825 * @cell_id: Name of nvmem cell to read.
1826 * @val: pointer to output value.
1827 *
1828 * Return: 0 on success or negative errno.
1829 */
1830int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
1831{
1832 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1833}
1834EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
1835
1836/**
1837 * nvmem_cell_read_u16() - Read a cell value as a u16
1838 *
1839 * @dev: Device that requests the nvmem cell.
1840 * @cell_id: Name of nvmem cell to read.
1841 * @val: pointer to output value.
1842 *
1843 * Return: 0 on success or negative errno.
1844 */
1845int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
1846{
1847 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1848}
1849EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
1850
1851/**
1852 * nvmem_cell_read_u32() - Read a cell value as a u32
1853 *
1854 * @dev: Device that requests the nvmem cell.
1855 * @cell_id: Name of nvmem cell to read.
1856 * @val: pointer to output value.
1857 *
1858 * Return: 0 on success or negative errno.
1859 */
1860int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
1861{
1862 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1863}
1864EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
1865
1866/**
1867 * nvmem_cell_read_u64() - Read a cell value as a u64
1868 *
1869 * @dev: Device that requests the nvmem cell.
1870 * @cell_id: Name of nvmem cell to read.
1871 * @val: pointer to output value.
1872 *
1873 * Return: 0 on success or negative errno.
1874 */
1875int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
1876{
1877 return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
1878}
1879EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
1880
1881static const void *nvmem_cell_read_variable_common(struct device *dev,
1882 const char *cell_id,
1883 size_t max_len, size_t *len)
1884{
1885 struct nvmem_cell *cell;
1886 int nbits;
1887 void *buf;
1888
1889 cell = nvmem_cell_get(dev, cell_id);
1890 if (IS_ERR(cell))
1891 return cell;
1892
1893 nbits = cell->entry->nbits;
1894 buf = nvmem_cell_read(cell, len);
1895 nvmem_cell_put(cell);
1896 if (IS_ERR(buf))
1897 return buf;
1898
1899 /*
1900 * If nbits is set then nvmem_cell_read() can significantly exaggerate
1901 * the length of the real data. Throw away the extra junk.
1902 */
1903 if (nbits)
1904 *len = DIV_ROUND_UP(nbits, 8);
1905
1906 if (*len > max_len) {
1907 kfree(buf);
1908 return ERR_PTR(-ERANGE);
1909 }
1910
1911 return buf;
1912}
1913
1914/**
1915 * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
1916 *
1917 * @dev: Device that requests the nvmem cell.
1918 * @cell_id: Name of nvmem cell to read.
1919 * @val: pointer to output value.
1920 *
1921 * Return: 0 on success or negative errno.
1922 */
1923int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
1924 u32 *val)
1925{
1926 size_t len;
1927 const u8 *buf;
1928 int i;
1929
1930 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1931 if (IS_ERR(buf))
1932 return PTR_ERR(buf);
1933
1934 /* Copy w/ implicit endian conversion */
1935 *val = 0;
1936 for (i = 0; i < len; i++)
1937 *val |= buf[i] << (8 * i);
1938
1939 kfree(buf);
1940
1941 return 0;
1942}
1943EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
1944
1945/**
1946 * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
1947 *
1948 * @dev: Device that requests the nvmem cell.
1949 * @cell_id: Name of nvmem cell to read.
1950 * @val: pointer to output value.
1951 *
1952 * Return: 0 on success or negative errno.
1953 */
1954int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
1955 u64 *val)
1956{
1957 size_t len;
1958 const u8 *buf;
1959 int i;
1960
1961 buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
1962 if (IS_ERR(buf))
1963 return PTR_ERR(buf);
1964
1965 /* Copy w/ implicit endian conversion */
1966 *val = 0;
1967 for (i = 0; i < len; i++)
1968 *val |= (uint64_t)buf[i] << (8 * i);
1969
1970 kfree(buf);
1971
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
1975
1976/**
1977 * nvmem_device_cell_read() - Read a given nvmem device and cell
1978 *
1979 * @nvmem: nvmem device to read from.
1980 * @info: nvmem cell info to be read.
1981 * @buf: buffer pointer which will be populated on successful read.
1982 *
1983 * Return: length of successful bytes read on success and negative
1984 * error code on error.
1985 */
1986ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
1987 struct nvmem_cell_info *info, void *buf)
1988{
1989 struct nvmem_cell_entry cell;
1990 int rc;
1991 ssize_t len;
1992
1993 if (!nvmem)
1994 return -EINVAL;
1995
1996 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
1997 if (rc)
1998 return rc;
1999
2000 rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
2001 if (rc)
2002 return rc;
2003
2004 return len;
2005}
2006EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
2007
2008/**
2009 * nvmem_device_cell_write() - Write cell to a given nvmem device
2010 *
2011 * @nvmem: nvmem device to be written to.
2012 * @info: nvmem cell info to be written.
2013 * @buf: buffer to be written to cell.
2014 *
2015 * Return: length of bytes written or negative error code on failure.
2016 */
2017int nvmem_device_cell_write(struct nvmem_device *nvmem,
2018 struct nvmem_cell_info *info, void *buf)
2019{
2020 struct nvmem_cell_entry cell;
2021 int rc;
2022
2023 if (!nvmem)
2024 return -EINVAL;
2025
2026 rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
2027 if (rc)
2028 return rc;
2029
2030 return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
2031}
2032EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
2033
2034/**
2035 * nvmem_device_read() - Read from a given nvmem device
2036 *
2037 * @nvmem: nvmem device to read from.
2038 * @offset: offset in nvmem device.
2039 * @bytes: number of bytes to read.
2040 * @buf: buffer pointer which will be populated on successful read.
2041 *
2042 * Return: length of successful bytes read on success and negative
2043 * error code on error.
2044 */
2045int nvmem_device_read(struct nvmem_device *nvmem,
2046 unsigned int offset,
2047 size_t bytes, void *buf)
2048{
2049 int rc;
2050
2051 if (!nvmem)
2052 return -EINVAL;
2053
2054 rc = nvmem_reg_read(nvmem, offset, buf, bytes);
2055
2056 if (rc)
2057 return rc;
2058
2059 return bytes;
2060}
2061EXPORT_SYMBOL_GPL(nvmem_device_read);
2062
2063/**
2064 * nvmem_device_write() - Write cell to a given nvmem device
2065 *
2066 * @nvmem: nvmem device to be written to.
2067 * @offset: offset in nvmem device.
2068 * @bytes: number of bytes to write.
2069 * @buf: buffer to be written.
2070 *
2071 * Return: length of bytes written or negative error code on failure.
2072 */
2073int nvmem_device_write(struct nvmem_device *nvmem,
2074 unsigned int offset,
2075 size_t bytes, void *buf)
2076{
2077 int rc;
2078
2079 if (!nvmem)
2080 return -EINVAL;
2081
2082 rc = nvmem_reg_write(nvmem, offset, buf, bytes);
2083
2084 if (rc)
2085 return rc;
2086
2087
2088 return bytes;
2089}
2090EXPORT_SYMBOL_GPL(nvmem_device_write);
2091
2092/**
2093 * nvmem_add_cell_table() - register a table of cell info entries
2094 *
2095 * @table: table of cell info entries
2096 */
2097void nvmem_add_cell_table(struct nvmem_cell_table *table)
2098{
2099 mutex_lock(&nvmem_cell_mutex);
2100 list_add_tail(&table->node, &nvmem_cell_tables);
2101 mutex_unlock(&nvmem_cell_mutex);
2102}
2103EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
2104
2105/**
2106 * nvmem_del_cell_table() - remove a previously registered cell info table
2107 *
2108 * @table: table of cell info entries
2109 */
2110void nvmem_del_cell_table(struct nvmem_cell_table *table)
2111{
2112 mutex_lock(&nvmem_cell_mutex);
2113 list_del(&table->node);
2114 mutex_unlock(&nvmem_cell_mutex);
2115}
2116EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
2117
2118/**
2119 * nvmem_add_cell_lookups() - register a list of cell lookup entries
2120 *
2121 * @entries: array of cell lookup entries
2122 * @nentries: number of cell lookup entries in the array
2123 */
2124void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2125{
2126 int i;
2127
2128 mutex_lock(&nvmem_lookup_mutex);
2129 for (i = 0; i < nentries; i++)
2130 list_add_tail(&entries[i].node, &nvmem_lookup_list);
2131 mutex_unlock(&nvmem_lookup_mutex);
2132}
2133EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
2134
2135/**
2136 * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
2137 * entries
2138 *
2139 * @entries: array of cell lookup entries
2140 * @nentries: number of cell lookup entries in the array
2141 */
2142void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
2143{
2144 int i;
2145
2146 mutex_lock(&nvmem_lookup_mutex);
2147 for (i = 0; i < nentries; i++)
2148 list_del(&entries[i].node);
2149 mutex_unlock(&nvmem_lookup_mutex);
2150}
2151EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
2152
2153/**
2154 * nvmem_dev_name() - Get the name of a given nvmem device.
2155 *
2156 * @nvmem: nvmem device.
2157 *
2158 * Return: name of the nvmem device.
2159 */
2160const char *nvmem_dev_name(struct nvmem_device *nvmem)
2161{
2162 return dev_name(&nvmem->dev);
2163}
2164EXPORT_SYMBOL_GPL(nvmem_dev_name);
2165
2166/**
2167 * nvmem_dev_size() - Get the size of a given nvmem device.
2168 *
2169 * @nvmem: nvmem device.
2170 *
2171 * Return: size of the nvmem device.
2172 */
2173size_t nvmem_dev_size(struct nvmem_device *nvmem)
2174{
2175 return nvmem->size;
2176}
2177EXPORT_SYMBOL_GPL(nvmem_dev_size);
2178
2179static int __init nvmem_init(void)
2180{
2181 int ret;
2182
2183 ret = bus_register(&nvmem_bus_type);
2184 if (ret)
2185 return ret;
2186
2187 ret = nvmem_layout_bus_register();
2188 if (ret)
2189 bus_unregister(&nvmem_bus_type);
2190
2191 return ret;
2192}
2193
2194static void __exit nvmem_exit(void)
2195{
2196 nvmem_layout_bus_unregister();
2197 bus_unregister(&nvmem_bus_type);
2198}
2199
2200subsys_initcall(nvmem_init);
2201module_exit(nvmem_exit);
2202
2203MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
2204MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
2205MODULE_DESCRIPTION("nvmem Driver Core");