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>
14 #define LOG_CATEGORY UCLASS_MUX
18 #include <mux-internal.h>
19 #include <dm/device-internal.h>
20 #include <dm/device_compat.h>
21 #include <dm/devres.h>
22 #include <dt-bindings/mux/mux.h>
23 #include <linux/bug.h>
26 * The idle-as-is "state" is not an actual state that may be selected, it
27 * only implies that the state should not be changed. So, use that state
28 * as indication that the cached state of the multiplexer is unknown.
30 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
33 * mux_control_ops() - Get the mux_control ops.
34 * @dev: The client device.
36 * Return: A pointer to the 'mux_control_ops' of the device.
38 static inline const struct mux_control_ops *mux_dev_ops(struct udevice *dev)
40 return (const struct mux_control_ops *)dev->driver->ops;
44 * mux_control_set() - Set the state of the given mux controller.
45 * @mux: A multiplexer control
46 * @state: The new requested state.
48 * Return: 0 if OK, or a negative error code.
50 static int mux_control_set(struct mux_control *mux, int state)
52 int ret = mux_dev_ops(mux->dev)->set(mux, state);
54 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
59 unsigned int mux_control_states(struct mux_control *mux)
65 * __mux_control_select() - Select the given multiplexer state.
66 * @mux: The mux-control to request a change of state from.
67 * @state: The new requested state.
69 * Try to set the mux to the requested state. If not, try to revert if
72 static int __mux_control_select(struct mux_control *mux, int state)
76 if (WARN_ON(state < 0 || state >= mux->states))
79 if (mux->cached_state == state)
82 ret = mux_control_set(mux, state);
86 /* The mux update failed, try to revert if appropriate... */
87 if (mux->idle_state != MUX_IDLE_AS_IS)
88 mux_control_set(mux, mux->idle_state);
93 int mux_control_select(struct mux_control *mux, unsigned int state)
100 ret = __mux_control_select(mux, state);
110 int mux_control_deselect(struct mux_control *mux)
114 if (mux->idle_state != MUX_IDLE_AS_IS &&
115 mux->idle_state != mux->cached_state)
116 ret = mux_control_set(mux, mux->idle_state);
123 static int mux_of_xlate_default(struct mux_chip *mux_chip,
124 struct ofnode_phandle_args *args,
125 struct mux_control **muxp)
127 struct mux_control *mux;
130 log_debug("%s(muxp=%p)\n", __func__, muxp);
132 if (args->args_count > 1) {
133 debug("Invalid args_count: %d\n", args->args_count);
137 if (args->args_count)
142 if (id >= mux_chip->controllers) {
143 pr_err("bad mux controller %u specified in %s\n",
144 id, ofnode_get_name(args->node));
148 mux = &mux_chip->mux[id];
155 * mux_get_by_indexed_prop() - Get a mux control by integer index
156 * @dev: The client device.
157 * @prop_name: Name of the device tree property.
158 * @index: The index of the mux to get
159 * @mux: A pointer to the 'mux_control' struct to initialize.
161 * Return: 0 of OK, -errno otherwise.
163 static int mux_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
164 int index, struct mux_control **mux)
167 struct ofnode_phandle_args args;
168 struct udevice *dev_mux;
169 const struct mux_control_ops *ops;
170 struct mux_chip *mux_chip;
172 log_debug("%s(dev=%p, index=%d, mux=%p)\n", __func__, dev, index, mux);
174 ret = dev_read_phandle_with_args(dev, prop_name, "#mux-control-cells",
177 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
182 ret = uclass_get_device_by_ofnode(UCLASS_MUX, args.node, &dev_mux);
184 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
189 mux_chip = dev_get_uclass_priv(dev_mux);
191 ops = mux_dev_ops(dev_mux);
193 ret = ops->of_xlate(mux_chip, &args, mux);
195 ret = mux_of_xlate_default(mux_chip, &args, mux);
197 debug("of_xlate() failed: %d\n", ret);
200 (*mux)->dev = dev_mux;
205 int mux_get_by_index(struct udevice *dev, int index, struct mux_control **mux)
207 return mux_get_by_indexed_prop(dev, "mux-controls", index, mux);
210 int mux_control_get(struct udevice *dev, const char *name,
211 struct mux_control **mux)
215 debug("%s(dev=%p, name=%s, mux=%p)\n", __func__, dev, name, mux);
217 index = dev_read_stringlist_search(dev, "mux-control-names", name);
219 debug("fdt_stringlist_search() failed: %d\n", index);
223 return mux_get_by_index(dev, index, mux);
226 void mux_control_put(struct mux_control *mux)
228 mux_control_deselect(mux);
232 * devm_mux_control_release() - Release the given managed mux.
233 * @dev: The client device.
234 * @res: Pointer to the mux to be released.
236 * This function is called by devres to release the mux. It reverses the
237 * effects of mux_control_get().
239 static void devm_mux_control_release(struct udevice *dev, void *res)
241 mux_control_put(*(struct mux_control **)res);
244 struct mux_control *devm_mux_control_get(struct udevice *dev, const char *id)
247 struct mux_control **mux;
249 mux = devres_alloc(devm_mux_control_release,
250 sizeof(struct mux_control *), __GFP_ZERO);
252 return ERR_PTR(-ENOMEM);
254 rc = mux_control_get(dev, id, mux);
258 devres_add(dev, mux);
262 int mux_alloc_controllers(struct udevice *dev, unsigned int controllers)
265 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
267 mux_chip->mux = devm_kmalloc(dev,
268 sizeof(struct mux_control) * controllers,
273 mux_chip->controllers = controllers;
275 for (i = 0; i < mux_chip->controllers; ++i) {
276 struct mux_control *mux = &mux_chip->mux[i];
279 mux->cached_state = MUX_CACHE_UNKNOWN;
280 mux->idle_state = MUX_IDLE_AS_IS;
288 static int mux_uclass_post_probe(struct udevice *dev)
291 struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
293 /* Set all mux controllers to their idle state. */
294 for (i = 0; i < mux_chip->controllers; ++i) {
295 struct mux_control *mux = &mux_chip->mux[i];
297 if (mux->idle_state == mux->cached_state)
300 ret = mux_control_set(mux, mux->idle_state);
302 dev_err(dev, "unable to set idle state\n");
309 int dm_mux_init(void)
315 ret = uclass_get(UCLASS_MUX, &uc);
317 log_debug("unable to get MUX uclass\n");
320 uclass_foreach_dev(dev, uc) {
321 if (dev_read_bool(dev, "u-boot,mux-autoprobe")) {
322 ret = device_probe(dev);
324 log_debug("unable to probe device %s\n",
332 UCLASS_DRIVER(mux) = {
335 .post_probe = mux_uclass_post_probe,
336 .per_device_auto = sizeof(struct mux_chip),