Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Multiplexer subsystem
  4 *
  5 * Copyright (C) 2017 Axentia Technologies AB
  6 *
  7 * Author: Peter Rosin <peda@axentia.se>
  8 */
  9
 10#define pr_fmt(fmt) "mux-core: " fmt
 11
 12#include <linux/delay.h>
 13#include <linux/device.h>
 14#include <linux/err.h>
 15#include <linux/export.h>
 16#include <linux/idr.h>
 17#include <linux/init.h>
 18#include <linux/module.h>
 19#include <linux/mux/consumer.h>
 20#include <linux/mux/driver.h>
 21#include <linux/of.h>
 
 22#include <linux/slab.h>
 23
 24/*
 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.
 28 */
 29#define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
 30
 31/**
 32 * struct mux_state -	Represents a mux controller state specific to a given
 33 *			consumer.
 34 * @mux:		Pointer to a mux controller.
 35 * @state:		State of the mux to be selected.
 36 *
 37 * This structure is specific to the consumer that acquires it and has
 38 * information specific to that consumer.
 39 */
 40struct mux_state {
 41	struct mux_control *mux;
 42	unsigned int state;
 43};
 44
 45static struct class mux_class = {
 46	.name = "mux",
 
 47};
 48
 49static DEFINE_IDA(mux_ida);
 50
 51static int __init mux_init(void)
 52{
 53	ida_init(&mux_ida);
 54	return class_register(&mux_class);
 55}
 56
 57static void __exit mux_exit(void)
 58{
 59	class_unregister(&mux_class);
 60	ida_destroy(&mux_ida);
 61}
 62
 63static void mux_chip_release(struct device *dev)
 64{
 65	struct mux_chip *mux_chip = to_mux_chip(dev);
 66
 67	ida_simple_remove(&mux_ida, mux_chip->id);
 68	kfree(mux_chip);
 69}
 70
 71static const struct device_type mux_type = {
 72	.name = "mux-chip",
 73	.release = mux_chip_release,
 74};
 75
 76/**
 77 * mux_chip_alloc() - Allocate a mux-chip.
 78 * @dev: The parent device implementing the mux interface.
 79 * @controllers: The number of mux controllers to allocate for this chip.
 80 * @sizeof_priv: Size of extra memory area for private use by the caller.
 81 *
 82 * After allocating the mux-chip with the desired number of mux controllers
 83 * but before registering the chip, the mux driver is required to configure
 84 * the number of valid mux states in the mux_chip->mux[N].states members and
 85 * the desired idle state in the returned mux_chip->mux[N].idle_state members.
 86 * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
 87 * provide a pointer to the operations struct in the mux_chip->ops member
 88 * before registering the mux-chip with mux_chip_register.
 89 *
 90 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
 91 */
 92struct mux_chip *mux_chip_alloc(struct device *dev,
 93				unsigned int controllers, size_t sizeof_priv)
 94{
 95	struct mux_chip *mux_chip;
 96	int i;
 97
 98	if (WARN_ON(!dev || !controllers))
 99		return ERR_PTR(-EINVAL);
100
101	mux_chip = kzalloc(sizeof(*mux_chip) +
102			   controllers * sizeof(*mux_chip->mux) +
103			   sizeof_priv, GFP_KERNEL);
104	if (!mux_chip)
105		return ERR_PTR(-ENOMEM);
106
107	mux_chip->mux = (struct mux_control *)(mux_chip + 1);
108	mux_chip->dev.class = &mux_class;
109	mux_chip->dev.type = &mux_type;
110	mux_chip->dev.parent = dev;
111	mux_chip->dev.of_node = dev->of_node;
112	dev_set_drvdata(&mux_chip->dev, mux_chip);
113
114	mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
115	if (mux_chip->id < 0) {
116		int err = mux_chip->id;
117
118		pr_err("muxchipX failed to get a device id\n");
119		kfree(mux_chip);
120		return ERR_PTR(err);
121	}
122	dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
123
124	mux_chip->controllers = controllers;
125	for (i = 0; i < controllers; ++i) {
126		struct mux_control *mux = &mux_chip->mux[i];
127
128		mux->chip = mux_chip;
129		sema_init(&mux->lock, 1);
130		mux->cached_state = MUX_CACHE_UNKNOWN;
131		mux->idle_state = MUX_IDLE_AS_IS;
132		mux->last_change = ktime_get();
133	}
134
135	device_initialize(&mux_chip->dev);
136
137	return mux_chip;
138}
139EXPORT_SYMBOL_GPL(mux_chip_alloc);
140
141static int mux_control_set(struct mux_control *mux, int state)
142{
143	int ret = mux->chip->ops->set(mux, state);
144
145	mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
146	if (ret >= 0)
147		mux->last_change = ktime_get();
148
149	return ret;
150}
151
152/**
153 * mux_chip_register() - Register a mux-chip, thus readying the controllers
154 *			 for use.
155 * @mux_chip: The mux-chip to register.
156 *
157 * Do not retry registration of the same mux-chip on failure. You should
158 * instead put it away with mux_chip_free() and allocate a new one, if you
159 * for some reason would like to retry registration.
160 *
161 * Return: Zero on success or a negative errno on error.
162 */
163int mux_chip_register(struct mux_chip *mux_chip)
164{
165	int i;
166	int ret;
167
168	for (i = 0; i < mux_chip->controllers; ++i) {
169		struct mux_control *mux = &mux_chip->mux[i];
170
171		if (mux->idle_state == mux->cached_state)
172			continue;
173
174		ret = mux_control_set(mux, mux->idle_state);
175		if (ret < 0) {
176			dev_err(&mux_chip->dev, "unable to set idle state\n");
177			return ret;
178		}
179	}
180
181	ret = device_add(&mux_chip->dev);
182	if (ret < 0)
183		dev_err(&mux_chip->dev,
184			"device_add failed in %s: %d\n", __func__, ret);
185	return ret;
186}
187EXPORT_SYMBOL_GPL(mux_chip_register);
188
189/**
190 * mux_chip_unregister() - Take the mux-chip off-line.
191 * @mux_chip: The mux-chip to unregister.
192 *
193 * mux_chip_unregister() reverses the effects of mux_chip_register().
194 * But not completely, you should not try to call mux_chip_register()
195 * on a mux-chip that has been registered before.
196 */
197void mux_chip_unregister(struct mux_chip *mux_chip)
198{
199	device_del(&mux_chip->dev);
200}
201EXPORT_SYMBOL_GPL(mux_chip_unregister);
202
203/**
204 * mux_chip_free() - Free the mux-chip for good.
205 * @mux_chip: The mux-chip to free.
206 *
207 * mux_chip_free() reverses the effects of mux_chip_alloc().
208 */
209void mux_chip_free(struct mux_chip *mux_chip)
210{
211	if (!mux_chip)
212		return;
213
214	put_device(&mux_chip->dev);
215}
216EXPORT_SYMBOL_GPL(mux_chip_free);
217
218static void devm_mux_chip_release(struct device *dev, void *res)
219{
220	struct mux_chip *mux_chip = *(struct mux_chip **)res;
221
222	mux_chip_free(mux_chip);
223}
224
225/**
226 * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
227 * @dev: The parent device implementing the mux interface.
228 * @controllers: The number of mux controllers to allocate for this chip.
229 * @sizeof_priv: Size of extra memory area for private use by the caller.
230 *
231 * See mux_chip_alloc() for more details.
232 *
233 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
234 */
235struct mux_chip *devm_mux_chip_alloc(struct device *dev,
236				     unsigned int controllers,
237				     size_t sizeof_priv)
238{
239	struct mux_chip **ptr, *mux_chip;
240
241	ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
242	if (!ptr)
243		return ERR_PTR(-ENOMEM);
244
245	mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
246	if (IS_ERR(mux_chip)) {
247		devres_free(ptr);
248		return mux_chip;
249	}
250
251	*ptr = mux_chip;
252	devres_add(dev, ptr);
253
254	return mux_chip;
255}
256EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
257
258static void devm_mux_chip_reg_release(struct device *dev, void *res)
259{
260	struct mux_chip *mux_chip = *(struct mux_chip **)res;
261
262	mux_chip_unregister(mux_chip);
263}
264
265/**
266 * devm_mux_chip_register() - Resource-managed version mux_chip_register().
267 * @dev: The parent device implementing the mux interface.
268 * @mux_chip: The mux-chip to register.
269 *
270 * See mux_chip_register() for more details.
271 *
272 * Return: Zero on success or a negative errno on error.
273 */
274int devm_mux_chip_register(struct device *dev,
275			   struct mux_chip *mux_chip)
276{
277	struct mux_chip **ptr;
278	int res;
279
280	ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
281	if (!ptr)
282		return -ENOMEM;
283
284	res = mux_chip_register(mux_chip);
285	if (res) {
286		devres_free(ptr);
287		return res;
288	}
289
290	*ptr = mux_chip;
291	devres_add(dev, ptr);
292
293	return res;
294}
295EXPORT_SYMBOL_GPL(devm_mux_chip_register);
296
297/**
298 * mux_control_states() - Query the number of multiplexer states.
299 * @mux: The mux-control to query.
300 *
301 * Return: The number of multiplexer states.
302 */
303unsigned int mux_control_states(struct mux_control *mux)
304{
305	return mux->states;
306}
307EXPORT_SYMBOL_GPL(mux_control_states);
308
309/*
310 * The mux->lock must be down when calling this function.
311 */
312static int __mux_control_select(struct mux_control *mux, int state)
313{
314	int ret;
315
316	if (WARN_ON(state < 0 || state >= mux->states))
317		return -EINVAL;
318
319	if (mux->cached_state == state)
320		return 0;
321
322	ret = mux_control_set(mux, state);
323	if (ret >= 0)
324		return 0;
325
326	/* The mux update failed, try to revert if appropriate... */
327	if (mux->idle_state != MUX_IDLE_AS_IS)
328		mux_control_set(mux, mux->idle_state);
329
330	return ret;
331}
332
333static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
334{
335	ktime_t delayend;
336	s64 remaining;
337
338	if (!delay_us)
339		return;
340
341	delayend = ktime_add_us(mux->last_change, delay_us);
342	remaining = ktime_us_delta(delayend, ktime_get());
343	if (remaining > 0)
344		fsleep(remaining);
345}
346
347/**
348 * mux_control_select_delay() - Select the given multiplexer state.
349 * @mux: The mux-control to request a change of state from.
350 * @state: The new requested state.
351 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
352 *
353 * On successfully selecting the mux-control state, it will be locked until
354 * there is a call to mux_control_deselect(). If the mux-control is already
355 * selected when mux_control_select() is called, the caller will be blocked
356 * until mux_control_deselect() or mux_state_deselect() is called (by someone
357 * else).
358 *
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_select() fails.
362 *
363 * Return: 0 when the mux-control state has the requested state or a negative
364 * errno on error.
365 */
366int mux_control_select_delay(struct mux_control *mux, unsigned int state,
367			     unsigned int delay_us)
368{
369	int ret;
370
371	ret = down_killable(&mux->lock);
372	if (ret < 0)
373		return ret;
374
375	ret = __mux_control_select(mux, state);
376	if (ret >= 0)
377		mux_control_delay(mux, delay_us);
378
379	if (ret < 0)
380		up(&mux->lock);
381
382	return ret;
383}
384EXPORT_SYMBOL_GPL(mux_control_select_delay);
385
386/**
387 * mux_state_select_delay() - Select the given multiplexer state.
388 * @mstate: The mux-state to select.
389 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
390 *
391 * On successfully selecting the mux-state, its mux-control will be locked
392 * until there is a call to mux_state_deselect(). If the mux-control is already
393 * selected when mux_state_select() is called, the caller will be blocked
394 * until mux_state_deselect() or mux_control_deselect() is called (by someone
395 * else).
396 *
397 * Therefore, make sure to call mux_state_deselect() when the operation is
398 * complete and the mux-control is free for others to use, but do not call
399 * mux_state_deselect() if mux_state_select() fails.
400 *
401 * Return: 0 when the mux-state has been selected or a negative
402 * errno on error.
403 */
404int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us)
405{
406	return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
407}
408EXPORT_SYMBOL_GPL(mux_state_select_delay);
409
410/**
411 * mux_control_try_select_delay() - Try to select the given multiplexer state.
412 * @mux: The mux-control to request a change of state from.
413 * @state: The new requested state.
414 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
415 *
416 * On successfully selecting the mux-control state, it will be locked until
417 * mux_control_deselect() is called.
418 *
419 * Therefore, make sure to call mux_control_deselect() when the operation is
420 * complete and the mux-control is free for others to use, but do not call
421 * mux_control_deselect() if mux_control_try_select() fails.
422 *
423 * Return: 0 when the mux-control state has the requested state or a negative
424 * errno on error. Specifically -EBUSY if the mux-control is contended.
425 */
426int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
427				 unsigned int delay_us)
428{
429	int ret;
430
431	if (down_trylock(&mux->lock))
432		return -EBUSY;
433
434	ret = __mux_control_select(mux, state);
435	if (ret >= 0)
436		mux_control_delay(mux, delay_us);
437
438	if (ret < 0)
439		up(&mux->lock);
440
441	return ret;
442}
443EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
444
445/**
446 * mux_state_try_select_delay() - Try to select the given multiplexer state.
447 * @mstate: The mux-state to select.
448 * @delay_us: The time to delay (in microseconds) if the mux state is changed.
449 *
450 * On successfully selecting the mux-state, its mux-control will be locked
451 * until mux_state_deselect() is called.
452 *
453 * Therefore, make sure to call mux_state_deselect() when the operation is
454 * complete and the mux-control is free for others to use, but do not call
455 * mux_state_deselect() if mux_state_try_select() fails.
456 *
457 * Return: 0 when the mux-state has been selected or a negative errno on
458 * error. Specifically -EBUSY if the mux-control is contended.
459 */
460int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us)
461{
462	return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
463}
464EXPORT_SYMBOL_GPL(mux_state_try_select_delay);
465
466/**
467 * mux_control_deselect() - Deselect the previously selected multiplexer state.
468 * @mux: The mux-control to deselect.
469 *
470 * It is required that a single call is made to mux_control_deselect() for
471 * each and every successful call made to either of mux_control_select() or
472 * mux_control_try_select().
473 *
474 * Return: 0 on success and a negative errno on error. An error can only
475 * occur if the mux has an idle state. Note that even if an error occurs, the
476 * mux-control is unlocked and is thus free for the next access.
477 */
478int mux_control_deselect(struct mux_control *mux)
479{
480	int ret = 0;
481
482	if (mux->idle_state != MUX_IDLE_AS_IS &&
483	    mux->idle_state != mux->cached_state)
484		ret = mux_control_set(mux, mux->idle_state);
485
486	up(&mux->lock);
487
488	return ret;
489}
490EXPORT_SYMBOL_GPL(mux_control_deselect);
491
492/**
493 * mux_state_deselect() - Deselect the previously selected multiplexer state.
494 * @mstate: The mux-state to deselect.
495 *
496 * It is required that a single call is made to mux_state_deselect() for
497 * each and every successful call made to either of mux_state_select() or
498 * mux_state_try_select().
499 *
500 * Return: 0 on success and a negative errno on error. An error can only
501 * occur if the mux has an idle state. Note that even if an error occurs, the
502 * mux-control is unlocked and is thus free for the next access.
503 */
504int mux_state_deselect(struct mux_state *mstate)
505{
506	return mux_control_deselect(mstate->mux);
507}
508EXPORT_SYMBOL_GPL(mux_state_deselect);
509
510/* Note this function returns a reference to the mux_chip dev. */
511static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
512{
513	struct device *dev;
514
515	dev = class_find_device_by_of_node(&mux_class, np);
516
517	return dev ? to_mux_chip(dev) : NULL;
518}
519
520/*
521 * mux_get() - Get the mux-control for a device.
522 * @dev: The device that needs a mux-control.
523 * @mux_name: The name identifying the mux-control.
524 * @state: Pointer to where the requested state is returned, or NULL when
525 *         the required multiplexer states are handled by other means.
526 *
527 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
528 */
529static struct mux_control *mux_get(struct device *dev, const char *mux_name,
530				   unsigned int *state)
531{
532	struct device_node *np = dev->of_node;
533	struct of_phandle_args args;
534	struct mux_chip *mux_chip;
535	unsigned int controller;
536	int index = 0;
537	int ret;
538
539	if (mux_name) {
540		if (state)
541			index = of_property_match_string(np, "mux-state-names",
542							 mux_name);
543		else
544			index = of_property_match_string(np, "mux-control-names",
545							 mux_name);
546		if (index < 0) {
547			dev_err(dev, "mux controller '%s' not found\n",
548				mux_name);
549			return ERR_PTR(index);
550		}
551	}
552
553	if (state)
554		ret = of_parse_phandle_with_args(np,
555						 "mux-states", "#mux-state-cells",
556						 index, &args);
557	else
558		ret = of_parse_phandle_with_args(np,
559						 "mux-controls", "#mux-control-cells",
560						 index, &args);
561	if (ret) {
562		dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
563			np, state ? "state" : "control", mux_name ?: "", index);
564		return ERR_PTR(ret);
565	}
566
567	mux_chip = of_find_mux_chip_by_node(args.np);
568	of_node_put(args.np);
569	if (!mux_chip)
570		return ERR_PTR(-EPROBE_DEFER);
571
572	controller = 0;
573	if (state) {
574		if (args.args_count > 2 || args.args_count == 0 ||
575		    (args.args_count < 2 && mux_chip->controllers > 1)) {
576			dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
577				np, args.np);
578			put_device(&mux_chip->dev);
579			return ERR_PTR(-EINVAL);
580		}
581
582		if (args.args_count == 2) {
583			controller = args.args[0];
584			*state = args.args[1];
585		} else {
586			*state = args.args[0];
587		}
588
589	} else {
590		if (args.args_count > 1 ||
591		    (!args.args_count && mux_chip->controllers > 1)) {
592			dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
593				np, args.np);
594			put_device(&mux_chip->dev);
595			return ERR_PTR(-EINVAL);
596		}
597
598		if (args.args_count)
599			controller = args.args[0];
600	}
601
 
 
 
 
602	if (controller >= mux_chip->controllers) {
603		dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
604			np, controller, args.np);
605		put_device(&mux_chip->dev);
606		return ERR_PTR(-EINVAL);
607	}
608
609	return &mux_chip->mux[controller];
610}
611
612/**
613 * mux_control_get() - Get the mux-control for a device.
614 * @dev: The device that needs a mux-control.
615 * @mux_name: The name identifying the mux-control.
616 *
617 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
618 */
619struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
620{
621	return mux_get(dev, mux_name, NULL);
622}
623EXPORT_SYMBOL_GPL(mux_control_get);
624
625/**
626 * mux_control_put() - Put away the mux-control for good.
627 * @mux: The mux-control to put away.
628 *
629 * mux_control_put() reverses the effects of mux_control_get().
630 */
631void mux_control_put(struct mux_control *mux)
632{
633	put_device(&mux->chip->dev);
634}
635EXPORT_SYMBOL_GPL(mux_control_put);
636
637static void devm_mux_control_release(struct device *dev, void *res)
638{
639	struct mux_control *mux = *(struct mux_control **)res;
640
641	mux_control_put(mux);
642}
643
644/**
645 * devm_mux_control_get() - Get the mux-control for a device, with resource
646 *			    management.
647 * @dev: The device that needs a mux-control.
648 * @mux_name: The name identifying the mux-control.
649 *
650 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
651 */
652struct mux_control *devm_mux_control_get(struct device *dev,
653					 const char *mux_name)
654{
655	struct mux_control **ptr, *mux;
656
657	ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
658	if (!ptr)
659		return ERR_PTR(-ENOMEM);
660
661	mux = mux_control_get(dev, mux_name);
662	if (IS_ERR(mux)) {
663		devres_free(ptr);
664		return mux;
665	}
666
667	*ptr = mux;
668	devres_add(dev, ptr);
669
670	return mux;
671}
672EXPORT_SYMBOL_GPL(devm_mux_control_get);
673
674/*
675 * mux_state_get() - Get the mux-state for a device.
676 * @dev: The device that needs a mux-state.
677 * @mux_name: The name identifying the mux-state.
678 *
679 * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno.
680 */
681static struct mux_state *mux_state_get(struct device *dev, const char *mux_name)
682{
683	struct mux_state *mstate;
684
685	mstate = kzalloc(sizeof(*mstate), GFP_KERNEL);
686	if (!mstate)
687		return ERR_PTR(-ENOMEM);
688
689	mstate->mux = mux_get(dev, mux_name, &mstate->state);
690	if (IS_ERR(mstate->mux)) {
691		int err = PTR_ERR(mstate->mux);
692
693		kfree(mstate);
694		return ERR_PTR(err);
695	}
696
697	return mstate;
698}
699
700/*
701 * mux_state_put() - Put away the mux-state for good.
702 * @mstate: The mux-state to put away.
703 *
704 * mux_state_put() reverses the effects of mux_state_get().
705 */
706static void mux_state_put(struct mux_state *mstate)
707{
708	mux_control_put(mstate->mux);
709	kfree(mstate);
710}
711
712static void devm_mux_state_release(struct device *dev, void *res)
713{
714	struct mux_state *mstate = *(struct mux_state **)res;
715
716	mux_state_put(mstate);
717}
718
719/**
720 * devm_mux_state_get() - Get the mux-state for a device, with resource
721 *			  management.
722 * @dev: The device that needs a mux-control.
723 * @mux_name: The name identifying the mux-control.
724 *
725 * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
726 */
727struct mux_state *devm_mux_state_get(struct device *dev,
728				     const char *mux_name)
729{
730	struct mux_state **ptr, *mstate;
731
732	ptr = devres_alloc(devm_mux_state_release, sizeof(*ptr), GFP_KERNEL);
733	if (!ptr)
734		return ERR_PTR(-ENOMEM);
735
736	mstate = mux_state_get(dev, mux_name);
737	if (IS_ERR(mstate)) {
738		devres_free(ptr);
739		return mstate;
740	}
741
742	*ptr = mstate;
743	devres_add(dev, ptr);
744
745	return mstate;
746}
747EXPORT_SYMBOL_GPL(devm_mux_state_get);
748
749/*
750 * Using subsys_initcall instead of module_init here to try to ensure - for
751 * the non-modular case - that the subsystem is initialized when mux consumers
752 * and mux controllers start to use it.
753 * For the modular case, the ordering is ensured with module dependencies.
754 */
755subsys_initcall(mux_init);
756module_exit(mux_exit);
757
758MODULE_DESCRIPTION("Multiplexer subsystem");
759MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
760MODULE_LICENSE("GPL v2");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Multiplexer subsystem
  4 *
  5 * Copyright (C) 2017 Axentia Technologies AB
  6 *
  7 * Author: Peter Rosin <peda@axentia.se>
  8 */
  9
 10#define pr_fmt(fmt) "mux-core: " fmt
 11
 
 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>
 23
 24/*
 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.
 28 */
 29#define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
 30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 31static struct class mux_class = {
 32	.name = "mux",
 33	.owner = THIS_MODULE,
 34};
 35
 36static DEFINE_IDA(mux_ida);
 37
 38static int __init mux_init(void)
 39{
 40	ida_init(&mux_ida);
 41	return class_register(&mux_class);
 42}
 43
 44static void __exit mux_exit(void)
 45{
 46	class_unregister(&mux_class);
 47	ida_destroy(&mux_ida);
 48}
 49
 50static void mux_chip_release(struct device *dev)
 51{
 52	struct mux_chip *mux_chip = to_mux_chip(dev);
 53
 54	ida_simple_remove(&mux_ida, mux_chip->id);
 55	kfree(mux_chip);
 56}
 57
 58static const struct device_type mux_type = {
 59	.name = "mux-chip",
 60	.release = mux_chip_release,
 61};
 62
 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.
 68 *
 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.
 76 *
 77 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
 78 */
 79struct mux_chip *mux_chip_alloc(struct device *dev,
 80				unsigned int controllers, size_t sizeof_priv)
 81{
 82	struct mux_chip *mux_chip;
 83	int i;
 84
 85	if (WARN_ON(!dev || !controllers))
 86		return ERR_PTR(-EINVAL);
 87
 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);
 93
 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);
