// SPDX-License-Identifier: GPL-2.0 /* * Multiplexer subsystem * * Copyright (C) 2017 Axentia Technologies AB * * Author: Peter Rosin <[email protected]> */ #define pr_fmt(fmt) … #include <linux/delay.h> #include <linux/device.h> #include <linux/err.h> #include <linux/export.h> #include <linux/idr.h> #include <linux/init.h> #include <linux/module.h> #include <linux/mux/consumer.h> #include <linux/mux/driver.h> #include <linux/of.h> #include <linux/slab.h> /* * The idle-as-is "state" is not an actual state that may be selected, it * only implies that the state should not be changed. So, use that state * as indication that the cached state of the multiplexer is unknown. */ #define MUX_CACHE_UNKNOWN … /** * struct mux_state - Represents a mux controller state specific to a given * consumer. * @mux: Pointer to a mux controller. * @state: State of the mux to be selected. * * This structure is specific to the consumer that acquires it and has * information specific to that consumer. */ struct mux_state { … }; static struct class mux_class = …; static DEFINE_IDA(mux_ida); static int __init mux_init(void) { … } static void __exit mux_exit(void) { … } static void mux_chip_release(struct device *dev) { … } static const struct device_type mux_type = …; /** * mux_chip_alloc() - Allocate a mux-chip. * @dev: The parent device implementing the mux interface. * @controllers: The number of mux controllers to allocate for this chip. * @sizeof_priv: Size of extra memory area for private use by the caller. * * After allocating the mux-chip with the desired number of mux controllers * but before registering the chip, the mux driver is required to configure * the number of valid mux states in the mux_chip->mux[N].states members and * the desired idle state in the returned mux_chip->mux[N].idle_state members. * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to * provide a pointer to the operations struct in the mux_chip->ops member * before registering the mux-chip with mux_chip_register. * * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno. */ struct mux_chip *mux_chip_alloc(struct device *dev, unsigned int controllers, size_t sizeof_priv) { … } EXPORT_SYMBOL_GPL(…); static int mux_control_set(struct mux_control *mux, int state) { … } /** * mux_chip_register() - Register a mux-chip, thus readying the controllers * for use. * @mux_chip: The mux-chip to register. * * Do not retry registration of the same mux-chip on failure. You should * instead put it away with mux_chip_free() and allocate a new one, if you * for some reason would like to retry registration. * * Return: Zero on success or a negative errno on error. */ int mux_chip_register(struct mux_chip *mux_chip) { … } EXPORT_SYMBOL_GPL(…); /** * mux_chip_unregister() - Take the mux-chip off-line. * @mux_chip: The mux-chip to unregister. * * mux_chip_unregister() reverses the effects of mux_chip_register(). * But not completely, you should not try to call mux_chip_register() * on a mux-chip that has been registered before. */ void mux_chip_unregister(struct mux_chip *mux_chip) { … } EXPORT_SYMBOL_GPL(…); /** * mux_chip_free() - Free the mux-chip for good. * @mux_chip: The mux-chip to free. * * mux_chip_free() reverses the effects of mux_chip_alloc(). */ void mux_chip_free(struct mux_chip *mux_chip) { … } EXPORT_SYMBOL_GPL(…); static void devm_mux_chip_release(struct device *dev, void *res) { … } /** * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc(). * @dev: The parent device implementing the mux interface. * @controllers: The number of mux controllers to allocate for this chip. * @sizeof_priv: Size of extra memory area for private use by the caller. * * See mux_chip_alloc() for more details. * * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno. */ struct mux_chip *devm_mux_chip_alloc(struct device *dev, unsigned int controllers, size_t sizeof_priv) { … } EXPORT_SYMBOL_GPL(…); static void devm_mux_chip_reg_release(struct device *dev, void *res) { … } /** * devm_mux_chip_register() - Resource-managed version mux_chip_register(). * @dev: The parent device implementing the mux interface. * @mux_chip: The mux-chip to register. * * See mux_chip_register() for more details. * * Return: Zero on success or a negative errno on error. */ int devm_mux_chip_register(struct device *dev, struct mux_chip *mux_chip) { … } EXPORT_SYMBOL_GPL(…); /** * mux_control_states() - Query the number of multiplexer states. * @mux: The mux-control to query. * * Return: The number of multiplexer states. */ unsigned int mux_control_states(struct mux_control *mux) { … } EXPORT_SYMBOL_GPL(…); /* * The mux->lock must be down when calling this function. */ static int __mux_control_select(struct mux_control *mux, int state) { … } static void mux_control_delay(struct mux_control *mux, unsigned int delay_us) { … } /** * mux_control_select_delay() - Select the given multiplexer state. * @mux: The mux-control to request a change of state from. * @state: The new requested state. * @delay_us: The time to delay (in microseconds) if the mux state is changed. * * On successfully selecting the mux-control state, it will be locked until * there is a call to mux_control_deselect(). If the mux-control is already * selected when mux_control_select() is called, the caller will be blocked * until mux_control_deselect() or mux_state_deselect() is called (by someone * else). * * Therefore, make sure to call mux_control_deselect() when the operation is * complete and the mux-control is free for others to use, but do not call * mux_control_deselect() if mux_control_select() fails. * * Return: 0 when the mux-control state has the requested state or a negative * errno on error. */ int mux_control_select_delay(struct mux_control *mux, unsigned int state, unsigned int delay_us) { … } EXPORT_SYMBOL_GPL(…); /** * mux_state_select_delay() - Select the given multiplexer state. * @mstate: The mux-state to select. * @delay_us: The time to delay (in microseconds) if the mux state is changed. * * On successfully selecting the mux-state, its mux-control will be locked * until there is a call to mux_state_deselect(). If the mux-control is already * selected when mux_state_select() is called, the caller will be blocked * until mux_state_deselect() or mux_control_deselect() is called (by someone * else). * * Therefore, make sure to call mux_state_deselect() when the operation is * complete and the mux-control is free for others to use, but do not call * mux_state_deselect() if mux_state_select() fails. * * Return: 0 when the mux-state has been selected or a negative * errno on error. */ int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us) { … } EXPORT_SYMBOL_GPL(…); /** * mux_control_try_select_delay() - Try to select the given multiplexer state. * @mux: The mux-control to request a change of state from. * @state: The new requested state. * @delay_us: The time to delay (in microseconds) if the mux state is changed. * * On successfully selecting the mux-control state, it will be locked until * mux_control_deselect() is called. * * Therefore, make sure to call mux_control_deselect() when the operation is * complete and the mux-control is free for others to use, but do not call * mux_control_deselect() if mux_control_try_select() fails. * * Return: 0 when the mux-control state has the requested state or a negative * errno on error. Specifically -EBUSY if the mux-control is contended. */ int mux_control_try_select_delay(struct mux_control *mux, unsigned int state, unsigned int delay_us) { … } EXPORT_SYMBOL_GPL(…); /** * mux_state_try_select_delay() - Try to select the given multiplexer state. * @mstate: The mux-state to select. * @delay_us: The time to delay (in microseconds) if the mux state is changed. * * On successfully selecting the mux-state, its mux-control will be locked * until mux_state_deselect() is called. * * Therefore, make sure to call mux_state_deselect() when the operation is * complete and the mux-control is free for others to use, but do not call * mux_state_deselect() if mux_state_try_select() fails. * * Return: 0 when the mux-state has been selected or a negative errno on * error. Specifically -EBUSY if the mux-control is contended. */ int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us) { … } EXPORT_SYMBOL_GPL(…); /** * mux_control_deselect() - Deselect the previously selected multiplexer state. * @mux: The mux-control to deselect. * * It is required that a single call is made to mux_control_deselect() for * each and every successful call made to either of mux_control_select() or * mux_control_try_select(). * * Return: 0 on success and a negative errno on error. An error can only * occur if the mux has an idle state. Note that even if an error occurs, the * mux-control is unlocked and is thus free for the next access. */ int mux_control_deselect(struct mux_control *mux) { … } EXPORT_SYMBOL_GPL(…); /** * mux_state_deselect() - Deselect the previously selected multiplexer state. * @mstate: The mux-state to deselect. * * It is required that a single call is made to mux_state_deselect() for * each and every successful call made to either of mux_state_select() or * mux_state_try_select(). * * Return: 0 on success and a negative errno on error. An error can only * occur if the mux has an idle state. Note that even if an error occurs, the * mux-control is unlocked and is thus free for the next access. */ int mux_state_deselect(struct mux_state *mstate) { … } EXPORT_SYMBOL_GPL(…); /* Note this function returns a reference to the mux_chip dev. */ static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np) { … } /* * mux_get() - Get the mux-control for a device. * @dev: The device that needs a mux-control. * @mux_name: The name identifying the mux-control. * @state: Pointer to where the requested state is returned, or NULL when * the required multiplexer states are handled by other means. * * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno. */ static struct mux_control *mux_get(struct device *dev, const char *mux_name, unsigned int *state) { … } /** * mux_control_get() - Get the mux-control for a device. * @dev: The device that needs a mux-control. * @mux_name: The name identifying the mux-control. * * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno. */ struct mux_control *mux_control_get(struct device *dev, const char *mux_name) { … } EXPORT_SYMBOL_GPL(…); /** * mux_control_put() - Put away the mux-control for good. * @mux: The mux-control to put away. * * mux_control_put() reverses the effects of mux_control_get(). */ void mux_control_put(struct mux_control *mux) { … } EXPORT_SYMBOL_GPL(…); static void devm_mux_control_release(struct device *dev, void *res) { … } /** * devm_mux_control_get() - Get the mux-control for a device, with resource * management. * @dev: The device that needs a mux-control. * @mux_name: The name identifying the mux-control. * * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno. */ struct mux_control *devm_mux_control_get(struct device *dev, const char *mux_name) { … } EXPORT_SYMBOL_GPL(…); /* * mux_state_get() - Get the mux-state for a device. * @dev: The device that needs a mux-state. * @mux_name: The name identifying the mux-state. * * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno. */ static struct mux_state *mux_state_get(struct device *dev, const char *mux_name) { … } /* * mux_state_put() - Put away the mux-state for good. * @mstate: The mux-state to put away. * * mux_state_put() reverses the effects of mux_state_get(). */ static void mux_state_put(struct mux_state *mstate) { … } static void devm_mux_state_release(struct device *dev, void *res) { … } /** * devm_mux_state_get() - Get the mux-state for a device, with resource * management. * @dev: The device that needs a mux-control. * @mux_name: The name identifying the mux-control. * * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno. */ struct mux_state *devm_mux_state_get(struct device *dev, const char *mux_name) { … } EXPORT_SYMBOL_GPL(…); /* * Using subsys_initcall instead of module_init here to try to ensure - for * the non-modular case - that the subsystem is initialized when mux consumers * and mux controllers start to use it. * For the modular case, the ordering is ensured with module dependencies. */ subsys_initcall(mux_init); module_exit(mux_exit); MODULE_DESCRIPTION(…) …; MODULE_AUTHOR(…) …; MODULE_LICENSE(…) …;