Lines Matching +full:mux +full:- +full:control +full:- +full:names
1 // SPDX-License-Identifier: GPL-2.0
10 #define pr_fmt(fmt) "mux-core: " fmt
19 #include <linux/mux/consumer.h>
20 #include <linux/mux/driver.h>
25 * The idle-as-is "state" is not an actual state that may be selected, it
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; member
46 .name = "mux",
67 ida_simple_remove(&mux_ida, mux_chip->id); in mux_chip_release()
72 .name = "mux-chip",
77 * mux_chip_alloc() - Allocate a mux-chip.
78 * @dev: The parent device implementing the mux interface.
79 * @controllers: The number of mux controllers to allocate for this chip.
82 * After allocating the mux-chip with the desired number of mux controllers
83 * but before registering the chip, the mux driver is required to configure
84 * the number of valid mux states in the mux_chip->mux[N].states members and
85 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
86 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
87 * provide a pointer to the operations struct in the mux_chip->ops member
88 * before registering the mux-chip with mux_chip_register.
90 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
99 return ERR_PTR(-EINVAL); in mux_chip_alloc()
102 controllers * sizeof(*mux_chip->mux) + in mux_chip_alloc()
105 return ERR_PTR(-ENOMEM); in mux_chip_alloc()
107 mux_chip->mux = (struct mux_control *)(mux_chip + 1); in mux_chip_alloc()
108 mux_chip->dev.class = &mux_class; in mux_chip_alloc()
109 mux_chip->dev.type = &mux_type; in mux_chip_alloc()
110 mux_chip->dev.parent = dev; in mux_chip_alloc()
111 mux_chip->dev.of_node = dev->of_node; in mux_chip_alloc()
112 dev_set_drvdata(&mux_chip->dev, mux_chip); in mux_chip_alloc()
114 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL); in mux_chip_alloc()
115 if (mux_chip->id < 0) { in mux_chip_alloc()
116 int err = mux_chip->id; in mux_chip_alloc()
122 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id); in mux_chip_alloc()
124 mux_chip->controllers = controllers; in mux_chip_alloc()
126 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_alloc() local
128 mux->chip = mux_chip; in mux_chip_alloc()
129 sema_init(&mux->lock, 1); in mux_chip_alloc()
130 mux->cached_state = MUX_CACHE_UNKNOWN; in mux_chip_alloc()
131 mux->idle_state = MUX_IDLE_AS_IS; in mux_chip_alloc()
132 mux->last_change = ktime_get(); in mux_chip_alloc()
135 device_initialize(&mux_chip->dev); in mux_chip_alloc()
141 static int mux_control_set(struct mux_control *mux, int state) in mux_control_set() argument
143 int ret = mux->chip->ops->set(mux, state); in mux_control_set()
145 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state; in mux_control_set()
147 mux->last_change = ktime_get(); in mux_control_set()
153 * mux_chip_register() - Register a mux-chip, thus readying the controllers
155 * @mux_chip: The mux-chip to register.
157 * Do not retry registration of the same mux-chip on failure. You should
168 for (i = 0; i < mux_chip->controllers; ++i) { in mux_chip_register()
169 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_register() local
171 if (mux->idle_state == mux->cached_state) in mux_chip_register()
174 ret = mux_control_set(mux, mux->idle_state); in mux_chip_register()
176 dev_err(&mux_chip->dev, "unable to set idle state\n"); in mux_chip_register()
181 ret = device_add(&mux_chip->dev); in mux_chip_register()
183 dev_err(&mux_chip->dev, in mux_chip_register()
190 * mux_chip_unregister() - Take the mux-chip off-line.
191 * @mux_chip: The mux-chip to unregister.
195 * on a mux-chip that has been registered before.
199 device_del(&mux_chip->dev); in mux_chip_unregister()
204 * mux_chip_free() - Free the mux-chip for good.
205 * @mux_chip: The mux-chip to free.
214 put_device(&mux_chip->dev); in mux_chip_free()
226 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
227 * @dev: The parent device implementing the mux interface.
228 * @controllers: The number of mux controllers to allocate for this chip.
233 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
243 return ERR_PTR(-ENOMEM); in devm_mux_chip_alloc()
266 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
267 * @dev: The parent device implementing the mux interface.
268 * @mux_chip: The mux-chip to register.
282 return -ENOMEM; in devm_mux_chip_register()
298 * mux_control_states() - Query the number of multiplexer states.
299 * @mux: The mux-control to query.
303 unsigned int mux_control_states(struct mux_control *mux) in mux_control_states() argument
305 return mux->states; in mux_control_states()
310 * The mux->lock must be down when calling this function.
312 static int __mux_control_select(struct mux_control *mux, int state) in __mux_control_select() argument
316 if (WARN_ON(state < 0 || state >= mux->states)) in __mux_control_select()
317 return -EINVAL; in __mux_control_select()
319 if (mux->cached_state == state) in __mux_control_select()
322 ret = mux_control_set(mux, state); in __mux_control_select()
326 /* The mux update failed, try to revert if appropriate... */ in __mux_control_select()
327 if (mux->idle_state != MUX_IDLE_AS_IS) in __mux_control_select()
328 mux_control_set(mux, mux->idle_state); in __mux_control_select()
333 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us) in mux_control_delay() argument
341 delayend = ktime_add_us(mux->last_change, delay_us); in mux_control_delay()
348 * mux_control_select_delay() - Select the given multiplexer state.
349 * @mux: The mux-control to request a change of state from.
351 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
353 * On successfully selecting the mux-control state, it will be locked until
354 * there is a call to mux_control_deselect(). If the mux-control is already
360 * complete and the mux-control is free for others to use, but do not call
363 * Return: 0 when the mux-control state has the requested state or a negative
366 int mux_control_select_delay(struct mux_control *mux, unsigned int state, in mux_control_select_delay() argument
371 ret = down_killable(&mux->lock); in mux_control_select_delay()
375 ret = __mux_control_select(mux, state); in mux_control_select_delay()
377 mux_control_delay(mux, delay_us); in mux_control_select_delay()
380 up(&mux->lock); in mux_control_select_delay()
387 * mux_state_select_delay() - Select the given multiplexer state.
388 * @mstate: The mux-state to select.
389 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
391 * On successfully selecting the mux-state, its mux-control will be locked
392 * until there is a call to mux_state_deselect(). If the mux-control is already
398 * complete and the mux-control is free for others to use, but do not call
401 * Return: 0 when the mux-state has been selected or a negative
406 return mux_control_select_delay(mstate->mux, mstate->state, delay_us); in mux_state_select_delay()
411 * mux_control_try_select_delay() - Try to select the given multiplexer state.
412 * @mux: The mux-control to request a change of state from.
414 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
416 * On successfully selecting the mux-control state, it will be locked until
420 * complete and the mux-control is free for others to use, but do not call
423 * Return: 0 when the mux-control state has the requested state or a negative
424 * errno on error. Specifically -EBUSY if the mux-control is contended.
426 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state, in mux_control_try_select_delay() argument
431 if (down_trylock(&mux->lock)) in mux_control_try_select_delay()
432 return -EBUSY; in mux_control_try_select_delay()
434 ret = __mux_control_select(mux, state); in mux_control_try_select_delay()
436 mux_control_delay(mux, delay_us); in mux_control_try_select_delay()
439 up(&mux->lock); in mux_control_try_select_delay()
446 * mux_state_try_select_delay() - Try to select the given multiplexer state.
447 * @mstate: The mux-state to select.
448 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
450 * On successfully selecting the mux-state, its mux-control will be locked
454 * complete and the mux-control is free for others to use, but do not call
457 * Return: 0 when the mux-state has been selected or a negative errno on
458 * error. Specifically -EBUSY if the mux-control is contended.
462 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us); in mux_state_try_select_delay()
467 * mux_control_deselect() - Deselect the previously selected multiplexer state.
468 * @mux: The mux-control to deselect.
475 * occur if the mux has an idle state. Note that even if an error occurs, the
476 * mux-control is unlocked and is thus free for the next access.
478 int mux_control_deselect(struct mux_control *mux) in mux_control_deselect() argument
482 if (mux->idle_state != MUX_IDLE_AS_IS && in mux_control_deselect()
483 mux->idle_state != mux->cached_state) in mux_control_deselect()
484 ret = mux_control_set(mux, mux->idle_state); in mux_control_deselect()
486 up(&mux->lock); in mux_control_deselect()
493 * mux_state_deselect() - Deselect the previously selected multiplexer state.
494 * @mstate: The mux-state to deselect.
501 * occur if the mux has an idle state. Note that even if an error occurs, the
502 * mux-control is unlocked and is thus free for the next access.
506 return mux_control_deselect(mstate->mux); in mux_state_deselect()
521 * mux_get() - Get the mux-control for a device.
522 * @dev: The device that needs a mux-control.
523 * @mux_name: The name identifying the mux-control.
527 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
532 struct device_node *np = dev->of_node; in mux_get()
541 index = of_property_match_string(np, "mux-state-names", in mux_get()
544 index = of_property_match_string(np, "mux-control-names", in mux_get()
547 dev_err(dev, "mux controller '%s' not found\n", in mux_get()
555 "mux-states", "#mux-state-cells", in mux_get()
559 "mux-controls", "#mux-control-cells", in mux_get()
562 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n", in mux_get()
563 np, state ? "state" : "control", mux_name ?: "", index); in mux_get()
570 return ERR_PTR(-EPROBE_DEFER); in mux_get()
575 (args.args_count < 2 && mux_chip->controllers > 1)) { in mux_get()
576 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n", in mux_get()
578 put_device(&mux_chip->dev); in mux_get()
579 return ERR_PTR(-EINVAL); in mux_get()
591 (!args.args_count && mux_chip->controllers > 1)) { in mux_get()
592 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n", in mux_get()
594 put_device(&mux_chip->dev); in mux_get()
595 return ERR_PTR(-EINVAL); in mux_get()
602 if (controller >= mux_chip->controllers) { in mux_get()
603 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n", in mux_get()
605 put_device(&mux_chip->dev); in mux_get()
606 return ERR_PTR(-EINVAL); in mux_get()
609 return &mux_chip->mux[controller]; in mux_get()
613 * mux_control_get() - Get the mux-control for a device.
614 * @dev: The device that needs a mux-control.
615 * @mux_name: The name identifying the mux-control.
617 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
626 * mux_control_put() - Put away the mux-control for good.
627 * @mux: The mux-control to put away.
631 void mux_control_put(struct mux_control *mux) in mux_control_put() argument
633 put_device(&mux->chip->dev); in mux_control_put()
639 struct mux_control *mux = *(struct mux_control **)res; in devm_mux_control_release() local
641 mux_control_put(mux); in devm_mux_control_release()
645 * devm_mux_control_get() - Get the mux-control for a device, with resource
647 * @dev: The device that needs a mux-control.
648 * @mux_name: The name identifying the mux-control.
650 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
655 struct mux_control **ptr, *mux; in devm_mux_control_get() local
659 return ERR_PTR(-ENOMEM); in devm_mux_control_get()
661 mux = mux_control_get(dev, mux_name); in devm_mux_control_get()
662 if (IS_ERR(mux)) { in devm_mux_control_get()
664 return mux; in devm_mux_control_get()
667 *ptr = mux; in devm_mux_control_get()
670 return mux; in devm_mux_control_get()
675 * mux_state_get() - Get the mux-state for a device.
676 * @dev: The device that needs a mux-state.
677 * @mux_name: The name identifying the mux-state.
679 * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno.
687 return ERR_PTR(-ENOMEM); in mux_state_get()
689 mstate->mux = mux_get(dev, mux_name, &mstate->state); in mux_state_get()
690 if (IS_ERR(mstate->mux)) { in mux_state_get()
691 int err = PTR_ERR(mstate->mux); in mux_state_get()
701 * mux_state_put() - Put away the mux-state for good.
702 * @mstate: The mux-state to put away.
708 mux_control_put(mstate->mux); in mux_state_put()
720 * devm_mux_state_get() - Get the mux-state for a device, with resource
722 * @dev: The device that needs a mux-control.
723 * @mux_name: The name identifying the mux-control.
725 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
734 return ERR_PTR(-ENOMEM); in devm_mux_state_get()
750 * Using subsys_initcall instead of module_init here to try to ensure - for
751 * the non-modular case - that the subsystem is initialized when mux consumers
752 * and mux controllers start to use it.