100
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;
104
105		pr_err("muxchipX failed to get a device id\n");
106		kfree(mux_chip);
107		return ERR_PTR(err);
108	}
109	dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
110
111	mux_chip->controllers = controllers;
112	for (i = 0; i < controllers; ++i) {
113		struct mux_control *mux = &mux_chip->mux[i];
114
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;
 
119	}
120
121	device_initialize(&mux_chip->dev);
122
123	return mux_chip;
124}
125EXPORT_SYMBOL_GPL(mux_chip_alloc);
126
127static int mux_control_set(struct mux_control *mux, int state)
128{
129	int ret = mux->chip->ops->set(mux, state);
130
131	mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
 
 
132
133	return ret;
134}
135
136/**
137 * mux_chip_register() - Register a mux-chip, thus readying the controllers
138 *			 for use.
139 * @mux_chip: The mux-chip to register.
140 *
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.
144 *
145 * Return: Zero on success or a negative errno on error.
146 */
147int mux_chip_register(struct mux_chip *mux_chip)
148{
149	int i;
150	int ret;
151
152	for (i = 0; i < mux_chip->controllers; ++i) {
153		struct mux_control *mux = &mux_chip->mux[i];
154
155		if (mux->idle_state == mux->cached_state)
156			continue;
157
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;
162		}
163	}
164
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;
170}
171EXPORT_SYMBOL_GPL(mux_chip_register);
172
173/**
174 * mux_chip_unregister() - Take the mux-chip off-line.
175 * @mux_chip: The mux-chip to unregister.
176 *
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.
180 */
181void mux_chip_unregister(struct mux_chip *mux_chip)
182{
183	device_del(&mux_chip->dev);
184}
185EXPORT_SYMBOL_GPL(mux_chip_unregister);
186
187/**
188 * mux_chip_free() - Free the mux-chip for good.
189 * @mux_chip: The mux-chip to free.
190 *
191 * mux_chip_free() reverses the effects of mux_chip_alloc().
192 */
193void mux_chip_free(struct mux_chip *mux_chip)
194{
195	if (!mux_chip)
196		return;
197
198	put_device(&mux_chip->dev);
199}
200EXPORT_SYMBOL_GPL(mux_chip_free);
201
202static void devm_mux_chip_release(struct device *dev, void *res)
203{
204	struct mux_chip *mux_chip = *(struct mux_chip **)res;
205
206	mux_chip_free(mux_chip);
207}
208
209/**
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.
214 *
215 * See mux_chip_alloc() for more details.
216 *
217 * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
218 */
219struct mux_chip *devm_mux_chip_alloc(struct device *dev,
220				     unsigned int controllers,
221				     size_t sizeof_priv)
222{
223	struct mux_chip **ptr, *mux_chip;
224
225	ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
226	if (!ptr)
227		return ERR_PTR(-ENOMEM);
228
229	mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
230	if (IS_ERR(mux_chip)) {
231		devres_free(ptr);
232		return mux_chip;
233	}
234
235	*ptr = mux_chip;
236	devres_add(dev, ptr);
237
238	return mux_chip;
239}
240EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
241
242static void devm_mux_chip_reg_release(struct device *dev, void *res)
243{
244	struct mux_chip *mux_chip = *(struct mux_chip **)res;
245
246	mux_chip_unregister(mux_chip);
247}
248
249/**
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.
253 *
254 * See mux_chip_register() for more details.
255 *
256 * Return: Zero on success or a negative errno on error.
257 */
258int devm_mux_chip_register(struct device *dev,
259			   struct mux_chip *mux_chip)
260{
261	struct mux_chip **ptr;
262	int res;
263
264	ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
265	if (!ptr)
266		return -ENOMEM;
267
268	res = mux_chip_register(mux_chip);
269	if (res) {
270		devres_free(ptr);
271		return res;
272	}
273
274	*ptr = mux_chip;
275	devres_add(dev, ptr);
276
277	return res;
278}
279EXPORT_SYMBOL_GPL(devm_mux_chip_register);
280
281/**
282 * mux_control_states() - Query the number of multiplexer states.
283 * @mux: The mux-control to query.
284 *
285 * Return: The number of multiplexer states.
286 */
287unsigned int mux_control_states(struct mux_control *mux)
288{
289	return mux->states;
290}
291EXPORT_SYMBOL_GPL(mux_control_states);
292
293/*
294 * The mux->lock must be down when calling this function.
295 */
296static int __mux_control_select(struct mux_control *mux, int state)
297{
298	int ret;
299
300	if (WARN_ON(state < 0 || state >= mux->states))
301		return -EINVAL;
302
303	if (mux->cached_state == state)
304		return 0;
305
306	ret = mux_control_set(mux, state);
307	if (ret >= 0)
308		return 0;
309
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);
313
314	return ret;
315}
316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317/**
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.
 
321 *
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).
 
326 *
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.
330 *
331 * Return: 0 when the mux-control state has the requested state or a negative
332 * errno on error.
333 */
334int mux_control_select(struct mux_control *mux, unsigned int state)
 
