blob: cb33facc7138939ad44e70f2c95c7401f2fd8b69 [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
Sean Anderson02a8f6d2020-10-04 21:39:43 -04007#define LOG_CATEGORY UCLASS_SYSCON
8
Simon Glass6a84aaf2015-06-23 15:38:43 -06009#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass6a84aaf2015-06-23 15:38:43 -060011#include <syscon.h>
12#include <dm.h>
13#include <errno.h>
14#include <regmap.h>
15#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glass6a84aaf2015-06-23 15:38:43 -060017#include <dm/lists.h>
18#include <dm/root.h>
19#include <linux/err.h>
20
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +090021/*
22 * Caution:
Heinrich Schuchardt43b05c02020-08-22 07:16:26 +020023 * This API requires the given device has already been bound to the syscon
24 * driver. For example,
25 *
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +090026 * compatible = "syscon", "simple-mfd";
Heinrich Schuchardt43b05c02020-08-22 07:16:26 +020027 *
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +090028 * works, but
Heinrich Schuchardt43b05c02020-08-22 07:16:26 +020029 *
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +090030 * compatible = "simple-mfd", "syscon";
Heinrich Schuchardt43b05c02020-08-22 07:16:26 +020031 *
32 * does not. The behavior is different from Linux.
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +090033 */
Simon Glass6a84aaf2015-06-23 15:38:43 -060034struct regmap *syscon_get_regmap(struct udevice *dev)
35{
Simon Glassead64972015-07-06 12:54:38 -060036 struct syscon_uc_info *priv;
Simon Glass6a84aaf2015-06-23 15:38:43 -060037
Simon Glassead64972015-07-06 12:54:38 -060038 if (device_get_uclass_id(dev) != UCLASS_SYSCON)
39 return ERR_PTR(-ENOEXEC);
40 priv = dev_get_uclass_priv(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -060041 return priv->regmap;
42}
43
44static int syscon_pre_probe(struct udevice *dev)
45{
46 struct syscon_uc_info *priv = dev_get_uclass_priv(dev);
47
Simon Glass7e75aba2019-02-16 20:24:38 -070048 /* Special case for PCI devices, which don't have a regmap */
49 if (device_get_uclass_id(dev->parent) == UCLASS_PCI)
50 return 0;
51
Simon Glass73dd0692016-07-04 11:58:00 -060052 /*
53 * With OF_PLATDATA we really have no way of knowing the format of
54 * the device-specific platform data. So we assume that it starts with
55 * a 'reg' member, and this holds a single address and size. Drivers
56 * using OF_PLATDATA will need to ensure that this is true.
57 */
58#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -070059 struct syscon_base_plat *plat = dev_get_plat(dev);
Simon Glass73dd0692016-07-04 11:58:00 -060060
Simon Glassb75b15b2020-12-03 16:55:23 -070061 return regmap_init_mem_plat(dev, plat->reg, ARRAY_SIZE(plat->reg),
Simon Glass73dd0692016-07-04 11:58:00 -060062 &priv->regmap);
63#else
Masahiro Yamadae4873e32018-04-19 12:14:03 +090064 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
Simon Glass73dd0692016-07-04 11:58:00 -060065#endif
Simon Glass6a84aaf2015-06-23 15:38:43 -060066}
67
Patrick Delaunayee010432019-03-07 09:57:13 +010068static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp)
69{
70 struct udevice *dev, *parent;
71 int ret;
72
73 /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */
74 if (!ofnode_device_is_compatible(node, "syscon")) {
Sean Anderson8d06ce42020-09-15 10:44:37 -040075 log_debug("invalid compatible for syscon device\n");
Patrick Delaunayee010432019-03-07 09:57:13 +010076 return -EINVAL;
77 }
78
79 /* bound to driver with same ofnode or to root if not found */
80 if (device_find_global_by_ofnode(node, &parent))
81 parent = dm_root();
82
83 /* force bound to syscon class */
84 ret = device_bind_driver_to_node(parent, "syscon",
85 ofnode_get_name(node),
86 node, &dev);
87 if (ret) {
88 dev_dbg(dev, "unable to bound syscon device\n");
89 return ret;
90 }
91 ret = device_probe(dev);
92 if (ret) {
93 dev_dbg(dev, "unable to probe syscon device\n");
94 return ret;
95 }
96
97 *devp = dev;
98 return 0;
99}
100
Jean-Jacques Hiblotdc44ea42018-11-29 10:57:37 +0100101struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev,
102 const char *name)
103{
104 struct udevice *syscon;
105 struct regmap *r;
Patrick Delaunayee010432019-03-07 09:57:13 +0100106 u32 phandle;
107 ofnode node;
Jean-Jacques Hiblotdc44ea42018-11-29 10:57:37 +0100108 int err;
109
110 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
111 name, &syscon);
112 if (err) {
Patrick Delaunayee010432019-03-07 09:57:13 +0100113 /* found node with "syscon" compatible, not bounded to SYSCON */
114 err = ofnode_read_u32(dev_ofnode(dev), name, &phandle);
115 if (err)
116 return ERR_PTR(err);
117
118 node = ofnode_get_by_phandle(phandle);
119 if (!ofnode_valid(node)) {
120 dev_dbg(dev, "unable to find syscon device\n");
121 return ERR_PTR(-EINVAL);
122 }
123 err = syscon_probe_by_ofnode(node, &syscon);
124 if (err)
125 return ERR_PTR(-ENODEV);
Jean-Jacques Hiblotdc44ea42018-11-29 10:57:37 +0100126 }
127
128 r = syscon_get_regmap(syscon);
129 if (!r) {
130 dev_dbg(dev, "unable to find regmap\n");
131 return ERR_PTR(-ENODEV);
132 }
133
134 return r;
135}
136
Simon Glass6d5579c2016-01-17 16:11:08 -0700137int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
Simon Glass6a84aaf2015-06-23 15:38:43 -0600138{
Simon Glass6a84aaf2015-06-23 15:38:43 -0600139 int ret;
140
Simon Glass36ab8e32016-03-11 22:06:49 -0700141 *devp = NULL;
Simon Glasse5f50c92020-02-06 09:54:51 -0700142
143 ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp);
Simon Glass6a84aaf2015-06-23 15:38:43 -0600144 if (ret)
Simon Glass72f0d992020-09-27 18:46:17 -0600145 return ret;
Simon Glass6a84aaf2015-06-23 15:38:43 -0600146
Simon Glasse5f50c92020-02-06 09:54:51 -0700147 return 0;
Simon Glass6d5579c2016-01-17 16:11:08 -0700148}
149
150struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
151{
152 struct syscon_uc_info *priv;
153 struct udevice *dev;
154 int ret;
155
156 ret = syscon_get_by_driver_data(driver_data, &dev);
157 if (ret)
158 return ERR_PTR(ret);
159 priv = dev_get_uclass_priv(dev);
160
161 return priv->regmap;
Simon Glass6a84aaf2015-06-23 15:38:43 -0600162}
163
164void *syscon_get_first_range(ulong driver_data)
165{
166 struct regmap *map;
167
168 map = syscon_get_regmap_by_driver_data(driver_data);
169 if (IS_ERR(map))
170 return map;
171 return regmap_get_range(map, 0);
172}
173
174UCLASS_DRIVER(syscon) = {
175 .id = UCLASS_SYSCON,
176 .name = "syscon",
Simon Glass8a2b47f2020-12-03 16:55:17 -0700177 .per_device_auto = sizeof(struct syscon_uc_info),
Simon Glass6a84aaf2015-06-23 15:38:43 -0600178 .pre_probe = syscon_pre_probe,
179};
Paul Burton8c946a72016-09-08 07:47:37 +0100180
181static const struct udevice_id generic_syscon_ids[] = {
182 { .compatible = "syscon" },
183 { }
184};
185
186U_BOOT_DRIVER(generic_syscon) = {
187 .name = "syscon",
188 .id = UCLASS_SYSCON,
Simon Glass8061bc62017-07-29 11:34:52 -0600189#if !CONFIG_IS_ENABLED(OF_PLATDATA)
190 .bind = dm_scan_fdt_dev,
191#endif
Paul Burton8c946a72016-09-08 07:47:37 +0100192 .of_match = generic_syscon_ids,
193};
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900194
195/*
196 * Linux-compatible syscon-to-regmap
197 * The syscon node can be bound to another driver, but still works
198 * as a syscon provider.
199 */
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100200struct regmap *syscon_node_to_regmap(ofnode node)
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900201{
Patrick Delaunayee010432019-03-07 09:57:13 +0100202 struct udevice *dev;
203 struct regmap *r;
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900204
Patrick Delaunayee010432019-03-07 09:57:13 +0100205 if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
206 if (syscon_probe_by_ofnode(node, &dev))
207 return ERR_PTR(-ENODEV);
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900208
Patrick Delaunayee010432019-03-07 09:57:13 +0100209 r = syscon_get_regmap(dev);
210 if (!r) {
211 dev_dbg(dev, "unable to find regmap\n");
212 return ERR_PTR(-ENODEV);
213 }
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900214
Patrick Delaunayee010432019-03-07 09:57:13 +0100215 return r;
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900216}