blob: 3cf9dd9dbe43b65e89c1a0831653554673ae4818 [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 Glass73dd0692016-07-04 11:58:00 -060040 /*
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
Masahiro Yamadae4873e32018-04-19 12:14:03 +090052 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
Simon Glass73dd0692016-07-04 11:58:00 -060053#endif
Simon Glass6a84aaf2015-06-23 15:38:43 -060054}
55
Jean-Jacques Hiblotdc44ea42018-11-29 10:57:37 +010056struct 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
Simon Glass6d5579c2016-01-17 16:11:08 -070079int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
Simon Glass6a84aaf2015-06-23 15:38:43 -060080{
81 struct udevice *dev;
82 struct uclass *uc;
83 int ret;
84
Simon Glass36ab8e32016-03-11 22:06:49 -070085 *devp = NULL;
Simon Glass6a84aaf2015-06-23 15:38:43 -060086 ret = uclass_get(UCLASS_SYSCON, &uc);
87 if (ret)
Simon Glass6d5579c2016-01-17 16:11:08 -070088 return ret;
Simon Glass6a84aaf2015-06-23 15:38:43 -060089 uclass_foreach_dev(dev, uc) {
90 if (dev->driver_data == driver_data) {
Simon Glass6d5579c2016-01-17 16:11:08 -070091 *devp = dev;
92 return device_probe(dev);
Simon Glass6a84aaf2015-06-23 15:38:43 -060093 }
94 }
95
Simon Glass6d5579c2016-01-17 16:11:08 -070096 return -ENODEV;
97}
98
99struct 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;
Simon Glass6a84aaf2015-06-23 15:38:43 -0600111}
112
113void *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
123UCLASS_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};
Paul Burton8c946a72016-09-08 07:47:37 +0100129
130static const struct udevice_id generic_syscon_ids[] = {
131 { .compatible = "syscon" },
132 { }
133};
134
135U_BOOT_DRIVER(generic_syscon) = {
136 .name = "syscon",
137 .id = UCLASS_SYSCON,
Simon Glass8061bc62017-07-29 11:34:52 -0600138#if !CONFIG_IS_ENABLED(OF_PLATDATA)
139 .bind = dm_scan_fdt_dev,
140#endif
Paul Burton8c946a72016-09-08 07:47:37 +0100141 .of_match = generic_syscon_ids,
142};
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900143
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 */
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100149struct regmap *syscon_node_to_regmap(ofnode node)
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900150{
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100151 struct udevice *dev, *parent;
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900152 int ret;
153
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100154 if (!uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev))
155 return syscon_get_regmap(dev);
156
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900157 if (!ofnode_device_is_compatible(node, "syscon"))
158 return ERR_PTR(-EINVAL);
159
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100160 /* bound to driver with same ofnode or to root if not found */
161 if (device_find_global_by_ofnode(node, &parent))
162 parent = dm_root();
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900163
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100164 /* force bound to syscon class */
165 ret = device_bind_driver_to_node(parent, "syscon",
166 ofnode_get_name(node),
167 node, &dev);
168 if (ret)
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900169 return ERR_PTR(ret);
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900170
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100171 ret = device_probe(dev);
172 if (ret)
173 return ERR_PTR(ret);
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900174
Patrick Delaunay8a5a8a72018-10-31 16:49:03 +0100175 return syscon_get_regmap(dev);
Masahiro Yamada60f9a1e2018-04-19 12:14:04 +0900176}