123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
#define pr_fmt(fmt) "mux-core: " 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>
#define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
struct mux_state {
struct mux_control *mux;
unsigned int state;
};
static const struct class mux_class = {
.name = "mux",
};
struct devm_mux_state_state {
struct mux_state *mstate;
int (*exit)(struct mux_state *mstate);
};
static DEFINE_IDA(mux_ida);
static int __init mux_init(void)
{
ida_init(&mux_ida);
return class_register(&mux_class);
}
static void __exit mux_exit(void)
{
class_unregister(&mux_class);
ida_destroy(&mux_ida);
}
static void mux_chip_release(struct device *dev)
{
struct mux_chip *mux_chip = to_mux_chip(dev);
ida_free(&mux_ida, mux_chip->id);
kfree(mux_chip);
}
static const struct device_type mux_type = {
.name = "mux-chip",
.release = mux_chip_release,
};
struct mux_chip *mux_chip_alloc(struct device *dev,
unsigned int controllers, size_t sizeof_priv)
{
struct mux_chip *mux_chip;
int i;
if (WARN_ON(!dev || !controllers))
return ERR_PTR(-EINVAL);
mux_chip = kzalloc(size_add(struct_size(mux_chip, mux, controllers),
sizeof_priv),
GFP_KERNEL);
if (!mux_chip)
return ERR_PTR(-ENOMEM);
mux_chip->dev.class = &mux_class;
mux_chip->dev.type = &mux_type;
mux_chip->dev.parent = dev;
mux_chip->dev.of_node = dev->of_node;
dev_set_drvdata(&mux_chip->dev, mux_chip);
mux_chip->id = ida_alloc(&mux_ida, GFP_KERNEL);
if (mux_chip->id < 0) {
int err = mux_chip->id;
pr_err("muxchipX failed to get a device id\n");
kfree(mux_chip);
return ERR_PTR(err);
}
dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
mux_chip->controllers = controllers;
for (i = 0; i < controllers; ++i) {
struct mux_control *mux = &mux_chip->mux[i];
mux->chip = mux_chip;
sema_init(&mux->lock, 1);
mux->cached_state = MUX_CACHE_UNKNOWN;
mux->idle_state = MUX_IDLE_AS_IS;
mux->last_change = ktime_get();
}
device_initialize(&mux_chip->dev);
return mux_chip;
}
EXPORT_SYMBOL_GPL(mux_chip_alloc);
static int mux_control_set(struct mux_control *mux, int state)
{
int ret = mux->chip->ops->set(mux, state);
mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
if (ret >= 0)
mux->last_change = ktime_get();
return ret;
}
int mux_chip_register(struct mux_chip *mux_chip)
{
int i;
int ret;
for (i = 0; i < mux_chip->controllers; ++i) {
struct mux_control *mux = &mux_chip->mux[i];
if (mux->idle_state == mux->cached_state)
continue;
ret = mux_control_set(mux, mux->idle_state);
if (ret < 0) {
dev_err(&mux_chip->dev, "unable to set idle state\n");
return ret;
}
}
ret = device_add(&mux_chip->dev);
if (ret < 0)
dev_err(&mux_chip->dev,
"device_add failed in %s: %d\n", __func__, ret);
return ret;
}
EXPORT_SYMBOL_GPL(mux_chip_register);
void mux_chip_unregister(struct mux_chip *mux_chip)
{
device_del(&mux_chip->dev);
}
EXPORT_SYMBOL_GPL(mux_chip_unregister);
void mux_chip_free(struct mux_chip *mux_chip)
{
if (!mux_chip)
return;
put_device(&mux_chip->dev);
}
EXPORT_SYMBOL_GPL(mux_chip_free);
static void devm_mux_chip_release(struct device *dev, void *res)
{
struct mux_chip *mux_chip = *(struct mux_chip **)res;
mux_chip_free(mux_chip);
}
struct mux_chip *devm_mux_chip_alloc(struct device *dev,
unsigned int controllers,
size_t sizeof_priv)
{
struct mux_chip **ptr, *mux_chip;
ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return ERR_PTR(-ENOMEM);
mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
if (IS_ERR(mux_chip)) {
devres_free(ptr);
return mux_chip;
}
*ptr = mux_chip;
devres_add(dev, ptr);
return mux_chip;
}
EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
static void devm_mux_chip_reg_release(struct device *dev, void *res)
{
struct mux_chip *mux_chip = *(struct mux_chip **)res;
mux_chip_unregister(mux_chip);
}
int devm_mux_chip_register(struct device *dev,
struct mux_chip *mux_chip)
{
struct mux_chip **ptr;
int res;
ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return -ENOMEM;
res = mux_chip_register(mux_chip);
if (res) {
devres_free(ptr);
return res;
}
*ptr = mux_chip;
devres_add(dev, ptr);
return res;
}
EXPORT_SYMBOL_GPL(devm_mux_chip_register);
unsigned int mux_control_states(struct mux_control *mux)
{
return mux->states;
}
EXPORT_SYMBOL_GPL(mux_control_states);
static int __mux_control_select(struct mux_control *mux, int state)
{
int ret;
if (WARN_ON(state < 0 || state >= mux->states))
return -EINVAL;
if (mux->cached_state == state)
return 0;
ret = mux_control_set(mux, state);
if (ret >= 0)
return 0;
if (mux->idle_state != MUX_IDLE_AS_IS)
mux_control_set(mux, mux->idle_state);
return ret;
}
static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
{
ktime_t delayend;
s64 remaining;
if (!delay_us)
return;
delayend = ktime_add_us(mux->last_change, delay_us);
remaining = ktime_us_delta(delayend, ktime_get());
if (remaining > 0)
fsleep(remaining);
}
int mux_control_select_delay(struct mux_control *mux, unsigned int state,
unsigned int delay_us)
{
int ret;
ret = down_killable(&mux->lock);
if (ret < 0)
return ret;
ret = __mux_control_select(mux, state);
if (ret >= 0)
mux_control_delay(mux, delay_us);
if (ret < 0)
up(&mux->lock);
return ret;
}
EXPORT_SYMBOL_GPL(mux_control_select_delay);
int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us)
{
return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
}
EXPORT_SYMBOL_GPL(mux_state_select_delay);
int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
unsigned int delay_us)
{
int ret;
if (down_trylock(&mux->lock))
return -EBUSY;
ret = __mux_control_select(mux, state);
if (ret >= 0)
mux_control_delay(mux, delay_us);
if (ret < 0)
up(&mux->lock);
return ret;
}
EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us)
{
return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
}
EXPORT_SYMBOL_GPL(mux_state_try_select_delay);
int mux_control_deselect(struct mux_control *mux)
{
int ret = 0;
if (mux->idle_state != MUX_IDLE_AS_IS &&
mux->idle_state != mux->cached_state)
ret = mux_control_set(mux, mux->idle_state);
up(&mux->lock);
return ret;
}
EXPORT_SYMBOL_GPL(mux_control_deselect);
int mux_state_deselect(struct mux_state *mstate)
{
return mux_control_deselect(mstate->mux);
}
EXPORT_SYMBOL_GPL(mux_state_deselect);
static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
{
struct device *dev;
dev = class_find_device_by_of_node(&mux_class, np);
return dev ? to_mux_chip(dev) : NULL;
}
static struct mux_control *mux_get(struct device *dev, const char *mux_name,
unsigned int *state, bool optional,
struct device_node *node)
{
struct device_node *np = node ? node : dev->of_node;
struct of_phandle_args args;
struct mux_chip *mux_chip;
unsigned int controller;
int index = 0;
int ret;
if (mux_name) {
if (state)
index = of_property_match_string(np, "mux-state-names",
mux_name);
else
index = of_property_match_string(np, "mux-control-names",
mux_name);
if (index < 0 && optional) {
return NULL;
} else if (index < 0) {
dev_err(dev, "mux controller '%s' not found\n",
mux_name);
return ERR_PTR(index);
}
}
if (state)
ret = of_parse_phandle_with_args(np,
"mux-states", "#mux-state-cells",
index, &args);
else
ret = of_parse_phandle_with_args(np,
"mux-controls", "#mux-control-cells",
index, &args);
if (ret) {
if (optional && ret == -ENOENT)
return NULL;
dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
np, state ? "state" : "control",
mux_name ?: "", index);
return ERR_PTR(ret);
}
mux_chip = of_find_mux_chip_by_node(args.np);
of_node_put(args.np);
if (!mux_chip)
return ERR_PTR(-EPROBE_DEFER);
controller = 0;
if (state) {
if (args.args_count > 2 || args.args_count == 0 ||
(args.args_count < 2 && mux_chip->controllers > 1)) {
dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
np, args.np);
put_device(&mux_chip->dev);
return ERR_PTR(-EINVAL);
}
if (args.args_count == 2) {
controller = args.args[0];
*state = args.args[1];
} else {
*state = args.args[0];
}
} else {
if (args.args_count > 1 ||
(!args.args_count && mux_chip->controllers > 1)) {
dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
np, args.np);
put_device(&mux_chip->dev);
return ERR_PTR(-EINVAL);
}
if (args.args_count)
controller = args.args[0];
}
if (controller >= mux_chip->controllers) {
dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
np, controller, args.np);
put_device(&mux_chip->dev);
return ERR_PTR(-EINVAL);
}
return &mux_chip->mux[controller];
}
struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
{
struct mux_control *mux = mux_get(dev, mux_name, NULL, false, NULL);
if (!mux)
return ERR_PTR(-ENOENT);
return mux;
}
EXPORT_SYMBOL_GPL(mux_control_get);
struct mux_control *mux_control_get_optional(struct device *dev, const char *mux_name)
{
return mux_get(dev, mux_name, NULL, true, NULL);
}
EXPORT_SYMBOL_GPL(mux_control_get_optional);
void mux_control_put(struct mux_control *mux)
{
put_device(&mux->chip->dev);
}
EXPORT_SYMBOL_GPL(mux_control_put);
static void devm_mux_control_release(struct device *dev, void *res)
{
struct mux_control *mux = *(struct mux_control **)res;
mux_control_put(mux);
}
struct mux_control *devm_mux_control_get(struct device *dev,
const char *mux_name)
{
struct mux_control **ptr, *mux;
ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return ERR_PTR(-ENOMEM);
mux = mux_control_get(dev, mux_name);
if (IS_ERR(mux)) {
devres_free(ptr);
return mux;
}
*ptr = mux;
devres_add(dev, ptr);
return mux;
}
EXPORT_SYMBOL_GPL(devm_mux_control_get);
static struct mux_state *
mux_state_get(struct device *dev, const char *mux_name, bool optional,
struct device_node *np)
{
struct mux_state *mstate;
mstate = kzalloc_obj(*mstate);
if (!mstate)
return ERR_PTR(-ENOMEM);
mstate->mux = mux_get(dev, mux_name, &mstate->state, optional, np);
if (IS_ERR(mstate->mux)) {
int err = PTR_ERR(mstate->mux);
kfree(mstate);
return ERR_PTR(err);
} else if (!mstate->mux) {
kfree(mstate);
return optional ? NULL : ERR_PTR(-ENOENT);
}
return mstate;
}
static void mux_state_put(struct mux_state *mstate)
{
mux_control_put(mstate->mux);
kfree(mstate);
}
static void devm_mux_state_release(struct device *dev, void *res)
{
struct devm_mux_state_state *devm_state = res;
if (devm_state->exit)
devm_state->exit(devm_state->mstate);
mux_state_put(devm_state->mstate);
}
static struct mux_state *__devm_mux_state_get(struct device *dev, const char *mux_name,
bool optional, struct device_node *np,
int (*init)(struct mux_state *mstate),
int (*exit)(struct mux_state *mstate))
{
struct devm_mux_state_state *devm_state;
struct mux_state *mstate;
int ret;
mstate = mux_state_get(dev, mux_name, optional, np);
if (IS_ERR(mstate))
return ERR_CAST(mstate);
else if (optional && !mstate)
return NULL;
else if (!mstate)
return ERR_PTR(-ENOENT);
devm_state = devres_alloc(devm_mux_state_release, sizeof(*devm_state), GFP_KERNEL);
if (!devm_state) {
ret = -ENOMEM;
goto err_devres_alloc;
}
if (init) {
ret = init(mstate);
if (ret)
goto err_mux_state_init;
}
devm_state->mstate = mstate;
devm_state->exit = exit;
devres_add(dev, devm_state);
return mstate;
err_mux_state_init:
devres_free(devm_state);
err_devres_alloc:
mux_state_put(mstate);
return ERR_PTR(ret);
}
struct mux_state *
devm_mux_state_get_from_np(struct device *dev, const char *mux_name,
struct device_node *np)
{
return __devm_mux_state_get(dev, mux_name, false, np, NULL, NULL);
}
EXPORT_SYMBOL_GPL(devm_mux_state_get_from_np);
struct mux_state *devm_mux_state_get_optional(struct device *dev, const char *mux_name)
{
return __devm_mux_state_get(dev, mux_name, true, NULL, NULL, NULL);
}
EXPORT_SYMBOL_GPL(devm_mux_state_get_optional);
struct mux_state *devm_mux_state_get_selected(struct device *dev, const char *mux_name)
{
return __devm_mux_state_get(dev, mux_name, false, NULL,
mux_state_select, mux_state_deselect);
}
EXPORT_SYMBOL_GPL(devm_mux_state_get_selected);
struct mux_state *devm_mux_state_get_optional_selected(struct device *dev,
const char *mux_name)
{
return __devm_mux_state_get(dev, mux_name, true, NULL,
mux_state_select, mux_state_deselect);
}
EXPORT_SYMBOL_GPL(devm_mux_state_get_optional_selected);
subsys_initcall(mux_init);
module_exit(mux_exit);
MODULE_DESCRIPTION("Multiplexer subsystem");
MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
MODULE_LICENSE("GPL v2");