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