Lines Matching +full:has +full:- +full:chip +full:- +full:id

1 // SPDX-License-Identifier: GPL-2.0
10 #define pr_fmt(fmt) "mux-core: " fmt
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
37 * This structure is specific to the consumer that acquires it and has
67 ida_free(&mux_ida, mux_chip->id); in mux_chip_release()
72 .name = "mux-chip",
77 * mux_chip_alloc() - Allocate a mux-chip.
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.
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()
105 return ERR_PTR(-ENOMEM); in mux_chip_alloc()
107 mux_chip->dev.class = &mux_class; in mux_chip_alloc()
108 mux_chip->dev.type = &mux_type; in mux_chip_alloc()
109 mux_chip->dev.parent = dev; in mux_chip_alloc()
110 mux_chip->dev.of_node = dev->of_node; in mux_chip_alloc()
111 dev_set_drvdata(&mux_chip->dev, mux_chip); in mux_chip_alloc()
113 mux_chip->id = ida_alloc(&mux_ida, GFP_KERNEL); in mux_chip_alloc()
114 if (mux_chip->id < 0) { in mux_chip_alloc()
115 int err = mux_chip->id; in mux_chip_alloc()
117 pr_err("muxchipX failed to get a device id\n"); in mux_chip_alloc()
121 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id); in mux_chip_alloc()
123 mux_chip->controllers = controllers; in mux_chip_alloc()
125 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_alloc()
127 mux->chip = mux_chip; in mux_chip_alloc()
128 sema_init(&mux->lock, 1); in mux_chip_alloc()
129 mux->cached_state = MUX_CACHE_UNKNOWN; in mux_chip_alloc()
130 mux->idle_state = MUX_IDLE_AS_IS; in mux_chip_alloc()
131 mux->last_change = ktime_get(); in mux_chip_alloc()
134 device_initialize(&mux_chip->dev); in mux_chip_alloc()
142 int ret = mux->chip->ops->set(mux, state); in mux_control_set()
144 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state; in mux_control_set()
146 mux->last_change = ktime_get(); in mux_control_set()
152 * mux_chip_register() - Register a mux-chip, thus readying the controllers
154 * @mux_chip: The mux-chip to register.
156 * Do not retry registration of the same mux-chip on failure. You should
167 for (i = 0; i < mux_chip->controllers; ++i) { in mux_chip_register()
168 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_register()
170 if (mux->idle_state == mux->cached_state) in mux_chip_register()
173 ret = mux_control_set(mux, mux->idle_state); in mux_chip_register()
175 dev_err(&mux_chip->dev, "unable to set idle state\n"); in mux_chip_register()
180 ret = device_add(&mux_chip->dev); in mux_chip_register()
182 dev_err(&mux_chip->dev, in mux_chip_register()
189 * mux_chip_unregister() - Take the mux-chip off-line.
190 * @mux_chip: The mux-chip to unregister.
194 * on a mux-chip that has been registered before.
198 device_del(&mux_chip->dev); in mux_chip_unregister()
203 * mux_chip_free() - Free the mux-chip for good.
204 * @mux_chip: The mux-chip to free.
213 put_device(&mux_chip->dev); in mux_chip_free()
225 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
227 * @controllers: The number of mux controllers to allocate for this chip.
232 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
242 return ERR_PTR(-ENOMEM); in devm_mux_chip_alloc()
265 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
267 * @mux_chip: The mux-chip to register.
281 return -ENOMEM; in devm_mux_chip_register()
297 * mux_control_states() - Query the number of multiplexer states.
298 * @mux: The mux-control to query.
304 return mux->states; in mux_control_states()
309 * The mux->lock must be down when calling this function.
315 if (WARN_ON(state < 0 || state >= mux->states)) in __mux_control_select()
316 return -EINVAL; in __mux_control_select()
318 if (mux->cached_state == state) in __mux_control_select()
326 if (mux->idle_state != MUX_IDLE_AS_IS) in __mux_control_select()
327 mux_control_set(mux, mux->idle_state); in __mux_control_select()
340 delayend = ktime_add_us(mux->last_change, delay_us); in mux_control_delay()
347 * mux_control_select_delay() - Select the given multiplexer state.
348 * @mux: The mux-control to request a change of state from.
352 * On successfully selecting the mux-control state, it will be locked until
353 * there is a call to mux_control_deselect(). If the mux-control is already
359 * complete and the mux-control is free for others to use, but do not call
362 * Return: 0 when the mux-control state has the requested state or a negative
370 ret = down_killable(&mux->lock); in mux_control_select_delay()
379 up(&mux->lock); in mux_control_select_delay()
386 * mux_state_select_delay() - Select the given multiplexer state.
387 * @mstate: The mux-state to select.
390 * On successfully selecting the mux-state, its mux-control will be locked
391 * until there is a call to mux_state_deselect(). If the mux-control is already
397 * complete and the mux-control is free for others to use, but do not call
400 * Return: 0 when the mux-state has been selected or a negative
405 return mux_control_select_delay(mstate->mux, mstate->state, delay_us); in mux_state_select_delay()
410 * mux_control_try_select_delay() - Try to select the given multiplexer state.
411 * @mux: The mux-control to request a change of state from.
415 * On successfully selecting the mux-control state, it will be locked until
419 * complete and the mux-control is free for others to use, but do not call
422 * Return: 0 when the mux-control state has the requested state or a negative
423 * errno on error. Specifically -EBUSY if the mux-control is contended.
430 if (down_trylock(&mux->lock)) in mux_control_try_select_delay()
431 return -EBUSY; in mux_control_try_select_delay()
438 up(&mux->lock); in mux_control_try_select_delay()
445 * mux_state_try_select_delay() - Try to select the given multiplexer state.
446 * @mstate: The mux-state to select.
449 * On successfully selecting the mux-state, its mux-control will be locked
453 * complete and the mux-control is free for others to use, but do not call
456 * Return: 0 when the mux-state has been selected or a negative errno on
457 * error. Specifically -EBUSY if the mux-control is contended.
461 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us); in mux_state_try_select_delay()
466 * mux_control_deselect() - Deselect the previously selected multiplexer state.
467 * @mux: The mux-control to deselect.
474 * occur if the mux has an idle state. Note that even if an error occurs, the
475 * mux-control is unlocked and is thus free for the next access.
481 if (mux->idle_state != MUX_IDLE_AS_IS && in mux_control_deselect()
482 mux->idle_state != mux->cached_state) in mux_control_deselect()
483 ret = mux_control_set(mux, mux->idle_state); in mux_control_deselect()
485 up(&mux->lock); in mux_control_deselect()
492 * mux_state_deselect() - Deselect the previously selected multiplexer state.
493 * @mstate: The mux-state to deselect.
500 * occur if the mux has an idle state. Note that even if an error occurs, the
501 * mux-control is unlocked and is thus free for the next access.
505 return mux_control_deselect(mstate->mux); in mux_state_deselect()
520 * mux_get() - Get the mux-control for a device.
521 * @dev: The device that needs a mux-control.
522 * @mux_name: The name identifying the mux-control.
526 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
531 struct device_node *np = dev->of_node; in mux_get()
540 index = of_property_match_string(np, "mux-state-names", in mux_get()
543 index = of_property_match_string(np, "mux-control-names", in mux_get()
554 "mux-states", "#mux-state-cells", in mux_get()
558 "mux-controls", "#mux-control-cells", in mux_get()
561 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n", in mux_get()
569 return ERR_PTR(-EPROBE_DEFER); in mux_get()
574 (args.args_count < 2 && mux_chip->controllers > 1)) { in mux_get()
575 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n", in mux_get()
577 put_device(&mux_chip->dev); in mux_get()
578 return ERR_PTR(-EINVAL); in mux_get()
590 (!args.args_count && mux_chip->controllers > 1)) { in mux_get()
591 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n", in mux_get()
593 put_device(&mux_chip->dev); in mux_get()
594 return ERR_PTR(-EINVAL); in mux_get()
601 if (controller >= mux_chip->controllers) { in mux_get()
604 put_device(&mux_chip->dev); in mux_get()
605 return ERR_PTR(-EINVAL); in mux_get()
608 return &mux_chip->mux[controller]; in mux_get()
612 * mux_control_get() - Get the mux-control for a device.
613 * @dev: The device that needs a mux-control.
614 * @mux_name: The name identifying the mux-control.
616 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
625 * mux_control_put() - Put away the mux-control for good.
626 * @mux: The mux-control to put away.
632 put_device(&mux->chip->dev); in mux_control_put()
644 * devm_mux_control_get() - Get the mux-control for a device, with resource
646 * @dev: The device that needs a mux-control.
647 * @mux_name: The name identifying the mux-control.
649 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
658 return ERR_PTR(-ENOMEM); in devm_mux_control_get()
674 * mux_state_get() - Get the mux-state for a device.
675 * @dev: The device that needs a mux-state.
676 * @mux_name: The name identifying the mux-state.
678 * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno.
686 return ERR_PTR(-ENOMEM); in mux_state_get()
688 mstate->mux = mux_get(dev, mux_name, &mstate->state); in mux_state_get()
689 if (IS_ERR(mstate->mux)) { in mux_state_get()
690 int err = PTR_ERR(mstate->mux); in mux_state_get()
700 * mux_state_put() - Put away the mux-state for good.
701 * @mstate: The mux-state to put away.
707 mux_control_put(mstate->mux); in mux_state_put()
719 * devm_mux_state_get() - Get the mux-state for a device, with resource
721 * @dev: The device that needs a mux-control.
722 * @mux_name: The name identifying the mux-control.
724 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
733 return ERR_PTR(-ENOMEM); in devm_mux_state_get()
749 * Using subsys_initcall instead of module_init here to try to ensure - for
750 * the non-modular case - that the subsystem is initialized when mux consumers