Jean-Jacques Hiblot | f4f9056 | 2020-10-16 16:16:30 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Multiplexer subsystem |
| 4 | * |
| 5 | * Based on the linux multiplexer framework |
| 6 | * |
| 7 | * Copyright (C) 2017 Axentia Technologies AB |
| 8 | * Author: Peter Rosin <peda@axentia.se> |
| 9 | * |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 10 | * Copyright (C) 2017-2018 Texas Instruments Incorporated - https://www.ti.com/ |
Jean-Jacques Hiblot | f4f9056 | 2020-10-16 16:16:30 +0530 | [diff] [blame] | 11 | * Jean-Jacques Hiblot <jjhiblot@ti.com> |
| 12 | */ |
| 13 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 14 | #define LOG_CATEGORY UCLASS_MUX |
| 15 | |
Jean-Jacques Hiblot | f4f9056 | 2020-10-16 16:16:30 +0530 | [diff] [blame] | 16 | #include <dm.h> |
| 17 | #include <mux-internal.h> |
| 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/device_compat.h> |
| 20 | #include <dm/devres.h> |
| 21 | #include <dt-bindings/mux/mux.h> |
| 22 | #include <linux/bug.h> |
| 23 | |
| 24 | /* |
| 25 | * The idle-as-is "state" is not an actual state that may be selected, it |
| 26 | * only implies that the state should not be changed. So, use that state |
| 27 | * as indication that the cached state of the multiplexer is unknown. |
| 28 | */ |
| 29 | #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS |
| 30 | |
| 31 | /** |
| 32 | * mux_control_ops() - Get the mux_control ops. |
| 33 | * @dev: The client device. |
| 34 | * |
| 35 | * Return: A pointer to the 'mux_control_ops' of the device. |
| 36 | */ |
| 37 | static inline const struct mux_control_ops *mux_dev_ops(struct udevice *dev) |
| 38 | { |
| 39 | return (const struct mux_control_ops *)dev->driver->ops; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * mux_control_set() - Set the state of the given mux controller. |
| 44 | * @mux: A multiplexer control |
| 45 | * @state: The new requested state. |
| 46 | * |
| 47 | * Return: 0 if OK, or a negative error code. |
| 48 | */ |
| 49 | static int mux_control_set(struct mux_control *mux, int state) |
| 50 | { |
| 51 | int ret = mux_dev_ops(mux->dev)->set(mux, state); |
| 52 | |
| 53 | mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state; |
| 54 | |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | unsigned int mux_control_states(struct mux_control *mux) |
| 59 | { |
| 60 | return mux->states; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * __mux_control_select() - Select the given multiplexer state. |
| 65 | * @mux: The mux-control to request a change of state from. |
| 66 | * @state: The new requested state. |
| 67 | * |
| 68 | * Try to set the mux to the requested state. If not, try to revert if |
| 69 | * appropriate. |
| 70 | */ |
| 71 | static int __mux_control_select(struct mux_control *mux, int state) |
| 72 | { |
| 73 | int ret; |
| 74 | |
| 75 | if (WARN_ON(state < 0 || state >= mux->states)) |
| 76 | return -EINVAL; |
| 77 | |
| 78 | if (mux->cached_state == state) |
| 79 | return 0; |
| 80 | |
| 81 | ret = mux_control_set(mux, state); |
| 82 | if (ret >= 0) |
| 83 | return 0; |
| 84 | |
| 85 | /* The mux update failed, try to revert if appropriate... */ |
| 86 | if (mux->idle_state != MUX_IDLE_AS_IS) |
| 87 | mux_control_set(mux, mux->idle_state); |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | int mux_control_select(struct mux_control *mux, unsigned int state) |
| 93 | { |
| 94 | int ret; |
| 95 | |
| 96 | if (mux->in_use) |
| 97 | return -EBUSY; |
| 98 | |
| 99 | ret = __mux_control_select(mux, state); |
| 100 | |
| 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | |
| 104 | mux->in_use = true; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int mux_control_deselect(struct mux_control *mux) |
| 110 | { |
| 111 | int ret = 0; |
| 112 | |
| 113 | if (mux->idle_state != MUX_IDLE_AS_IS && |
| 114 | mux->idle_state != mux->cached_state) |
| 115 | ret = mux_control_set(mux, mux->idle_state); |
| 116 | |
| 117 | mux->in_use = false; |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static int mux_of_xlate_default(struct mux_chip *mux_chip, |
| 123 | struct ofnode_phandle_args *args, |
| 124 | struct mux_control **muxp) |
| 125 | { |
| 126 | struct mux_control *mux; |
| 127 | int id; |
| 128 | |
| 129 | log_debug("%s(muxp=%p)\n", __func__, muxp); |
| 130 | |
| 131 | if (args->args_count > 1) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 132 | debug("Invalid args_count: %d\n", args->args_count); |
Jean-Jacques Hiblot | f4f9056 | 2020-10-16 16:16:30 +0530 | [diff] [blame] | 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | if (args->args_count) |
| 137 | id = args->args[0]; |
| 138 | else |
| 139 | id = 0; |
| 140 | |
| 141 | if (id >= mux_chip->controllers) { |
| 142 | pr_err("bad mux controller %u specified in %s\n", |
| 143 | id, ofnode_get_name(args->node)); |
| 144 | return -ERANGE; |
| 145 | } |
| 146 | |
| 147 | mux = &mux_chip->mux[id]; |
| 148 | mux->id = id; |
| 149 | *muxp = mux; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * mux_get_by_indexed_prop() - Get a mux control by integer index |
| 155 | * @dev: The client device. |
| 156 | * @prop_name: Name of the device tree property. |
| 157 | * @index: The index of the mux to get |
| 158 | * @mux: A pointer to the 'mux_control' struct to initialize. |
| 159 | * |
| 160 | * Return: 0 of OK, -errno otherwise. |
| 161 | */ |
| 162 | static int mux_get_by_indexed_prop(struct udevice *dev, const char *prop_name, |
| 163 | int index, struct mux_control **mux) |
| 164 | { |
| 165 | int ret; |
| 166 | struct ofnode_phandle_args args; |
| 167 | struct udevice *dev_mux; |
| 168 | const struct mux_control_ops *ops; |
| 169 | struct mux_chip *mux_chip; |
| 170 | |
| 171 | log_debug("%s(dev=%p, index=%d, mux=%p)\n", __func__, dev, index, mux); |
| 172 | |
| 173 | ret = dev_read_phandle_with_args(dev, prop_name, "#mux-control-cells", |
| 174 | 0, index, &args); |
| 175 | if (ret) { |
| 176 | debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n", |
| 177 | __func__, ret); |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | ret = uclass_get_device_by_ofnode(UCLASS_MUX, args.node, &dev_mux); |
| 182 | if (ret) { |
| 183 | debug("%s: uclass_get_device_by_ofnode failed: err=%d\n", |
| 184 | __func__, ret); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | mux_chip = dev_get_uclass_priv(dev_mux); |
| 189 | |
| 190 | ops = mux_dev_ops(dev_mux); |
| 191 | if (ops->of_xlate) |
| 192 | ret = ops->of_xlate(mux_chip, &args, mux); |
| 193 | else |
| 194 | ret = mux_of_xlate_default(mux_chip, &args, mux); |
| 195 | if (ret) { |
| 196 | debug("of_xlate() failed: %d\n", ret); |
| 197 | return ret; |
| 198 | } |
| 199 | (*mux)->dev = dev_mux; |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int mux_get_by_index(struct udevice *dev, int index, struct mux_control **mux) |
| 205 | { |
| 206 | return mux_get_by_indexed_prop(dev, "mux-controls", index, mux); |
| 207 | } |
| 208 | |
| 209 | int mux_control_get(struct udevice *dev, const char *name, |
| 210 | struct mux_control **mux) |
| 211 | { |
| 212 | int index; |
| 213 | |
| 214 | debug("%s(dev=%p, name=%s, mux=%p)\n", __func__, dev, name, mux); |
| 215 | |
| 216 | index = dev_read_stringlist_search(dev, "mux-control-names", name); |
| 217 | if (index < 0) { |
| 218 | debug("fdt_stringlist_search() failed: %d\n", index); |
| 219 | return index; |
| 220 | } |
| 221 | |
| 222 | return mux_get_by_index(dev, index, mux); |
| 223 | } |
| 224 | |
| 225 | void mux_control_put(struct mux_control *mux) |
| 226 | { |
| 227 | mux_control_deselect(mux); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * devm_mux_control_release() - Release the given managed mux. |
| 232 | * @dev: The client device. |
| 233 | * @res: Pointer to the mux to be released. |
| 234 | * |
| 235 | * This function is called by devres to release the mux. It reverses the |
| 236 | * effects of mux_control_get(). |
| 237 | */ |
| 238 | static void devm_mux_control_release(struct udevice *dev, void *res) |
| 239 | { |
| 240 | mux_control_put(*(struct mux_control **)res); |
| 241 | } |
| 242 | |
| 243 | struct mux_control *devm_mux_control_get(struct udevice *dev, const char *id) |
| 244 | { |
| 245 | int rc; |
| 246 | struct mux_control **mux; |
| 247 | |
| 248 | mux = devres_alloc(devm_mux_control_release, |
| 249 | sizeof(struct mux_control *), __GFP_ZERO); |
| 250 | if (unlikely(!mux)) |
| 251 | return ERR_PTR(-ENOMEM); |
| 252 | |
| 253 | rc = mux_control_get(dev, id, mux); |
| 254 | if (rc) |
| 255 | return ERR_PTR(rc); |
| 256 | |
| 257 | devres_add(dev, mux); |
| 258 | return *mux; |
| 259 | } |
| 260 | |
| 261 | int mux_alloc_controllers(struct udevice *dev, unsigned int controllers) |
| 262 | { |
| 263 | int i; |
| 264 | struct mux_chip *mux_chip = dev_get_uclass_priv(dev); |
| 265 | |
| 266 | mux_chip->mux = devm_kmalloc(dev, |
| 267 | sizeof(struct mux_control) * controllers, |
| 268 | __GFP_ZERO); |
| 269 | if (!mux_chip->mux) |
| 270 | return -ENOMEM; |
| 271 | |
| 272 | mux_chip->controllers = controllers; |
| 273 | |
| 274 | for (i = 0; i < mux_chip->controllers; ++i) { |
| 275 | struct mux_control *mux = &mux_chip->mux[i]; |
| 276 | |
| 277 | mux->dev = dev; |
| 278 | mux->cached_state = MUX_CACHE_UNKNOWN; |
| 279 | mux->idle_state = MUX_IDLE_AS_IS; |
| 280 | mux->in_use = false; |
| 281 | mux->id = i; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int mux_uclass_post_probe(struct udevice *dev) |
| 288 | { |
| 289 | int i, ret; |
| 290 | struct mux_chip *mux_chip = dev_get_uclass_priv(dev); |
| 291 | |
| 292 | /* Set all mux controllers to their idle state. */ |
| 293 | for (i = 0; i < mux_chip->controllers; ++i) { |
| 294 | struct mux_control *mux = &mux_chip->mux[i]; |
| 295 | |
| 296 | if (mux->idle_state == mux->cached_state) |
| 297 | continue; |
| 298 | |
| 299 | ret = mux_control_set(mux, mux->idle_state); |
| 300 | if (ret < 0) { |
| 301 | dev_err(dev, "unable to set idle state\n"); |
| 302 | return ret; |
| 303 | } |
| 304 | } |
| 305 | return 0; |
| 306 | } |
| 307 | |
Jean-Jacques Hiblot | 0c1b557 | 2020-10-16 16:16:31 +0530 | [diff] [blame] | 308 | int dm_mux_init(void) |
| 309 | { |
| 310 | struct uclass *uc; |
| 311 | struct udevice *dev; |
| 312 | int ret; |
| 313 | |
| 314 | ret = uclass_get(UCLASS_MUX, &uc); |
| 315 | if (ret < 0) { |
| 316 | log_debug("unable to get MUX uclass\n"); |
| 317 | return ret; |
| 318 | } |
| 319 | uclass_foreach_dev(dev, uc) { |
Roger Quadros | f876e7f | 2024-01-31 15:33:46 +0200 | [diff] [blame] | 320 | if (dev_read_bool(dev, "u-boot,mux-autoprobe") || |
| 321 | dev_read_bool(dev, "idle-states")) { |
Jean-Jacques Hiblot | 0c1b557 | 2020-10-16 16:16:31 +0530 | [diff] [blame] | 322 | ret = device_probe(dev); |
| 323 | if (ret) |
| 324 | log_debug("unable to probe device %s\n", |
| 325 | dev->name); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Jean-Jacques Hiblot | f4f9056 | 2020-10-16 16:16:30 +0530 | [diff] [blame] | 332 | UCLASS_DRIVER(mux) = { |
| 333 | .id = UCLASS_MUX, |
| 334 | .name = "mux", |
| 335 | .post_probe = mux_uclass_post_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 336 | .per_device_auto = sizeof(struct mux_chip), |
Jean-Jacques Hiblot | f4f9056 | 2020-10-16 16:16:30 +0530 | [diff] [blame] | 337 | }; |