blob: 303e166a69c1b769f5006712c06dc3d855e1c2c4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass57251282015-06-23 15:38:43 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass57251282015-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 Yamadae151a1c2018-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 Glass57251282015-06-23 15:38:43 -060026struct regmap *syscon_get_regmap(struct udevice *dev)
27{
Simon Glass9f4629b2015-07-06 12:54:38 -060028 struct syscon_uc_info *priv;
Simon Glass57251282015-06-23 15:38:43 -060029
Simon Glass9f4629b2015-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 Glass57251282015-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 Glass04ecf362016-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 Yamadad3581232018-04-19 12:14:03 +090052 return regmap_init_mem(dev_ofnode(dev), &priv->regmap);
Simon Glass04ecf362016-07-04 11:58:00 -060053#endif
Simon Glass57251282015-06-23 15:38:43 -060054}
55
Simon Glassac94b7b2016-01-17 16:11:08 -070056int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp)
Simon Glass57251282015-06-23 15:38:43 -060057{
58 struct udevice *dev;
59 struct uclass *uc;
60 int ret;
61
Simon Glass532f2432016-03-11 22:06:49 -070062 *devp = NULL;
Simon Glass57251282015-06-23 15:38:43 -060063 ret = uclass_get(UCLASS_SYSCON, &uc);
64 if (ret)
Simon Glassac94b7b2016-01-17 16:11:08 -070065 return ret;
Simon Glass57251282015-06-23 15:38:43 -060066 uclass_foreach_dev(dev, uc) {
67 if (dev->driver_data == driver_data) {
Simon Glassac94b7b2016-01-17 16:11:08 -070068 *devp = dev;
69 return device_probe(dev);
Simon Glass57251282015-06-23 15:38:43 -060070 }
71 }
72
Simon Glassac94b7b2016-01-17 16:11:08 -070073 return -ENODEV;
74}
75
76struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data)
77{
78 struct syscon_uc_info *priv;
79 struct udevice *dev;
80 int ret;
81
82 ret = syscon_get_by_driver_data(driver_data, &dev);
83 if (ret)
84 return ERR_PTR(ret);
85 priv = dev_get_uclass_priv(dev);
86
87 return priv->regmap;
Simon Glass57251282015-06-23 15:38:43 -060088}
89
90void *syscon_get_first_range(ulong driver_data)
91{
92 struct regmap *map;
93
94 map = syscon_get_regmap_by_driver_data(driver_data);
95 if (IS_ERR(map))
96 return map;
97 return regmap_get_range(map, 0);
98}
99
100UCLASS_DRIVER(syscon) = {
101 .id = UCLASS_SYSCON,
102 .name = "syscon",
103 .per_device_auto_alloc_size = sizeof(struct syscon_uc_info),
104 .pre_probe = syscon_pre_probe,
105};
Paul Burton8291bc82016-09-08 07:47:37 +0100106
107static const struct udevice_id generic_syscon_ids[] = {
108 { .compatible = "syscon" },
109 { }
110};
111
112U_BOOT_DRIVER(generic_syscon) = {
113 .name = "syscon",
114 .id = UCLASS_SYSCON,
Simon Glass745fb9c2017-07-29 11:34:52 -0600115#if !CONFIG_IS_ENABLED(OF_PLATDATA)
116 .bind = dm_scan_fdt_dev,
117#endif
Paul Burton8291bc82016-09-08 07:47:37 +0100118 .of_match = generic_syscon_ids,
119};
Masahiro Yamadae151a1c2018-04-19 12:14:04 +0900120
121/*
122 * Linux-compatible syscon-to-regmap
123 * The syscon node can be bound to another driver, but still works
124 * as a syscon provider.
125 */
126static LIST_HEAD(syscon_list);
127
128struct syscon {
129 ofnode node;
130 struct regmap *regmap;
131 struct list_head list;
132};
133
134static struct syscon *of_syscon_register(ofnode node)
135{
136 struct syscon *syscon;
137 int ret;
138
139 if (!ofnode_device_is_compatible(node, "syscon"))
140 return ERR_PTR(-EINVAL);
141
142 syscon = malloc(sizeof(*syscon));
143 if (!syscon)
144 return ERR_PTR(-ENOMEM);
145
146 ret = regmap_init_mem(node, &syscon->regmap);
147 if (ret) {
148 free(syscon);
149 return ERR_PTR(ret);
150 }
151
152 list_add_tail(&syscon->list, &syscon_list);
153
154 return syscon;
155}
156
157struct regmap *syscon_node_to_regmap(ofnode node)
158{
159 struct syscon *entry, *syscon = NULL;
160
161 list_for_each_entry(entry, &syscon_list, list)
162 if (ofnode_equal(entry->node, node)) {
163 syscon = entry;
164 break;
165 }
166
167 if (!syscon)
168 syscon = of_syscon_register(node);
169
170 if (IS_ERR(syscon))
171 return ERR_CAST(syscon);
172
173 return syscon->regmap;
174}