1 // SPDX-License-Identifier: GPL-2.0
3 * Multiplexer subsystem
5 * Based on the linux multiplexer framework
7 * Copyright (C) 2017 Axentia Technologies AB
8 * Author: Peter Rosin <peda@axentia.se>
10 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
11 * Jean-Jacques Hiblot <jjhiblot@ti.com>
16 #include <mux-internal.h>
17 #include <dm/device-internal.h>
18 #include <dm/device_compat.h>
19 #include <dm/devres.h>
20 #include <dt-bindings/mux/mux.h>
21 #include <linux/bug.h>
24 * The idle-as-is "state" is not an actual state that may be selected, it
25 * only implies that the state should not be changed. So, use that state
26 * as indication that the cached state of the multiplexer is unknown.
28 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
31 * mux_control_ops() - Get the mux_control ops.
32 * @dev: The client device.
34 * Return: A pointer to the 'mux_control_ops' of the device.
36 static inline const struct mux_control_ops *mux_dev_ops(struct udevice *dev)
38 return (const struct mux_control_ops *)dev->driver->ops;
42 * mux_control_set() - Set the state of the given mux controller.
43 * @mux: A multiplexer control
44 * @state: The new requested state.
46 * Return: 0 if OK, or a negative error code.
48 static int mux_control_set(struct mux_control *mux, int state)
50 int ret = mux_dev_ops(mux->dev)->set(mux, state);
52 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
57 unsigned int mux_control_states(struct mux_control *mux)
63 * __mux_control_select() - Select the given multiplexer state.
64 * @mux: The mux-control to request a change of state from.
65 * @state: The new requested state.
67 * Try to set the mux to the requested state. If not, try to revert if
70 static int __mux_control_select(struct mux_control *mux, int state)
74 if (WARN_ON(state < 0 || state >= mux->states))
77 if (mux->cached_state == state)
80 ret = mux_control_set(mux, state);
84 /* The mux update failed, try to revert if appropriate... */
85 if (mux->idle_state != MUX_IDLE_AS_IS)
86 mux_control_set(mux, mux->idle_state);
91 int mux_control_select(struct mux_control *mux, unsigned int state)
98 ret = __mux_control_select(mux, state);
108 int mux_control_deselect(struct mux_control *mux)
112 if (mux->idle_state != MUX_IDLE_AS_IS &&
113 mux->idle_state != mux->cached_state)
114 ret = mux_control_set(mux, mux->idle_state);
121 static int mux_of_xlate_default(struct mux_chip *mux_chip,
122 struct ofnode_phandle_args *args,
123 struct mux_control **muxp)
125 struct mux_control *mux;
128 log_debug("%s(muxp=%p)\n", __func__, muxp);
130 if (args->args_count > 1) {
131 debug("Invaild args_count: %d\n", args->args_count);
135 if (args->args_count)
140 if (id >= mux_chip->controllers) {
141 pr_err("bad mux controller %u specified in %s\n",
142 id, ofnode_get_name(args->node));
146 mux = &mux_chip->mux[id];
153 * mux_get_by_indexed_prop() - Get a mux control by integer index
154 * @dev: The client device.
155 * @prop_name: Name of the device tree property.
156 * @index: The index of the mux to get
157 * @mux: A pointer to the 'mux_control' struct to initialize.
159 * Return: 0 of OK, -errno otherwise.
161 static int mux_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
162 int index, struct mux_control **mux)
165 struct ofnode_phandle_args args;
166 struct udevice *dev_mux;
167 const struct mux_control_ops *ops;
168 struct mux_chip *mux_chip;
170 log_debug("%s(dev=%p, index=%d, mux=%p)\n", __func__, dev, index, mux);
172 ret = dev_read_phandle_with_args(dev, prop_name, "#mux-control-cells",
175 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
180 ret = uclass_get_device_by_ofnode(UCLASS_MUX, args.node, &dev_mux);
182 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
187 mux_chip = dev_get_uclass_priv(dev_mux);
189 ops = mux_dev_ops(dev_mux);
191 ret = ops->of_xlate(mux_chip, &args, mux);
193 ret = mux_of_xlate_default(mux_chip, &args, mux);
195 debug("of_xlate() failed: %d\n", ret);
198 (*mux)->dev = dev_mux;
203 int mux_get_by_index(struct udevice *dev, int index, struct mux_control **mux)
205 return mux_get_by_indexed_prop(dev, "mux-controls", index, mux);
208 int mux_control_get(struct udevice *dev, const char *name,
209 struct mux_control **mux)
213 debug("%s(dev=%p, name=%s, mux=%p)\n", __func__, dev, name, mux);
215 index = dev_read_stringlist_search(dev, "mux-control-names", name);
217 debug("fdt_stringlist_search() failed: %d\n", index);
221 return mux_get_by_index(dev, index, mux);
224 void mux_control_put(struct mux_control *mux)
226 mux_control_deselect(mux);
230 * devm_mux_control_release() - Release the given managed mux.
231 * @dev: The client device.
232 * @res: Pointer to the mux to be released.
234 * This function is called by devres to release the mux. It reverses the
235 * effects of mux_control_get().
237 static void devm_mux_control_release(struct udevice *dev, void *res)
239 mux_control_put(*(struct mux_control **)res);
242 struct mux_control *devm_mux_control_get(struct udevice *dev, const char *id)
245 struct mux_control **mux;
247 mux = devres_alloc(devm_mux_control_release,
248 sizeof(struct mux_control *), __GFP_ZERO);
250 return ERR_PTR(-ENOMEM);
252 rc = mux_control_get(dev, id, mux);
256 devres_add(dev, mux);
260 int mux_alloc_controllers(struct udevice *dev, unsigned int controllers)
263 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
265 mux_chip->mux = devm_kmalloc(dev,
266 sizeof(struct mux_control) * controllers,
271 mux_chip->controllers = controllers;
273 for (i = 0; i < mux_chip->controllers; ++i) {
274 struct mux_control *mux = &mux_chip->mux[i];
277 mux->cached_state = MUX_CACHE_UNKNOWN;
278 mux->idle_state = MUX_IDLE_AS_IS;
286 static int mux_uclass_post_probe(struct udevice *dev)
289 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
291 /* Set all mux controllers to their idle state. */
292 for (i = 0; i < mux_chip->controllers; ++i) {
293 struct mux_control *mux = &mux_chip->mux[i];
295 if (mux->idle_state == mux->cached_state)
298 ret = mux_control_set(mux, mux->idle_state);
300 dev_err(dev, "unable to set idle state\n");
307 int dm_mux_init(void)
313 ret = uclass_get(UCLASS_MUX, &uc);
315 log_debug("unable to get MUX uclass\n");
318 uclass_foreach_dev(dev, uc) {
319 if (dev_read_bool(dev, "u-boot,mux-autoprobe")) {
320 ret = device_probe(dev);
322 log_debug("unable to probe device %s\n",
330 UCLASS_DRIVER(mux) = {
333 .post_probe = mux_uclass_post_probe,
334 .per_device_auto_alloc_size = sizeof(struct mux_chip),