335{
336	int ret;
337
338	ret = down_killable(&mux->lock);
339	if (ret < 0)
340		return ret;
341
342	ret = __mux_control_select(mux, state);
 
 
343
344	if (ret < 0)
345		up(&mux->lock);
346
347	return ret;
348}
349EXPORT_SYMBOL_GPL(mux_control_select);
350
351/**
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.
 
355 *
356 * On successfully selecting the mux-control state, it will be locked until
357 * mux_control_deselect() called.
358 *
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.
362 *
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.
365 */
366int mux_control_try_select(struct mux_control *mux, unsigned int state)
 
367{
368	int ret;
369
370	if (down_trylock(&mux->lock))
371		return -EBUSY;
372
373	ret = __mux_control_select(mux, state);
 
 
374
375	if (ret < 0)
376		up(&mux->lock);
377
378	return ret;
379}
380EXPORT_SYMBOL_GPL(mux_control_try_select);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
382/**
383 * mux_control_deselect() - Deselect the previously selected multiplexer state.
384 * @mux: The mux-control to deselect.
385 *
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().
389 *
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.
393 */
394int mux_control_deselect(struct mux_control *mux)
395{
396	int ret = 0;
397
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);
401
402	up(&mux->lock);
403
404	return ret;
405}
406EXPORT_SYMBOL_GPL(mux_control_deselect);
407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408/* Note this function returns a reference to the mux_chip dev. */
409static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
410{
411	struct device *dev;
412
413	dev = class_find_device_by_of_node(&mux_class, np);
414
415	return dev ? to_mux_chip(dev) : NULL;
416}
417
418/**
419 * mux_control_get() - Get the mux-control for a device.
420 * @dev: The device that needs a mux-control.
421 * @mux_name: The name identifying the mux-control.
 
 
422 *
423 * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
424 */
425struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
 
