Lines Matching full:mux

10 #define pr_fmt(fmt) "mux-core: " fmt
19 #include <linux/mux/consumer.h>
20 #include <linux/mux/driver.h>
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.
42 struct mux_control *mux; member
47 .name = "mux",
74 .name = "mux-chip",
79 * mux_chip_alloc() - Allocate a mux-chip.
80 * @dev: The parent device implementing the mux interface.
81 * @controllers: The number of mux controllers to allocate for this chip.
84 * After allocating the mux-chip with the desired number of mux controllers
85 * but before registering the chip, the mux driver is required to configure
86 * the number of valid mux states in the mux_chip->mux[N].states members and
87 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
88 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
90 * before registering the mux-chip with mux_chip_register.
92 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
104 controllers * sizeof(*mux_chip->mux) + in mux_chip_alloc()
109 mux_chip->mux = (struct mux_control *)(mux_chip + 1); in mux_chip_alloc()
128 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_alloc() local
130 mux->chip = mux_chip; in mux_chip_alloc()
131 sema_init(&mux->lock, 1); in mux_chip_alloc()
132 mux->cached_state = MUX_CACHE_UNKNOWN; in mux_chip_alloc()
133 mux->idle_state = MUX_IDLE_AS_IS; in mux_chip_alloc()
134 mux->last_change = ktime_get(); in mux_chip_alloc()
143 static int mux_control_set(struct mux_control *mux, int state) in mux_control_set() argument
145 int ret = mux->chip->ops->set(mux, state); in mux_control_set()
147 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state; in mux_control_set()
149 mux->last_change = ktime_get(); in mux_control_set()
155 * mux_chip_register() - Register a mux-chip, thus readying the controllers
157 * @mux_chip: The mux-chip to register.
159 * Do not retry registration of the same mux-chip on failure. You should
171 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_register() local
173 if (mux->idle_state == mux->cached_state) in mux_chip_register()
176 ret = mux_control_set(mux, mux->idle_state); in mux_chip_register()
192 * mux_chip_unregister() - Take the mux-chip off-line.
193 * @mux_chip: The mux-chip to unregister.
197 * on a mux-chip that has been registered before.
206 * mux_chip_free() - Free the mux-chip for good.
207 * @mux_chip: The mux-chip to free.
229 * @dev: The parent device implementing the mux interface.
230 * @controllers: The number of mux controllers to allocate for this chip.
235 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
269 * @dev: The parent device implementing the mux interface.
270 * @mux_chip: The mux-chip to register.
301 * @mux: The mux-control to query.
305 unsigned int mux_control_states(struct mux_control *mux) in mux_control_states() argument
307 return mux->states; in mux_control_states()
312 * The mux->lock must be down when calling this function.
314 static int __mux_control_select(struct mux_control *mux, int state) in __mux_control_select() argument
318 if (WARN_ON(state < 0 || state >= mux->states)) in __mux_control_select()
321 if (mux->cached_state == state) in __mux_control_select()
324 ret = mux_control_set(mux, state); in __mux_control_select()
328 /* The mux update failed, try to revert if appropriate... */ in __mux_control_select()
329 if (mux->idle_state != MUX_IDLE_AS_IS) in __mux_control_select()
330 mux_control_set(mux, mux->idle_state); in __mux_control_select()
335 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us) in mux_control_delay() argument
343 delayend = ktime_add_us(mux->last_change, delay_us); in mux_control_delay()
351 * @mux: The mux-control to request a change of state from.
353 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
355 * On successfully selecting the mux-control state, it will be locked until
356 * there is a call to mux_control_deselect(). If the mux-control is already
362 * complete and the mux-control is free for others to use, but do not call
365 * Return: 0 when the mux-control state has the requested state or a negative
368 int mux_control_select_delay(struct mux_control *mux, unsigned int state, in mux_control_select_delay() argument
373 ret = down_killable(&mux->lock); in mux_control_select_delay()
377 ret = __mux_control_select(mux, state); in mux_control_select_delay()
379 mux_control_delay(mux, delay_us); in mux_control_select_delay()
382 up(&mux->lock); in mux_control_select_delay()
390 * @mstate: The mux-state to select.
391 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
393 * On successfully selecting the mux-state, its mux-control will be locked
394 * until there is a call to mux_state_deselect(). If the mux-control is already
400 * complete and the mux-control is free for others to use, but do not call
403 * Return: 0 when the mux-state has been selected or a negative
408 return mux_control_select_delay(mstate->mux, mstate->state, delay_us); in mux_state_select_delay()
414 * @mux: The mux-control to request a change of state from.
416 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
418 * On successfully selecting the mux-control state, it will be locked until
422 * complete and the mux-control is free for others to use, but do not call
425 * Return: 0 when the mux-control state has the requested state or a negative
426 * errno on error. Specifically -EBUSY if the mux-control is contended.
428 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state, in mux_control_try_select_delay() argument
433 if (down_trylock(&mux->lock)) in mux_control_try_select_delay()
436 ret = __mux_control_select(mux, state); in mux_control_try_select_delay()
438 mux_control_delay(mux, delay_us); in mux_control_try_select_delay()
441 up(&mux->lock); in mux_control_try_select_delay()
449 * @mstate: The mux-state to select.
450 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
452 * On successfully selecting the mux-state, its mux-control will be locked
456 * complete and the mux-control is free for others to use, but do not call
459 * Return: 0 when the mux-state has been selected or a negative errno on
460 * error. Specifically -EBUSY if the mux-control is contended.
464 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us); in mux_state_try_select_delay()
470 * @mux: The mux-control to deselect.
477 * occur if the mux has an idle state. Note that even if an error occurs, the
478 * mux-control is unlocked and is thus free for the next access.
480 int mux_control_deselect(struct mux_control *mux) in mux_control_deselect() argument
484 if (mux->idle_state != MUX_IDLE_AS_IS && in mux_control_deselect()
485 mux->idle_state != mux->cached_state) in mux_control_deselect()
486 ret = mux_control_set(mux, mux->idle_state); in mux_control_deselect()
488 up(&mux->lock); in mux_control_deselect()
496 * @mstate: The mux-state to deselect.
503 * occur if the mux has an idle state. Note that even if an error occurs, the
504 * mux-control is unlocked and is thus free for the next access.
508 return mux_control_deselect(mstate->mux); in mux_state_deselect()
523 * mux_get() - Get the mux-control for a device.
524 * @dev: The device that needs a mux-control.
525 * @mux_name: The name identifying the mux-control.
529 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
543 index = of_property_match_string(np, "mux-state-names", in mux_get()
546 index = of_property_match_string(np, "mux-control-names", in mux_get()
549 dev_err(dev, "mux controller '%s' not found\n", in mux_get()
557 "mux-states", "#mux-state-cells", in mux_get()
561 "mux-controls", "#mux-control-cells", in mux_get()
564 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n", in mux_get()
578 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n", in mux_get()
594 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n", in mux_get()
605 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n", in mux_get()
611 return &mux_chip->mux[controller]; in mux_get()
615 * mux_control_get() - Get the mux-control for a device.
616 * @dev: The device that needs a mux-control.
617 * @mux_name: The name identifying the mux-control.
619 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
628 * mux_control_put() - Put away the mux-control for good.
629 * @mux: The mux-control to put away.
633 void mux_control_put(struct mux_control *mux) in mux_control_put() argument
635 put_device(&mux->chip->dev); in mux_control_put()
641 struct mux_control *mux = *(struct mux_control **)res; in devm_mux_control_release() local
643 mux_control_put(mux); in devm_mux_control_release()
647 * devm_mux_control_get() - Get the mux-control for a device, with resource
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, or an ERR_PTR with a negative errno.
657 struct mux_control **ptr, *mux; in devm_mux_control_get() local
663 mux = mux_control_get(dev, mux_name); in devm_mux_control_get()
664 if (IS_ERR(mux)) { in devm_mux_control_get()
666 return mux; in devm_mux_control_get()
669 *ptr = mux; in devm_mux_control_get()
672 return mux; in devm_mux_control_get()
677 * mux_state_get() - Get the mux-state for a device.
678 * @dev: The device that needs a mux-state.
679 * @mux_name: The name identifying the mux-state.
681 * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno.
691 mstate->mux = mux_get(dev, mux_name, &mstate->state); in mux_state_get()
692 if (IS_ERR(mstate->mux)) { in mux_state_get()
693 int err = PTR_ERR(mstate->mux); in mux_state_get()
703 * mux_state_put() - Put away the mux-state for good.
704 * @mstate: The mux-state to put away.
710 mux_control_put(mstate->mux); in mux_state_put()
722 * devm_mux_state_get() - Get the mux-state for a device, with resource
724 * @dev: The device that needs a mux-control.
725 * @mux_name: The name identifying the mux-control.
727 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
753 * the non-modular case - that the subsystem is initialized when mux consumers
754 * and mux controllers start to use it.