summaryrefslogtreecommitdiffstats
path: root/drivers/nvmem/regmap.c
blob: 24712fbb0f332e98474955aa433c0a5e02502054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: GPL-2.0-only
#include <common.h>
#include <driver.h>
#include <malloc.h>
#include <xfuncs.h>
#include <errno.h>
#include <init.h>
#include <io.h>
#include <linux/regmap.h>
#include <linux/nvmem-provider.h>

static int nvmem_regmap_write(void *ctx, unsigned offset, const void *val, size_t bytes)
{
	struct regmap *map = ctx;

	/*
	 * eFuse writes going through this function may be irreversible,
	 * so expect users to observe alignment.
	 */
	if (bytes % regmap_get_val_bytes(map))
		return -EINVAL;

	return regmap_bulk_write(map, offset, val,
				 bytes / regmap_get_val_bytes(map));
}

static int nvmem_regmap_read(void *ctx, unsigned offset, void *buf, size_t bytes)
{
	struct regmap *map = ctx;
	size_t rbytes, stride, skip_bytes;
	u32 roffset, val;
	u8 *buf8 = buf, *val8 = (u8 *)&val;
	int i, j = 0, ret, size;

	stride = regmap_get_reg_stride(map);

	roffset = rounddown(offset, stride);
	skip_bytes = offset & (stride - 1);
	rbytes = roundup(bytes + skip_bytes, stride);

	if (roffset + rbytes > regmap_size_bytes(map))
		return -EINVAL;

	for (i = roffset; i < roffset + rbytes; i += stride) {
		ret = regmap_read(map, i, &val);
		if (ret) {
			dev_err(regmap_get_device(map), "Can't read data%d (%d)\n", i, ret);
			return ret;
		}

		/* skip first bytes in case of unaligned read */
		if (skip_bytes)
			size = min(bytes, stride - skip_bytes);
		else
			size = min(bytes, stride);

		memcpy(&buf8[j], &val8[skip_bytes], size);
		bytes -= size;
		j += size;
		skip_bytes = 0;
	}

	return 0;
}

struct nvmem_device *
nvmem_regmap_register_with_pp(struct regmap *map, const char *name,
			      nvmem_cell_post_process_t cell_post_process)
{
	struct nvmem_config config = {};

	/* Can be retrofitted if needed */
	if (regmap_get_reg_stride(map) != regmap_get_val_bytes(map))
		return ERR_PTR(-EINVAL);

	config.name = name;
	config.dev = regmap_get_device(map);
	config.priv = map;
	config.stride = 1;
	config.word_size = 1;
	config.size = regmap_size_bytes(map);
	config.cell_post_process = cell_post_process;
	config.reg_write = nvmem_regmap_write;
	config.reg_read = nvmem_regmap_read;

	return nvmem_register(&config);
}

struct nvmem_device *nvmem_regmap_register(struct regmap *map, const char *name)
{
	return nvmem_regmap_register_with_pp(map, name, NULL);
}