Add support for array and optional APIs
cover Add support for array and optional APIs
These patches introduces APIs for managing multiple mux-states using an array. Since for mux-control the state is not defined in DT and needs to passed when selecting in driver, array like wrapper is not possible.
Also provide optional APIs which returns NULL instead of error when DT property is not present in client drivers. Ensure that select/deselect operations pass for such cases.
Aniket (2):
mux: Add support for handling array of mux-state
mux: Introduce optional variant of consumer APIs
drivers/mux/core.c | 185 +++++++++++++++++++++++++++++++++-- include/linux/mux/consumer.h | 29 ++++++ 2 files changed, 208 insertions(+), 6 deletions(-)
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
PATCH 1/2 mux: Add support for handling array of mux-state
Sometimes consumer needs to manage a number of mux- states, so it is easier if they can control it using an array of such mux-states.
Add support to devm_get, select and deselect the array.
--- drivers/mux/core.c | 161 +++++++++++++++++++++++++++++++++-- include/linux/mux/consumer.h | 7 ++ 2 files changed, 162 insertions(+), 6 deletions(-)
drivers/mux/core.c +155 -6
@@ -43,6 +43,16 @@ struct mux_state {unsigned int state; }; +/** +* struct mux_state_array - Represents an array of mux states +* @num: number of mux state +* @mstate: array of mux states +*/ +struct mux_state_array { +unsigned int num; +struct mux_state *mstate[]; +}; +static struct class mux_class = { .name ="mux" , };
@@ -408,6 +418,44 @@ int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us) }EXPORT_SYMBOL_GPL (mux_state_select_delay); +/** +* mux_state_array_select() - Select the given multiplexer state. +* @mstates: The mux-state-array which contains multiple mux-state to select. +* +* On successfully selecting all the mux-states, their mux-control will be +* locked until there is a call to mux_state_array_deselect(). +* If a mux-controls is already selected when mux_state_array_select() is +* called, then caller will be blocked. +* +* Therefore, make sure to call mux_state_array_deselect() when the operation +* is complete and the mux-state-array is free for others to use, but do not +* call mux_state_array_deselect() if mux_state_array_select() fails. +* If a mux_state_array_select() fails inbetween the already selected +* mux-states in the array are deselected. +* +* Return: 0 when the mux-state has been selected or a negative +* errno on error. +*/ +int mux_state_array_select (struct mux_state_array *mstates) +{ +int ret, i; + +for (i =0 ; i < mstates->num; ++i) { + ret =mux_state_select (mstates->mstate[i]); +if (ret <0 ) + goto err_mstates; + } + +return 0 ; + +err_mstates: +while (--i >=0 ) +mux_state_deselect (mstates->mstate[i]); + +return ret; +} +EXPORT_SYMBOL_GPL (mux_state_array_select); +/** * mux_control_try_select_delay() - Try to select the given multiplexer state. * @mux: The mux-control to request a change of state from.
@@ -508,6 +556,29 @@ int mux_state_deselect(struct mux_state *mstate) }EXPORT_SYMBOL_GPL (mux_state_deselect); +/** +* mux_state_array_deselect() - Deselect the previously selected multiplexer state. +* @mstates: Array of mux-states to deselect. +* +* It is required that a single call is made to mux_state_array_deselect() for +* each and every successful call made to either of mux_state_array_select(). +* +* Return: 0 on success and a negative errno on error. +*/ +int mux_state_array_deselect (struct mux_state_array *mstates) +{ +int ret, i; + +for (i =0 ; i < mstates->num; ++i) { + ret =mux_state_deselect (mstates->mstate[i]); +if (ret <0 ) +return ret; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (mux_state_array_deselect); +/* 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) {
@@ -522,19 +593,19 @@ 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. +* @index: Index to identify mux-control if mux-name is NULL. * @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) +int index,unsigned int *state) {struct device_node *np = dev->of_node;struct of_phandle_args args;struct mux_chip *mux_chip;unsigned int controller; -int index =0 ;int ret;if (mux_name) {
@@ -619,7 +690,7 @@ static struct mux_control *mux_get(struct device *dev, const char *mux_name,*/ struct mux_control *mux_control_get (struct device *dev,const char *mux_name) { -return mux_get (dev, mux_name,NULL ); +return mux_get (dev, mux_name,0 ,NULL ); }EXPORT_SYMBOL_GPL (mux_control_get);
@@ -676,10 +747,12 @@ EXPORT_SYMBOL_GPL(devm_mux_control_get);* 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. +* @index: Index of the mux-state, to be used when mux-name is NULL * * 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) +static struct mux_state *mux_state_get (struct device *dev,const char *mux_name, +int index) {struct mux_state *mstate;
@@ -687,7 +760,7 @@ static struct mux_state *mux_state_get(struct device *dev, const char *mux_name)if (!mstate)return ERR_PTR (-ENOMEM ); - mstate->mux =mux_get (dev, mux_name, &mstate->state); + mstate->mux =mux_get (dev, mux_name, index, &mstate->state);if (IS_ERR (mstate->mux)) {int err =PTR_ERR (mstate->mux);
@@ -734,7 +807,7 @@ struct mux_state *devm_mux_state_get(struct device *dev,if (!ptr)return ERR_PTR (-ENOMEM ); - mstate =mux_state_get (dev, mux_name); + mstate =mux_state_get (dev, mux_name,0 );if (IS_ERR (mstate)) {devres_free (ptr);return mstate;
@@ -747,6 +820,82 @@ struct mux_state *devm_mux_state_get(struct device *dev, }EXPORT_SYMBOL_GPL (devm_mux_state_get); +/* +* mux_state_array_get() - Get the all the mux-states for a device. +* @dev: The device that needs a mux-state. +* +* Return: A pointer to the mux-state, or an ERR_PTR with a negative errno. +*/ +static struct mux_state_array *mux_state_array_get (struct device *dev) +{ +struct mux_state_array *mstates; +struct mux_state *mstate; +int num, i; + + num =of_count_phandle_with_args (dev->of_node,"mux-states" ,"#mux-state-cells" ); +if (num <0 ) +return ERR_PTR (num); + + mstates =kzalloc (struct_size (mstates, mstate, num),GFP_KERNEL ); +if (!mstates) +return ERR_PTR (-ENOMEM ); + +for (i =0 ; i < num; i++) { + mstate =mux_state_get (dev,NULL , i); +if (IS_ERR (mstate)) + goto err_mstates; + mstates->mstate[i] = mstate; + } + mstates->num = num; + +return mstates; + +err_mstates: +while (--i >=0 ) +mux_state_put (mstates->mstate[i]); +kfree (mstates); + +return ERR_PTR (PTR_ERR (mstate)); +} + +static void devm_mux_state_array_release (struct device *dev,void *res) +{ +int i; +struct mux_state_array *mstates = *(struct mux_state_array **)res; + +for (i =0 ; i < mstates->num; i++) +mux_state_put (mstates->mstate[i]); +kfree (mstates); +} + +/** +* devm_mux_state_array_get() - Get the all the mux-states for a device, with +* resource management. +* @dev: The device that needs a mux-control. +* +* Return: Pointer to the mux-state-array, or an ERR_PTR with a negative errno. +*/ +struct mux_state_array *devm_mux_state_array_get (struct device *dev) +{ +struct mux_state_array **ptr, *mstates; + + ptr =devres_alloc (devm_mux_state_array_release,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return ERR_PTR (-ENOMEM ); + + mstates =mux_state_array_get (dev); +if (IS_ERR (mstates)) { +devres_free (ptr); +return mstates; + } + + *ptr = mstates; +devres_add (dev, ptr); + +return mstates; +} +EXPORT_SYMBOL_GPL (devm_mux_state_array_get); +/* * 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
include/linux/mux/consumer.h +7 -0
@@ -15,6 +15,7 @@struct device ;struct mux_control ;struct mux_state ; +struct mux_state_array ;unsigned int mux_control_states (struct mux_control *mux); int __must_check mux_control_select_delay(struct mux_control *mux,
@@ -39,6 +40,8 @@ static inline int __must_check mux_state_select(struct mux_state *mstate)return mux_state_select_delay (mstate,0 ); } +int mux_state_array_select (struct mux_state_array *mstates); +static inline int __must_check mux_control_try_select (struct mux_control *mux,unsigned int state) {
@@ -53,6 +56,8 @@ static inline int __must_check mux_state_try_select(struct mux_state *mstate)int mux_control_deselect (struct mux_control *mux);int mux_state_deselect (struct mux_state *mstate); +int mux_state_array_deselect (struct mux_state_array *mstates); +struct mux_control *mux_control_get (struct device *dev,const char *mux_name);void mux_control_put (struct mux_control *mux);
@@ -61,4 +66,6 @@ struct mux_control *devm_mux_control_get(struct device *dev,struct mux_state *devm_mux_state_get (struct device *dev,const char *mux_name); +struct mux_state_array *devm_mux_state_array_get (struct device *dev); +#endif /* _LINUX_MUX_CONSUMER_H */
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
PATCH 2/2 mux: Introduce optional variant of consumer APIs
In reply to AniketAdd optional variant of get APIs for mux-control,
mux-state and mux-state-array.
These APIs allow client drivers to not fail in
case mux is not present on the platform.
--- drivers/mux/core.c | 24 ++++++++++++++++++++++++ include/linux/mux/consumer.h | 22 ++++++++++++++++++++++ 2 files changed, 46 insertions(+)
drivers/mux/core.c +24 -0
@@ -379,6 +379,9 @@ int mux_control_select_delay(struct mux_control *mux, unsigned int state, {int ret; +if (!mux) +return 0 ; + ret =down_killable (&mux->lock);if (ret <0 )return ret;
@@ -414,6 +417,9 @@ EXPORT_SYMBOL_GPL(mux_control_select_delay);*/ int mux_state_select_delay (struct mux_state *mstate,unsigned int delay_us) { +if (!mstate) +return 0 ; +return mux_control_select_delay (mstate->mux, mstate->state, delay_us); }EXPORT_SYMBOL_GPL (mux_state_select_delay);
@@ -440,6 +446,9 @@ int mux_state_array_select(struct mux_state_array *mstates) {int ret, i; +if (!mstates) +return 0 ; +for (i =0 ; i < mstates->num; ++i) { ret =mux_state_select (mstates->mstate[i]);if (ret <0 )
@@ -477,6 +486,9 @@ int mux_control_try_select_delay(struct mux_control *mux, unsigned int state, {int ret; +if (!mux) +return 0 ; +if (down_trylock (&mux->lock))return -EBUSY ;
@@ -508,6 +520,9 @@ EXPORT_SYMBOL_GPL(mux_control_try_select_delay);*/ int mux_state_try_select_delay (struct mux_state *mstate,unsigned int delay_us) { +if (!mstate) +return 0 ; +return mux_control_try_select_delay (mstate->mux, mstate->state, delay_us); }EXPORT_SYMBOL_GPL (mux_state_try_select_delay);
@@ -528,6 +543,9 @@ int mux_control_deselect(struct mux_control *mux) {int ret =0 ; +if (!mux) +return 0 ; +if (mux->idle_state !=MUX_IDLE_AS_IS && mux->idle_state != mux->cached_state) ret =mux_control_set (mux, mux->idle_state);
@@ -552,6 +570,9 @@ EXPORT_SYMBOL_GPL(mux_control_deselect);*/ int mux_state_deselect (struct mux_state *mstate) { +if (!mstate) +return 0 ; +return mux_control_deselect (mstate->mux); }EXPORT_SYMBOL_GPL (mux_state_deselect);
@@ -569,6 +590,9 @@ int mux_state_array_deselect(struct mux_state_array *mstates) {int ret, i; +if (!mstates) +return 0 ; +for (i =0 ; i < mstates->num; ++i) { ret =mux_state_deselect (mstates->mstate[i]);if (ret <0 )
include/linux/mux/consumer.h +22 -0
@@ -63,9 +63,31 @@ void mux_control_put(struct mux_control *mux);struct mux_control *devm_mux_control_get (struct device *dev,const char *mux_name); +static inline struct mux_control * +devm_mux_control_get_optional (struct device *dev,const char *mux_name) +{ +struct mux_control *mux =devm_mux_control_get (dev, mux_name); + +return (PTR_ERR (mux) == -ENOENT ) ?NULL : mux; +} +struct mux_state *devm_mux_state_get (struct device *dev,const char *mux_name); +static inline struct mux_state * +devm_mux_state_get_optional (struct device *dev,const char *mux_name) +{ +struct mux_state *mstate =devm_mux_state_get (dev, mux_name); + +return (PTR_ERR (mstate) == -ENOENT ) ?NULL : mstate; +}struct mux_state_array *devm_mux_state_array_get (struct device *dev); +static inline struct mux_state_array * +devm_mux_state_array_get_optional (struct device *dev) +{ +struct mux_state_array *mstates =devm_mux_state_array_get (dev); + +return (PTR_ERR (mstates) == -ENOENT ) ?NULL : mstates; +}#endif /* _LINUX_MUX_CONSUMER_H */
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog