Linux 4.16.11
[linux/fpc-iii.git] / drivers / mux / core.c
blobd1271c1ee23ced2c5da81826ec7fbdac02bd87c6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Multiplexer subsystem
5 * Copyright (C) 2017 Axentia Technologies AB
7 * Author: Peter Rosin <peda@axentia.se>
8 */
10 #define pr_fmt(fmt) "mux-core: " fmt
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/idr.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mux/consumer.h>
19 #include <linux/mux/driver.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/slab.h>
25 * The idle-as-is "state" is not an actual state that may be selected, it
26 * only implies that the state should not be changed. So, use that state
27 * as indication that the cached state of the multiplexer is unknown.
29 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
31 static struct class mux_class = {
32 .name = "mux",
33 .owner = THIS_MODULE,
36 static DEFINE_IDA(mux_ida);
38 static int __init mux_init(void)
40 ida_init(&mux_ida);
41 return class_register(&mux_class);
44 static void __exit mux_exit(void)
46 class_unregister(&mux_class);
47 ida_destroy(&mux_ida);
50 static void mux_chip_release(struct device *dev)
52 struct mux_chip *mux_chip = to_mux_chip(dev);
54 ida_simple_remove(&mux_ida, mux_chip->id);
55 kfree(mux_chip);
58 static const struct device_type mux_type = {
59 .name = "mux-chip",
60 .release = mux_chip_release,
63 /**
64 * mux_chip_alloc() - Allocate a mux-chip.
65 * @dev: The parent device implementing the mux interface.
66 * @controllers: The number of mux controllers to allocate for this chip.
67 * @sizeof_priv: Size of extra memory area for private use by the caller.
69 * After allocating the mux-chip with the desired number of mux controllers
70 * but before registering the chip, the mux driver is required to configure
71 * the number of valid mux states in the mux_chip->mux[N].states members and
72 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
73 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
74 * provide a pointer to the operations struct in the mux_chip->ops member
75 * before registering the mux-chip with mux_chip_register.
77 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
79 struct mux_chip *mux_chip_alloc(struct device *dev,
80 unsigned int controllers, size_t sizeof_priv)
82 struct mux_chip *mux_chip;
83 int i;
85 if (WARN_ON(!dev || !controllers))
86 return ERR_PTR(-EINVAL);
88 mux_chip = kzalloc(sizeof(*mux_chip) +
89 controllers * sizeof(*mux_chip->mux) +
90 sizeof_priv, GFP_KERNEL);
91 if (!mux_chip)
92 return ERR_PTR(-ENOMEM);
94 mux_chip->mux = (struct mux_control *)(mux_chip + 1);
95 mux_chip->dev.class = &mux_class;
96 mux_chip->dev.type = &mux_type;
97 mux_chip->dev.parent = dev;
98 mux_chip->dev.of_node = dev->of_node;
99 dev_set_drvdata(&mux_chip->dev, mux_chip);
101 mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
102 if (mux_chip->id < 0) {
103 int err = mux_chip->id;
105 pr_err("muxchipX failed to get a device id\n");
106 kfree(mux_chip);
107 return ERR_PTR(err);
109 dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
111 mux_chip->controllers = controllers;
112 for (i = 0; i < controllers; ++i) {
113 struct mux_control *mux = &mux_chip->mux[i];
115 mux->chip = mux_chip;
116 sema_init(&mux->lock, 1);
117 mux->cached_state = MUX_CACHE_UNKNOWN;
118 mux->idle_state = MUX_IDLE_AS_IS;
121 device_initialize(&mux_chip->dev);
123 return mux_chip;
125 EXPORT_SYMBOL_GPL(mux_chip_alloc);
127 static int mux_control_set(struct mux_control *mux, int state)
129 int ret = mux->chip->ops->set(mux, state);
131 mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
133 return ret;
137 * mux_chip_register() - Register a mux-chip, thus readying the controllers
138 * for use.
139 * @mux_chip: The mux-chip to register.
141 * Do not retry registration of the same mux-chip on failure. You should
142 * instead put it away with mux_chip_free() and allocate a new one, if you
143 * for some reason would like to retry registration.
145 * Return: Zero on success or a negative errno on error.
147 int mux_chip_register(struct mux_chip *mux_chip)
149 int i;
150 int ret;
152 for (i = 0; i < mux_chip->controllers; ++i) {
153 struct mux_control *mux = &mux_chip->mux[i];
155 if (mux->idle_state == mux->cached_state)
156 continue;
158 ret = mux_control_set(mux, mux->idle_state);
159 if (ret < 0) {
160 dev_err(&mux_chip->dev, "unable to set idle state\n");
161 return ret;
165 ret = device_add(&mux_chip->dev);
166 if (ret < 0)
167 dev_err(&mux_chip->dev,
168 "device_add failed in %s: %d\n", __func__, ret);
169 return ret;
171 EXPORT_SYMBOL_GPL(mux_chip_register);
174 * mux_chip_unregister() - Take the mux-chip off-line.
175 * @mux_chip: The mux-chip to unregister.
177 * mux_chip_unregister() reverses the effects of mux_chip_register().
178 * But not completely, you should not try to call mux_chip_register()
179 * on a mux-chip that has been registered before.
181 void mux_chip_unregister(struct mux_chip *mux_chip)
183 device_del(&mux_chip->dev);
185 EXPORT_SYMBOL_GPL(mux_chip_unregister);
188 * mux_chip_free() - Free the mux-chip for good.
189 * @mux_chip: The mux-chip to free.
191 * mux_chip_free() reverses the effects of mux_chip_alloc().
193 void mux_chip_free(struct mux_chip *mux_chip)
195 if (!mux_chip)
196 return;
198 put_device(&mux_chip->dev);
200 EXPORT_SYMBOL_GPL(mux_chip_free);
202 static void devm_mux_chip_release(struct device *dev, void *res)
204 struct mux_chip *mux_chip = *(struct mux_chip **)res;
206 mux_chip_free(mux_chip);
210 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
211 * @dev: The parent device implementing the mux interface.
212 * @controllers: The number of mux controllers to allocate for this chip.
213 * @sizeof_priv: Size of extra memory area for private use by the caller.
215 * See mux_chip_alloc() for more details.
217 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
219 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
220 unsigned int controllers,
221 size_t sizeof_priv)
223 struct mux_chip **ptr, *mux_chip;
225 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
226 if (!ptr)
227 return ERR_PTR(-ENOMEM);
229 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
230 if (IS_ERR(mux_chip)) {
231 devres_free(ptr);
232 return mux_chip;
235 *ptr = mux_chip;
236 devres_add(dev, ptr);
238 return mux_chip;
240 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
242 static void devm_mux_chip_reg_release(struct device *dev, void *res)
244 struct mux_chip *mux_chip = *(struct mux_chip **)res;
246 mux_chip_unregister(mux_chip);
250 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
251 * @dev: The parent device implementing the mux interface.
252 * @mux_chip: The mux-chip to register.
254 * See mux_chip_register() for more details.
256 * Return: Zero on success or a negative errno on error.
258 int devm_mux_chip_register(struct device *dev,
259 struct mux_chip *mux_chip)
261 struct mux_chip **ptr;
262 int res;
264 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
265 if (!ptr)
266 return -ENOMEM;
268 res = mux_chip_register(mux_chip);
269 if (res) {
270 devres_free(ptr);
271 return res;
274 *ptr = mux_chip;
275 devres_add(dev, ptr);
277 return res;
279 EXPORT_SYMBOL_GPL(devm_mux_chip_register);
282 * mux_control_states() - Query the number of multiplexer states.
283 * @mux: The mux-control to query.
285 * Return: The number of multiplexer states.
287 unsigned int mux_control_states(struct mux_control *mux)
289 return mux->states;
291 EXPORT_SYMBOL_GPL(mux_control_states);
294 * The mux->lock must be down when calling this function.
296 static int __mux_control_select(struct mux_control *mux, int state)
298 int ret;
300 if (WARN_ON(state < 0 || state >= mux->states))
301 return -EINVAL;
303 if (mux->cached_state == state)
304 return 0;
306 ret = mux_control_set(mux, state);
307 if (ret >= 0)
308 return 0;
310 /* The mux update failed, try to revert if appropriate... */
311 if (mux->idle_state != MUX_IDLE_AS_IS)
312 mux_control_set(mux, mux->idle_state);
314 return ret;
318 * mux_control_select() - Select the given multiplexer state.
319 * @mux: The mux-control to request a change of state from.
320 * @state: The new requested state.
322 * On successfully selecting the mux-control state, it will be locked until
323 * there is a call to mux_control_deselect(). If the mux-control is already
324 * selected when mux_control_select() is called, the caller will be blocked
325 * until mux_control_deselect() is called (by someone else).
327 * Therefore, make sure to call mux_control_deselect() when the operation is
328 * complete and the mux-control is free for others to use, but do not call
329 * mux_control_deselect() if mux_control_select() fails.
331 * Return: 0 when the mux-control state has the requested state or a negative
332 * errno on error.
334 int mux_control_select(struct mux_control *mux, unsigned int state)
336 int ret;
338 ret = down_killable(&mux->lock);
339 if (ret < 0)
340 return ret;
342 ret = __mux_control_select(mux, state);
344 if (ret < 0)
345 up(&mux->lock);
347 return ret;
349 EXPORT_SYMBOL_GPL(mux_control_select);
352 * mux_control_try_select() - Try to select the given multiplexer state.
353 * @mux: The mux-control to request a change of state from.
354 * @state: The new requested state.
356 * On successfully selecting the mux-control state, it will be locked until
357 * mux_control_deselect() called.
359 * Therefore, make sure to call mux_control_deselect() when the operation is
360 * complete and the mux-control is free for others to use, but do not call
361 * mux_control_deselect() if mux_control_try_select() fails.
363 * Return: 0 when the mux-control state has the requested state or a negative
364 * errno on error. Specifically -EBUSY if the mux-control is contended.
366 int mux_control_try_select(struct mux_control *mux, unsigned int state)
368 int ret;
370 if (down_trylock(&mux->lock))
371 return -EBUSY;
373 ret = __mux_control_select(mux, state);
375 if (ret < 0)
376 up(&mux->lock);
378 return ret;
380 EXPORT_SYMBOL_GPL(mux_control_try_select);
383 * mux_control_deselect() - Deselect the previously selected multiplexer state.
384 * @mux: The mux-control to deselect.
386 * It is required that a single call is made to mux_control_deselect() for
387 * each and every successful call made to either of mux_control_select() or
388 * mux_control_try_select().
390 * Return: 0 on success and a negative errno on error. An error can only
391 * occur if the mux has an idle state. Note that even if an error occurs, the
392 * mux-control is unlocked and is thus free for the next access.
394 int mux_control_deselect(struct mux_control *mux)
396 int ret = 0;
398 if (mux->idle_state != MUX_IDLE_AS_IS &&
399 mux->idle_state != mux->cached_state)
400 ret = mux_control_set(mux, mux->idle_state);
402 up(&mux->lock);
404 return ret;
406 EXPORT_SYMBOL_GPL(mux_control_deselect);
408 static int of_dev_node_match(struct device *dev, const void *data)
410 return dev->of_node == data;
413 /* Note this function returns a reference to the mux_chip dev. */
414 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
416 struct device *dev;
418 dev = class_find_device(&mux_class, NULL, np, of_dev_node_match);
420 return dev ? to_mux_chip(dev) : NULL;
424 * mux_control_get() - Get the mux-control for a device.
425 * @dev: The device that needs a mux-control.
426 * @mux_name: The name identifying the mux-control.
428 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
430 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
432 struct device_node *np = dev->of_node;
433 struct of_phandle_args args;
434 struct mux_chip *mux_chip;
435 unsigned int controller;
436 int index = 0;
437 int ret;
439 if (mux_name) {
440 index = of_property_match_string(np, "mux-control-names",
441 mux_name);
442 if (index < 0) {
443 dev_err(dev, "mux controller '%s' not found\n",
444 mux_name);
445 return ERR_PTR(index);
449 ret = of_parse_phandle_with_args(np,
450 "mux-controls", "#mux-control-cells",
451 index, &args);
452 if (ret) {
453 dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
454 np, mux_name ?: "", index);
455 return ERR_PTR(ret);
458 mux_chip = of_find_mux_chip_by_node(args.np);
459 of_node_put(args.np);
460 if (!mux_chip)
461 return ERR_PTR(-EPROBE_DEFER);
463 if (args.args_count > 1 ||
464 (!args.args_count && (mux_chip->controllers > 1))) {
465 dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
466 np, args.np);
467 put_device(&mux_chip->dev);
468 return ERR_PTR(-EINVAL);
471 controller = 0;
472 if (args.args_count)
473 controller = args.args[0];
475 if (controller >= mux_chip->controllers) {
476 dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
477 np, controller, args.np);
478 put_device(&mux_chip->dev);
479 return ERR_PTR(-EINVAL);
482 return &mux_chip->mux[controller];
484 EXPORT_SYMBOL_GPL(mux_control_get);
487 * mux_control_put() - Put away the mux-control for good.
488 * @mux: The mux-control to put away.
490 * mux_control_put() reverses the effects of mux_control_get().
492 void mux_control_put(struct mux_control *mux)
494 put_device(&mux->chip->dev);
496 EXPORT_SYMBOL_GPL(mux_control_put);
498 static void devm_mux_control_release(struct device *dev, void *res)
500 struct mux_control *mux = *(struct mux_control **)res;
502 mux_control_put(mux);
506 * devm_mux_control_get() - Get the mux-control for a device, with resource
507 * management.
508 * @dev: The device that needs a mux-control.
509 * @mux_name: The name identifying the mux-control.
511 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
513 struct mux_control *devm_mux_control_get(struct device *dev,
514 const char *mux_name)
516 struct mux_control **ptr, *mux;
518 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
519 if (!ptr)
520 return ERR_PTR(-ENOMEM);
522 mux = mux_control_get(dev, mux_name);
523 if (IS_ERR(mux)) {
524 devres_free(ptr);
525 return mux;
528 *ptr = mux;
529 devres_add(dev, ptr);
531 return mux;
533 EXPORT_SYMBOL_GPL(devm_mux_control_get);
536 * Using subsys_initcall instead of module_init here to try to ensure - for
537 * the non-modular case - that the subsystem is initialized when mux consumers
538 * and mux controllers start to use it.
539 * For the modular case, the ordering is ensured with module dependencies.
541 subsys_initcall(mux_init);
542 module_exit(mux_exit);
544 MODULE_DESCRIPTION("Multiplexer subsystem");
545 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
546 MODULE_LICENSE("GPL v2");