mei: me: add cannon point device ids
[linux/fpc-iii.git] / drivers / mux / core.c
blob6e5cf9d9cd9927f4264e3a01394e2b84d80d7d3b
1 /*
2 * Multiplexer subsystem
4 * Copyright (C) 2017 Axentia Technologies AB
6 * Author: Peter Rosin <peda@axentia.se>
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>
23 #include <linux/of.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 = {
35 .name = "mux",
36 .owner = THIS_MODULE,
39 static DEFINE_IDA(mux_ida);
41 static int __init mux_init(void)
43 ida_init(&mux_ida);
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);
58 kfree(mux_chip);
61 static const struct device_type mux_type = {
62 .name = "mux-chip",
63 .release = mux_chip_release,
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.
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;
86 int i;
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);
94 if (!mux_chip)
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");
109 kfree(mux_chip);
110 return ERR_PTR(err);
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);
126 return mux_chip;
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;
136 return ret;
140 * mux_chip_register() - Register a mux-chip, thus readying the controllers
141 * for use.
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)
152 int i;
153 int ret;
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)
159 continue;
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;
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;
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)
198 if (!mux_chip)
199 return;
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,
224 size_t sizeof_priv)
226 struct mux_chip **ptr, *mux_chip;
228 ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
229 if (!ptr)
230 return ERR_PTR(-ENOMEM);
232 mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
233 if (IS_ERR(mux_chip)) {
234 devres_free(ptr);
235 return mux_chip;
238 *ptr = mux_chip;
239 devres_add(dev, ptr);
241 return mux_chip;
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;
265 int res;
267 ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
268 if (!ptr)
269 return -ENOMEM;
271 res = mux_chip_register(mux_chip);
272 if (res) {
273 devres_free(ptr);
274 return res;
277 *ptr = mux_chip;
278 devres_add(dev, ptr);
280 return res;
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)
292 return mux->states;
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)
301 int ret;
303 if (WARN_ON(state < 0 || state >= mux->states))
304 return -EINVAL;
306 if (mux->cached_state == state)
307 return 0;
309 ret = mux_control_set(mux, state);
310 if (ret >= 0)
311 return 0;
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);
317 return ret;
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
335 * errno on error.
337 int mux_control_select(struct mux_control *mux, unsigned int state)
339 int ret;
341 ret = down_killable(&mux->lock);
342 if (ret < 0)
343 return ret;
345 ret = __mux_control_select(mux, state);
347 if (ret < 0)
348 up(&mux->lock);
350 return ret;
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)
371 int ret;
373 if (down_trylock(&mux->lock))
374 return -EBUSY;
376 ret = __mux_control_select(mux, state);
378 if (ret < 0)
379 up(&mux->lock);
381 return ret;
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)
399 int ret = 0;
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);
405 up(&mux->lock);
407 return ret;
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 /* 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)
419 struct device *dev;
421 dev = class_find_device(&mux_class, NULL, np, of_dev_node_match);
423 return dev ? to_mux_chip(dev) : NULL;
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.
431 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
433 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
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;
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);
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);
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);
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);
474 controller = 0;
475 if (args.args_count)
476 controller = args.args[0];
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);
485 return &mux_chip->mux[controller];
487 EXPORT_SYMBOL_GPL(mux_control_get);
490 * mux_control_put() - Put away the mux-control for good.
491 * @mux: The mux-control to put away.
493 * mux_control_put() reverses the effects of mux_control_get().
495 void mux_control_put(struct mux_control *mux)
497 put_device(&mux->chip->dev);
499 EXPORT_SYMBOL_GPL(mux_control_put);
501 static void devm_mux_control_release(struct device *dev, void *res)
503 struct mux_control *mux = *(struct mux_control **)res;
505 mux_control_put(mux);
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.
514 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
516 struct mux_control *devm_mux_control_get(struct device *dev,
517 const char *mux_name)
519 struct mux_control **ptr, *mux;
521 ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
522 if (!ptr)
523 return ERR_PTR(-ENOMEM);
525 mux = mux_control_get(dev, mux_name);
526 if (IS_ERR(mux)) {
527 devres_free(ptr);
528 return mux;
531 *ptr = mux;
532 devres_add(dev, ptr);
534 return mux;
536 EXPORT_SYMBOL_GPL(devm_mux_control_get);
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.
544 subsys_initcall(mux_init);
545 module_exit(mux_exit);
547 MODULE_DESCRIPTION("Multiplexer subsystem");
548 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
549 MODULE_LICENSE("GPL v2");