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/slab.h>
25 * The idle-as-is "state" is not an actual state that may be selected, it
26 * only implies that the state should not be changed. So, use that state
27 * as indication that the cached state of the multiplexer is unknown.
29 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
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.
37 * This structure is specific to the consumer that acquires it and has
38 * information specific to that consumer.
41 struct mux_control
*mux
;
45 static struct class mux_class
= {
49 static DEFINE_IDA(mux_ida
);
51 static int __init
mux_init(void)
54 return class_register(&mux_class
);
57 static void __exit
mux_exit(void)
59 class_unregister(&mux_class
);
60 ida_destroy(&mux_ida
);
63 static void mux_chip_release(struct device
*dev
)
65 struct mux_chip
*mux_chip
= to_mux_chip(dev
);
67 ida_free(&mux_ida
, mux_chip
->id
);
71 static const struct device_type mux_type
= {
73 .release
= mux_chip_release
,
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.
80 * @sizeof_priv: Size of extra memory area for private use by the caller.
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.
92 struct mux_chip
*mux_chip_alloc(struct device
*dev
,
93 unsigned int controllers
, size_t sizeof_priv
)
95 struct mux_chip
*mux_chip
;
98 if (WARN_ON(!dev
|| !controllers
))
99 return ERR_PTR(-EINVAL
);
101 mux_chip
= kzalloc(sizeof(*mux_chip
) +
102 controllers
* sizeof(*mux_chip
->mux
) +
103 sizeof_priv
, GFP_KERNEL
);
105 return ERR_PTR(-ENOMEM
);
107 mux_chip
->mux
= (struct mux_control
*)(mux_chip
+ 1);
108 mux_chip
->dev
.class = &mux_class
;
109 mux_chip
->dev
.type
= &mux_type
;
110 mux_chip
->dev
.parent
= dev
;
111 mux_chip
->dev
.of_node
= dev
->of_node
;
112 dev_set_drvdata(&mux_chip
->dev
, mux_chip
);
114 mux_chip
->id
= ida_alloc(&mux_ida
, GFP_KERNEL
);
115 if (mux_chip
->id
< 0) {
116 int err
= mux_chip
->id
;
118 pr_err("muxchipX failed to get a device id\n");
122 dev_set_name(&mux_chip
->dev
, "muxchip%d", mux_chip
->id
);
124 mux_chip
->controllers
= controllers
;
125 for (i
= 0; i
< controllers
; ++i
) {
126 struct mux_control
*mux
= &mux_chip
->mux
[i
];
128 mux
->chip
= mux_chip
;
129 sema_init(&mux
->lock
, 1);
130 mux
->cached_state
= MUX_CACHE_UNKNOWN
;
131 mux
->idle_state
= MUX_IDLE_AS_IS
;
132 mux
->last_change
= ktime_get();
135 device_initialize(&mux_chip
->dev
);
139 EXPORT_SYMBOL_GPL(mux_chip_alloc
);
141 static int mux_control_set(struct mux_control
*mux
, int state
)
143 int ret
= mux
->chip
->ops
->set(mux
, state
);
145 mux
->cached_state
= ret
< 0 ? MUX_CACHE_UNKNOWN
: state
;
147 mux
->last_change
= ktime_get();
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
158 * instead put it away with mux_chip_free() and allocate a new one, if you
159 * for some reason would like to retry registration.
161 * Return: Zero on success or a negative errno on error.
163 int mux_chip_register(struct mux_chip
*mux_chip
)
168 for (i
= 0; i
< mux_chip
->controllers
; ++i
) {
169 struct mux_control
*mux
= &mux_chip
->mux
[i
];
171 if (mux
->idle_state
== mux
->cached_state
)
174 ret
= mux_control_set(mux
, mux
->idle_state
);
176 dev_err(&mux_chip
->dev
, "unable to set idle state\n");
181 ret
= device_add(&mux_chip
->dev
);
183 dev_err(&mux_chip
->dev
,
184 "device_add failed in %s: %d\n", __func__
, ret
);
187 EXPORT_SYMBOL_GPL(mux_chip_register
);
190 * mux_chip_unregister() - Take the mux-chip off-line.
191 * @mux_chip: The mux-chip to unregister.
193 * mux_chip_unregister() reverses the effects of mux_chip_register().
194 * But not completely, you should not try to call mux_chip_register()
195 * on a mux-chip that has been registered before.
197 void mux_chip_unregister(struct mux_chip
*mux_chip
)
199 device_del(&mux_chip
->dev
);
201 EXPORT_SYMBOL_GPL(mux_chip_unregister
);
204 * mux_chip_free() - Free the mux-chip for good.
205 * @mux_chip: The mux-chip to free.
207 * mux_chip_free() reverses the effects of mux_chip_alloc().
209 void mux_chip_free(struct mux_chip
*mux_chip
)
214 put_device(&mux_chip
->dev
);
216 EXPORT_SYMBOL_GPL(mux_chip_free
);
218 static void devm_mux_chip_release(struct device
*dev
, void *res
)
220 struct mux_chip
*mux_chip
= *(struct mux_chip
**)res
;
222 mux_chip_free(mux_chip
);
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.
229 * @sizeof_priv: Size of extra memory area for private use by the caller.
231 * See mux_chip_alloc() for more details.
233 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
235 struct mux_chip
*devm_mux_chip_alloc(struct device
*dev
,
236 unsigned int controllers
,
239 struct mux_chip
**ptr
, *mux_chip
;
241 ptr
= devres_alloc(devm_mux_chip_release
, sizeof(*ptr
), GFP_KERNEL
);
243 return ERR_PTR(-ENOMEM
);
245 mux_chip
= mux_chip_alloc(dev
, controllers
, sizeof_priv
);
246 if (IS_ERR(mux_chip
)) {
252 devres_add(dev
, ptr
);
256 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc
);
258 static void devm_mux_chip_reg_release(struct device
*dev
, void *res
)
260 struct mux_chip
*mux_chip
= *(struct mux_chip
**)res
;
262 mux_chip_unregister(mux_chip
);
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.
270 * See mux_chip_register() for more details.
272 * Return: Zero on success or a negative errno on error.
274 int devm_mux_chip_register(struct device
*dev
,
275 struct mux_chip
*mux_chip
)
277 struct mux_chip
**ptr
;
280 ptr
= devres_alloc(devm_mux_chip_reg_release
, sizeof(*ptr
), GFP_KERNEL
);
284 res
= mux_chip_register(mux_chip
);
291 devres_add(dev
, ptr
);
295 EXPORT_SYMBOL_GPL(devm_mux_chip_register
);
298 * mux_control_states() - Query the number of multiplexer states.
299 * @mux: The mux-control to query.
301 * Return: The number of multiplexer states.
303 unsigned int mux_control_states(struct mux_control
*mux
)
307 EXPORT_SYMBOL_GPL(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
)
316 if (WARN_ON(state
< 0 || state
>= mux
->states
))
319 if (mux
->cached_state
== state
)
322 ret
= mux_control_set(mux
, state
);
326 /* The mux update failed, try to revert if appropriate... */
327 if (mux
->idle_state
!= MUX_IDLE_AS_IS
)
328 mux_control_set(mux
, mux
->idle_state
);
333 static void mux_control_delay(struct mux_control
*mux
, unsigned int delay_us
)
341 delayend
= ktime_add_us(mux
->last_change
, delay_us
);
342 remaining
= ktime_us_delta(delayend
, ktime_get());
348 * mux_control_select_delay() - Select the given multiplexer state.
349 * @mux: The mux-control to request a change of state from.
350 * @state: The new requested state.
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
355 * selected when mux_control_select() is called, the caller will be blocked
356 * until mux_control_deselect() or mux_state_deselect() is called (by someone
359 * Therefore, make sure to call mux_control_deselect() when the operation is
360 * complete and the mux-control is free for others to use, but do not call
361 * mux_control_deselect() if mux_control_select() fails.
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
,
367 unsigned int delay_us
)
371 ret
= down_killable(&mux
->lock
);
375 ret
= __mux_control_select(mux
, state
);
377 mux_control_delay(mux
, delay_us
);
384 EXPORT_SYMBOL_GPL(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
393 * selected when mux_state_select() is called, the caller will be blocked
394 * until mux_state_deselect() or mux_control_deselect() is called (by someone
397 * Therefore, make sure to call mux_state_deselect() when the operation is
398 * complete and the mux-control is free for others to use, but do not call
399 * mux_state_deselect() if mux_state_select() fails.
401 * Return: 0 when the mux-state has been selected or a negative
404 int mux_state_select_delay(struct mux_state
*mstate
, unsigned int delay_us
)
406 return mux_control_select_delay(mstate
->mux
, mstate
->state
, delay_us
);
408 EXPORT_SYMBOL_GPL(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.
413 * @state: The new requested state.
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
417 * mux_control_deselect() is called.
419 * Therefore, make sure to call mux_control_deselect() when the operation is
420 * complete and the mux-control is free for others to use, but do not call
421 * mux_control_deselect() if mux_control_try_select() fails.
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
,
427 unsigned int delay_us
)
431 if (down_trylock(&mux
->lock
))
434 ret
= __mux_control_select(mux
, state
);
436 mux_control_delay(mux
, delay_us
);
443 EXPORT_SYMBOL_GPL(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
451 * until mux_state_deselect() is called.
453 * Therefore, make sure to call mux_state_deselect() when the operation is
454 * complete and the mux-control is free for others to use, but do not call
455 * mux_state_deselect() if mux_state_try_select() fails.
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.
460 int mux_state_try_select_delay(struct mux_state
*mstate
, unsigned int delay_us
)
462 return mux_control_try_select_delay(mstate
->mux
, mstate
->state
, delay_us
);
464 EXPORT_SYMBOL_GPL(mux_state_try_select_delay
);
467 * mux_control_deselect() - Deselect the previously selected multiplexer state.
468 * @mux: The mux-control to deselect.
470 * It is required that a single call is made to mux_control_deselect() for
471 * each and every successful call made to either of mux_control_select() or
472 * mux_control_try_select().
474 * Return: 0 on success and a negative errno on error. An error can only
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
)
482 if (mux
->idle_state
!= MUX_IDLE_AS_IS
&&
483 mux
->idle_state
!= mux
->cached_state
)
484 ret
= mux_control_set(mux
, mux
->idle_state
);
490 EXPORT_SYMBOL_GPL(mux_control_deselect
);
493 * mux_state_deselect() - Deselect the previously selected multiplexer state.
494 * @mstate: The mux-state to deselect.
496 * It is required that a single call is made to mux_state_deselect() for
497 * each and every successful call made to either of mux_state_select() or
498 * mux_state_try_select().
500 * Return: 0 on success and a negative errno on error. An error can only
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.
504 int mux_state_deselect(struct mux_state
*mstate
)
506 return mux_control_deselect(mstate
->mux
);
508 EXPORT_SYMBOL_GPL(mux_state_deselect
);
510 /* Note this function returns a reference to the mux_chip dev. */
511 static struct mux_chip
*of_find_mux_chip_by_node(struct device_node
*np
)
515 dev
= class_find_device_by_of_node(&mux_class
, np
);
517 return dev
? to_mux_chip(dev
) : NULL
;
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.
524 * @state: Pointer to where the requested state is returned, or NULL when
525 * the required multiplexer states are handled by other means.
527 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
529 static struct mux_control
*mux_get(struct device
*dev
, const char *mux_name
,
532 struct device_node
*np
= dev
->of_node
;
533 struct of_phandle_args args
;
534 struct mux_chip
*mux_chip
;
535 unsigned int controller
;
541 index
= of_property_match_string(np
, "mux-state-names",
544 index
= of_property_match_string(np
, "mux-control-names",
547 dev_err(dev
, "mux controller '%s' not found\n",
549 return ERR_PTR(index
);
554 ret
= of_parse_phandle_with_args(np
,
555 "mux-states", "#mux-state-cells",
558 ret
= of_parse_phandle_with_args(np
,
559 "mux-controls", "#mux-control-cells",
562 dev_err(dev
, "%pOF: failed to get mux-%s %s(%i)\n",
563 np
, state
? "state" : "control", mux_name
?: "", index
);
567 mux_chip
= of_find_mux_chip_by_node(args
.np
);
568 of_node_put(args
.np
);
570 return ERR_PTR(-EPROBE_DEFER
);
574 if (args
.args_count
> 2 || args
.args_count
== 0 ||
575 (args
.args_count
< 2 && mux_chip
->controllers
> 1)) {
576 dev_err(dev
, "%pOF: wrong #mux-state-cells for %pOF\n",
578 put_device(&mux_chip
->dev
);
579 return ERR_PTR(-EINVAL
);
582 if (args
.args_count
== 2) {
583 controller
= args
.args
[0];
584 *state
= args
.args
[1];
586 *state
= args
.args
[0];
590 if (args
.args_count
> 1 ||
591 (!args
.args_count
&& mux_chip
->controllers
> 1)) {
592 dev_err(dev
, "%pOF: wrong #mux-control-cells for %pOF\n",
594 put_device(&mux_chip
->dev
);
595 return ERR_PTR(-EINVAL
);
599 controller
= args
.args
[0];
602 if (controller
>= mux_chip
->controllers
) {
603 dev_err(dev
, "%pOF: bad mux controller %u specified in %pOF\n",
604 np
, controller
, args
.np
);
605 put_device(&mux_chip
->dev
);
606 return ERR_PTR(-EINVAL
);
609 return &mux_chip
->mux
[controller
];
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.
619 struct mux_control
*mux_control_get(struct device
*dev
, const char *mux_name
)
621 return mux_get(dev
, mux_name
, NULL
);
623 EXPORT_SYMBOL_GPL(mux_control_get
);
626 * mux_control_put() - Put away the mux-control for good.
627 * @mux: The mux-control to put away.
629 * mux_control_put() reverses the effects of mux_control_get().
631 void mux_control_put(struct mux_control
*mux
)
633 put_device(&mux
->chip
->dev
);
635 EXPORT_SYMBOL_GPL(mux_control_put
);
637 static void devm_mux_control_release(struct device
*dev
, void *res
)
639 struct mux_control
*mux
= *(struct mux_control
**)res
;
641 mux_control_put(mux
);
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.
652 struct mux_control
*devm_mux_control_get(struct device
*dev
,
653 const char *mux_name
)
655 struct mux_control
**ptr
, *mux
;
657 ptr
= devres_alloc(devm_mux_control_release
, sizeof(*ptr
), GFP_KERNEL
);
659 return ERR_PTR(-ENOMEM
);
661 mux
= mux_control_get(dev
, mux_name
);
668 devres_add(dev
, ptr
);
672 EXPORT_SYMBOL_GPL(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.
681 static struct mux_state
*mux_state_get(struct device
*dev
, const char *mux_name
)
683 struct mux_state
*mstate
;
685 mstate
= kzalloc(sizeof(*mstate
), GFP_KERNEL
);
687 return ERR_PTR(-ENOMEM
);
689 mstate
->mux
= mux_get(dev
, mux_name
, &mstate
->state
);
690 if (IS_ERR(mstate
->mux
)) {
691 int err
= PTR_ERR(mstate
->mux
);
701 * mux_state_put() - Put away the mux-state for good.
702 * @mstate: The mux-state to put away.
704 * mux_state_put() reverses the effects of mux_state_get().
706 static void mux_state_put(struct mux_state
*mstate
)
708 mux_control_put(mstate
->mux
);
712 static void devm_mux_state_release(struct device
*dev
, void *res
)
714 struct mux_state
*mstate
= *(struct mux_state
**)res
;
716 mux_state_put(mstate
);
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.
727 struct mux_state
*devm_mux_state_get(struct device
*dev
,
728 const char *mux_name
)
730 struct mux_state
**ptr
, *mstate
;
732 ptr
= devres_alloc(devm_mux_state_release
, sizeof(*ptr
), GFP_KERNEL
);
734 return ERR_PTR(-ENOMEM
);
736 mstate
= mux_state_get(dev
, mux_name
);
737 if (IS_ERR(mstate
)) {
743 devres_add(dev
, ptr
);
747 EXPORT_SYMBOL_GPL(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.
753 * For the modular case, the ordering is ensured with module dependencies.
755 subsys_initcall(mux_init
);
756 module_exit(mux_exit
);
758 MODULE_DESCRIPTION("Multiplexer subsystem");
759 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
760 MODULE_LICENSE("GPL v2");