Lines Matching full:mux
10 #define pr_fmt(fmt) "mux-core: " fmt
19 #include <linux/mux/consumer.h>
20 #include <linux/mux/driver.h>
32 * struct mux_state - Represents a mux controller state specific to a given
34 * @mux: Pointer to a mux controller.
35 * @state: State of the mux to be selected.
41 struct mux_control *mux;
46 .name = "mux",
50 * struct devm_mux_state_state - Tracks managed resources for mux-state objects.
51 * @mstate: Pointer to a mux state.
82 .name = "mux-chip",
87 * mux_chip_alloc() - Allocate a mux-chip.
88 * @dev: The parent device implementing the mux interface.
89 * @controllers: The number of mux controllers to allocate for this chip.
92 * After allocating the mux-chip with the desired number of mux controllers
93 * but before registering the chip, the mux driver is required to configure
94 * the number of valid mux states in the mux_chip->mux[N].states members and
95 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
96 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
98 * before registering the mux-chip with mux_chip_register.
100 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
111 mux_chip = kzalloc(size_add(struct_size(mux_chip, mux, controllers),
135 struct mux_control *mux = &mux_chip->mux[i];
137 mux->chip = mux_chip;
138 sema_init(&mux->lock, 1);
139 mux->cached_state = MUX_CACHE_UNKNOWN;
140 mux->idle_state = MUX_IDLE_AS_IS;
141 mux->last_change = ktime_get();
150 static int mux_control_set(struct mux_control *mux, int state)
152 int ret = mux->chip->ops->set(mux, state);
154 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
156 mux->last_change = ktime_get();
162 * mux_chip_register() - Register a mux-chip, thus readying the controllers
164 * @mux_chip: The mux-chip to register.
166 * Do not retry registration of the same mux-chip on failure. You should
178 struct mux_control *mux = &mux_chip->mux[i];
180 if (mux->idle_state == mux->cached_state)
183 ret = mux_control_set(mux, mux->idle_state);
199 * mux_chip_unregister() - Take the mux-chip off-line.
200 * @mux_chip: The mux-chip to unregister.
204 * on a mux-chip that has been registered before.
213 * mux_chip_free() - Free the mux-chip for good.
214 * @mux_chip: The mux-chip to free.
236 * @dev: The parent device implementing the mux interface.
237 * @controllers: The number of mux controllers to allocate for this chip.
242 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
276 * @dev: The parent device implementing the mux interface.
277 * @mux_chip: The mux-chip to register.
308 * @mux: The mux-control to query.
312 unsigned int mux_control_states(struct mux_control *mux)
314 return mux->states;
319 * The mux->lock must be down when calling this function.
321 static int __mux_control_select(struct mux_control *mux, int state)
325 if (WARN_ON(state < 0 || state >= mux->states))
328 if (mux->cached_state == state)
331 ret = mux_control_set(mux, state);
335 /* The mux update failed, try to revert if appropriate... */
336 if (mux->idle_state != MUX_IDLE_AS_IS)
337 mux_control_set(mux, mux->idle_state);
342 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
350 delayend = ktime_add_us(mux->last_change, delay_us);
358 * @mux: The mux-control to request a change of state from.
360 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
362 * On successfully selecting the mux-control state, it will be locked until
363 * there is a call to mux_control_deselect(). If the mux-control is already
369 * complete and the mux-control is free for others to use, but do not call
372 * Return: 0 when the mux-control state has the requested state or a negative
375 int mux_control_select_delay(struct mux_control *mux, unsigned int state,
380 ret = down_killable(&mux->lock);
384 ret = __mux_control_select(mux, state);
386 mux_control_delay(mux, delay_us);
389 up(&mux->lock);
397 * @mstate: The mux-state to select.
398 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
400 * On successfully selecting the mux-state, its mux-control will be locked
401 * until there is a call to mux_state_deselect(). If the mux-control is already
407 * complete and the mux-control is free for others to use, but do not call
410 * Return: 0 when the mux-state has been selected or a negative
415 return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
421 * @mux: The mux-control to request a change of state from.
423 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
425 * On successfully selecting the mux-control state, it will be locked until
429 * complete and the mux-control is free for others to use, but do not call
432 * Return: 0 when the mux-control state has the requested state or a negative
433 * errno on error. Specifically -EBUSY if the mux-control is contended.
435 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
440 if (down_trylock(&mux->lock))
443 ret = __mux_control_select(mux, state);
445 mux_control_delay(mux, delay_us);
448 up(&mux->lock);
456 * @mstate: The mux-state to select.
457 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
459 * On successfully selecting the mux-state, its mux-control will be locked
463 * complete and the mux-control is free for others to use, but do not call
466 * Return: 0 when the mux-state has been selected or a negative errno on
467 * error. Specifically -EBUSY if the mux-control is contended.
471 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
477 * @mux: The mux-control to deselect.
484 * occur if the mux has an idle state. Note that even if an error occurs, the
485 * mux-control is unlocked and is thus free for the next access.
487 int mux_control_deselect(struct mux_control *mux)
491 if (mux->idle_state != MUX_IDLE_AS_IS &&
492 mux->idle_state != mux->cached_state)
493 ret = mux_control_set(mux, mux->idle_state);
495 up(&mux->lock);
503 * @mstate: The mux-state to deselect.
510 * occur if the mux has an idle state. Note that even if an error occurs, the
511 * mux-control is unlocked and is thus free for the next access.
515 return mux_control_deselect(mstate->mux);
530 * mux_get() - Get the mux-control for a device.
531 * @dev: The device that needs a mux-control.
532 * @mux_name: The name identifying the mux-control.
535 * @optional: Whether to return NULL and silence errors when mux doesn't exist.
537 * Return: Pointer to the mux-control on success, an ERR_PTR with a negative
538 * errno on error, or NULL if optional is true and mux doesn't exist.
552 index = of_property_match_string(np, "mux-state-names",
555 index = of_property_match_string(np, "mux-control-names",
560 dev_err(dev, "mux controller '%s' not found\n",
568 "mux-states", "#mux-state-cells",
572 "mux-controls", "#mux-control-cells",
578 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
593 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
609 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
620 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
626 return &mux_chip->mux[controller];
630 * mux_control_get() - Get the mux-control for a device.
631 * @dev: The device that needs a mux-control.
632 * @mux_name: The name identifying the mux-control.
634 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
638 struct mux_control *mux = mux_get(dev, mux_name, NULL, false);
640 if (!mux)
643 return mux;
648 * mux_control_get_optional() - Get the optional mux-control for a device.
649 * @dev: The device that needs a mux-control.
650 * @mux_name: The name identifying the mux-control.
652 * Return: Pointer to the mux-control on success, an ERR_PTR with a negative
653 * errno on error, or NULL if mux doesn't exist.
662 * mux_control_put() - Put away the mux-control for good.
663 * @mux: The mux-control to put away.
667 void mux_control_put(struct mux_control *mux)
669 put_device(&mux->chip->dev);
675 struct mux_control *mux = *(struct mux_control **)res;
677 mux_control_put(mux);
681 * devm_mux_control_get() - Get the mux-control for a device, with resource
683 * @dev: The device that needs a mux-control.
684 * @mux_name: The name identifying the mux-control.
686 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
691 struct mux_control **ptr, *mux;
697 mux = mux_control_get(dev, mux_name);
698 if (IS_ERR(mux)) {
700 return mux;
703 *ptr = mux;
706 return mux;
711 * mux_state_get() - Get the mux-state for a device.
712 * @dev: The device that needs a mux-state.
713 * @mux_name: The name identifying the mux-state.
714 * @optional: Whether to return NULL and silence errors when mux doesn't exist.
716 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
717 * errno on error, or NULL if optional is true and mux doesn't exist.
727 mstate->mux = mux_get(dev, mux_name, &mstate->state, optional);
728 if (IS_ERR(mstate->mux)) {
729 int err = PTR_ERR(mstate->mux);
733 } else if (!mstate->mux) {
742 * mux_state_put() - Put away the mux-state for good.
743 * @mstate: The mux-state to put away.
749 mux_control_put(mstate->mux);
764 * __devm_mux_state_get() - Get the optional mux-state for a device,
766 * @dev: The device that needs a mux-state.
767 * @mux_name: The name identifying the mux-state.
768 * @optional: Whether to return NULL and silence errors when mux doesn't exist.
769 * @init: Optional function pointer for mux-state object initialisation.
770 * @exit: Optional function pointer for mux-state object cleanup on release.
772 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
773 * errno on error, or NULL if optional is true and mux doesn't exist.
818 * devm_mux_state_get() - Get the mux-state for a device, with resource
820 * @dev: The device that needs a mux-control.
821 * @mux_name: The name identifying the mux-control.
823 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
825 * The mux-state will automatically be freed on release.
834 * devm_mux_state_get_optional() - Get the optional mux-state for a device,
836 * @dev: The device that needs a mux-state.
837 * @mux_name: The name identifying the mux-state.
839 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
840 * errno on error, or NULL if mux doesn't exist.
842 * The mux-state will automatically be freed on release.
851 * devm_mux_state_get_selected() - Get the mux-state for a device, with
853 * @dev: The device that needs a mux-state.
854 * @mux_name: The name identifying the mux-state.
856 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
858 * The returned mux-state (if valid) is already selected.
860 * The mux-state will automatically be deselected and freed on release.
869 * devm_mux_state_get_optional_selected() - Get the optional mux-state for
871 * @dev: The device that needs a mux-state.
872 * @mux_name: The name identifying the mux-state.
874 * Return: Pointer to the mux-state on success, an ERR_PTR with a negative
875 * errno on error, or NULL if mux doesn't exist.
877 * The returned mux-state (if valid) is already selected.
879 * The mux-state will automatically be deselected and freed on release.
890 * the non-modular case - that the subsystem is initialized when mux consumers
891 * and mux controllers start to use it.