2 * Multiplexer subsystem
4 * Copyright (C) 2017 Axentia Technologies AB
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.
13 #define pr_fmt(fmt) "mux-core: " fmt
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>
24 #include <linux/of_platform.h>
25 #include <linux/slab.h>
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.
32 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
34 static struct class mux_class = {
39 static DEFINE_IDA(mux_ida);
41 static int __init mux_init(void)
44 return class_register(&mux_class);
47 static void __exit mux_exit(void)
49 class_unregister(&mux_class);
50 ida_destroy(&mux_ida);
53 static void mux_chip_release(struct device *dev)
55 struct mux_chip *mux_chip = to_mux_chip(dev);
57 ida_simple_remove(&mux_ida, mux_chip->id);
61 static const struct device_type mux_type = {
63 .release = mux_chip_release,
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.
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.
80 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
82 struct mux_chip *mux_chip_alloc(struct device *dev,
83 unsigned int controllers, size_t sizeof_priv)
85 struct mux_chip *mux_chip;
88 if (WARN_ON(!dev || !controllers))
89 return ERR_PTR(-EINVAL);
91 mux_chip = kzalloc(sizeof(*mux_chip) +
92 controllers * sizeof(*mux_chip->mux) +
93 sizeof_priv, GFP_KERNEL);
95 return ERR_PTR(-ENOMEM);
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);
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;
108 pr_err("muxchipX failed to get a device id\n");
112 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
114 mux_chip->controllers = controllers;
115 for (i = 0; i < controllers; ++i) {
116 struct mux_control *mux = &mux_chip->mux[i];
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;
124 device_initialize(&mux_chip->dev);
128 EXPORT_SYMBOL_GPL(mux_chip_alloc);
130 static int mux_control_set(struct mux_control *mux, int state)
132 int ret = mux->chip->ops->set(mux, state);
134 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
140 * mux_chip_register() - Register a mux-chip, thus readying the controllers
142 * @mux_chip: The mux-chip to register.
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.
148 * Return: Zero on success or a negative errno on error.
150 int mux_chip_register(struct mux_chip *mux_chip)
155 for (i = 0; i < mux_chip->controllers; ++i) {
156 struct mux_control *mux = &mux_chip->mux[i];
158 if (mux->idle_state == mux->cached_state)
161 ret = mux_control_set(mux, mux->idle_state);
163 dev_err(&mux_chip->dev, "unable to set idle state\n");
168 ret = device_add(&mux_chip->dev);
170 dev_err(&mux_chip->dev,
171 "device_add failed in %s: %d\n", __func__, ret);
174 EXPORT_SYMBOL_GPL(mux_chip_register);
177 * mux_chip_unregister() - Take the mux-chip off-line.
178 * @mux_chip: The mux-chip to unregister.
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.
184 void mux_chip_unregister(struct mux_chip *mux_chip)
186 device_del(&mux_chip->dev);
188 EXPORT_SYMBOL_GPL(mux_chip_unregister);
191 * mux_chip_free() - Free the mux-chip for good.
192 * @mux_chip: The mux-chip to free.
194 * mux_chip_free() reverses the effects of mux_chip_alloc().
196 void mux_chip_free(struct mux_chip *mux_chip)
201 put_device(&mux_chip->dev);
203 EXPORT_SYMBOL_GPL(mux_chip_free);
205 static void devm_mux_chip_release(struct device *dev, void *res)
207 struct mux_chip *mux_chip = *(struct mux_chip **)res;
209 mux_chip_free(mux_chip);
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.
218 * See mux_chip_alloc() for more details.
220 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
222 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
223 unsigned int controllers,
226 struct mux_chip **ptr, *mux_chip;
228 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
230 return ERR_PTR(-ENOMEM);
232 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
233 if (IS_ERR(mux_chip)) {
239 devres_add(dev, ptr);
243 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
245 static void devm_mux_chip_reg_release(struct device *dev, void *res)
247 struct mux_chip *mux_chip = *(struct mux_chip **)res;
249 mux_chip_unregister(mux_chip);
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.
257 * See mux_chip_register() for more details.
259 * Return: Zero on success or a negative errno on error.
261 int devm_mux_chip_register(struct device *dev,
262 struct mux_chip *mux_chip)
264 struct mux_chip **ptr;
267 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
271 res = mux_chip_register(mux_chip);
278 devres_add(dev, ptr);
282 EXPORT_SYMBOL_GPL(devm_mux_chip_register);
285 * mux_control_states() - Query the number of multiplexer states.
286 * @mux: The mux-control to query.
288 * Return: The number of multiplexer states.
290 unsigned int mux_control_states(struct mux_control *mux)
294 EXPORT_SYMBOL_GPL(mux_control_states);
297 * The mux->lock must be down when calling this function.
299 static int __mux_control_select(struct mux_control *mux, int state)
303 if (WARN_ON(state < 0 || state >= mux->states))
306 if (mux->cached_state == state)
309 ret = mux_control_set(mux, state);
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);
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.
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).
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.
334 * Return: 0 when the mux-control state has the requested state or a negative
337 int mux_control_select(struct mux_control *mux, unsigned int state)
341 ret = down_killable(&mux->lock);
345 ret = __mux_control_select(mux, state);
352 EXPORT_SYMBOL_GPL(mux_control_select);
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.
359 * On successfully selecting the mux-control state, it will be locked until
360 * mux_control_deselect() called.
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.
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.
369 int mux_control_try_select(struct mux_control *mux, unsigned int state)
373 if (down_trylock(&mux->lock))
376 ret = __mux_control_select(mux, state);
383 EXPORT_SYMBOL_GPL(mux_control_try_select);
386 * mux_control_deselect() - Deselect the previously selected multiplexer state.
387 * @mux: The mux-control to deselect.
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().
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.
397 int mux_control_deselect(struct mux_control *mux)
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);
409 EXPORT_SYMBOL_GPL(mux_control_deselect);
411 static int of_dev_node_match(struct device *dev, const void *data)
413 return dev->of_node == data;
416 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
420 dev = class_find_device(&mux_class, NULL, np, of_dev_node_match);
422 return dev ? to_mux_chip(dev) : NULL;
426 * mux_control_get() - Get the mux-control for a device.
427 * @dev: The device that needs a mux-control.
428 * @mux_name: The name identifying the mux-control.
430 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
432 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
434 struct device_node *np = dev->of_node;
435 struct of_phandle_args args;
436 struct mux_chip *mux_chip;
437 unsigned int controller;
442 index = of_property_match_string(np, "mux-control-names",
445 dev_err(dev, "mux controller '%s' not found\n",
447 return ERR_PTR(index);
451 ret = of_parse_phandle_with_args(np,
452 "mux-controls", "#mux-control-cells",
455 dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
456 np, mux_name ?: "", index);
460 mux_chip = of_find_mux_chip_by_node(args.np);
461 of_node_put(args.np);
463 return ERR_PTR(-EPROBE_DEFER);
465 if (args.args_count > 1 ||
466 (!args.args_count && (mux_chip->controllers > 1))) {
467 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
469 return ERR_PTR(-EINVAL);
474 controller = args.args[0];
476 if (controller >= mux_chip->controllers) {
477 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
478 np, controller, args.np);
479 return ERR_PTR(-EINVAL);
482 get_device(&mux_chip->dev);
483 return &mux_chip->mux[controller];
485 EXPORT_SYMBOL_GPL(mux_control_get);
488 * mux_control_put() - Put away the mux-control for good.
489 * @mux: The mux-control to put away.
491 * mux_control_put() reverses the effects of mux_control_get().
493 void mux_control_put(struct mux_control *mux)
495 put_device(&mux->chip->dev);
497 EXPORT_SYMBOL_GPL(mux_control_put);
499 static void devm_mux_control_release(struct device *dev, void *res)
501 struct mux_control *mux = *(struct mux_control **)res;
503 mux_control_put(mux);
507 * devm_mux_control_get() - Get the mux-control for a device, with resource
509 * @dev: The device that needs a mux-control.
510 * @mux_name: The name identifying the mux-control.
512 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
514 struct mux_control *devm_mux_control_get(struct device *dev,
515 const char *mux_name)
517 struct mux_control **ptr, *mux;
519 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
521 return ERR_PTR(-ENOMEM);
523 mux = mux_control_get(dev, mux_name);
530 devres_add(dev, ptr);
534 EXPORT_SYMBOL_GPL(devm_mux_control_get);
537 * Using subsys_initcall instead of module_init here to try to ensure - for
538 * the non-modular case - that the subsystem is initialized when mux consumers
539 * and mux controllers start to use it.
540 * For the modular case, the ordering is ensured with module dependencies.
542 subsys_initcall(mux_init);
543 module_exit(mux_exit);
545 MODULE_DESCRIPTION("Multiplexer subsystem");
547 MODULE_LICENSE("GPL v2");