1 // SPDX-License-Identifier: GPL-2.0
3 * Multiplexer subsystem
5 * Copyright (C) 2017 Axentia Technologies AB
7 * Author: Peter Rosin <peda@axentia.se>
10 #define pr_fmt(fmt) "mux-core: " fmt
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/export.h>
16 #include <linux/idr.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mux/consumer.h>
20 #include <linux/mux/driver.h>
22 #include <linux/of_platform.h>
23 #include <linux/slab.h>
26 * The idle-as-is "state" is not an actual state that may be selected, it
27 * only implies that the state should not be changed. So, use that state
28 * as indication that the cached state of the multiplexer is unknown.
30 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
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.
38 * This structure is specific to the consumer that acquires it and has
39 * information specific to that consumer.
42 struct mux_control *mux;
46 static struct class mux_class = {
51 static DEFINE_IDA(mux_ida);
53 static int __init mux_init(void)
56 return class_register(&mux_class);
59 static void __exit mux_exit(void)
61 class_unregister(&mux_class);
62 ida_destroy(&mux_ida);
65 static void mux_chip_release(struct device *dev)
67 struct mux_chip *mux_chip = to_mux_chip(dev);
69 ida_simple_remove(&mux_ida, mux_chip->id);
73 static const struct device_type mux_type = {
75 .release = mux_chip_release,
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.
82 * @sizeof_priv: Size of extra memory area for private use by the caller.
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
89 * provide a pointer to the operations struct in the mux_chip->ops member
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.
94 struct mux_chip *mux_chip_alloc(struct device *dev,
95 unsigned int controllers, size_t sizeof_priv)
97 struct mux_chip *mux_chip;
100 if (WARN_ON(!dev || !controllers))
101 return ERR_PTR(-EINVAL);
103 mux_chip = kzalloc(sizeof(*mux_chip) +
104 controllers * sizeof(*mux_chip->mux) +
105 sizeof_priv, GFP_KERNEL);
107 return ERR_PTR(-ENOMEM);
109 mux_chip->mux = (struct mux_control *)(mux_chip + 1);
110 mux_chip->dev.class = &mux_class;
111 mux_chip->dev.type = &mux_type;
112 mux_chip->dev.parent = dev;
113 mux_chip->dev.of_node = dev->of_node;
114 dev_set_drvdata(&mux_chip->dev, mux_chip);
116 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
117 if (mux_chip->id < 0) {
118 int err = mux_chip->id;
120 pr_err("muxchipX failed to get a device id\n");
124 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
126 mux_chip->controllers = controllers;
127 for (i = 0; i < controllers; ++i) {
128 struct mux_control *mux = &mux_chip->mux[i];
130 mux->chip = mux_chip;
131 sema_init(&mux->lock, 1);
132 mux->cached_state = MUX_CACHE_UNKNOWN;
133 mux->idle_state = MUX_IDLE_AS_IS;
134 mux->last_change = ktime_get();
137 device_initialize(&mux_chip->dev);
141 EXPORT_SYMBOL_GPL(mux_chip_alloc);
143 static int mux_control_set(struct mux_control *mux, int state)
145 int ret = mux->chip->ops->set(mux, state);
147 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
149 mux->last_change = ktime_get();
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
160 * instead put it away with mux_chip_free() and allocate a new one, if you
161 * for some reason would like to retry registration.
163 * Return: Zero on success or a negative errno on error.
165 int mux_chip_register(struct mux_chip *mux_chip)
170 for (i = 0; i < mux_chip->controllers; ++i) {
171 struct mux_control *mux = &mux_chip->mux[i];
173 if (mux->idle_state == mux->cached_state)
176 ret = mux_control_set(mux, mux->idle_state);
178 dev_err(&mux_chip->dev, "unable to set idle state\n");
183 ret = device_add(&mux_chip->dev);
185 dev_err(&mux_chip->dev,
186 "device_add failed in %s: %d\n", __func__, ret);
189 EXPORT_SYMBOL_GPL(mux_chip_register);
192 * mux_chip_unregister() - Take the mux-chip off-line.
193 * @mux_chip: The mux-chip to unregister.
195 * mux_chip_unregister() reverses the effects of mux_chip_register().
196 * But not completely, you should not try to call mux_chip_register()
197 * on a mux-chip that has been registered before.
199 void mux_chip_unregister(struct mux_chip *mux_chip)
201 device_del(&mux_chip->dev);
203 EXPORT_SYMBOL_GPL(mux_chip_unregister);
206 * mux_chip_free() - Free the mux-chip for good.
207 * @mux_chip: The mux-chip to free.
209 * mux_chip_free() reverses the effects of mux_chip_alloc().
211 void mux_chip_free(struct mux_chip *mux_chip)
216 put_device(&mux_chip->dev);
218 EXPORT_SYMBOL_GPL(mux_chip_free);
220 static void devm_mux_chip_release(struct device *dev, void *res)
222 struct mux_chip *mux_chip = *(struct mux_chip **)res;
224 mux_chip_free(mux_chip);
228 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
229 * @dev: The parent device implementing the mux interface.
230 * @controllers: The number of mux controllers to allocate for this chip.
231 * @sizeof_priv: Size of extra memory area for private use by the caller.
233 * See mux_chip_alloc() for more details.
235 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
237 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
238 unsigned int controllers,
241 struct mux_chip **ptr, *mux_chip;
243 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
245 return ERR_PTR(-ENOMEM);
247 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
248 if (IS_ERR(mux_chip)) {
254 devres_add(dev, ptr);
258 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
260 static void devm_mux_chip_reg_release(struct device *dev, void *res)
262 struct mux_chip *mux_chip = *(struct mux_chip **)res;
264 mux_chip_unregister(mux_chip);
268 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
269 * @dev: The parent device implementing the mux interface.
270 * @mux_chip: The mux-chip to register.
272 * See mux_chip_register() for more details.
274 * Return: Zero on success or a negative errno on error.
276 int devm_mux_chip_register(struct device *dev,
277 struct mux_chip *mux_chip)
279 struct mux_chip **ptr;
282 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
286 res = mux_chip_register(mux_chip);
293 devres_add(dev, ptr);
297 EXPORT_SYMBOL_GPL(devm_mux_chip_register);
300 * mux_control_states() - Query the number of multiplexer states.
301 * @mux: The mux-control to query.
303 * Return: The number of multiplexer states.
305 unsigned int mux_control_states(struct mux_control *mux)
309 EXPORT_SYMBOL_GPL(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)
318 if (WARN_ON(state < 0 || state >= mux->states))
321 if (mux->cached_state == state)
324 ret = mux_control_set(mux, state);
328 /* The mux update failed, try to revert if appropriate... */
329 if (mux->idle_state != MUX_IDLE_AS_IS)
330 mux_control_set(mux, mux->idle_state);
335 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
343 delayend = ktime_add_us(mux->last_change, delay_us);
344 remaining = ktime_us_delta(delayend, ktime_get());
350 * mux_control_select_delay() - Select the given multiplexer state.
351 * @mux: The mux-control to request a change of state from.
352 * @state: The new requested state.
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
357 * selected when mux_control_select() is called, the caller will be blocked
358 * until mux_control_deselect() or mux_state_deselect() is called (by someone
361 * Therefore, make sure to call mux_control_deselect() when the operation is
362 * complete and the mux-control is free for others to use, but do not call
363 * mux_control_deselect() if mux_control_select() fails.
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,
369 unsigned int delay_us)
373 ret = down_killable(&mux->lock);
377 ret = __mux_control_select(mux, state);
379 mux_control_delay(mux, delay_us);
386 EXPORT_SYMBOL_GPL(mux_control_select_delay);
389 * mux_state_select_delay() - Select the given multiplexer state.
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
395 * selected when mux_state_select() is called, the caller will be blocked
396 * until mux_state_deselect() or mux_control_deselect() is called (by someone
399 * Therefore, make sure to call mux_state_deselect() when the operation is
400 * complete and the mux-control is free for others to use, but do not call
401 * mux_state_deselect() if mux_state_select() fails.
403 * Return: 0 when the mux-state has been selected or a negative
406 int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us)
408 return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
410 EXPORT_SYMBOL_GPL(mux_state_select_delay);
413 * mux_control_try_select_delay() - Try to select the given multiplexer state.
414 * @mux: The mux-control to request a change of state from.
415 * @state: The new requested state.
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
419 * mux_control_deselect() is called.
421 * Therefore, make sure to call mux_control_deselect() when the operation is
422 * complete and the mux-control is free for others to use, but do not call
423 * mux_control_deselect() if mux_control_try_select() fails.
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,
429 unsigned int delay_us)
433 if (down_trylock(&mux->lock))
436 ret = __mux_control_select(mux, state);
438 mux_control_delay(mux, delay_us);
445 EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
448 * mux_state_try_select_delay() - Try to select the given multiplexer state.
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
453 * until mux_state_deselect() is called.
455 * Therefore, make sure to call mux_state_deselect() when the operation is
456 * complete and the mux-control is free for others to use, but do not call
457 * mux_state_deselect() if mux_state_try_select() fails.
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.
462 int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us)
464 return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
466 EXPORT_SYMBOL_GPL(mux_state_try_select_delay);
469 * mux_control_deselect() - Deselect the previously selected multiplexer state.
470 * @mux: The mux-control to deselect.
472 * It is required that a single call is made to mux_control_deselect() for
473 * each and every successful call made to either of mux_control_select() or
474 * mux_control_try_select().
476 * Return: 0 on success and a negative errno on error. An error can only
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)
484 if (mux->idle_state != MUX_IDLE_AS_IS &&
485 mux->idle_state != mux->cached_state)
486 ret = mux_control_set(mux, mux->idle_state);
492 EXPORT_SYMBOL_GPL(mux_control_deselect);
495 * mux_state_deselect() - Deselect the previously selected multiplexer state.
496 * @mstate: The mux-state to deselect.
498 * It is required that a single call is made to mux_state_deselect() for
499 * each and every successful call made to either of mux_state_select() or
500 * mux_state_try_select().
502 * Return: 0 on success and a negative errno on error. An error can only
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.
506 int mux_state_deselect(struct mux_state *mstate)
508 return mux_control_deselect(mstate->mux);
510 EXPORT_SYMBOL_GPL(mux_state_deselect);
512 /* Note this function returns a reference to the mux_chip dev. */
513 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
517 dev = class_find_device_by_of_node(&mux_class, np);
519 return dev ? to_mux_chip(dev) : NULL;
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.
526 * @state: Pointer to where the requested state is returned, or NULL when
527 * the required multiplexer states are handled by other means.
529 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
531 static struct mux_control *mux_get(struct device *dev, const char *mux_name,
534 struct device_node *np = dev->of_node;
535 struct of_phandle_args args;
536 struct mux_chip *mux_chip;
537 unsigned int controller;
543 index = of_property_match_string(np, "mux-state-names",
546 index = of_property_match_string(np, "mux-control-names",
549 dev_err(dev, "mux controller '%s' not found\n",
551 return ERR_PTR(index);
556 ret = of_parse_phandle_with_args(np,
557 "mux-states", "#mux-state-cells",
560 ret = of_parse_phandle_with_args(np,
561 "mux-controls", "#mux-control-cells",
564 dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
565 np, state ? "state" : "control", mux_name ?: "", index);
569 mux_chip = of_find_mux_chip_by_node(args.np);
570 of_node_put(args.np);
572 return ERR_PTR(-EPROBE_DEFER);
576 if (args.args_count > 2 || args.args_count == 0 ||
577 (args.args_count < 2 && mux_chip->controllers > 1)) {
578 dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
580 put_device(&mux_chip->dev);
581 return ERR_PTR(-EINVAL);
584 if (args.args_count == 2) {
585 controller = args.args[0];
586 *state = args.args[1];
588 *state = args.args[0];
592 if (args.args_count > 1 ||
593 (!args.args_count && mux_chip->controllers > 1)) {
594 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
596 put_device(&mux_chip->dev);
597 return ERR_PTR(-EINVAL);
601 controller = args.args[0];
604 if (controller >= mux_chip->controllers) {
605 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
606 np, controller, args.np);
607 put_device(&mux_chip->dev);
608 return ERR_PTR(-EINVAL);
611 return &mux_chip->mux[controller];
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.
621 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
623 return mux_get(dev, mux_name, NULL);
625 EXPORT_SYMBOL_GPL(mux_control_get);
628 * mux_control_put() - Put away the mux-control for good.
629 * @mux: The mux-control to put away.
631 * mux_control_put() reverses the effects of mux_control_get().
633 void mux_control_put(struct mux_control *mux)
635 put_device(&mux->chip->dev);
637 EXPORT_SYMBOL_GPL(mux_control_put);
639 static void devm_mux_control_release(struct device *dev, void *res)
641 struct mux_control *mux = *(struct mux_control **)res;
643 mux_control_put(mux);
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.
654 struct mux_control *devm_mux_control_get(struct device *dev,
655 const char *mux_name)
657 struct mux_control **ptr, *mux;
659 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
661 return ERR_PTR(-ENOMEM);
663 mux = mux_control_get(dev, mux_name);
670 devres_add(dev, ptr);
674 EXPORT_SYMBOL_GPL(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.
683 static struct mux_state *mux_state_get(struct device *dev, const char *mux_name)
685 struct mux_state *mstate;
687 mstate = kzalloc(sizeof(*mstate), GFP_KERNEL);
689 return ERR_PTR(-ENOMEM);
691 mstate->mux = mux_get(dev, mux_name, &mstate->state);
692 if (IS_ERR(mstate->mux)) {
693 int err = PTR_ERR(mstate->mux);
703 * mux_state_put() - Put away the mux-state for good.
704 * @mstate: The mux-state to put away.
706 * mux_state_put() reverses the effects of mux_state_get().
708 static void mux_state_put(struct mux_state *mstate)
710 mux_control_put(mstate->mux);
714 static void devm_mux_state_release(struct device *dev, void *res)
716 struct mux_state *mstate = *(struct mux_state **)res;
718 mux_state_put(mstate);
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.
729 struct mux_state *devm_mux_state_get(struct device *dev,
730 const char *mux_name)
732 struct mux_state **ptr, *mstate;
734 ptr = devres_alloc(devm_mux_state_release, sizeof(*ptr), GFP_KERNEL);
736 return ERR_PTR(-ENOMEM);
738 mstate = mux_state_get(dev, mux_name);
739 if (IS_ERR(mstate)) {
745 devres_add(dev, ptr);
749 EXPORT_SYMBOL_GPL(devm_mux_state_get);
752 * Using subsys_initcall instead of module_init here to try to ensure - for
753 * the non-modular case - that the subsystem is initialized when mux consumers
754 * and mux controllers start to use it.
755 * For the modular case, the ordering is ensured with module dependencies.
757 subsys_initcall(mux_init);
758 module_exit(mux_exit);
760 MODULE_DESCRIPTION("Multiplexer subsystem");
761 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
762 MODULE_LICENSE("GPL v2");