1 // SPDX-License-Identifier: GPL-2.0
3 * Multiplexer subsystem
5 * Copyright (C) 2017 Axentia Technologies AB
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
32 static struct class mux_class = {
37 static DEFINE_IDA(mux_ida);
39 static int __init mux_init(void)
42 return class_register(&mux_class);
45 static void __exit mux_exit(void)
47 class_unregister(&mux_class);
48 ida_destroy(&mux_ida);
51 static void mux_chip_release(struct device *dev)
53 struct mux_chip *mux_chip = to_mux_chip(dev);
55 ida_simple_remove(&mux_ida, mux_chip->id);
59 static const struct device_type mux_type = {
61 .release = mux_chip_release,
65 * mux_chip_alloc() - Allocate a mux-chip.
66 * @dev: The parent device implementing the mux interface.
67 * @controllers: The number of mux controllers to allocate for this chip.
68 * @sizeof_priv: Size of extra memory area for private use by the caller.
70 * After allocating the mux-chip with the desired number of mux controllers
71 * but before registering the chip, the mux driver is required to configure
72 * the number of valid mux states in the mux_chip->mux[N].states members and
73 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
74 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
75 * provide a pointer to the operations struct in the mux_chip->ops member
76 * before registering the mux-chip with mux_chip_register.
78 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
80 struct mux_chip *mux_chip_alloc(struct device *dev,
81 unsigned int controllers, size_t sizeof_priv)
83 struct mux_chip *mux_chip;
86 if (WARN_ON(!dev || !controllers))
87 return ERR_PTR(-EINVAL);
89 mux_chip = kzalloc(sizeof(*mux_chip) +
90 controllers * sizeof(*mux_chip->mux) +
91 sizeof_priv, GFP_KERNEL);
93 return ERR_PTR(-ENOMEM);
95 mux_chip->mux = (struct mux_control *)(mux_chip + 1);
96 mux_chip->dev.class = &mux_class;
97 mux_chip->dev.type = &mux_type;
98 mux_chip->dev.parent = dev;
99 mux_chip->dev.of_node = dev->of_node;
100 dev_set_drvdata(&mux_chip->dev, mux_chip);
102 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
103 if (mux_chip->id < 0) {
104 int err = mux_chip->id;
106 pr_err("muxchipX failed to get a device id\n");
110 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
112 mux_chip->controllers = controllers;
113 for (i = 0; i < controllers; ++i) {
114 struct mux_control *mux = &mux_chip->mux[i];
116 mux->chip = mux_chip;
117 sema_init(&mux->lock, 1);
118 mux->cached_state = MUX_CACHE_UNKNOWN;
119 mux->idle_state = MUX_IDLE_AS_IS;
120 mux->last_change = ktime_get();
123 device_initialize(&mux_chip->dev);
127 EXPORT_SYMBOL_GPL(mux_chip_alloc);
129 static int mux_control_set(struct mux_control *mux, int state)
131 int ret = mux->chip->ops->set(mux, state);
133 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
135 mux->last_change = ktime_get();
141 * mux_chip_register() - Register a mux-chip, thus readying the controllers
143 * @mux_chip: The mux-chip to register.
145 * Do not retry registration of the same mux-chip on failure. You should
146 * instead put it away with mux_chip_free() and allocate a new one, if you
147 * for some reason would like to retry registration.
149 * Return: Zero on success or a negative errno on error.
151 int mux_chip_register(struct mux_chip *mux_chip)
156 for (i = 0; i < mux_chip->controllers; ++i) {
157 struct mux_control *mux = &mux_chip->mux[i];
159 if (mux->idle_state == mux->cached_state)
162 ret = mux_control_set(mux, mux->idle_state);
164 dev_err(&mux_chip->dev, "unable to set idle state\n");
169 ret = device_add(&mux_chip->dev);
171 dev_err(&mux_chip->dev,
172 "device_add failed in %s: %d\n", __func__, ret);
175 EXPORT_SYMBOL_GPL(mux_chip_register);
178 * mux_chip_unregister() - Take the mux-chip off-line.
179 * @mux_chip: The mux-chip to unregister.
181 * mux_chip_unregister() reverses the effects of mux_chip_register().
182 * But not completely, you should not try to call mux_chip_register()
183 * on a mux-chip that has been registered before.
185 void mux_chip_unregister(struct mux_chip *mux_chip)
187 device_del(&mux_chip->dev);
189 EXPORT_SYMBOL_GPL(mux_chip_unregister);
192 * mux_chip_free() - Free the mux-chip for good.
193 * @mux_chip: The mux-chip to free.
195 * mux_chip_free() reverses the effects of mux_chip_alloc().
197 void mux_chip_free(struct mux_chip *mux_chip)
202 put_device(&mux_chip->dev);
204 EXPORT_SYMBOL_GPL(mux_chip_free);
206 static void devm_mux_chip_release(struct device *dev, void *res)
208 struct mux_chip *mux_chip = *(struct mux_chip **)res;
210 mux_chip_free(mux_chip);
214 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
215 * @dev: The parent device implementing the mux interface.
216 * @controllers: The number of mux controllers to allocate for this chip.
217 * @sizeof_priv: Size of extra memory area for private use by the caller.
219 * See mux_chip_alloc() for more details.
221 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
223 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
224 unsigned int controllers,
227 struct mux_chip **ptr, *mux_chip;
229 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
231 return ERR_PTR(-ENOMEM);
233 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
234 if (IS_ERR(mux_chip)) {
240 devres_add(dev, ptr);
244 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
246 static void devm_mux_chip_reg_release(struct device *dev, void *res)
248 struct mux_chip *mux_chip = *(struct mux_chip **)res;
250 mux_chip_unregister(mux_chip);
254 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
255 * @dev: The parent device implementing the mux interface.
256 * @mux_chip: The mux-chip to register.
258 * See mux_chip_register() for more details.
260 * Return: Zero on success or a negative errno on error.
262 int devm_mux_chip_register(struct device *dev,
263 struct mux_chip *mux_chip)
265 struct mux_chip **ptr;
268 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
272 res = mux_chip_register(mux_chip);
279 devres_add(dev, ptr);
283 EXPORT_SYMBOL_GPL(devm_mux_chip_register);
286 * mux_control_states() - Query the number of multiplexer states.
287 * @mux: The mux-control to query.
289 * Return: The number of multiplexer states.
291 unsigned int mux_control_states(struct mux_control *mux)
295 EXPORT_SYMBOL_GPL(mux_control_states);
298 * The mux->lock must be down when calling this function.
300 static int __mux_control_select(struct mux_control *mux, int state)
304 if (WARN_ON(state < 0 || state >= mux->states))
307 if (mux->cached_state == state)
310 ret = mux_control_set(mux, state);
314 /* The mux update failed, try to revert if appropriate... */
315 if (mux->idle_state != MUX_IDLE_AS_IS)
316 mux_control_set(mux, mux->idle_state);
321 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
329 delayend = ktime_add_us(mux->last_change, delay_us);
330 remaining = ktime_us_delta(delayend, ktime_get());
336 * mux_control_select_delay() - Select the given multiplexer state.
337 * @mux: The mux-control to request a change of state from.
338 * @state: The new requested state.
339 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
341 * On successfully selecting the mux-control state, it will be locked until
342 * there is a call to mux_control_deselect(). If the mux-control is already
343 * selected when mux_control_select() is called, the caller will be blocked
344 * until mux_control_deselect() is called (by someone else).
346 * Therefore, make sure to call mux_control_deselect() when the operation is
347 * complete and the mux-control is free for others to use, but do not call
348 * mux_control_deselect() if mux_control_select() fails.
350 * Return: 0 when the mux-control state has the requested state or a negative
353 int mux_control_select_delay(struct mux_control *mux, unsigned int state,
354 unsigned int delay_us)
358 ret = down_killable(&mux->lock);
362 ret = __mux_control_select(mux, state);
364 mux_control_delay(mux, delay_us);
371 EXPORT_SYMBOL_GPL(mux_control_select_delay);
374 * mux_control_try_select_delay() - Try to select the given multiplexer state.
375 * @mux: The mux-control to request a change of state from.
376 * @state: The new requested state.
377 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
379 * On successfully selecting the mux-control state, it will be locked until
380 * mux_control_deselect() called.
382 * Therefore, make sure to call mux_control_deselect() when the operation is
383 * complete and the mux-control is free for others to use, but do not call
384 * mux_control_deselect() if mux_control_try_select() fails.
386 * Return: 0 when the mux-control state has the requested state or a negative
387 * errno on error. Specifically -EBUSY if the mux-control is contended.
389 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
390 unsigned int delay_us)
394 if (down_trylock(&mux->lock))
397 ret = __mux_control_select(mux, state);
399 mux_control_delay(mux, delay_us);
406 EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
409 * mux_control_deselect() - Deselect the previously selected multiplexer state.
410 * @mux: The mux-control to deselect.
412 * It is required that a single call is made to mux_control_deselect() for
413 * each and every successful call made to either of mux_control_select() or
414 * mux_control_try_select().
416 * Return: 0 on success and a negative errno on error. An error can only
417 * occur if the mux has an idle state. Note that even if an error occurs, the
418 * mux-control is unlocked and is thus free for the next access.
420 int mux_control_deselect(struct mux_control *mux)
424 if (mux->idle_state != MUX_IDLE_AS_IS &&
425 mux->idle_state != mux->cached_state)
426 ret = mux_control_set(mux, mux->idle_state);
432 EXPORT_SYMBOL_GPL(mux_control_deselect);
434 /* Note this function returns a reference to the mux_chip dev. */
435 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
439 dev = class_find_device_by_of_node(&mux_class, np);
441 return dev ? to_mux_chip(dev) : NULL;
445 * mux_control_get() - Get the mux-control for a device.
446 * @dev: The device that needs a mux-control.
447 * @mux_name: The name identifying the mux-control.
449 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
451 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
453 struct device_node *np = dev->of_node;
454 struct of_phandle_args args;
455 struct mux_chip *mux_chip;
456 unsigned int controller;
461 index = of_property_match_string(np, "mux-control-names",
464 dev_err(dev, "mux controller '%s' not found\n",
466 return ERR_PTR(index);
470 ret = of_parse_phandle_with_args(np,
471 "mux-controls", "#mux-control-cells",
474 dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
475 np, mux_name ?: "", index);
479 mux_chip = of_find_mux_chip_by_node(args.np);
480 of_node_put(args.np);
482 return ERR_PTR(-EPROBE_DEFER);
484 if (args.args_count > 1 ||
485 (!args.args_count && (mux_chip->controllers > 1))) {
486 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
488 put_device(&mux_chip->dev);
489 return ERR_PTR(-EINVAL);
494 controller = args.args[0];
496 if (controller >= mux_chip->controllers) {
497 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
498 np, controller, args.np);
499 put_device(&mux_chip->dev);
500 return ERR_PTR(-EINVAL);
503 return &mux_chip->mux[controller];
505 EXPORT_SYMBOL_GPL(mux_control_get);
508 * mux_control_put() - Put away the mux-control for good.
509 * @mux: The mux-control to put away.
511 * mux_control_put() reverses the effects of mux_control_get().
513 void mux_control_put(struct mux_control *mux)
515 put_device(&mux->chip->dev);
517 EXPORT_SYMBOL_GPL(mux_control_put);
519 static void devm_mux_control_release(struct device *dev, void *res)
521 struct mux_control *mux = *(struct mux_control **)res;
523 mux_control_put(mux);
527 * devm_mux_control_get() - Get the mux-control for a device, with resource
529 * @dev: The device that needs a mux-control.
530 * @mux_name: The name identifying the mux-control.
532 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
534 struct mux_control *devm_mux_control_get(struct device *dev,
535 const char *mux_name)
537 struct mux_control **ptr, *mux;
539 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
541 return ERR_PTR(-ENOMEM);
543 mux = mux_control_get(dev, mux_name);
550 devres_add(dev, ptr);
554 EXPORT_SYMBOL_GPL(devm_mux_control_get);
557 * Using subsys_initcall instead of module_init here to try to ensure - for
558 * the non-modular case - that the subsystem is initialized when mux consumers
559 * and mux controllers start to use it.
560 * For the modular case, the ordering is ensured with module dependencies.
562 subsys_initcall(mux_init);
563 module_exit(mux_exit);
565 MODULE_DESCRIPTION("Multiplexer subsystem");
567 MODULE_LICENSE("GPL v2");