blob: 5bb38e329cb847c3201e5a5c0f925f3a99856364 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6a84aaf2015-06-23 15:38:43 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass6a84aaf2015-06-23 15:38:43 -06005 */
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
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +090017/*
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 */
Simon Glass6a84aaf2015-06-23 15:38:43 -060026struct regmap *syscon_get_regmap(struct udevice *dev)
27{
Simon Glassead64972015-07-06 12:54:38 -060028 struct syscon_uc_info *priv;
Simon Glass6a84aaf2015-06-23 15:38:43 -060029
Simon Glassead64972015-07-06 12:54:38 -060030 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
31 return ERR_PTR(-ENOEXEC);
32 priv = dev_get_uclass_priv(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -060033 return priv->regmap;
34}
35
36static int syscon_pre_probe(struct udevice *dev)
37{
38 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
39
Simon Glass7e75aba2019-02-16 20:24:38 -070040 /* Special case for PCI devices, which don't have a regmap */
41 if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
42 return 0;
43
Simon Glass73dd0692016-07-04 11:58:00 -060044 /*
45 * With OF_PLATDATA we really have no way of knowing the format of
46 * the device-specific platform data. So we assume that it starts with
47 * a 'reg' member, and this holds a single address and size. Drivers
48 * using OF_PLATDATA will need to ensure that this is true.
49 */
50#if CONFIG_IS_ENABLED(OF_PLATDATA)
51 struct syscon_base_platdata *plat = dev_get_platdata(dev);
52
53 return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg),
54 &priv->regmap);
55#else
Masahiro Yamadae4873e32018-04-19 12:14:03 +090056 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
Simon Glass73dd0692016-07-04 11:58:00 -060057#endif
Simon Glass6a84aaf2015-06-23 15:38:43 -060058}
59
Patrick Delaunayee010432019-03-07 09:57:13 +010060static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
61{
62 struct udevice *dev, *parent;
63 int ret;
64
65 /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
66 if (!ofnode_device_is_compatible(node, "syscon")) {
67 dev_dbg(dev, "invalid compatible for syscon device\n");
68 return -EINVAL;
69 }
70
71 /* bound to driver with same ofnode or to root if not found */
72 if (device_find_global_by_ofnode(node, &parent))
73 parent = dm_root();
74
75 /* force bound to syscon class */
76 ret = device_bind_driver_to_node(parent, "syscon",
77 ofnode_get_name(node),
78 node, &dev);
79 if (ret) {
80 dev_dbg(dev, "unable to bound syscon device\n");
81 return ret;
82 }
83 ret = device_probe(dev);
84 if (ret) {
85 dev_dbg(dev, "unable to probe syscon device\n");
86 return ret;
87 }
88
89 *devp = dev;
90 return 0;
91}
92
Jean-Jacques Hiblotdc44ea42018-11-29 10:57:37 +010093struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
94 const char *name)
95{
96 struct udevice *syscon;
97 struct regmap *r;
Patrick Delaunayee010432019-03-07 09:57:13 +010098 u32 phandle;
99 ofnode node;
Jean-Jacques Hiblotdc44ea42018-11-29 10:57:37 +0100100 int err;
101
102 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
103 name, &syscon);
104 if (err) {
Patrick Delaunayee010432019-03-07 09:57:13 +0100105 /* found node with "syscon" compatible, not bounded to SYSCON */
106 err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
107 if (err)
108 return ERR_PTR(err);
109
110 node = ofnode_get_by_phandle(phandle);
111 if (!ofnode_valid(node)) {
112 dev_dbg(dev, "unable to find syscon device\n");
113 return ERR_PTR(-EINVAL);
114 }
115 err = syscon_probe_by_ofnode(node, &syscon);
116 if (err)
117 return ERR_PTR(-ENODEV);
Jean-Jacques Hiblotdc44ea42018-11-29 10:57:37 +0100118 }
119
120 r = syscon_get_regmap(syscon);
121 if (!r) {
122 dev_dbg(dev, "unable to find regmap\n");
123 return ERR_PTR(-ENODEV);
124 }
125
126 return r;
127}
128
Simon Glass6d5579c2016-01-17 16:11:08 -0700129int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
Simon Glass6a84aaf2015-06-23 15:38:43 -0600130{
131 struct udevice *dev;
132 struct uclass *uc;
133 int ret;
134
Simon Glass36ab8e32016-03-11 22:06:49 -0700135 *devp = NULL;
Simon Glass6a84aaf2015-06-23 15:38:43 -0600136 ret = uclass_get(UCLASS_SYSCON, &uc);
137 if (ret)
Simon Glass6d5579c2016-01-17 16:11:08 -0700138 return ret;
Simon Glass6a84aaf2015-06-23 15:38:43 -0600139 uclass_foreach_dev(dev, uc) {
140 if (dev->driver_data == driver_data) {
Simon Glass6d5579c2016-01-17 16:11:08 -0700141 *devp = dev;
142 return device_probe(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -0600143 }
144 }
145
Simon Glass6d5579c2016-01-17 16:11:08 -0700146 return -ENODEV;
147}
148
149struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
150{
151 struct syscon_uc_info *priv;
152 struct udevice *dev;
153 int ret;
154
155 ret = syscon_get_by_driver_data(driver_data, &dev);
156 if (ret)
157 return ERR_PTR(ret);
158 priv = dev_get_uclass_priv(dev);
159
160 return priv->regmap;
Simon Glass6a84aaf2015-06-23 15:38:43 -0600161}
162
163void *syscon_get_first_range(ulong driver_data)
164{
165 struct regmap *map;
166
167 map = syscon_get_regmap_by_driver_data(driver_data);
168 if (IS_ERR(map))
169 return map;
170 return regmap_get_range(map, 0);
171}
172
173UCLASS_DRIVER(syscon) = {
174 .id = UCLASS_SYSCON,
175 .name = "syscon",
176 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
177 .pre_probe = syscon_pre_probe,
178};
Paul Burton8c946a72016-09-08 07:47:37 +0100179
180static const struct udevice_id generic_syscon_ids[] = {
181 { .compatible = "syscon" },
182 { }
183};
184
185U_BOOT_DRIVER(generic_syscon) = {
186 .name = "syscon",
187 .id = UCLASS_SYSCON,
Simon Glass8061bc62017-07-29 11:34:52 -0600188#if !CONFIG_IS_ENABLED(OF_PLATDATA)
189 .bind = dm_scan_fdt_dev,
190#endif
Paul Burton8c946a72016-09-08 07:47:37 +0100191 .of_match = generic_syscon_ids,
192};
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900193
194/*
195 * Linux-compatible syscon-to-regmap
196 * The syscon node can be bound to another driver, but still works
197 * as a syscon provider.
198 */
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100199struct regmap *syscon_node_to_regmap(ofnode node)
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900200{
Patrick Delaunayee010432019-03-07 09:57:13 +0100201 struct udevice *dev;
202 struct regmap *r;
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900203
Patrick Delaunayee010432019-03-07 09:57:13 +0100204 if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
205 if (syscon_probe_by_ofnode(node, &dev))
206 return ERR_PTR(-ENODEV);
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900207
Patrick Delaunayee010432019-03-07 09:57:13 +0100208 r = syscon_get_regmap(dev);
209 if (!r) {
210 dev_dbg(dev, "unable to find regmap\n");
211 return ERR_PTR(-ENODEV);
212 }
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900213
Patrick Delaunayee010432019-03-07 09:57:13 +0100214 return r;
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900215}