xref: /OK3568_Linux_fs/kernel/drivers/mux/core.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Multiplexer subsystem
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2017 Axentia Technologies AB
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Peter Rosin <peda@axentia.se>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #define pr_fmt(fmt) "mux-core: " fmt
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/err.h>
14*4882a593Smuzhiyun #include <linux/export.h>
15*4882a593Smuzhiyun #include <linux/idr.h>
16*4882a593Smuzhiyun #include <linux/init.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/mux/consumer.h>
19*4882a593Smuzhiyun #include <linux/mux/driver.h>
20*4882a593Smuzhiyun #include <linux/of.h>
21*4882a593Smuzhiyun #include <linux/of_platform.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * The idle-as-is "state" is not an actual state that may be selected, it
26*4882a593Smuzhiyun  * only implies that the state should not be changed. So, use that state
27*4882a593Smuzhiyun  * as indication that the cached state of the multiplexer is unknown.
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun static struct class mux_class = {
32*4882a593Smuzhiyun 	.name = "mux",
33*4882a593Smuzhiyun 	.owner = THIS_MODULE,
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static DEFINE_IDA(mux_ida);
37*4882a593Smuzhiyun 
mux_init(void)38*4882a593Smuzhiyun static int __init mux_init(void)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	ida_init(&mux_ida);
41*4882a593Smuzhiyun 	return class_register(&mux_class);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
mux_exit(void)44*4882a593Smuzhiyun static void __exit mux_exit(void)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	class_unregister(&mux_class);
47*4882a593Smuzhiyun 	ida_destroy(&mux_ida);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
mux_chip_release(struct device * dev)50*4882a593Smuzhiyun static void mux_chip_release(struct device *dev)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct mux_chip *mux_chip = to_mux_chip(dev);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	ida_simple_remove(&mux_ida, mux_chip->id);
55*4882a593Smuzhiyun 	kfree(mux_chip);
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun static const struct device_type mux_type = {
59*4882a593Smuzhiyun 	.name = "mux-chip",
60*4882a593Smuzhiyun 	.release = mux_chip_release,
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /**
64*4882a593Smuzhiyun  * mux_chip_alloc() - Allocate a mux-chip.
65*4882a593Smuzhiyun  * @dev: The parent device implementing the mux interface.
66*4882a593Smuzhiyun  * @controllers: The number of mux controllers to allocate for this chip.
67*4882a593Smuzhiyun  * @sizeof_priv: Size of extra memory area for private use by the caller.
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * After allocating the mux-chip with the desired number of mux controllers
70*4882a593Smuzhiyun  * but before registering the chip, the mux driver is required to configure
71*4882a593Smuzhiyun  * the number of valid mux states in the mux_chip->mux[N].states members and
72*4882a593Smuzhiyun  * the desired idle state in the returned mux_chip->mux[N].idle_state members.
73*4882a593Smuzhiyun  * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
74*4882a593Smuzhiyun  * provide a pointer to the operations struct in the mux_chip->ops member
75*4882a593Smuzhiyun  * before registering the mux-chip with mux_chip_register.
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
78*4882a593Smuzhiyun  */
mux_chip_alloc(struct device * dev,unsigned int controllers,size_t sizeof_priv)79*4882a593Smuzhiyun struct mux_chip *mux_chip_alloc(struct device *dev,
80*4882a593Smuzhiyun 				unsigned int controllers, size_t sizeof_priv)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	struct mux_chip *mux_chip;
83*4882a593Smuzhiyun 	int i;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (WARN_ON(!dev || !controllers))
86*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	mux_chip = kzalloc(sizeof(*mux_chip) +
89*4882a593Smuzhiyun 			   controllers * sizeof(*mux_chip->mux) +
90*4882a593Smuzhiyun 			   sizeof_priv, GFP_KERNEL);
91*4882a593Smuzhiyun 	if (!mux_chip)
92*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	mux_chip->mux = (struct mux_control *)(mux_chip + 1);
95*4882a593Smuzhiyun 	mux_chip->dev.class = &mux_class;
96*4882a593Smuzhiyun 	mux_chip->dev.type = &mux_type;
97*4882a593Smuzhiyun 	mux_chip->dev.parent = dev;
98*4882a593Smuzhiyun 	mux_chip->dev.of_node = dev->of_node;
99*4882a593Smuzhiyun 	dev_set_drvdata(&mux_chip->dev, mux_chip);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
102*4882a593Smuzhiyun 	if (mux_chip->id < 0) {
103*4882a593Smuzhiyun 		int err = mux_chip->id;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 		pr_err("muxchipX failed to get a device id\n");
106*4882a593Smuzhiyun 		kfree(mux_chip);
107*4882a593Smuzhiyun 		return ERR_PTR(err);
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 	dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	mux_chip->controllers = controllers;
112*4882a593Smuzhiyun 	for (i = 0; i < controllers; ++i) {
113*4882a593Smuzhiyun 		struct mux_control *mux = &mux_chip->mux[i];
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		mux->chip = mux_chip;
116*4882a593Smuzhiyun 		sema_init(&mux->lock, 1);
117*4882a593Smuzhiyun 		mux->cached_state = MUX_CACHE_UNKNOWN;
118*4882a593Smuzhiyun 		mux->idle_state = MUX_IDLE_AS_IS;
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	device_initialize(&mux_chip->dev);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	return mux_chip;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_chip_alloc);
126*4882a593Smuzhiyun 
mux_control_set(struct mux_control * mux,int state)127*4882a593Smuzhiyun static int mux_control_set(struct mux_control *mux, int state)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	int ret = mux->chip->ops->set(mux, state);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	return ret;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * mux_chip_register() - Register a mux-chip, thus readying the controllers
138*4882a593Smuzhiyun  *			 for use.
139*4882a593Smuzhiyun  * @mux_chip: The mux-chip to register.
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * Do not retry registration of the same mux-chip on failure. You should
142*4882a593Smuzhiyun  * instead put it away with mux_chip_free() and allocate a new one, if you
143*4882a593Smuzhiyun  * for some reason would like to retry registration.
144*4882a593Smuzhiyun  *
145*4882a593Smuzhiyun  * Return: Zero on success or a negative errno on error.
146*4882a593Smuzhiyun  */
mux_chip_register(struct mux_chip * mux_chip)147*4882a593Smuzhiyun int mux_chip_register(struct mux_chip *mux_chip)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	int i;
150*4882a593Smuzhiyun 	int ret;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	for (i = 0; i < mux_chip->controllers; ++i) {
153*4882a593Smuzhiyun 		struct mux_control *mux = &mux_chip->mux[i];
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		if (mux->idle_state == mux->cached_state)
156*4882a593Smuzhiyun 			continue;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		ret = mux_control_set(mux, mux->idle_state);
159*4882a593Smuzhiyun 		if (ret < 0) {
160*4882a593Smuzhiyun 			dev_err(&mux_chip->dev, "unable to set idle state\n");
161*4882a593Smuzhiyun 			return ret;
162*4882a593Smuzhiyun 		}
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	ret = device_add(&mux_chip->dev);
166*4882a593Smuzhiyun 	if (ret < 0)
167*4882a593Smuzhiyun 		dev_err(&mux_chip->dev,
168*4882a593Smuzhiyun 			"device_add failed in %s: %d\n", __func__, ret);
169*4882a593Smuzhiyun 	return ret;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_chip_register);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * mux_chip_unregister() - Take the mux-chip off-line.
175*4882a593Smuzhiyun  * @mux_chip: The mux-chip to unregister.
176*4882a593Smuzhiyun  *
177*4882a593Smuzhiyun  * mux_chip_unregister() reverses the effects of mux_chip_register().
178*4882a593Smuzhiyun  * But not completely, you should not try to call mux_chip_register()
179*4882a593Smuzhiyun  * on a mux-chip that has been registered before.
180*4882a593Smuzhiyun  */
mux_chip_unregister(struct mux_chip * mux_chip)181*4882a593Smuzhiyun void mux_chip_unregister(struct mux_chip *mux_chip)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	device_del(&mux_chip->dev);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_chip_unregister);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /**
188*4882a593Smuzhiyun  * mux_chip_free() - Free the mux-chip for good.
189*4882a593Smuzhiyun  * @mux_chip: The mux-chip to free.
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * mux_chip_free() reverses the effects of mux_chip_alloc().
192*4882a593Smuzhiyun  */
mux_chip_free(struct mux_chip * mux_chip)193*4882a593Smuzhiyun void mux_chip_free(struct mux_chip *mux_chip)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	if (!mux_chip)
196*4882a593Smuzhiyun 		return;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	put_device(&mux_chip->dev);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_chip_free);
201*4882a593Smuzhiyun 
devm_mux_chip_release(struct device * dev,void * res)202*4882a593Smuzhiyun static void devm_mux_chip_release(struct device *dev, void *res)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct mux_chip *mux_chip = *(struct mux_chip **)res;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	mux_chip_free(mux_chip);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun /**
210*4882a593Smuzhiyun  * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
211*4882a593Smuzhiyun  * @dev: The parent device implementing the mux interface.
212*4882a593Smuzhiyun  * @controllers: The number of mux controllers to allocate for this chip.
213*4882a593Smuzhiyun  * @sizeof_priv: Size of extra memory area for private use by the caller.
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * See mux_chip_alloc() for more details.
216*4882a593Smuzhiyun  *
217*4882a593Smuzhiyun  * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
218*4882a593Smuzhiyun  */
devm_mux_chip_alloc(struct device * dev,unsigned int controllers,size_t sizeof_priv)219*4882a593Smuzhiyun struct mux_chip *devm_mux_chip_alloc(struct device *dev,
220*4882a593Smuzhiyun 				     unsigned int controllers,
221*4882a593Smuzhiyun 				     size_t sizeof_priv)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	struct mux_chip **ptr, *mux_chip;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
226*4882a593Smuzhiyun 	if (!ptr)
227*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
230*4882a593Smuzhiyun 	if (IS_ERR(mux_chip)) {
231*4882a593Smuzhiyun 		devres_free(ptr);
232*4882a593Smuzhiyun 		return mux_chip;
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	*ptr = mux_chip;
236*4882a593Smuzhiyun 	devres_add(dev, ptr);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	return mux_chip;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
241*4882a593Smuzhiyun 
devm_mux_chip_reg_release(struct device * dev,void * res)242*4882a593Smuzhiyun static void devm_mux_chip_reg_release(struct device *dev, void *res)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	struct mux_chip *mux_chip = *(struct mux_chip **)res;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	mux_chip_unregister(mux_chip);
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun /**
250*4882a593Smuzhiyun  * devm_mux_chip_register() - Resource-managed version mux_chip_register().
251*4882a593Smuzhiyun  * @dev: The parent device implementing the mux interface.
252*4882a593Smuzhiyun  * @mux_chip: The mux-chip to register.
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * See mux_chip_register() for more details.
255*4882a593Smuzhiyun  *
256*4882a593Smuzhiyun  * Return: Zero on success or a negative errno on error.
257*4882a593Smuzhiyun  */
devm_mux_chip_register(struct device * dev,struct mux_chip * mux_chip)258*4882a593Smuzhiyun int devm_mux_chip_register(struct device *dev,
259*4882a593Smuzhiyun 			   struct mux_chip *mux_chip)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	struct mux_chip **ptr;
262*4882a593Smuzhiyun 	int res;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
265*4882a593Smuzhiyun 	if (!ptr)
266*4882a593Smuzhiyun 		return -ENOMEM;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	res = mux_chip_register(mux_chip);
269*4882a593Smuzhiyun 	if (res) {
270*4882a593Smuzhiyun 		devres_free(ptr);
271*4882a593Smuzhiyun 		return res;
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	*ptr = mux_chip;
275*4882a593Smuzhiyun 	devres_add(dev, ptr);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	return res;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_mux_chip_register);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun /**
282*4882a593Smuzhiyun  * mux_control_states() - Query the number of multiplexer states.
283*4882a593Smuzhiyun  * @mux: The mux-control to query.
284*4882a593Smuzhiyun  *
285*4882a593Smuzhiyun  * Return: The number of multiplexer states.
286*4882a593Smuzhiyun  */
mux_control_states(struct mux_control * mux)287*4882a593Smuzhiyun unsigned int mux_control_states(struct mux_control *mux)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	return mux->states;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_control_states);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun /*
294*4882a593Smuzhiyun  * The mux->lock must be down when calling this function.
295*4882a593Smuzhiyun  */
__mux_control_select(struct mux_control * mux,int state)296*4882a593Smuzhiyun static int __mux_control_select(struct mux_control *mux, int state)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	int ret;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	if (WARN_ON(state < 0 || state >= mux->states))
301*4882a593Smuzhiyun 		return -EINVAL;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (mux->cached_state == state)
304*4882a593Smuzhiyun 		return 0;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	ret = mux_control_set(mux, state);
307*4882a593Smuzhiyun 	if (ret >= 0)
308*4882a593Smuzhiyun 		return 0;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	/* The mux update failed, try to revert if appropriate... */
311*4882a593Smuzhiyun 	if (mux->idle_state != MUX_IDLE_AS_IS)
312*4882a593Smuzhiyun 		mux_control_set(mux, mux->idle_state);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	return ret;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun /**
318*4882a593Smuzhiyun  * mux_control_select() - Select the given multiplexer state.
319*4882a593Smuzhiyun  * @mux: The mux-control to request a change of state from.
320*4882a593Smuzhiyun  * @state: The new requested state.
321*4882a593Smuzhiyun  *
322*4882a593Smuzhiyun  * On successfully selecting the mux-control state, it will be locked until
323*4882a593Smuzhiyun  * there is a call to mux_control_deselect(). If the mux-control is already
324*4882a593Smuzhiyun  * selected when mux_control_select() is called, the caller will be blocked
325*4882a593Smuzhiyun  * until mux_control_deselect() is called (by someone else).
326*4882a593Smuzhiyun  *
327*4882a593Smuzhiyun  * Therefore, make sure to call mux_control_deselect() when the operation is
328*4882a593Smuzhiyun  * complete and the mux-control is free for others to use, but do not call
329*4882a593Smuzhiyun  * mux_control_deselect() if mux_control_select() fails.
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * Return: 0 when the mux-control state has the requested state or a negative
332*4882a593Smuzhiyun  * errno on error.
333*4882a593Smuzhiyun  */
mux_control_select(struct mux_control * mux,unsigned int state)334*4882a593Smuzhiyun int mux_control_select(struct mux_control *mux, unsigned int state)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	int ret;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	ret = down_killable(&mux->lock);
339*4882a593Smuzhiyun 	if (ret < 0)
340*4882a593Smuzhiyun 		return ret;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	ret = __mux_control_select(mux, state);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	if (ret < 0)
345*4882a593Smuzhiyun 		up(&mux->lock);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	return ret;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_control_select);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun  * mux_control_try_select() - Try to select the given multiplexer state.
353*4882a593Smuzhiyun  * @mux: The mux-control to request a change of state from.
354*4882a593Smuzhiyun  * @state: The new requested state.
355*4882a593Smuzhiyun  *
356*4882a593Smuzhiyun  * On successfully selecting the mux-control state, it will be locked until
357*4882a593Smuzhiyun  * mux_control_deselect() called.
358*4882a593Smuzhiyun  *
359*4882a593Smuzhiyun  * Therefore, make sure to call mux_control_deselect() when the operation is
360*4882a593Smuzhiyun  * complete and the mux-control is free for others to use, but do not call
361*4882a593Smuzhiyun  * mux_control_deselect() if mux_control_try_select() fails.
362*4882a593Smuzhiyun  *
363*4882a593Smuzhiyun  * Return: 0 when the mux-control state has the requested state or a negative
364*4882a593Smuzhiyun  * errno on error. Specifically -EBUSY if the mux-control is contended.
365*4882a593Smuzhiyun  */
mux_control_try_select(struct mux_control * mux,unsigned int state)366*4882a593Smuzhiyun int mux_control_try_select(struct mux_control *mux, unsigned int state)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	int ret;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	if (down_trylock(&mux->lock))
371*4882a593Smuzhiyun 		return -EBUSY;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	ret = __mux_control_select(mux, state);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	if (ret < 0)
376*4882a593Smuzhiyun 		up(&mux->lock);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	return ret;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_control_try_select);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /**
383*4882a593Smuzhiyun  * mux_control_deselect() - Deselect the previously selected multiplexer state.
384*4882a593Smuzhiyun  * @mux: The mux-control to deselect.
385*4882a593Smuzhiyun  *
386*4882a593Smuzhiyun  * It is required that a single call is made to mux_control_deselect() for
387*4882a593Smuzhiyun  * each and every successful call made to either of mux_control_select() or
388*4882a593Smuzhiyun  * mux_control_try_select().
389*4882a593Smuzhiyun  *
390*4882a593Smuzhiyun  * Return: 0 on success and a negative errno on error. An error can only
391*4882a593Smuzhiyun  * occur if the mux has an idle state. Note that even if an error occurs, the
392*4882a593Smuzhiyun  * mux-control is unlocked and is thus free for the next access.
393*4882a593Smuzhiyun  */
mux_control_deselect(struct mux_control * mux)394*4882a593Smuzhiyun int mux_control_deselect(struct mux_control *mux)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun 	int ret = 0;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	if (mux->idle_state != MUX_IDLE_AS_IS &&
399*4882a593Smuzhiyun 	    mux->idle_state != mux->cached_state)
400*4882a593Smuzhiyun 		ret = mux_control_set(mux, mux->idle_state);
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	up(&mux->lock);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	return ret;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_control_deselect);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun /* Note this function returns a reference to the mux_chip dev. */
of_find_mux_chip_by_node(struct device_node * np)409*4882a593Smuzhiyun static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	struct device *dev;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	dev = class_find_device_by_of_node(&mux_class, np);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	return dev ? to_mux_chip(dev) : NULL;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun /**
419*4882a593Smuzhiyun  * mux_control_get() - Get the mux-control for a device.
420*4882a593Smuzhiyun  * @dev: The device that needs a mux-control.
421*4882a593Smuzhiyun  * @mux_name: The name identifying the mux-control.
422*4882a593Smuzhiyun  *
423*4882a593Smuzhiyun  * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
424*4882a593Smuzhiyun  */
mux_control_get(struct device * dev,const char * mux_name)425*4882a593Smuzhiyun struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
428*4882a593Smuzhiyun 	struct of_phandle_args args;
429*4882a593Smuzhiyun 	struct mux_chip *mux_chip;
430*4882a593Smuzhiyun 	unsigned int controller;
431*4882a593Smuzhiyun 	int index = 0;
432*4882a593Smuzhiyun 	int ret;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	if (mux_name) {
435*4882a593Smuzhiyun 		index = of_property_match_string(np, "mux-control-names",
436*4882a593Smuzhiyun 						 mux_name);
437*4882a593Smuzhiyun 		if (index < 0) {
438*4882a593Smuzhiyun 			dev_err(dev, "mux controller '%s' not found\n",
439*4882a593Smuzhiyun 				mux_name);
440*4882a593Smuzhiyun 			return ERR_PTR(index);
441*4882a593Smuzhiyun 		}
442*4882a593Smuzhiyun 	}
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	ret = of_parse_phandle_with_args(np,
445*4882a593Smuzhiyun 					 "mux-controls", "#mux-control-cells",
446*4882a593Smuzhiyun 					 index, &args);
447*4882a593Smuzhiyun 	if (ret) {
448*4882a593Smuzhiyun 		dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
449*4882a593Smuzhiyun 			np, mux_name ?: "", index);
450*4882a593Smuzhiyun 		return ERR_PTR(ret);
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	mux_chip = of_find_mux_chip_by_node(args.np);
454*4882a593Smuzhiyun 	of_node_put(args.np);
455*4882a593Smuzhiyun 	if (!mux_chip)
456*4882a593Smuzhiyun 		return ERR_PTR(-EPROBE_DEFER);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	if (args.args_count > 1 ||
459*4882a593Smuzhiyun 	    (!args.args_count && (mux_chip->controllers > 1))) {
460*4882a593Smuzhiyun 		dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
461*4882a593Smuzhiyun 			np, args.np);
462*4882a593Smuzhiyun 		put_device(&mux_chip->dev);
463*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	controller = 0;
467*4882a593Smuzhiyun 	if (args.args_count)
468*4882a593Smuzhiyun 		controller = args.args[0];
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	if (controller >= mux_chip->controllers) {
471*4882a593Smuzhiyun 		dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
472*4882a593Smuzhiyun 			np, controller, args.np);
473*4882a593Smuzhiyun 		put_device(&mux_chip->dev);
474*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	return &mux_chip->mux[controller];
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_control_get);
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun /**
482*4882a593Smuzhiyun  * mux_control_put() - Put away the mux-control for good.
483*4882a593Smuzhiyun  * @mux: The mux-control to put away.
484*4882a593Smuzhiyun  *
485*4882a593Smuzhiyun  * mux_control_put() reverses the effects of mux_control_get().
486*4882a593Smuzhiyun  */
mux_control_put(struct mux_control * mux)487*4882a593Smuzhiyun void mux_control_put(struct mux_control *mux)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	put_device(&mux->chip->dev);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(mux_control_put);
492*4882a593Smuzhiyun 
devm_mux_control_release(struct device * dev,void * res)493*4882a593Smuzhiyun static void devm_mux_control_release(struct device *dev, void *res)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	struct mux_control *mux = *(struct mux_control **)res;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	mux_control_put(mux);
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /**
501*4882a593Smuzhiyun  * devm_mux_control_get() - Get the mux-control for a device, with resource
502*4882a593Smuzhiyun  *			    management.
503*4882a593Smuzhiyun  * @dev: The device that needs a mux-control.
504*4882a593Smuzhiyun  * @mux_name: The name identifying the mux-control.
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
507*4882a593Smuzhiyun  */
devm_mux_control_get(struct device * dev,const char * mux_name)508*4882a593Smuzhiyun struct mux_control *devm_mux_control_get(struct device *dev,
509*4882a593Smuzhiyun 					 const char *mux_name)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	struct mux_control **ptr, *mux;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
514*4882a593Smuzhiyun 	if (!ptr)
515*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	mux = mux_control_get(dev, mux_name);
518*4882a593Smuzhiyun 	if (IS_ERR(mux)) {
519*4882a593Smuzhiyun 		devres_free(ptr);
520*4882a593Smuzhiyun 		return mux;
521*4882a593Smuzhiyun 	}
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	*ptr = mux;
524*4882a593Smuzhiyun 	devres_add(dev, ptr);
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	return mux;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_mux_control_get);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun /*
531*4882a593Smuzhiyun  * Using subsys_initcall instead of module_init here to try to ensure - for
532*4882a593Smuzhiyun  * the non-modular case - that the subsystem is initialized when mux consumers
533*4882a593Smuzhiyun  * and mux controllers start to use it.
534*4882a593Smuzhiyun  * For the modular case, the ordering is ensured with module dependencies.
535*4882a593Smuzhiyun  */
536*4882a593Smuzhiyun subsys_initcall(mux_init);
537*4882a593Smuzhiyun module_exit(mux_exit);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun MODULE_DESCRIPTION("Multiplexer subsystem");
540*4882a593Smuzhiyun MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
541*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
542