1 // SPDX-License-Identifier: GPL-2.0
3 * Multiplexer subsystem
5 * Copyright (C) 2017 Axentia Technologies AB
7 * Author: Peter Rosin <peda@axentia.se>
10 #define pr_fmt(fmt) "mux-core: " fmt
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/export.h>
16 #include <linux/idr.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mux/consumer.h>
20 #include <linux/mux/driver.h>
22 #include <linux/of_platform.h>
23 #include <linux/slab.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 * struct mux_state - Represents a mux controller state specific to a given
35 * @mux: Pointer to a mux controller.
36 * @state: State of the mux to be selected.
38 * This structure is specific to the consumer that acquires it and has
39 * information specific to that consumer.
42 struct mux_control *mux;
46 static struct class mux_class = {
50 static DEFINE_IDA(mux_ida);
52 static int __init mux_init(void)
55 return class_register(&mux_class);
58 static void __exit mux_exit(void)
60 class_unregister(&mux_class);
61 ida_destroy(&mux_ida);
64 static void mux_chip_release(struct device *dev)
66 struct mux_chip *mux_chip = to_mux_chip(dev);
68 ida_simple_remove(&mux_ida, mux_chip->id);
72 static const struct device_type mux_type = {
74 .release = mux_chip_release,
78 * mux_chip_alloc() - Allocate a mux-chip.
79 * @dev: The parent device implementing the mux interface.
80 * @controllers: The number of mux controllers to allocate for this chip.
81 * @sizeof_priv: Size of extra memory area for private use by the caller.
83 * After allocating the mux-chip with the desired number of mux controllers
84 * but before registering the chip, the mux driver is required to configure
85 * the number of valid mux states in the mux_chip->mux[N].states members and
86 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
87 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
88 * provide a pointer to the operations struct in the mux_chip->ops member
89 * before registering the mux-chip with mux_chip_register.
91 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
93 struct mux_chip *mux_chip_alloc(struct device *dev,
94 unsigned int controllers, size_t sizeof_priv)
96 struct mux_chip *mux_chip;
99 if (WARN_ON(!dev || !controllers))
100 return ERR_PTR(-EINVAL);
102 mux_chip = kzalloc(sizeof(*mux_chip) +
103 controllers * sizeof(*mux_chip->mux) +
104 sizeof_priv, GFP_KERNEL);
106 return ERR_PTR(-ENOMEM);
108 mux_chip->mux = (struct mux_control *)(mux_chip + 1);
109 mux_chip->dev.class = &mux_class;
110 mux_chip->dev.type = &mux_type;
111 mux_chip->dev.parent = dev;
112 mux_chip->dev.of_node = dev->of_node;
113 dev_set_drvdata(&mux_chip->dev, mux_chip);
115 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
116 if (mux_chip->id < 0) {
117 int err = mux_chip->id;
119 pr_err("muxchipX failed to get a device id\n");
123 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
125 mux_chip->controllers = controllers;
126 for (i = 0; i < controllers; ++i) {
127 struct mux_control *mux = &mux_chip->mux[i];
129 mux->chip = mux_chip;
130 sema_init(&mux->lock, 1);
131 mux->cached_state = MUX_CACHE_UNKNOWN;
132 mux->idle_state = MUX_IDLE_AS_IS;
133 mux->last_change = ktime_get();
136 device_initialize(&mux_chip->dev);
140 EXPORT_SYMBOL_GPL(mux_chip_alloc);
142 static int mux_control_set(struct mux_control *mux, int state)
144 int ret = mux->chip->ops->set(mux, state);
146 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
148 mux->last_change = ktime_get();
154 * mux_chip_register() - Register a mux-chip, thus readying the controllers
156 * @mux_chip: The mux-chip to register.
158 * Do not retry registration of the same mux-chip on failure. You should
159 * instead put it away with mux_chip_free() and allocate a new one, if you
160 * for some reason would like to retry registration.
162 * Return: Zero on success or a negative errno on error.
164 int mux_chip_register(struct mux_chip *mux_chip)
169 for (i = 0; i < mux_chip->controllers; ++i) {
170 struct mux_control *mux = &mux_chip->mux[i];
172 if (mux->idle_state == mux->cached_state)
175 ret = mux_control_set(mux, mux->idle_state);
177 dev_err(&mux_chip->dev, "unable to set idle state\n");
182 ret = device_add(&mux_chip->dev);
184 dev_err(&mux_chip->dev,
185 "device_add failed in %s: %d\n", __func__, ret);
188 EXPORT_SYMBOL_GPL(mux_chip_register);
191 * mux_chip_unregister() - Take the mux-chip off-line.
192 * @mux_chip: The mux-chip to unregister.
194 * mux_chip_unregister() reverses the effects of mux_chip_register().
195 * But not completely, you should not try to call mux_chip_register()
196 * on a mux-chip that has been registered before.
198 void mux_chip_unregister(struct mux_chip *mux_chip)
200 device_del(&mux_chip->dev);
202 EXPORT_SYMBOL_GPL(mux_chip_unregister);
205 * mux_chip_free() - Free the mux-chip for good.
206 * @mux_chip: The mux-chip to free.
208 * mux_chip_free() reverses the effects of mux_chip_alloc().
210 void mux_chip_free(struct mux_chip *mux_chip)
215 put_device(&mux_chip->dev);
217 EXPORT_SYMBOL_GPL(mux_chip_free);
219 static void devm_mux_chip_release(struct device *dev, void *res)
221 struct mux_chip *mux_chip = *(struct mux_chip **)res;
223 mux_chip_free(mux_chip);
227 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
228 * @dev: The parent device implementing the mux interface.
229 * @controllers: The number of mux controllers to allocate for this chip.
230 * @sizeof_priv: Size of extra memory area for private use by the caller.
232 * See mux_chip_alloc() for more details.
234 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
236 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
237 unsigned int controllers,
240 struct mux_chip **ptr, *mux_chip;
242 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
244 return ERR_PTR(-ENOMEM);
246 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
247 if (IS_ERR(mux_chip)) {
253 devres_add(dev, ptr);
257 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
259 static void devm_mux_chip_reg_release(struct device *dev, void *res)
261 struct mux_chip *mux_chip = *(struct mux_chip **)res;
263 mux_chip_unregister(mux_chip);
267 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
268 * @dev: The parent device implementing the mux interface.
269 * @mux_chip: The mux-chip to register.
271 * See mux_chip_register() for more details.
273 * Return: Zero on success or a negative errno on error.
275 int devm_mux_chip_register(struct device *dev,
276 struct mux_chip *mux_chip)
278 struct mux_chip **ptr;
281 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
285 res = mux_chip_register(mux_chip);
292 devres_add(dev, ptr);
296 EXPORT_SYMBOL_GPL(devm_mux_chip_register);
299 * mux_control_states() - Query the number of multiplexer states.
300 * @mux: The mux-control to query.
302 * Return: The number of multiplexer states.
304 unsigned int mux_control_states(struct mux_control *mux)
308 EXPORT_SYMBOL_GPL(mux_control_states);
311 * The mux->lock must be down when calling this function.
313 static int __mux_control_select(struct mux_control *mux, int state)
317 if (WARN_ON(state < 0 || state >= mux->states))
320 if (mux->cached_state == state)
323 ret = mux_control_set(mux, state);
327 /* The mux update failed, try to revert if appropriate... */
328 if (mux->idle_state != MUX_IDLE_AS_IS)
329 mux_control_set(mux, mux->idle_state);
334 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
342 delayend = ktime_add_us(mux->last_change, delay_us);
343 remaining = ktime_us_delta(delayend, ktime_get());
349 * mux_control_select_delay() - Select the given multiplexer state.
350 * @mux: The mux-control to request a change of state from.
351 * @state: The new requested state.
352 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
354 * On successfully selecting the mux-control state, it will be locked until
355 * there is a call to mux_control_deselect(). If the mux-control is already
356 * selected when mux_control_select() is called, the caller will be blocked
357 * until mux_control_deselect() or mux_state_deselect() is called (by someone
360 * Therefore, make sure to call mux_control_deselect() when the operation is
361 * complete and the mux-control is free for others to use, but do not call
362 * mux_control_deselect() if mux_control_select() fails.
364 * Return: 0 when the mux-control state has the requested state or a negative
367 int mux_control_select_delay(struct mux_control *mux, unsigned int state,
368 unsigned int delay_us)
372 ret = down_killable(&mux->lock);
376 ret = __mux_control_select(mux, state);
378 mux_control_delay(mux, delay_us);
385 EXPORT_SYMBOL_GPL(mux_control_select_delay);
388 * mux_state_select_delay() - Select the given multiplexer state.
389 * @mstate: The mux-state to select.
390 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
392 * On successfully selecting the mux-state, its mux-control will be locked
393 * until there is a call to mux_state_deselect(). If the mux-control is already
394 * selected when mux_state_select() is called, the caller will be blocked
395 * until mux_state_deselect() or mux_control_deselect() is called (by someone
398 * Therefore, make sure to call mux_state_deselect() when the operation is
399 * complete and the mux-control is free for others to use, but do not call
400 * mux_state_deselect() if mux_state_select() fails.
402 * Return: 0 when the mux-state has been selected or a negative
405 int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us)
407 return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
409 EXPORT_SYMBOL_GPL(mux_state_select_delay);
412 * mux_control_try_select_delay() - Try to select the given multiplexer state.
413 * @mux: The mux-control to request a change of state from.
414 * @state: The new requested state.
415 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
417 * On successfully selecting the mux-control state, it will be locked until
418 * mux_control_deselect() is called.
420 * Therefore, make sure to call mux_control_deselect() when the operation is
421 * complete and the mux-control is free for others to use, but do not call
422 * mux_control_deselect() if mux_control_try_select() fails.
424 * Return: 0 when the mux-control state has the requested state or a negative
425 * errno on error. Specifically -EBUSY if the mux-control is contended.
427 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
428 unsigned int delay_us)
432 if (down_trylock(&mux->lock))
435 ret = __mux_control_select(mux, state);
437 mux_control_delay(mux, delay_us);
444 EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
447 * mux_state_try_select_delay() - Try to select the given multiplexer state.
448 * @mstate: The mux-state to select.
449 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
451 * On successfully selecting the mux-state, its mux-control will be locked
452 * until mux_state_deselect() is called.
454 * Therefore, make sure to call mux_state_deselect() when the operation is
455 * complete and the mux-control is free for others to use, but do not call
456 * mux_state_deselect() if mux_state_try_select() fails.
458 * Return: 0 when the mux-state has been selected or a negative errno on
459 * error. Specifically -EBUSY if the mux-control is contended.
461 int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us)
463 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
465 EXPORT_SYMBOL_GPL(mux_state_try_select_delay);
468 * mux_control_deselect() - Deselect the previously selected multiplexer state.
469 * @mux: The mux-control to deselect.
471 * It is required that a single call is made to mux_control_deselect() for
472 * each and every successful call made to either of mux_control_select() or
473 * mux_control_try_select().
475 * Return: 0 on success and a negative errno on error. An error can only
476 * occur if the mux has an idle state. Note that even if an error occurs, the
477 * mux-control is unlocked and is thus free for the next access.
479 int mux_control_deselect(struct mux_control *mux)
483 if (mux->idle_state != MUX_IDLE_AS_IS &&
484 mux->idle_state != mux->cached_state)
485 ret = mux_control_set(mux, mux->idle_state);
491 EXPORT_SYMBOL_GPL(mux_control_deselect);
494 * mux_state_deselect() - Deselect the previously selected multiplexer state.
495 * @mstate: The mux-state to deselect.
497 * It is required that a single call is made to mux_state_deselect() for
498 * each and every successful call made to either of mux_state_select() or
499 * mux_state_try_select().
501 * Return: 0 on success and a negative errno on error. An error can only
502 * occur if the mux has an idle state. Note that even if an error occurs, the
503 * mux-control is unlocked and is thus free for the next access.
505 int mux_state_deselect(struct mux_state *mstate)
507 return mux_control_deselect(mstate->mux);
509 EXPORT_SYMBOL_GPL(mux_state_deselect);
511 /* Note this function returns a reference to the mux_chip dev. */
512 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
516 dev = class_find_device_by_of_node(&mux_class, np);
518 return dev ? to_mux_chip(dev) : NULL;
522 * mux_get() - Get the mux-control for a device.
523 * @dev: The device that needs a mux-control.
524 * @mux_name: The name identifying the mux-control.
525 * @state: Pointer to where the requested state is returned, or NULL when
526 * the required multiplexer states are handled by other means.
528 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
530 static struct mux_control *mux_get(struct device *dev, const char *mux_name,
533 struct device_node *np = dev->of_node;
534 struct of_phandle_args args;
535 struct mux_chip *mux_chip;
536 unsigned int controller;
542 index = of_property_match_string(np, "mux-state-names",
545 index = of_property_match_string(np, "mux-control-names",
548 dev_err(dev, "mux controller '%s' not found\n",
550 return ERR_PTR(index);
555 ret = of_parse_phandle_with_args(np,
556 "mux-states", "#mux-state-cells",
559 ret = of_parse_phandle_with_args(np,
560 "mux-controls", "#mux-control-cells",
563 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
564 np, state ? "state" : "control", mux_name ?: "", index);
568 mux_chip = of_find_mux_chip_by_node(args.np);
569 of_node_put(args.np);
571 return ERR_PTR(-EPROBE_DEFER);
575 if (args.args_count > 2 || args.args_count == 0 ||
576 (args.args_count < 2 && mux_chip->controllers > 1)) {
577 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
579 put_device(&mux_chip->dev);
580 return ERR_PTR(-EINVAL);
583 if (args.args_count == 2) {
584 controller = args.args[0];
585 *state = args.args[1];
587 *state = args.args[0];
591 if (args.args_count > 1 ||
592 (!args.args_count && mux_chip->controllers > 1)) {
593 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
595 put_device(&mux_chip->dev);
596 return ERR_PTR(-EINVAL);
600 controller = args.args[0];
603 if (controller >= mux_chip->controllers) {
604 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
605 np, controller, args.np);
606 put_device(&mux_chip->dev);
607 return ERR_PTR(-EINVAL);
610 return &mux_chip->mux[controller];
614 * mux_control_get() - Get the mux-control for a device.
615 * @dev: The device that needs a mux-control.
616 * @mux_name: The name identifying the mux-control.
618 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
620 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
622 return mux_get(dev, mux_name, NULL);
624 EXPORT_SYMBOL_GPL(mux_control_get);
627 * mux_control_put() - Put away the mux-control for good.
628 * @mux: The mux-control to put away.
630 * mux_control_put() reverses the effects of mux_control_get().
632 void mux_control_put(struct mux_control *mux)
634 put_device(&mux->chip->dev);
636 EXPORT_SYMBOL_GPL(mux_control_put);
638 static void devm_mux_control_release(struct device *dev, void *res)
640 struct mux_control *mux = *(struct mux_control **)res;
642 mux_control_put(mux);
646 * devm_mux_control_get() - Get the mux-control for a device, with resource
648 * @dev: The device that needs a mux-control.
649 * @mux_name: The name identifying the mux-control.
651 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
653 struct mux_control *devm_mux_control_get(struct device *dev,
654 const char *mux_name)
656 struct mux_control **ptr, *mux;
658 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
660 return ERR_PTR(-ENOMEM);
662 mux = mux_control_get(dev, mux_name);
669 devres_add(dev, ptr);
673 EXPORT_SYMBOL_GPL(devm_mux_control_get);
676 * mux_state_get() - Get the mux-state for a device.
677 * @dev: The device that needs a mux-state.
678 * @mux_name: The name identifying the mux-state.
680 * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno.
682 static struct mux_state *mux_state_get(struct device *dev, const char *mux_name)
684 struct mux_state *mstate;
686 mstate = kzalloc(sizeof(*mstate), GFP_KERNEL);
688 return ERR_PTR(-ENOMEM);
690 mstate->mux = mux_get(dev, mux_name, &mstate->state);
691 if (IS_ERR(mstate->mux)) {
692 int err = PTR_ERR(mstate->mux);
702 * mux_state_put() - Put away the mux-state for good.
703 * @mstate: The mux-state to put away.
705 * mux_state_put() reverses the effects of mux_state_get().
707 static void mux_state_put(struct mux_state *mstate)
709 mux_control_put(mstate->mux);
713 static void devm_mux_state_release(struct device *dev, void *res)
715 struct mux_state *mstate = *(struct mux_state **)res;
717 mux_state_put(mstate);
721 * devm_mux_state_get() - Get the mux-state for a device, with resource
723 * @dev: The device that needs a mux-control.
724 * @mux_name: The name identifying the mux-control.
726 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
728 struct mux_state *devm_mux_state_get(struct device *dev,
729 const char *mux_name)
731 struct mux_state **ptr, *mstate;
733 ptr = devres_alloc(devm_mux_state_release, sizeof(*ptr), GFP_KERNEL);
735 return ERR_PTR(-ENOMEM);
737 mstate = mux_state_get(dev, mux_name);
738 if (IS_ERR(mstate)) {
744 devres_add(dev, ptr);
748 EXPORT_SYMBOL_GPL(devm_mux_state_get);
751 * Using subsys_initcall instead of module_init here to try to ensure - for
752 * the non-modular case - that the subsystem is initialized when mux consumers
753 * and mux controllers start to use it.
754 * For the modular case, the ordering is ensured with module dependencies.
756 subsys_initcall(mux_init);
757 module_exit(mux_exit);
759 MODULE_DESCRIPTION("Multiplexer subsystem");
760 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
761 MODULE_LICENSE("GPL v2");