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