xref: /openbmc/u-boot/drivers/core/syscon-uclass.c (revision e3963c09)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <syscon.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <regmap.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14 #include <dm/root.h>
15 #include <linux/err.h>
16 
17 /*
18  * Caution:
19  * This API requires the given device has alerady been bound to syscon driver.
20  * For example,
21  *    compatible = "syscon", "simple-mfd";
22  * works, but
23  *    compatible = "simple-mfd", "syscon";
24  * does not.  The behavior is different from Linux.
25  */
26 struct regmap *syscon_get_regmap(struct udevice *dev)
27 {
28 	struct syscon_uc_info *priv;
29 
30 	if (device_get_uclass_id(dev) != UCLASS_SYSCON)
31 		return ERR_PTR(-ENOEXEC);
32 	priv = dev_get_uclass_priv(dev);
33 	return priv->regmap;
34 }
35 
36 static int syscon_pre_probe(struct udevice *dev)
37 {
38 	struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
39 
40 	/*
41 	 * With OF_PLATDATA we really have no way of knowing the format of
42 	 * the device-specific platform data. So we assume that it starts with
43 	 * a 'reg' member, and this holds a single address and size. Drivers
44 	 * using OF_PLATDATA will need to ensure that this is true.
45 	 */
46 #if CONFIG_IS_ENABLED(OF_PLATDATA)
47 	struct syscon_base_platdata *plat = dev_get_platdata(dev);
48 
49 	return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
50 					&priv->regmap);
51 #else
52 	return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
53 #endif
54 }
55 
56 struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
57 					       const char *name)
58 {
59 	struct udevice *syscon;
60 	struct regmap *r;
61 	int err;
62 
63 	err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
64 					   name, &syscon);
65 	if (err) {
66 		dev_dbg(dev, "unable to find syscon device\n");
67 		return ERR_PTR(err);
68 	}
69 
70 	r = syscon_get_regmap(syscon);
71 	if (!r) {
72 		dev_dbg(dev, "unable to find regmap\n");
73 		return ERR_PTR(-ENODEV);
74 	}
75 
76 	return r;
77 }
78 
79 int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
80 {
81 	struct udevice *dev;
82 	struct uclass *uc;
83 	int ret;
84 
85 	*devp = NULL;
86 	ret = uclass_get(UCLASS_SYSCON, &uc);
87 	if (ret)
88 		return ret;
89 	uclass_foreach_dev(dev, uc) {
90 		if (dev->driver_data == driver_data) {
91 			*devp = dev;
92 			return device_probe(dev);
93 		}
94 	}
95 
96 	return -ENODEV;
97 }
98 
99 struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
100 {
101 	struct syscon_uc_info *priv;
102 	struct udevice *dev;
103 	int ret;
104 
105 	ret = syscon_get_by_driver_data(driver_data, &dev);
106 	if (ret)
107 		return ERR_PTR(ret);
108 	priv = dev_get_uclass_priv(dev);
109 
110 	return priv->regmap;
111 }
112 
113 void *syscon_get_first_range(ulong driver_data)
114 {
115 	struct regmap *map;
116 
117 	map = syscon_get_regmap_by_driver_data(driver_data);
118 	if (IS_ERR(map))
119 		return map;
120 	return regmap_get_range(map, 0);
121 }
122 
123 UCLASS_DRIVER(syscon) = {
124 	.id		= UCLASS_SYSCON,
125 	.name		= "syscon",
126 	.per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
127 	.pre_probe = syscon_pre_probe,
128 };
129 
130 static const struct udevice_id generic_syscon_ids[] = {
131 	{ .compatible = "syscon" },
132 	{ }
133 };
134 
135 U_BOOT_DRIVER(generic_syscon) = {
136 	.name	= "syscon",
137 	.id	= UCLASS_SYSCON,
138 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
139 	.bind           = dm_scan_fdt_dev,
140 #endif
141 	.of_match = generic_syscon_ids,
142 };
143 
144 /*
145  * Linux-compatible syscon-to-regmap
146  * The syscon node can be bound to another driver, but still works
147  * as a syscon provider.
148  */
149 static LIST_HEAD(syscon_list);
150 
151 struct syscon {
152 	ofnode node;
153 	struct regmap *regmap;
154 	struct list_head list;
155 };
156 
157 static struct syscon *of_syscon_register(ofnode node)
158 {
159 	struct syscon *syscon;
160 	int ret;
161 
162 	if (!ofnode_device_is_compatible(node, "syscon"))
163 		return ERR_PTR(-EINVAL);
164 
165 	syscon = malloc(sizeof(*syscon));
166 	if (!syscon)
167 		return ERR_PTR(-ENOMEM);
168 
169 	ret = regmap_init_mem(node, &syscon->regmap);
170 	if (ret) {
171 		free(syscon);
172 		return ERR_PTR(ret);
173 	}
174 
175 	list_add_tail(&syscon->list, &syscon_list);
176 
177 	return syscon;
178 }
179 
180 struct regmap *syscon_node_to_regmap(ofnode node)
181 {
182 	struct syscon *entry, *syscon = NULL;
183 
184 	list_for_each_entry(entry, &syscon_list, list)
185 		if (ofnode_equal(entry->node, node)) {
186 			syscon = entry;
187 			break;
188 		}
189 
190 	if (!syscon)
191 		syscon = of_syscon_register(node);
192 
193 	if (IS_ERR(syscon))
194 		return ERR_CAST(syscon);
195 
196 	return syscon->regmap;
197 }
198