Skip to content
Snippets Groups Projects
Commit fa934c06 authored by Stephen Boyd's avatar Stephen Boyd Committed by Peter Rosin
Browse files

mux: add mux_control_get_optional() API


Sometimes drivers only use muxes under certain scenarios. For
example, the chipidea usb controller may be connected to a usb
switch on some platforms, and connected directly to a usb port on
others. The driver won't know one way or the other though, so add
a mux_control_get_optional() API that allows the driver to
differentiate errors getting the mux from there not being a mux
for the driver to use at all.

Signed-off-by: default avatarStephen Boyd <stephen.boyd@linaro.org>
[minor adjustments to logic, comments and made a function static]
Signed-off-by: default avatarPeter Rosin <peda@axentia.se>
parent 4a3928c6
No related branches found
No related tags found
No related merge requests found
...@@ -344,6 +344,7 @@ MUX ...@@ -344,6 +344,7 @@ MUX
devm_mux_chip_alloc() devm_mux_chip_alloc()
devm_mux_chip_register() devm_mux_chip_register()
devm_mux_control_get() devm_mux_control_get()
devm_mux_control_get_optional()
PER-CPU MEM PER-CPU MEM
devm_alloc_percpu() devm_alloc_percpu()
......
...@@ -286,6 +286,9 @@ EXPORT_SYMBOL_GPL(devm_mux_chip_register); ...@@ -286,6 +286,9 @@ EXPORT_SYMBOL_GPL(devm_mux_chip_register);
*/ */
unsigned int mux_control_states(struct mux_control *mux) unsigned int mux_control_states(struct mux_control *mux)
{ {
if (!mux)
return 0;
return mux->states; return mux->states;
} }
EXPORT_SYMBOL_GPL(mux_control_states); EXPORT_SYMBOL_GPL(mux_control_states);
...@@ -335,6 +338,9 @@ int mux_control_select(struct mux_control *mux, unsigned int state) ...@@ -335,6 +338,9 @@ int mux_control_select(struct mux_control *mux, unsigned int state)
{ {
int ret; int ret;
if (!mux)
return 0;
ret = down_killable(&mux->lock); ret = down_killable(&mux->lock);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -367,6 +373,9 @@ int mux_control_try_select(struct mux_control *mux, unsigned int state) ...@@ -367,6 +373,9 @@ int mux_control_try_select(struct mux_control *mux, unsigned int state)
{ {
int ret; int ret;
if (!mux)
return 0;
if (down_trylock(&mux->lock)) if (down_trylock(&mux->lock))
return -EBUSY; return -EBUSY;
...@@ -395,6 +404,9 @@ int mux_control_deselect(struct mux_control *mux) ...@@ -395,6 +404,9 @@ int mux_control_deselect(struct mux_control *mux)
{ {
int ret = 0; int ret = 0;
if (!mux)
return 0;
if (mux->idle_state != MUX_IDLE_AS_IS && if (mux->idle_state != MUX_IDLE_AS_IS &&
mux->idle_state != mux->cached_state) mux->idle_state != mux->cached_state)
ret = mux_control_set(mux, mux->idle_state); ret = mux_control_set(mux, mux->idle_state);
...@@ -420,14 +432,8 @@ static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np) ...@@ -420,14 +432,8 @@ static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
return dev ? to_mux_chip(dev) : NULL; return dev ? to_mux_chip(dev) : NULL;
} }
/** static struct mux_control *
* mux_control_get() - Get the mux-control for a device. __mux_control_get(struct device *dev, const char *mux_name, bool optional)
* @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)
{ {
struct device_node *np = dev->of_node; struct device_node *np = dev->of_node;
struct of_phandle_args args; struct of_phandle_args args;
...@@ -439,16 +445,22 @@ struct mux_control *mux_control_get(struct device *dev, const char *mux_name) ...@@ -439,16 +445,22 @@ struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
if (mux_name) { if (mux_name) {
index = of_property_match_string(np, "mux-control-names", index = of_property_match_string(np, "mux-control-names",
mux_name); mux_name);
if ((index == -EINVAL || index == -ENODATA) && optional)
return NULL;
if (index < 0) { if (index < 0) {
dev_err(dev, "mux controller '%s' not found\n", dev_err(dev, "mux controller '%s' not found\n",
mux_name); mux_name);
return ERR_PTR(index); return ERR_PTR(index);
} }
/* OF does point to a mux, so it's no longer optional */
optional = false;
} }
ret = of_parse_phandle_with_args(np, ret = of_parse_phandle_with_args(np,
"mux-controls", "#mux-control-cells", "mux-controls", "#mux-control-cells",
index, &args); index, &args);
if (ret == -ENOENT && optional)
return NULL;
if (ret) { if (ret) {
dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n", dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
np, mux_name ?: "", index); np, mux_name ?: "", index);
...@@ -481,8 +493,35 @@ struct mux_control *mux_control_get(struct device *dev, const char *mux_name) ...@@ -481,8 +493,35 @@ struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
return &mux_chip->mux[controller]; return &mux_chip->mux[controller];
} }
/**
* 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)
{
return __mux_control_get(dev, mux_name, false);
}
EXPORT_SYMBOL_GPL(mux_control_get); EXPORT_SYMBOL_GPL(mux_control_get);
/**
* mux_control_get_optional() - Get the optional mux-control for a device.
* @dev: The device that needs a mux-control.
* @mux_name: The name identifying the mux-control.
*
* Return: NULL if no mux with the provided name is found, a pointer to
* the named mux-control or an ERR_PTR with a negative errno.
*/
struct mux_control *
mux_control_get_optional(struct device *dev, const char *mux_name)
{
return __mux_control_get(dev, mux_name, true);
}
EXPORT_SYMBOL_GPL(mux_control_get_optional);
/** /**
* mux_control_put() - Put away the mux-control for good. * mux_control_put() - Put away the mux-control for good.
* @mux: The mux-control to put away. * @mux: The mux-control to put away.
...@@ -491,6 +530,9 @@ EXPORT_SYMBOL_GPL(mux_control_get); ...@@ -491,6 +530,9 @@ EXPORT_SYMBOL_GPL(mux_control_get);
*/ */
void mux_control_put(struct mux_control *mux) void mux_control_put(struct mux_control *mux)
{ {
if (!mux)
return;
put_device(&mux->chip->dev); put_device(&mux->chip->dev);
} }
EXPORT_SYMBOL_GPL(mux_control_put); EXPORT_SYMBOL_GPL(mux_control_put);
...@@ -502,16 +544,8 @@ static void devm_mux_control_release(struct device *dev, void *res) ...@@ -502,16 +544,8 @@ static void devm_mux_control_release(struct device *dev, void *res)
mux_control_put(mux); mux_control_put(mux);
} }
/** static struct mux_control *
* devm_mux_control_get() - Get the mux-control for a device, with resource __devm_mux_control_get(struct device *dev, const char *mux_name, bool optional)
* 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)
{ {
struct mux_control **ptr, *mux; struct mux_control **ptr, *mux;
...@@ -519,8 +553,8 @@ struct mux_control *devm_mux_control_get(struct device *dev, ...@@ -519,8 +553,8 @@ struct mux_control *devm_mux_control_get(struct device *dev,
if (!ptr) if (!ptr)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
mux = mux_control_get(dev, mux_name); mux = __mux_control_get(dev, mux_name, optional);
if (IS_ERR(mux)) { if (IS_ERR_OR_NULL(mux)) {
devres_free(ptr); devres_free(ptr);
return mux; return mux;
} }
...@@ -530,8 +564,38 @@ struct mux_control *devm_mux_control_get(struct device *dev, ...@@ -530,8 +564,38 @@ struct mux_control *devm_mux_control_get(struct device *dev,
return mux; return mux;
} }
/**
* 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)
{
return __devm_mux_control_get(dev, mux_name, false);
}
EXPORT_SYMBOL_GPL(devm_mux_control_get); EXPORT_SYMBOL_GPL(devm_mux_control_get);
/**
* devm_mux_control_get_optional() - Get the optional 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: NULL if no mux with the provided name is found, a pointer to
* the named mux-control or an ERR_PTR with a negative errno.
*/
struct mux_control *
devm_mux_control_get_optional(struct device *dev, const char *mux_name)
{
return __devm_mux_control_get(dev, mux_name, true);
}
EXPORT_SYMBOL_GPL(devm_mux_control_get_optional);
/* /*
* Using subsys_initcall instead of module_init here to try to ensure - for * 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 * the non-modular case - that the subsystem is initialized when mux consumers
......
...@@ -23,9 +23,13 @@ int __must_check mux_control_try_select(struct mux_control *mux, ...@@ -23,9 +23,13 @@ int __must_check mux_control_try_select(struct mux_control *mux,
int mux_control_deselect(struct mux_control *mux); int mux_control_deselect(struct mux_control *mux);
struct mux_control *mux_control_get(struct device *dev, const char *mux_name); struct mux_control *mux_control_get(struct device *dev, const char *mux_name);
struct mux_control *mux_control_get_optional(struct device *dev,
const char *mux_name);
void mux_control_put(struct mux_control *mux); void mux_control_put(struct mux_control *mux);
struct mux_control *devm_mux_control_get(struct device *dev, struct mux_control *devm_mux_control_get(struct device *dev,
const char *mux_name); const char *mux_name);
struct mux_control *devm_mux_control_get_optional(struct device *dev,
const char *mux_name);
#endif /* _LINUX_MUX_CONSUMER_H */ #endif /* _LINUX_MUX_CONSUMER_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment