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