summaryrefslogtreecommitdiff
path: root/lib/utils/regmap/fdt_regmap.c
blob: 2e8c21435ea1d7ea6fd012430b6c98e0297c1525 (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
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2023 Ventana Micro Systems Inc.
 *
 * Authors:
 *   Anup Patel <apatel@ventanamicro.com>
 */

#include <libfdt.h>
#include <sbi/sbi_error.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi_utils/regmap/fdt_regmap.h>

/* List of FDT regmap drivers generated at compile time */
extern const struct fdt_driver *const fdt_regmap_drivers[];

static int fdt_regmap_find(const void *fdt, int nodeoff,
			   struct regmap **out_rmap)
{
	int rc;
	struct regmap *rmap = regmap_find(nodeoff);

	if (!rmap) {
		/* Regmap not found so initialize matching driver */
		rc = fdt_driver_init_by_offset(fdt, nodeoff,
					       fdt_regmap_drivers);
		if (rc)
			return rc;

		/* Try to find regmap again */
		rmap = regmap_find(nodeoff);
		if (!rmap)
			return SBI_ENOSYS;
	}

	if (out_rmap)
		*out_rmap = rmap;

	return 0;
}

int fdt_regmap_get_by_phandle(const void *fdt, u32 phandle,
			      struct regmap **out_rmap)
{
	int pnodeoff;

	if (!fdt || !out_rmap)
		return SBI_EINVAL;

	pnodeoff = fdt_node_offset_by_phandle(fdt, phandle);
	if (pnodeoff < 0)
		return pnodeoff;

	return fdt_regmap_find(fdt, pnodeoff, out_rmap);
}

int fdt_regmap_get(const void *fdt, int nodeoff, struct regmap **out_rmap)
{
	int len;
	const fdt32_t *val;

	if (!fdt || (nodeoff < 0) || !out_rmap)
		return SBI_EINVAL;

	val = fdt_getprop(fdt, nodeoff, "regmap", &len);
	if (!val)
		return SBI_ENOENT;

	return fdt_regmap_get_by_phandle(fdt, fdt32_to_cpu(*val), out_rmap);
}