[PATCH v1 2/2] mux: Introduce optional variant of consumer APIs
From: Aniket
Date: Thu Aug 24 2023 - 08:19:10 EST
Add 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.
Signed-off-by: Aniket <aniketmaurya@xxxxxxxxxx>
---
drivers/mux/core.c | 24 ++++++++++++++++++++++++
include/linux/mux/consumer.h | 22 ++++++++++++++++++++++
2 files changed, 46 insertions(+)
diff --git a/drivers/mux/core.c b/drivers/mux/core.c
index e4e57847e904..a6772f23ef6e 100644
--- a/drivers/mux/core.c
+++ b/drivers/mux/core.c
@@ -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)
diff --git a/include/linux/mux/consumer.h b/include/linux/mux/consumer.h
index da15d0542008..397b1c5848c7 100644
--- a/include/linux/mux/consumer.h
+++ b/include/linux/mux/consumer.h
@@ -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 */
--
2.42.0.rc1.204.g551eb34607-goog