Lines Matching +full:mux +full:-

1 // SPDX-License-Identifier: GPL-2.0
10 #define pr_fmt(fmt) "mux-core: " fmt
18 #include <linux/mux/consumer.h>
19 #include <linux/mux/driver.h>
25 * The idle-as-is "state" is not an actual state that may be selected, it
32 .name = "mux",
54 ida_simple_remove(&mux_ida, mux_chip->id); in mux_chip_release()
59 .name = "mux-chip",
64 * mux_chip_alloc() - Allocate a mux-chip.
65 * @dev: The parent device implementing the mux interface.
66 * @controllers: The number of mux controllers to allocate for this chip.
69 * After allocating the mux-chip with the desired number of mux controllers
70 * but before registering the chip, the mux driver is required to configure
71 * the number of valid mux states in the mux_chip->mux[N].states members and
72 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
73 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
74 * provide a pointer to the operations struct in the mux_chip->ops member
75 * before registering the mux-chip with mux_chip_register.
77 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
86 return ERR_PTR(-EINVAL); in mux_chip_alloc()
89 controllers * sizeof(*mux_chip->mux) + in mux_chip_alloc()
92 return ERR_PTR(-ENOMEM); in mux_chip_alloc()
94 mux_chip->mux = (struct mux_control *)(mux_chip + 1); in mux_chip_alloc()
95 mux_chip->dev.class = &mux_class; in mux_chip_alloc()
96 mux_chip->dev.type = &mux_type; in mux_chip_alloc()
97 mux_chip->dev.parent = dev; in mux_chip_alloc()
98 mux_chip->dev.of_node = dev->of_node; in mux_chip_alloc()
99 dev_set_drvdata(&mux_chip->dev, mux_chip); in mux_chip_alloc()
101 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL); in mux_chip_alloc()
102 if (mux_chip->id < 0) { in mux_chip_alloc()
103 int err = mux_chip->id; in mux_chip_alloc()
109 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id); in mux_chip_alloc()
111 mux_chip->controllers = controllers; in mux_chip_alloc()
113 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_alloc() local
115 mux->chip = mux_chip; in mux_chip_alloc()
116 sema_init(&mux->lock, 1); in mux_chip_alloc()
117 mux->cached_state = MUX_CACHE_UNKNOWN; in mux_chip_alloc()
118 mux->idle_state = MUX_IDLE_AS_IS; in mux_chip_alloc()
121 device_initialize(&mux_chip->dev); in mux_chip_alloc()
127 static int mux_control_set(struct mux_control *mux, int state) in mux_control_set() argument
129 int ret = mux->chip->ops->set(mux, state); in mux_control_set()
131 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state; in mux_control_set()
137 * mux_chip_register() - Register a mux-chip, thus readying the controllers
139 * @mux_chip: The mux-chip to register.
141 * Do not retry registration of the same mux-chip on failure. You should
152 for (i = 0; i < mux_chip->controllers; ++i) { in mux_chip_register()
153 struct mux_control *mux = &mux_chip->mux[i]; in mux_chip_register() local
155 if (mux->idle_state == mux->cached_state) in mux_chip_register()
158 ret = mux_control_set(mux, mux->idle_state); in mux_chip_register()
160 dev_err(&mux_chip->dev, "unable to set idle state\n"); in mux_chip_register()
165 ret = device_add(&mux_chip->dev); in mux_chip_register()
167 dev_err(&mux_chip->dev, in mux_chip_register()
174 * mux_chip_unregister() - Take the mux-chip off-line.
175 * @mux_chip: The mux-chip to unregister.
179 * on a mux-chip that has been registered before.
183 device_del(&mux_chip->dev); in mux_chip_unregister()
188 * mux_chip_free() - Free the mux-chip for good.
189 * @mux_chip: The mux-chip to free.
198 put_device(&mux_chip->dev); in mux_chip_free()
210 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
211 * @dev: The parent device implementing the mux interface.
212 * @controllers: The number of mux controllers to allocate for this chip.
217 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
227 return ERR_PTR(-ENOMEM); in devm_mux_chip_alloc()
250 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
251 * @dev: The parent device implementing the mux interface.
252 * @mux_chip: The mux-chip to register.
266 return -ENOMEM; in devm_mux_chip_register()
282 * mux_control_states() - Query the number of multiplexer states.
283 * @mux: The mux-control to query.
287 unsigned int mux_control_states(struct mux_control *mux) in mux_control_states() argument
289 return mux->states; in mux_control_states()
294 * The mux->lock must be down when calling this function.
296 static int __mux_control_select(struct mux_control *mux, int state) in __mux_control_select() argument
300 if (WARN_ON(state < 0 || state >= mux->states)) in __mux_control_select()
301 return -EINVAL; in __mux_control_select()
303 if (mux->cached_state == state) in __mux_control_select()
306 ret = mux_control_set(mux, state); in __mux_control_select()
310 /* The mux update failed, try to revert if appropriate... */ in __mux_control_select()
311 if (mux->idle_state != MUX_IDLE_AS_IS) in __mux_control_select()
312 mux_control_set(mux, mux->idle_state); in __mux_control_select()
318 * mux_control_select() - Select the given multiplexer state.
319 * @mux: The mux-control to request a change of state from.
322 * On successfully selecting the mux-control state, it will be locked until
323 * there is a call to mux_control_deselect(). If the mux-control is already
328 * complete and the mux-control is free for others to use, but do not call
331 * Return: 0 when the mux-control state has the requested state or a negative
334 int mux_control_select(struct mux_control *mux, unsigned int state) in mux_control_select() argument
338 ret = down_killable(&mux->lock); in mux_control_select()
342 ret = __mux_control_select(mux, state); in mux_control_select()
345 up(&mux->lock); in mux_control_select()
352 * mux_control_try_select() - Try to select the given multiplexer state.
353 * @mux: The mux-control to request a change of state from.
356 * On successfully selecting the mux-control state, it will be locked until
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
364 * errno on error. Specifically -EBUSY if the mux-control is contended.
366 int mux_control_try_select(struct mux_control *mux, unsigned int state) in mux_control_try_select() argument
370 if (down_trylock(&mux->lock)) in mux_control_try_select()
371 return -EBUSY; in mux_control_try_select()
373 ret = __mux_control_select(mux, state); in mux_control_try_select()
376 up(&mux->lock); in mux_control_try_select()
383 * mux_control_deselect() - Deselect the previously selected multiplexer state.
384 * @mux: The mux-control to deselect.
391 * occur if the mux has an idle state. Note that even if an error occurs, the
392 * mux-control is unlocked and is thus free for the next access.
394 int mux_control_deselect(struct mux_control *mux) in mux_control_deselect() argument
398 if (mux->idle_state != MUX_IDLE_AS_IS && in mux_control_deselect()
399 mux->idle_state != mux->cached_state) in mux_control_deselect()
400 ret = mux_control_set(mux, mux->idle_state); in mux_control_deselect()
402 up(&mux->lock); in mux_control_deselect()
419 * mux_control_get() - Get the mux-control for a device.
420 * @dev: The device that needs a mux-control.
421 * @mux_name: The name identifying the mux-control.
423 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
427 struct device_node *np = dev->of_node; in mux_control_get()
435 index = of_property_match_string(np, "mux-control-names", in mux_control_get()
438 dev_err(dev, "mux controller '%s' not found\n", in mux_control_get()
445 "mux-controls", "#mux-control-cells", in mux_control_get()
448 dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n", in mux_control_get()
456 return ERR_PTR(-EPROBE_DEFER); in mux_control_get()
459 (!args.args_count && (mux_chip->controllers > 1))) { in mux_control_get()
460 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n", in mux_control_get()
462 put_device(&mux_chip->dev); in mux_control_get()
463 return ERR_PTR(-EINVAL); in mux_control_get()
470 if (controller >= mux_chip->controllers) { in mux_control_get()
471 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n", in mux_control_get()
473 put_device(&mux_chip->dev); in mux_control_get()
474 return ERR_PTR(-EINVAL); in mux_control_get()
477 return &mux_chip->mux[controller]; in mux_control_get()
482 * mux_control_put() - Put away the mux-control for good.
483 * @mux: The mux-control to put away.
487 void mux_control_put(struct mux_control *mux) in mux_control_put() argument
489 put_device(&mux->chip->dev); in mux_control_put()
495 struct mux_control *mux = *(struct mux_control **)res; in devm_mux_control_release() local
497 mux_control_put(mux); in devm_mux_control_release()
501 * devm_mux_control_get() - Get the mux-control for a device, with resource
503 * @dev: The device that needs a mux-control.
504 * @mux_name: The name identifying the mux-control.
506 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
511 struct mux_control **ptr, *mux; in devm_mux_control_get() local
515 return ERR_PTR(-ENOMEM); in devm_mux_control_get()
517 mux = mux_control_get(dev, mux_name); in devm_mux_control_get()
518 if (IS_ERR(mux)) { in devm_mux_control_get()
520 return mux; in devm_mux_control_get()
523 *ptr = mux; in devm_mux_control_get()
526 return mux; in devm_mux_control_get()
531 * Using subsys_initcall instead of module_init here to try to ensure - for
532 * the non-modular case - that the subsystem is initialized when mux consumers
533 * and mux controllers start to use it.