xref: /openbmc/linux/drivers/mux/core.c (revision 8f3addf1)
18f3addf1SPeter Rosin /*
28f3addf1SPeter Rosin  * Multiplexer subsystem
38f3addf1SPeter Rosin  *
48f3addf1SPeter Rosin  * Copyright (C) 2017 Axentia Technologies AB
58f3addf1SPeter Rosin  *
68f3addf1SPeter Rosin  * Author: Peter Rosin <peda@axentia.se>
78f3addf1SPeter Rosin  *
88f3addf1SPeter Rosin  * This program is free software; you can redistribute it and/or modify
98f3addf1SPeter Rosin  * it under the terms of the GNU General Public License version 2 as
108f3addf1SPeter Rosin  * published by the Free Software Foundation.
118f3addf1SPeter Rosin  */
128f3addf1SPeter Rosin 
138f3addf1SPeter Rosin #define pr_fmt(fmt) "mux-core: " fmt
148f3addf1SPeter Rosin 
158f3addf1SPeter Rosin #include <linux/device.h>
168f3addf1SPeter Rosin #include <linux/err.h>
178f3addf1SPeter Rosin #include <linux/export.h>
188f3addf1SPeter Rosin #include <linux/idr.h>
198f3addf1SPeter Rosin #include <linux/init.h>
208f3addf1SPeter Rosin #include <linux/module.h>
218f3addf1SPeter Rosin #include <linux/mux/consumer.h>
228f3addf1SPeter Rosin #include <linux/mux/driver.h>
238f3addf1SPeter Rosin #include <linux/of.h>
248f3addf1SPeter Rosin #include <linux/of_platform.h>
258f3addf1SPeter Rosin #include <linux/slab.h>
268f3addf1SPeter Rosin 
278f3addf1SPeter Rosin /*
288f3addf1SPeter Rosin  * The idle-as-is "state" is not an actual state that may be selected, it
298f3addf1SPeter Rosin  * only implies that the state should not be changed. So, use that state
308f3addf1SPeter Rosin  * as indication that the cached state of the multiplexer is unknown.
318f3addf1SPeter Rosin  */
328f3addf1SPeter Rosin #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
338f3addf1SPeter Rosin 
348f3addf1SPeter Rosin static struct class mux_class = {
358f3addf1SPeter Rosin 	.name = "mux",
368f3addf1SPeter Rosin 	.owner = THIS_MODULE,
378f3addf1SPeter Rosin };
388f3addf1SPeter Rosin 
398f3addf1SPeter Rosin static DEFINE_IDA(mux_ida);
408f3addf1SPeter Rosin 
418f3addf1SPeter Rosin static int __init mux_init(void)
428f3addf1SPeter Rosin {
438f3addf1SPeter Rosin 	ida_init(&mux_ida);
448f3addf1SPeter Rosin 	return class_register(&mux_class);
458f3addf1SPeter Rosin }
468f3addf1SPeter Rosin 
478f3addf1SPeter Rosin static void __exit mux_exit(void)
488f3addf1SPeter Rosin {
498f3addf1SPeter Rosin 	class_unregister(&mux_class);
508f3addf1SPeter Rosin 	ida_destroy(&mux_ida);
518f3addf1SPeter Rosin }
528f3addf1SPeter Rosin 
538f3addf1SPeter Rosin static void mux_chip_release(struct device *dev)
548f3addf1SPeter Rosin {
558f3addf1SPeter Rosin 	struct mux_chip *mux_chip = to_mux_chip(dev);
568f3addf1SPeter Rosin 
578f3addf1SPeter Rosin 	ida_simple_remove(&mux_ida, mux_chip->id);
588f3addf1SPeter Rosin 	kfree(mux_chip);
598f3addf1SPeter Rosin }
608f3addf1SPeter Rosin 
618f3addf1SPeter Rosin static struct device_type mux_type = {
628f3addf1SPeter Rosin 	.name = "mux-chip",
638f3addf1SPeter Rosin 	.release = mux_chip_release,
648f3addf1SPeter Rosin };
658f3addf1SPeter Rosin 
668f3addf1SPeter Rosin /**
678f3addf1SPeter Rosin  * mux_chip_alloc() - Allocate a mux-chip.
688f3addf1SPeter Rosin  * @dev: The parent device implementing the mux interface.
698f3addf1SPeter Rosin  * @controllers: The number of mux controllers to allocate for this chip.
708f3addf1SPeter Rosin  * @sizeof_priv: Size of extra memory area for private use by the caller.
718f3addf1SPeter Rosin  *
728f3addf1SPeter Rosin  * After allocating the mux-chip with the desired number of mux controllers
738f3addf1SPeter Rosin  * but before registering the chip, the mux driver is required to configure
748f3addf1SPeter Rosin  * the number of valid mux states in the mux_chip->mux[N].states members and
758f3addf1SPeter Rosin  * the desired idle state in the returned mux_chip->mux[N].idle_state members.
768f3addf1SPeter Rosin  * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
778f3addf1SPeter Rosin  * provide a pointer to the operations struct in the mux_chip->ops member
788f3addf1SPeter Rosin  * before registering the mux-chip with mux_chip_register.
798f3addf1SPeter Rosin  *
808f3addf1SPeter Rosin  * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
818f3addf1SPeter Rosin  */
828f3addf1SPeter Rosin struct mux_chip *mux_chip_alloc(struct device *dev,
838f3addf1SPeter Rosin 				unsigned int controllers, size_t sizeof_priv)
848f3addf1SPeter Rosin {
858f3addf1SPeter Rosin 	struct mux_chip *mux_chip;
868f3addf1SPeter Rosin 	int i;
878f3addf1SPeter Rosin 
888f3addf1SPeter Rosin 	if (WARN_ON(!dev || !controllers))
898f3addf1SPeter Rosin 		return ERR_PTR(-EINVAL);
908f3addf1SPeter Rosin 
918f3addf1SPeter Rosin 	mux_chip = kzalloc(sizeof(*mux_chip) +
928f3addf1SPeter Rosin 			   controllers * sizeof(*mux_chip->mux) +
938f3addf1SPeter Rosin 			   sizeof_priv, GFP_KERNEL);
948f3addf1SPeter Rosin 	if (!mux_chip)
958f3addf1SPeter Rosin 		return ERR_PTR(-ENOMEM);
968f3addf1SPeter Rosin 
978f3addf1SPeter Rosin 	mux_chip->mux = (struct mux_control *)(mux_chip + 1);
988f3addf1SPeter Rosin 	mux_chip->dev.class = &mux_class;
998f3addf1SPeter Rosin 	mux_chip->dev.type = &mux_type;
1008f3addf1SPeter Rosin 	mux_chip->dev.parent = dev;
1018f3addf1SPeter Rosin 	mux_chip->dev.of_node = dev->of_node;
1028f3addf1SPeter Rosin 	dev_set_drvdata(&mux_chip->dev, mux_chip);
1038f3addf1SPeter Rosin 
1048f3addf1SPeter Rosin 	mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
1058f3addf1SPeter Rosin 	if (mux_chip->id < 0) {
1068f3addf1SPeter Rosin 		int err = mux_chip->id;
1078f3addf1SPeter Rosin 
1088f3addf1SPeter Rosin 		pr_err("muxchipX failed to get a device id\n");
1098f3addf1SPeter Rosin 		kfree(mux_chip);
1108f3addf1SPeter Rosin 		return ERR_PTR(err);
1118f3addf1SPeter Rosin 	}
1128f3addf1SPeter Rosin 	dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
1138f3addf1SPeter Rosin 
1148f3addf1SPeter Rosin 	mux_chip->controllers = controllers;
1158f3addf1SPeter Rosin 	for (i = 0; i < controllers; ++i) {
1168f3addf1SPeter Rosin 		struct mux_control *mux = &mux_chip->mux[i];
1178f3addf1SPeter Rosin 
1188f3addf1SPeter Rosin 		mux->chip = mux_chip;
1198f3addf1SPeter Rosin 		sema_init(&mux->lock, 1);
1208f3addf1SPeter Rosin 		mux->cached_state = MUX_CACHE_UNKNOWN;
1218f3addf1SPeter Rosin 		mux->idle_state = MUX_IDLE_AS_IS;
1228f3addf1SPeter Rosin 	}
1238f3addf1SPeter Rosin 
1248f3addf1SPeter Rosin 	device_initialize(&mux_chip->dev);
1258f3addf1SPeter Rosin 
1268f3addf1SPeter Rosin 	return mux_chip;
1278f3addf1SPeter Rosin }
1288f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_chip_alloc);
1298f3addf1SPeter Rosin 
1308f3addf1SPeter Rosin static int mux_control_set(struct mux_control *mux, int state)
1318f3addf1SPeter Rosin {
1328f3addf1SPeter Rosin 	int ret = mux->chip->ops->set(mux, state);
1338f3addf1SPeter Rosin 
1348f3addf1SPeter Rosin 	mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
1358f3addf1SPeter Rosin 
1368f3addf1SPeter Rosin 	return ret;
1378f3addf1SPeter Rosin }
1388f3addf1SPeter Rosin 
1398f3addf1SPeter Rosin /**
1408f3addf1SPeter Rosin  * mux_chip_register() - Register a mux-chip, thus readying the controllers
1418f3addf1SPeter Rosin  *			 for use.
1428f3addf1SPeter Rosin  * @mux_chip: The mux-chip to register.
1438f3addf1SPeter Rosin  *
1448f3addf1SPeter Rosin  * Do not retry registration of the same mux-chip on failure. You should
1458f3addf1SPeter Rosin  * instead put it away with mux_chip_free() and allocate a new one, if you
1468f3addf1SPeter Rosin  * for some reason would like to retry registration.
1478f3addf1SPeter Rosin  *
1488f3addf1SPeter Rosin  * Return: Zero on success or a negative errno on error.
1498f3addf1SPeter Rosin  */
1508f3addf1SPeter Rosin int mux_chip_register(struct mux_chip *mux_chip)
1518f3addf1SPeter Rosin {
1528f3addf1SPeter Rosin 	int i;
1538f3addf1SPeter Rosin 	int ret;
1548f3addf1SPeter Rosin 
1558f3addf1SPeter Rosin 	for (i = 0; i < mux_chip->controllers; ++i) {
1568f3addf1SPeter Rosin 		struct mux_control *mux = &mux_chip->mux[i];
1578f3addf1SPeter Rosin 
1588f3addf1SPeter Rosin 		if (mux->idle_state == mux->cached_state)
1598f3addf1SPeter Rosin 			continue;
1608f3addf1SPeter Rosin 
1618f3addf1SPeter Rosin 		ret = mux_control_set(mux, mux->idle_state);
1628f3addf1SPeter Rosin 		if (ret < 0) {
1638f3addf1SPeter Rosin 			dev_err(&mux_chip->dev, "unable to set idle state\n");
1648f3addf1SPeter Rosin 			return ret;
1658f3addf1SPeter Rosin 		}
1668f3addf1SPeter Rosin 	}
1678f3addf1SPeter Rosin 
1688f3addf1SPeter Rosin 	ret = device_add(&mux_chip->dev);
1698f3addf1SPeter Rosin 	if (ret < 0)
1708f3addf1SPeter Rosin 		dev_err(&mux_chip->dev,
1718f3addf1SPeter Rosin 			"device_add failed in %s: %d\n", __func__, ret);
1728f3addf1SPeter Rosin 	return ret;
1738f3addf1SPeter Rosin }
1748f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_chip_register);
1758f3addf1SPeter Rosin 
1768f3addf1SPeter Rosin /**
1778f3addf1SPeter Rosin  * mux_chip_unregister() - Take the mux-chip off-line.
1788f3addf1SPeter Rosin  * @mux_chip: The mux-chip to unregister.
1798f3addf1SPeter Rosin  *
1808f3addf1SPeter Rosin  * mux_chip_unregister() reverses the effects of mux_chip_register().
1818f3addf1SPeter Rosin  * But not completely, you should not try to call mux_chip_register()
1828f3addf1SPeter Rosin  * on a mux-chip that has been registered before.
1838f3addf1SPeter Rosin  */
1848f3addf1SPeter Rosin void mux_chip_unregister(struct mux_chip *mux_chip)
1858f3addf1SPeter Rosin {
1868f3addf1SPeter Rosin 	device_del(&mux_chip->dev);
1878f3addf1SPeter Rosin }
1888f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_chip_unregister);
1898f3addf1SPeter Rosin 
1908f3addf1SPeter Rosin /**
1918f3addf1SPeter Rosin  * mux_chip_free() - Free the mux-chip for good.
1928f3addf1SPeter Rosin  * @mux_chip: The mux-chip to free.
1938f3addf1SPeter Rosin  *
1948f3addf1SPeter Rosin  * mux_chip_free() reverses the effects of mux_chip_alloc().
1958f3addf1SPeter Rosin  */
1968f3addf1SPeter Rosin void mux_chip_free(struct mux_chip *mux_chip)
1978f3addf1SPeter Rosin {
1988f3addf1SPeter Rosin 	if (!mux_chip)
1998f3addf1SPeter Rosin 		return;
2008f3addf1SPeter Rosin 
2018f3addf1SPeter Rosin 	put_device(&mux_chip->dev);
2028f3addf1SPeter Rosin }
2038f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_chip_free);
2048f3addf1SPeter Rosin 
2058f3addf1SPeter Rosin static void devm_mux_chip_release(struct device *dev, void *res)
2068f3addf1SPeter Rosin {
2078f3addf1SPeter Rosin 	struct mux_chip *mux_chip = *(struct mux_chip **)res;
2088f3addf1SPeter Rosin 
2098f3addf1SPeter Rosin 	mux_chip_free(mux_chip);
2108f3addf1SPeter Rosin }
2118f3addf1SPeter Rosin 
2128f3addf1SPeter Rosin /**
2138f3addf1SPeter Rosin  * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
2148f3addf1SPeter Rosin  * @dev: The parent device implementing the mux interface.
2158f3addf1SPeter Rosin  * @controllers: The number of mux controllers to allocate for this chip.
2168f3addf1SPeter Rosin  * @sizeof_priv: Size of extra memory area for private use by the caller.
2178f3addf1SPeter Rosin  *
2188f3addf1SPeter Rosin  * See mux_chip_alloc() for more details.
2198f3addf1SPeter Rosin  *
2208f3addf1SPeter Rosin  * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
2218f3addf1SPeter Rosin  */
2228f3addf1SPeter Rosin struct mux_chip *devm_mux_chip_alloc(struct device *dev,
2238f3addf1SPeter Rosin 				     unsigned int controllers,
2248f3addf1SPeter Rosin 				     size_t sizeof_priv)
2258f3addf1SPeter Rosin {
2268f3addf1SPeter Rosin 	struct mux_chip **ptr, *mux_chip;
2278f3addf1SPeter Rosin 
2288f3addf1SPeter Rosin 	ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
2298f3addf1SPeter Rosin 	if (!ptr)
2308f3addf1SPeter Rosin 		return ERR_PTR(-ENOMEM);
2318f3addf1SPeter Rosin 
2328f3addf1SPeter Rosin 	mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
2338f3addf1SPeter Rosin 	if (IS_ERR(mux_chip)) {
2348f3addf1SPeter Rosin 		devres_free(ptr);
2358f3addf1SPeter Rosin 		return mux_chip;
2368f3addf1SPeter Rosin 	}
2378f3addf1SPeter Rosin 
2388f3addf1SPeter Rosin 	*ptr = mux_chip;
2398f3addf1SPeter Rosin 	devres_add(dev, ptr);
2408f3addf1SPeter Rosin 
2418f3addf1SPeter Rosin 	return mux_chip;
2428f3addf1SPeter Rosin }
2438f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
2448f3addf1SPeter Rosin 
2458f3addf1SPeter Rosin static void devm_mux_chip_reg_release(struct device *dev, void *res)
2468f3addf1SPeter Rosin {
2478f3addf1SPeter Rosin 	struct mux_chip *mux_chip = *(struct mux_chip **)res;
2488f3addf1SPeter Rosin 
2498f3addf1SPeter Rosin 	mux_chip_unregister(mux_chip);
2508f3addf1SPeter Rosin }
2518f3addf1SPeter Rosin 
2528f3addf1SPeter Rosin /**
2538f3addf1SPeter Rosin  * devm_mux_chip_register() - Resource-managed version mux_chip_register().
2548f3addf1SPeter Rosin  * @dev: The parent device implementing the mux interface.
2558f3addf1SPeter Rosin  * @mux_chip: The mux-chip to register.
2568f3addf1SPeter Rosin  *
2578f3addf1SPeter Rosin  * See mux_chip_register() for more details.
2588f3addf1SPeter Rosin  *
2598f3addf1SPeter Rosin  * Return: Zero on success or a negative errno on error.
2608f3addf1SPeter Rosin  */
2618f3addf1SPeter Rosin int devm_mux_chip_register(struct device *dev,
2628f3addf1SPeter Rosin 			   struct mux_chip *mux_chip)
2638f3addf1SPeter Rosin {
2648f3addf1SPeter Rosin 	struct mux_chip **ptr;
2658f3addf1SPeter Rosin 	int res;
2668f3addf1SPeter Rosin 
2678f3addf1SPeter Rosin 	ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
2688f3addf1SPeter Rosin 	if (!ptr)
2698f3addf1SPeter Rosin 		return -ENOMEM;
2708f3addf1SPeter Rosin 
2718f3addf1SPeter Rosin 	res = mux_chip_register(mux_chip);
2728f3addf1SPeter Rosin 	if (res) {
2738f3addf1SPeter Rosin 		devres_free(ptr);
2748f3addf1SPeter Rosin 		return res;
2758f3addf1SPeter Rosin 	}
2768f3addf1SPeter Rosin 
2778f3addf1SPeter Rosin 	*ptr = mux_chip;
2788f3addf1SPeter Rosin 	devres_add(dev, ptr);
2798f3addf1SPeter Rosin 
2808f3addf1SPeter Rosin 	return res;
2818f3addf1SPeter Rosin }
2828f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(devm_mux_chip_register);
2838f3addf1SPeter Rosin 
2848f3addf1SPeter Rosin /**
2858f3addf1SPeter Rosin  * mux_control_states() - Query the number of multiplexer states.
2868f3addf1SPeter Rosin  * @mux: The mux-control to query.
2878f3addf1SPeter Rosin  *
2888f3addf1SPeter Rosin  * Return: The number of multiplexer states.
2898f3addf1SPeter Rosin  */
2908f3addf1SPeter Rosin unsigned int mux_control_states(struct mux_control *mux)
2918f3addf1SPeter Rosin {
2928f3addf1SPeter Rosin 	return mux->states;
2938f3addf1SPeter Rosin }
2948f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_control_states);
2958f3addf1SPeter Rosin 
2968f3addf1SPeter Rosin /*
2978f3addf1SPeter Rosin  * The mux->lock must be down when calling this function.
2988f3addf1SPeter Rosin  */
2998f3addf1SPeter Rosin static int __mux_control_select(struct mux_control *mux, int state)
3008f3addf1SPeter Rosin {
3018f3addf1SPeter Rosin 	int ret;
3028f3addf1SPeter Rosin 
3038f3addf1SPeter Rosin 	if (WARN_ON(state < 0 || state >= mux->states))
3048f3addf1SPeter Rosin 		return -EINVAL;
3058f3addf1SPeter Rosin 
3068f3addf1SPeter Rosin 	if (mux->cached_state == state)
3078f3addf1SPeter Rosin 		return 0;
3088f3addf1SPeter Rosin 
3098f3addf1SPeter Rosin 	ret = mux_control_set(mux, state);
3108f3addf1SPeter Rosin 	if (ret >= 0)
3118f3addf1SPeter Rosin 		return 0;
3128f3addf1SPeter Rosin 
3138f3addf1SPeter Rosin 	/* The mux update failed, try to revert if appropriate... */
3148f3addf1SPeter Rosin 	if (mux->idle_state != MUX_IDLE_AS_IS)
3158f3addf1SPeter Rosin 		mux_control_set(mux, mux->idle_state);
3168f3addf1SPeter Rosin 
3178f3addf1SPeter Rosin 	return ret;
3188f3addf1SPeter Rosin }
3198f3addf1SPeter Rosin 
3208f3addf1SPeter Rosin /**
3218f3addf1SPeter Rosin  * mux_control_select() - Select the given multiplexer state.
3228f3addf1SPeter Rosin  * @mux: The mux-control to request a change of state from.
3238f3addf1SPeter Rosin  * @state: The new requested state.
3248f3addf1SPeter Rosin  *
3258f3addf1SPeter Rosin  * On successfully selecting the mux-control state, it will be locked until
3268f3addf1SPeter Rosin  * there is a call to mux_control_deselect(). If the mux-control is already
3278f3addf1SPeter Rosin  * selected when mux_control_select() is called, the caller will be blocked
3288f3addf1SPeter Rosin  * until mux_control_deselect() is called (by someone else).
3298f3addf1SPeter Rosin  *
3308f3addf1SPeter Rosin  * Therefore, make sure to call mux_control_deselect() when the operation is
3318f3addf1SPeter Rosin  * complete and the mux-control is free for others to use, but do not call
3328f3addf1SPeter Rosin  * mux_control_deselect() if mux_control_select() fails.
3338f3addf1SPeter Rosin  *
3348f3addf1SPeter Rosin  * Return: 0 when the mux-control state has the requested state or a negative
3358f3addf1SPeter Rosin  * errno on error.
3368f3addf1SPeter Rosin  */
3378f3addf1SPeter Rosin int mux_control_select(struct mux_control *mux, unsigned int state)
3388f3addf1SPeter Rosin {
3398f3addf1SPeter Rosin 	int ret;
3408f3addf1SPeter Rosin 
3418f3addf1SPeter Rosin 	ret = down_killable(&mux->lock);
3428f3addf1SPeter Rosin 	if (ret < 0)
3438f3addf1SPeter Rosin 		return ret;
3448f3addf1SPeter Rosin 
3458f3addf1SPeter Rosin 	ret = __mux_control_select(mux, state);
3468f3addf1SPeter Rosin 
3478f3addf1SPeter Rosin 	if (ret < 0)
3488f3addf1SPeter Rosin 		up(&mux->lock);
3498f3addf1SPeter Rosin 
3508f3addf1SPeter Rosin 	return ret;
3518f3addf1SPeter Rosin }
3528f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_control_select);
3538f3addf1SPeter Rosin 
3548f3addf1SPeter Rosin /**
3558f3addf1SPeter Rosin  * mux_control_try_select() - Try to select the given multiplexer state.
3568f3addf1SPeter Rosin  * @mux: The mux-control to request a change of state from.
3578f3addf1SPeter Rosin  * @state: The new requested state.
3588f3addf1SPeter Rosin  *
3598f3addf1SPeter Rosin  * On successfully selecting the mux-control state, it will be locked until
3608f3addf1SPeter Rosin  * mux_control_deselect() called.
3618f3addf1SPeter Rosin  *
3628f3addf1SPeter Rosin  * Therefore, make sure to call mux_control_deselect() when the operation is
3638f3addf1SPeter Rosin  * complete and the mux-control is free for others to use, but do not call
3648f3addf1SPeter Rosin  * mux_control_deselect() if mux_control_try_select() fails.
3658f3addf1SPeter Rosin  *
3668f3addf1SPeter Rosin  * Return: 0 when the mux-control state has the requested state or a negative
3678f3addf1SPeter Rosin  * errno on error. Specifically -EBUSY if the mux-control is contended.
3688f3addf1SPeter Rosin  */
3698f3addf1SPeter Rosin int mux_control_try_select(struct mux_control *mux, unsigned int state)
3708f3addf1SPeter Rosin {
3718f3addf1SPeter Rosin 	int ret;
3728f3addf1SPeter Rosin 
3738f3addf1SPeter Rosin 	if (down_trylock(&mux->lock))
3748f3addf1SPeter Rosin 		return -EBUSY;
3758f3addf1SPeter Rosin 
3768f3addf1SPeter Rosin 	ret = __mux_control_select(mux, state);
3778f3addf1SPeter Rosin 
3788f3addf1SPeter Rosin 	if (ret < 0)
3798f3addf1SPeter Rosin 		up(&mux->lock);
3808f3addf1SPeter Rosin 
3818f3addf1SPeter Rosin 	return ret;
3828f3addf1SPeter Rosin }
3838f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_control_try_select);
3848f3addf1SPeter Rosin 
3858f3addf1SPeter Rosin /**
3868f3addf1SPeter Rosin  * mux_control_deselect() - Deselect the previously selected multiplexer state.
3878f3addf1SPeter Rosin  * @mux: The mux-control to deselect.
3888f3addf1SPeter Rosin  *
3898f3addf1SPeter Rosin  * It is required that a single call is made to mux_control_deselect() for
3908f3addf1SPeter Rosin  * each and every successful call made to either of mux_control_select() or
3918f3addf1SPeter Rosin  * mux_control_try_select().
3928f3addf1SPeter Rosin  *
3938f3addf1SPeter Rosin  * Return: 0 on success and a negative errno on error. An error can only
3948f3addf1SPeter Rosin  * occur if the mux has an idle state. Note that even if an error occurs, the
3958f3addf1SPeter Rosin  * mux-control is unlocked and is thus free for the next access.
3968f3addf1SPeter Rosin  */
3978f3addf1SPeter Rosin int mux_control_deselect(struct mux_control *mux)
3988f3addf1SPeter Rosin {
3998f3addf1SPeter Rosin 	int ret = 0;
4008f3addf1SPeter Rosin 
4018f3addf1SPeter Rosin 	if (mux->idle_state != MUX_IDLE_AS_IS &&
4028f3addf1SPeter Rosin 	    mux->idle_state != mux->cached_state)
4038f3addf1SPeter Rosin 		ret = mux_control_set(mux, mux->idle_state);
4048f3addf1SPeter Rosin 
4058f3addf1SPeter Rosin 	up(&mux->lock);
4068f3addf1SPeter Rosin 
4078f3addf1SPeter Rosin 	return ret;
4088f3addf1SPeter Rosin }
4098f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_control_deselect);
4108f3addf1SPeter Rosin 
4118f3addf1SPeter Rosin static int of_dev_node_match(struct device *dev, const void *data)
4128f3addf1SPeter Rosin {
4138f3addf1SPeter Rosin 	return dev->of_node == data;
4148f3addf1SPeter Rosin }
4158f3addf1SPeter Rosin 
4168f3addf1SPeter Rosin static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
4178f3addf1SPeter Rosin {
4188f3addf1SPeter Rosin 	struct device *dev;
4198f3addf1SPeter Rosin 
4208f3addf1SPeter Rosin 	dev = class_find_device(&mux_class, NULL, np, of_dev_node_match);
4218f3addf1SPeter Rosin 
4228f3addf1SPeter Rosin 	return dev ? to_mux_chip(dev) : NULL;
4238f3addf1SPeter Rosin }
4248f3addf1SPeter Rosin 
4258f3addf1SPeter Rosin /**
4268f3addf1SPeter Rosin  * mux_control_get() - Get the mux-control for a device.
4278f3addf1SPeter Rosin  * @dev: The device that needs a mux-control.
4288f3addf1SPeter Rosin  * @mux_name: The name identifying the mux-control.
4298f3addf1SPeter Rosin  *
4308f3addf1SPeter Rosin  * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
4318f3addf1SPeter Rosin  */
4328f3addf1SPeter Rosin struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
4338f3addf1SPeter Rosin {
4348f3addf1SPeter Rosin 	struct device_node *np = dev->of_node;
4358f3addf1SPeter Rosin 	struct of_phandle_args args;
4368f3addf1SPeter Rosin 	struct mux_chip *mux_chip;
4378f3addf1SPeter Rosin 	unsigned int controller;
4388f3addf1SPeter Rosin 	int index = 0;
4398f3addf1SPeter Rosin 	int ret;
4408f3addf1SPeter Rosin 
4418f3addf1SPeter Rosin 	if (mux_name) {
4428f3addf1SPeter Rosin 		index = of_property_match_string(np, "mux-control-names",
4438f3addf1SPeter Rosin 						 mux_name);
4448f3addf1SPeter Rosin 		if (index < 0) {
4458f3addf1SPeter Rosin 			dev_err(dev, "mux controller '%s' not found\n",
4468f3addf1SPeter Rosin 				mux_name);
4478f3addf1SPeter Rosin 			return ERR_PTR(index);
4488f3addf1SPeter Rosin 		}
4498f3addf1SPeter Rosin 	}
4508f3addf1SPeter Rosin 
4518f3addf1SPeter Rosin 	ret = of_parse_phandle_with_args(np,
4528f3addf1SPeter Rosin 					 "mux-controls", "#mux-control-cells",
4538f3addf1SPeter Rosin 					 index, &args);
4548f3addf1SPeter Rosin 	if (ret) {
4558f3addf1SPeter Rosin 		dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
4568f3addf1SPeter Rosin 			np, mux_name ?: "", index);
4578f3addf1SPeter Rosin 		return ERR_PTR(ret);
4588f3addf1SPeter Rosin 	}
4598f3addf1SPeter Rosin 
4608f3addf1SPeter Rosin 	mux_chip = of_find_mux_chip_by_node(args.np);
4618f3addf1SPeter Rosin 	of_node_put(args.np);
4628f3addf1SPeter Rosin 	if (!mux_chip)
4638f3addf1SPeter Rosin 		return ERR_PTR(-EPROBE_DEFER);
4648f3addf1SPeter Rosin 
4658f3addf1SPeter Rosin 	if (args.args_count > 1 ||
4668f3addf1SPeter Rosin 	    (!args.args_count && (mux_chip->controllers > 1))) {
4678f3addf1SPeter Rosin 		dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
4688f3addf1SPeter Rosin 			np, args.np);
4698f3addf1SPeter Rosin 		return ERR_PTR(-EINVAL);
4708f3addf1SPeter Rosin 	}
4718f3addf1SPeter Rosin 
4728f3addf1SPeter Rosin 	controller = 0;
4738f3addf1SPeter Rosin 	if (args.args_count)
4748f3addf1SPeter Rosin 		controller = args.args[0];
4758f3addf1SPeter Rosin 
4768f3addf1SPeter Rosin 	if (controller >= mux_chip->controllers) {
4778f3addf1SPeter Rosin 		dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
4788f3addf1SPeter Rosin 			np, controller, args.np);
4798f3addf1SPeter Rosin 		return ERR_PTR(-EINVAL);
4808f3addf1SPeter Rosin 	}
4818f3addf1SPeter Rosin 
4828f3addf1SPeter Rosin 	get_device(&mux_chip->dev);
4838f3addf1SPeter Rosin 	return &mux_chip->mux[controller];
4848f3addf1SPeter Rosin }
4858f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_control_get);
4868f3addf1SPeter Rosin 
4878f3addf1SPeter Rosin /**
4888f3addf1SPeter Rosin  * mux_control_put() - Put away the mux-control for good.
4898f3addf1SPeter Rosin  * @mux: The mux-control to put away.
4908f3addf1SPeter Rosin  *
4918f3addf1SPeter Rosin  * mux_control_put() reverses the effects of mux_control_get().
4928f3addf1SPeter Rosin  */
4938f3addf1SPeter Rosin void mux_control_put(struct mux_control *mux)
4948f3addf1SPeter Rosin {
4958f3addf1SPeter Rosin 	put_device(&mux->chip->dev);
4968f3addf1SPeter Rosin }
4978f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(mux_control_put);
4988f3addf1SPeter Rosin 
4998f3addf1SPeter Rosin static void devm_mux_control_release(struct device *dev, void *res)
5008f3addf1SPeter Rosin {
5018f3addf1SPeter Rosin 	struct mux_control *mux = *(struct mux_control **)res;
5028f3addf1SPeter Rosin 
5038f3addf1SPeter Rosin 	mux_control_put(mux);
5048f3addf1SPeter Rosin }
5058f3addf1SPeter Rosin 
5068f3addf1SPeter Rosin /**
5078f3addf1SPeter Rosin  * devm_mux_control_get() - Get the mux-control for a device, with resource
5088f3addf1SPeter Rosin  *			    management.
5098f3addf1SPeter Rosin  * @dev: The device that needs a mux-control.
5108f3addf1SPeter Rosin  * @mux_name: The name identifying the mux-control.
5118f3addf1SPeter Rosin  *
5128f3addf1SPeter Rosin  * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
5138f3addf1SPeter Rosin  */
5148f3addf1SPeter Rosin struct mux_control *devm_mux_control_get(struct device *dev,
5158f3addf1SPeter Rosin 					 const char *mux_name)
5168f3addf1SPeter Rosin {
5178f3addf1SPeter Rosin 	struct mux_control **ptr, *mux;
5188f3addf1SPeter Rosin 
5198f3addf1SPeter Rosin 	ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
5208f3addf1SPeter Rosin 	if (!ptr)
5218f3addf1SPeter Rosin 		return ERR_PTR(-ENOMEM);
5228f3addf1SPeter Rosin 
5238f3addf1SPeter Rosin 	mux = mux_control_get(dev, mux_name);
5248f3addf1SPeter Rosin 	if (IS_ERR(mux)) {
5258f3addf1SPeter Rosin 		devres_free(ptr);
5268f3addf1SPeter Rosin 		return mux;
5278f3addf1SPeter Rosin 	}
5288f3addf1SPeter Rosin 
5298f3addf1SPeter Rosin 	*ptr = mux;
5308f3addf1SPeter Rosin 	devres_add(dev, ptr);
5318f3addf1SPeter Rosin 
5328f3addf1SPeter Rosin 	return mux;
5338f3addf1SPeter Rosin }
5348f3addf1SPeter Rosin EXPORT_SYMBOL_GPL(devm_mux_control_get);
5358f3addf1SPeter Rosin 
5368f3addf1SPeter Rosin /*
5378f3addf1SPeter Rosin  * Using subsys_initcall instead of module_init here to try to ensure - for
5388f3addf1SPeter Rosin  * the non-modular case - that the subsystem is initialized when mux consumers
5398f3addf1SPeter Rosin  * and mux controllers start to use it.
5408f3addf1SPeter Rosin  * For the modular case, the ordering is ensured with module dependencies.
5418f3addf1SPeter Rosin  */
5428f3addf1SPeter Rosin subsys_initcall(mux_init);
5438f3addf1SPeter Rosin module_exit(mux_exit);
5448f3addf1SPeter Rosin 
5458f3addf1SPeter Rosin MODULE_DESCRIPTION("Multiplexer subsystem");
5468f3addf1SPeter Rosin MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
5478f3addf1SPeter Rosin MODULE_LICENSE("GPL v2");
548