426{
427	struct device_node *np = dev->of_node;
428	struct of_phandle_args args;
429	struct mux_chip *mux_chip;
430	unsigned int controller;
431	int index = 0;
432	int ret;
433
434	if (mux_name) {
435		index = of_property_match_string(np, "mux-control-names",
436						 mux_name);
 
 
 
 
437		if (index < 0) {
438			dev_err(dev, "mux controller '%s' not found\n",
439				mux_name);
440			return ERR_PTR(index);
441		}
442	}
443
444	ret = of_parse_phandle_with_args(np,
445					 "mux-controls", "#mux-control-cells",
446					 index, &args);
 
 
 
 
 
447	if (ret) {
448		dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
449			np, mux_name ?: "", index);
450		return ERR_PTR(ret);
451	}
452
453	mux_chip = of_find_mux_chip_by_node(args.np);
454	of_node_put(args.np);
455	if (!mux_chip)
456		return ERR_PTR(-EPROBE_DEFER);
457
458	if (args.args_count > 1 ||
459	    (!args.args_count && (mux_chip->controllers > 1))) {
460		dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
461			np, args.np);
462		put_device(&mux_chip->dev);
463		return ERR_PTR(-EINVAL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464	}
465
466	controller = 0;
467	if (args.args_count)
468		controller = args.args[0];
469
470	if (controller >= mux_chip->controllers) {
471		dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
472			np, controller, args.np);
473		put_device(&mux_chip->dev);
474		return ERR_PTR(-EINVAL);
475	}
476
477	return &mux_chip->mux[controller];
478}
 
 
 
 
 
 
 
 
 
 
 
 
479EXPORT_SYMBOL_GPL(mux_control_get);
480
481/**
482 * mux_control_put() - Put away the mux-control for good.
483 * @mux: The mux-control to put away.
484 *
485 * mux_control_put() reverses the effects of mux_control_get().
486 */
487void mux_control_put(struct mux_control *mux)
488{
489	put_device(&mux->chip->dev);
490}
491EXPORT_SYMBOL_GPL(mux_control_put);
492
493static void devm_mux_control_release(struct device *dev, void *res)
494{
495	struct mux_control *mux = *(struct mux_control **)res;
496
497	mux_control_put(mux);
498}
499
500/**
501 * devm_mux_control_get() - Get the mux-control for a device, with resource
502 *			    management.
503 * @dev: The device that needs a mux-control.
504 * @mux_name: The name identifying the mux-control.
505 *
506 * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
507 */
508struct mux_control *devm_mux_control_get(struct device *dev,
509					 const char *mux_name)
510{
511	struct mux_control **ptr, *mux;
512
513	ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
514	if (!ptr)
515		return ERR_PTR(-ENOMEM);
516
517	mux = mux_control_get(dev, mux_name);
518	if (IS_ERR(mux)) {
519		devres_free(ptr);
520		return mux;
521	}
522
523	*ptr = mux;
524	devres_add(dev, ptr);
525
526	return mux;
527}
528EXPORT_SYMBOL_GPL(devm_mux_control_get);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
530/*
531 * Using subsys_initcall instead of module_init here to try to ensure - for
532 * the non-modular case - that the subsystem is initialized when mux consumers
533 * and mux controllers start to use it.
534 * For the modular case, the ordering is ensured with module dependencies.
535 */
536subsys_initcall(mux_init);
537module_exit(mux_exit);
538
539MODULE_DESCRIPTION("Multiplexer subsystem");
540MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
541MODULE_LICENSE("GPL v2");