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