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