Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 9 | #include <syscon.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <regmap.h> |
| 13 | #include <dm/device-internal.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 15 | #include <dm/lists.h> |
| 16 | #include <dm/root.h> |
| 17 | #include <linux/err.h> |
| 18 | |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 19 | /* |
| 20 | * Caution: |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 21 | * This API requires the given device has already been bound to the syscon |
| 22 | * driver. For example, |
| 23 | * |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 24 | * compatible = "syscon", "simple-mfd"; |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 25 | * |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 26 | * works, but |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 27 | * |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 28 | * compatible = "simple-mfd", "syscon"; |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 29 | * |
| 30 | * does not. The behavior is different from Linux. |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 31 | */ |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 32 | struct regmap *syscon_get_regmap(struct udevice *dev) |
| 33 | { |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 34 | struct syscon_uc_info *priv; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 35 | |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 36 | if (device_get_uclass_id(dev) != UCLASS_SYSCON) |
| 37 | return ERR_PTR(-ENOEXEC); |
| 38 | priv = dev_get_uclass_priv(dev); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 39 | return priv->regmap; |
| 40 | } |
| 41 | |
| 42 | static int syscon_pre_probe(struct udevice *dev) |
| 43 | { |
| 44 | struct syscon_uc_info *priv = dev_get_uclass_priv(dev); |
| 45 | |
Simon Glass | 7e75aba | 2019-02-16 20:24:38 -0700 | [diff] [blame] | 46 | /* Special case for PCI devices, which don't have a regmap */ |
| 47 | if (device_get_uclass_id(dev->parent) == UCLASS_PCI) |
| 48 | return 0; |
| 49 | |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 50 | /* |
| 51 | * With OF_PLATDATA we really have no way of knowing the format of |
| 52 | * the device-specific platform data. So we assume that it starts with |
| 53 | * a 'reg' member, and this holds a single address and size. Drivers |
| 54 | * using OF_PLATDATA will need to ensure that this is true. |
| 55 | */ |
| 56 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 57 | struct syscon_base_platdata *plat = dev_get_platdata(dev); |
| 58 | |
| 59 | return regmap_init_mem_platdata(dev, plat->reg, ARRAY_SIZE(plat->reg), |
| 60 | &priv->regmap); |
| 61 | #else |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 62 | return regmap_init_mem(dev_ofnode(dev), &priv->regmap); |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 63 | #endif |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 64 | } |
| 65 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 66 | static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp) |
| 67 | { |
| 68 | struct udevice *dev, *parent; |
| 69 | int ret; |
| 70 | |
| 71 | /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */ |
| 72 | if (!ofnode_device_is_compatible(node, "syscon")) { |
Sean Anderson | 8d06ce4 | 2020-09-15 10:44:37 -0400 | [diff] [blame] | 73 | log_debug("invalid compatible for syscon device\n"); |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 74 | return -EINVAL; |
| 75 | } |
| 76 | |
| 77 | /* bound to driver with same ofnode or to root if not found */ |
| 78 | if (device_find_global_by_ofnode(node, &parent)) |
| 79 | parent = dm_root(); |
| 80 | |
| 81 | /* force bound to syscon class */ |
| 82 | ret = device_bind_driver_to_node(parent, "syscon", |
| 83 | ofnode_get_name(node), |
| 84 | node, &dev); |
| 85 | if (ret) { |
| 86 | dev_dbg(dev, "unable to bound syscon device\n"); |
| 87 | return ret; |
| 88 | } |
| 89 | ret = device_probe(dev); |
| 90 | if (ret) { |
| 91 | dev_dbg(dev, "unable to probe syscon device\n"); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | *devp = dev; |
| 96 | return 0; |
| 97 | } |
| 98 | |
Jean-Jacques Hiblot | dc44ea4 | 2018-11-29 10:57:37 +0100 | [diff] [blame] | 99 | struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev, |
| 100 | const char *name) |
| 101 | { |
| 102 | struct udevice *syscon; |
| 103 | struct regmap *r; |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 104 | u32 phandle; |
| 105 | ofnode node; |
Jean-Jacques Hiblot | dc44ea4 | 2018-11-29 10:57:37 +0100 | [diff] [blame] | 106 | int err; |
| 107 | |
| 108 | err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, |
| 109 | name, &syscon); |
| 110 | if (err) { |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 111 | /* found node with "syscon" compatible, not bounded to SYSCON */ |
| 112 | err = ofnode_read_u32(dev_ofnode(dev), name, &phandle); |
| 113 | if (err) |
| 114 | return ERR_PTR(err); |
| 115 | |
| 116 | node = ofnode_get_by_phandle(phandle); |
| 117 | if (!ofnode_valid(node)) { |
| 118 | dev_dbg(dev, "unable to find syscon device\n"); |
| 119 | return ERR_PTR(-EINVAL); |
| 120 | } |
| 121 | err = syscon_probe_by_ofnode(node, &syscon); |
| 122 | if (err) |
| 123 | return ERR_PTR(-ENODEV); |
Jean-Jacques Hiblot | dc44ea4 | 2018-11-29 10:57:37 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | r = syscon_get_regmap(syscon); |
| 127 | if (!r) { |
| 128 | dev_dbg(dev, "unable to find regmap\n"); |
| 129 | return ERR_PTR(-ENODEV); |
| 130 | } |
| 131 | |
| 132 | return r; |
| 133 | } |
| 134 | |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 135 | int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp) |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 136 | { |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 137 | int ret; |
| 138 | |
Simon Glass | 36ab8e3 | 2016-03-11 22:06:49 -0700 | [diff] [blame] | 139 | *devp = NULL; |
Simon Glass | e5f50c9 | 2020-02-06 09:54:51 -0700 | [diff] [blame] | 140 | |
| 141 | ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 142 | if (ret) |
Simon Glass | e5f50c9 | 2020-02-06 09:54:51 -0700 | [diff] [blame] | 143 | return log_msg_ret("find", ret); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 144 | |
Simon Glass | e5f50c9 | 2020-02-06 09:54:51 -0700 | [diff] [blame] | 145 | return 0; |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data) |
| 149 | { |
| 150 | struct syscon_uc_info *priv; |
| 151 | struct udevice *dev; |
| 152 | int ret; |
| 153 | |
| 154 | ret = syscon_get_by_driver_data(driver_data, &dev); |
| 155 | if (ret) |
| 156 | return ERR_PTR(ret); |
| 157 | priv = dev_get_uclass_priv(dev); |
| 158 | |
| 159 | return priv->regmap; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void *syscon_get_first_range(ulong driver_data) |
| 163 | { |
| 164 | struct regmap *map; |
| 165 | |
| 166 | map = syscon_get_regmap_by_driver_data(driver_data); |
| 167 | if (IS_ERR(map)) |
| 168 | return map; |
| 169 | return regmap_get_range(map, 0); |
| 170 | } |
| 171 | |
| 172 | UCLASS_DRIVER(syscon) = { |
| 173 | .id = UCLASS_SYSCON, |
| 174 | .name = "syscon", |
| 175 | .per_device_auto_alloc_size = sizeof(struct syscon_uc_info), |
| 176 | .pre_probe = syscon_pre_probe, |
| 177 | }; |
Paul Burton | 8c946a7 | 2016-09-08 07:47:37 +0100 | [diff] [blame] | 178 | |
| 179 | static const struct udevice_id generic_syscon_ids[] = { |
| 180 | { .compatible = "syscon" }, |
| 181 | { } |
| 182 | }; |
| 183 | |
| 184 | U_BOOT_DRIVER(generic_syscon) = { |
| 185 | .name = "syscon", |
| 186 | .id = UCLASS_SYSCON, |
Simon Glass | 8061bc6 | 2017-07-29 11:34:52 -0600 | [diff] [blame] | 187 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 188 | .bind = dm_scan_fdt_dev, |
| 189 | #endif |
Paul Burton | 8c946a7 | 2016-09-08 07:47:37 +0100 | [diff] [blame] | 190 | .of_match = generic_syscon_ids, |
| 191 | }; |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * Linux-compatible syscon-to-regmap |
| 195 | * The syscon node can be bound to another driver, but still works |
| 196 | * as a syscon provider. |
| 197 | */ |
Patrick Delaunay | 8a5a8a7 | 2018-10-31 16:49:03 +0100 | [diff] [blame] | 198 | struct regmap *syscon_node_to_regmap(ofnode node) |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 199 | { |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 200 | struct udevice *dev; |
| 201 | struct regmap *r; |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 202 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 203 | if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev)) |
| 204 | if (syscon_probe_by_ofnode(node, &dev)) |
| 205 | return ERR_PTR(-ENODEV); |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 206 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 207 | r = syscon_get_regmap(dev); |
| 208 | if (!r) { |
| 209 | dev_dbg(dev, "unable to find regmap\n"); |
| 210 | return ERR_PTR(-ENODEV); |
| 211 | } |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 212 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 213 | return r; |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 214 | } |