Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Multiplexer subsystem
0004  *
0005  * Copyright (C) 2017 Axentia Technologies AB
0006  *
0007  * Author: Peter Rosin <peda@axentia.se>
0008  */
0009 
0010 #define pr_fmt(fmt) "mux-core: " fmt
0011 
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/export.h>
0016 #include <linux/idr.h>
0017 #include <linux/init.h>
0018 #include <linux/module.h>
0019 #include <linux/mux/consumer.h>
0020 #include <linux/mux/driver.h>
0021 #include <linux/of.h>
0022 #include <linux/of_platform.h>
0023 #include <linux/slab.h>
0024 
0025 /*
0026  * The idle-as-is "state" is not an actual state that may be selected, it
0027  * only implies that the state should not be changed. So, use that state
0028  * as indication that the cached state of the multiplexer is unknown.
0029  */
0030 #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
0031 
0032 /**
0033  * struct mux_state -   Represents a mux controller state specific to a given
0034  *          consumer.
0035  * @mux:        Pointer to a mux controller.
0036  * @state:      State of the mux to be selected.
0037  *
0038  * This structure is specific to the consumer that acquires it and has
0039  * information specific to that consumer.
0040  */
0041 struct mux_state {
0042     struct mux_control *mux;
0043     unsigned int state;
0044 };
0045 
0046 static struct class mux_class = {
0047     .name = "mux",
0048     .owner = THIS_MODULE,
0049 };
0050 
0051 static DEFINE_IDA(mux_ida);
0052 
0053 static int __init mux_init(void)
0054 {
0055     ida_init(&mux_ida);
0056     return class_register(&mux_class);
0057 }
0058 
0059 static void __exit mux_exit(void)
0060 {
0061     class_unregister(&mux_class);
0062     ida_destroy(&mux_ida);
0063 }
0064 
0065 static void mux_chip_release(struct device *dev)
0066 {
0067     struct mux_chip *mux_chip = to_mux_chip(dev);
0068 
0069     ida_simple_remove(&mux_ida, mux_chip->id);
0070     kfree(mux_chip);
0071 }
0072 
0073 static const struct device_type mux_type = {
0074     .name = "mux-chip",
0075     .release = mux_chip_release,
0076 };
0077 
0078 /**
0079  * mux_chip_alloc() - Allocate a mux-chip.
0080  * @dev: The parent device implementing the mux interface.
0081  * @controllers: The number of mux controllers to allocate for this chip.
0082  * @sizeof_priv: Size of extra memory area for private use by the caller.
0083  *
0084  * After allocating the mux-chip with the desired number of mux controllers
0085  * but before registering the chip, the mux driver is required to configure
0086  * the number of valid mux states in the mux_chip->mux[N].states members and
0087  * the desired idle state in the returned mux_chip->mux[N].idle_state members.
0088  * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
0089  * provide a pointer to the operations struct in the mux_chip->ops member
0090  * before registering the mux-chip with mux_chip_register.
0091  *
0092  * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
0093  */
0094 struct mux_chip *mux_chip_alloc(struct device *dev,
0095                 unsigned int controllers, size_t sizeof_priv)
0096 {
0097     struct mux_chip *mux_chip;
0098     int i;
0099 
0100     if (WARN_ON(!dev || !controllers))
0101         return ERR_PTR(-EINVAL);
0102 
0103     mux_chip = kzalloc(sizeof(*mux_chip) +
0104                controllers * sizeof(*mux_chip->mux) +
0105                sizeof_priv, GFP_KERNEL);
0106     if (!mux_chip)
0107         return ERR_PTR(-ENOMEM);
0108 
0109     mux_chip->mux = (struct mux_control *)(mux_chip + 1);
0110     mux_chip->dev.class = &mux_class;
0111     mux_chip->dev.type = &mux_type;
0112     mux_chip->dev.parent = dev;
0113     mux_chip->dev.of_node = dev->of_node;
0114     dev_set_drvdata(&mux_chip->dev, mux_chip);
0115 
0116     mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
0117     if (mux_chip->id < 0) {
0118         int err = mux_chip->id;
0119 
0120         pr_err("muxchipX failed to get a device id\n");
0121         kfree(mux_chip);
0122         return ERR_PTR(err);
0123     }
0124     dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
0125 
0126     mux_chip->controllers = controllers;
0127     for (i = 0; i < controllers; ++i) {
0128         struct mux_control *mux = &mux_chip->mux[i];
0129 
0130         mux->chip = mux_chip;
0131         sema_init(&mux->lock, 1);
0132         mux->cached_state = MUX_CACHE_UNKNOWN;
0133         mux->idle_state = MUX_IDLE_AS_IS;
0134         mux->last_change = ktime_get();
0135     }
0136 
0137     device_initialize(&mux_chip->dev);
0138 
0139     return mux_chip;
0140 }
0141 EXPORT_SYMBOL_GPL(mux_chip_alloc);
0142 
0143 static int mux_control_set(struct mux_control *mux, int state)
0144 {
0145     int ret = mux->chip->ops->set(mux, state);
0146 
0147     mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
0148     if (ret >= 0)
0149         mux->last_change = ktime_get();
0150 
0151     return ret;
0152 }
0153 
0154 /**
0155  * mux_chip_register() - Register a mux-chip, thus readying the controllers
0156  *           for use.
0157  * @mux_chip: The mux-chip to register.
0158  *
0159  * Do not retry registration of the same mux-chip on failure. You should
0160  * instead put it away with mux_chip_free() and allocate a new one, if you
0161  * for some reason would like to retry registration.
0162  *
0163  * Return: Zero on success or a negative errno on error.
0164  */
0165 int mux_chip_register(struct mux_chip *mux_chip)
0166 {
0167     int i;
0168     int ret;
0169 
0170     for (i = 0; i < mux_chip->controllers; ++i) {
0171         struct mux_control *mux = &mux_chip->mux[i];
0172 
0173         if (mux->idle_state == mux->cached_state)
0174             continue;
0175 
0176         ret = mux_control_set(mux, mux->idle_state);
0177         if (ret < 0) {
0178             dev_err(&mux_chip->dev, "unable to set idle state\n");
0179             return ret;
0180         }
0181     }
0182 
0183     ret = device_add(&mux_chip->dev);
0184     if (ret < 0)
0185         dev_err(&mux_chip->dev,
0186             "device_add failed in %s: %d\n", __func__, ret);
0187     return ret;
0188 }
0189 EXPORT_SYMBOL_GPL(mux_chip_register);
0190 
0191 /**
0192  * mux_chip_unregister() - Take the mux-chip off-line.
0193  * @mux_chip: The mux-chip to unregister.
0194  *
0195  * mux_chip_unregister() reverses the effects of mux_chip_register().
0196  * But not completely, you should not try to call mux_chip_register()
0197  * on a mux-chip that has been registered before.
0198  */
0199 void mux_chip_unregister(struct mux_chip *mux_chip)
0200 {
0201     device_del(&mux_chip->dev);
0202 }
0203 EXPORT_SYMBOL_GPL(mux_chip_unregister);
0204 
0205 /**
0206  * mux_chip_free() - Free the mux-chip for good.
0207  * @mux_chip: The mux-chip to free.
0208  *
0209  * mux_chip_free() reverses the effects of mux_chip_alloc().
0210  */
0211 void mux_chip_free(struct mux_chip *mux_chip)
0212 {
0213     if (!mux_chip)
0214         return;
0215 
0216     put_device(&mux_chip->dev);
0217 }
0218 EXPORT_SYMBOL_GPL(mux_chip_free);
0219 
0220 static void devm_mux_chip_release(struct device *dev, void *res)
0221 {
0222     struct mux_chip *mux_chip = *(struct mux_chip **)res;
0223 
0224     mux_chip_free(mux_chip);
0225 }
0226 
0227 /**
0228  * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
0229  * @dev: The parent device implementing the mux interface.
0230  * @controllers: The number of mux controllers to allocate for this chip.
0231  * @sizeof_priv: Size of extra memory area for private use by the caller.
0232  *
0233  * See mux_chip_alloc() for more details.
0234  *
0235  * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
0236  */
0237 struct mux_chip *devm_mux_chip_alloc(struct device *dev,
0238                      unsigned int controllers,
0239                      size_t sizeof_priv)
0240 {
0241     struct mux_chip **ptr, *mux_chip;
0242 
0243     ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
0244     if (!ptr)
0245         return ERR_PTR(-ENOMEM);
0246 
0247     mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
0248     if (IS_ERR(mux_chip)) {
0249         devres_free(ptr);
0250         return mux_chip;
0251     }
0252 
0253     *ptr = mux_chip;
0254     devres_add(dev, ptr);
0255 
0256     return mux_chip;
0257 }
0258 EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
0259 
0260 static void devm_mux_chip_reg_release(struct device *dev, void *res)
0261 {
0262     struct mux_chip *mux_chip = *(struct mux_chip **)res;
0263 
0264     mux_chip_unregister(mux_chip);
0265 }
0266 
0267 /**
0268  * devm_mux_chip_register() - Resource-managed version mux_chip_register().
0269  * @dev: The parent device implementing the mux interface.
0270  * @mux_chip: The mux-chip to register.
0271  *
0272  * See mux_chip_register() for more details.
0273  *
0274  * Return: Zero on success or a negative errno on error.
0275  */
0276 int devm_mux_chip_register(struct device *dev,
0277                struct mux_chip *mux_chip)
0278 {
0279     struct mux_chip **ptr;
0280     int res;
0281 
0282     ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
0283     if (!ptr)
0284         return -ENOMEM;
0285 
0286     res = mux_chip_register(mux_chip);
0287     if (res) {
0288         devres_free(ptr);
0289         return res;
0290     }
0291 
0292     *ptr = mux_chip;
0293     devres_add(dev, ptr);
0294 
0295     return res;
0296 }
0297 EXPORT_SYMBOL_GPL(devm_mux_chip_register);
0298 
0299 /**
0300  * mux_control_states() - Query the number of multiplexer states.
0301  * @mux: The mux-control to query.
0302  *
0303  * Return: The number of multiplexer states.
0304  */
0305 unsigned int mux_control_states(struct mux_control *mux)
0306 {
0307     return mux->states;
0308 }
0309 EXPORT_SYMBOL_GPL(mux_control_states);
0310 
0311 /*
0312  * The mux->lock must be down when calling this function.
0313  */
0314 static int __mux_control_select(struct mux_control *mux, int state)
0315 {
0316     int ret;
0317 
0318     if (WARN_ON(state < 0 || state >= mux->states))
0319         return -EINVAL;
0320 
0321     if (mux->cached_state == state)
0322         return 0;
0323 
0324     ret = mux_control_set(mux, state);
0325     if (ret >= 0)
0326         return 0;
0327 
0328     /* The mux update failed, try to revert if appropriate... */
0329     if (mux->idle_state != MUX_IDLE_AS_IS)
0330         mux_control_set(mux, mux->idle_state);
0331 
0332     return ret;
0333 }
0334 
0335 static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
0336 {
0337     ktime_t delayend;
0338     s64 remaining;
0339 
0340     if (!delay_us)
0341         return;
0342 
0343     delayend = ktime_add_us(mux->last_change, delay_us);
0344     remaining = ktime_us_delta(delayend, ktime_get());
0345     if (remaining > 0)
0346         fsleep(remaining);
0347 }
0348 
0349 /**
0350  * mux_control_select_delay() - Select the given multiplexer state.
0351  * @mux: The mux-control to request a change of state from.
0352  * @state: The new requested state.
0353  * @delay_us: The time to delay (in microseconds) if the mux state is changed.
0354  *
0355  * On successfully selecting the mux-control state, it will be locked until
0356  * there is a call to mux_control_deselect(). If the mux-control is already
0357  * selected when mux_control_select() is called, the caller will be blocked
0358  * until mux_control_deselect() or mux_state_deselect() is called (by someone
0359  * else).
0360  *
0361  * Therefore, make sure to call mux_control_deselect() when the operation is
0362  * complete and the mux-control is free for others to use, but do not call
0363  * mux_control_deselect() if mux_control_select() fails.
0364  *
0365  * Return: 0 when the mux-control state has the requested state or a negative
0366  * errno on error.
0367  */
0368 int mux_control_select_delay(struct mux_control *mux, unsigned int state,
0369                  unsigned int delay_us)
0370 {
0371     int ret;
0372 
0373     ret = down_killable(&mux->lock);
0374     if (ret < 0)
0375         return ret;
0376 
0377     ret = __mux_control_select(mux, state);
0378     if (ret >= 0)
0379         mux_control_delay(mux, delay_us);
0380 
0381     if (ret < 0)
0382         up(&mux->lock);
0383 
0384     return ret;
0385 }
0386 EXPORT_SYMBOL_GPL(mux_control_select_delay);
0387 
0388 /**
0389  * mux_state_select_delay() - Select the given multiplexer state.
0390  * @mstate: The mux-state to select.
0391  * @delay_us: The time to delay (in microseconds) if the mux state is changed.
0392  *
0393  * On successfully selecting the mux-state, its mux-control will be locked
0394  * until there is a call to mux_state_deselect(). If the mux-control is already
0395  * selected when mux_state_select() is called, the caller will be blocked
0396  * until mux_state_deselect() or mux_control_deselect() is called (by someone
0397  * else).
0398  *
0399  * Therefore, make sure to call mux_state_deselect() when the operation is
0400  * complete and the mux-control is free for others to use, but do not call
0401  * mux_state_deselect() if mux_state_select() fails.
0402  *
0403  * Return: 0 when the mux-state has been selected or a negative
0404  * errno on error.
0405  */
0406 int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us)
0407 {
0408     return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
0409 }
0410 EXPORT_SYMBOL_GPL(mux_state_select_delay);
0411 
0412 /**
0413  * mux_control_try_select_delay() - Try to select the given multiplexer state.
0414  * @mux: The mux-control to request a change of state from.
0415  * @state: The new requested state.
0416  * @delay_us: The time to delay (in microseconds) if the mux state is changed.
0417  *
0418  * On successfully selecting the mux-control state, it will be locked until
0419  * mux_control_deselect() is called.
0420  *
0421  * Therefore, make sure to call mux_control_deselect() when the operation is
0422  * complete and the mux-control is free for others to use, but do not call
0423  * mux_control_deselect() if mux_control_try_select() fails.
0424  *
0425  * Return: 0 when the mux-control state has the requested state or a negative
0426  * errno on error. Specifically -EBUSY if the mux-control is contended.
0427  */
0428 int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
0429                  unsigned int delay_us)
0430 {
0431     int ret;
0432 
0433     if (down_trylock(&mux->lock))
0434         return -EBUSY;
0435 
0436     ret = __mux_control_select(mux, state);
0437     if (ret >= 0)
0438         mux_control_delay(mux, delay_us);
0439 
0440     if (ret < 0)
0441         up(&mux->lock);
0442 
0443     return ret;
0444 }
0445 EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
0446 
0447 /**
0448  * mux_state_try_select_delay() - Try to select the given multiplexer state.
0449  * @mstate: The mux-state to select.
0450  * @delay_us: The time to delay (in microseconds) if the mux state is changed.
0451  *
0452  * On successfully selecting the mux-state, its mux-control will be locked
0453  * until mux_state_deselect() is called.
0454  *
0455  * Therefore, make sure to call mux_state_deselect() when the operation is
0456  * complete and the mux-control is free for others to use, but do not call
0457  * mux_state_deselect() if mux_state_try_select() fails.
0458  *
0459  * Return: 0 when the mux-state has been selected or a negative errno on
0460  * error. Specifically -EBUSY if the mux-control is contended.
0461  */
0462 int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us)
0463 {
0464     return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
0465 }
0466 EXPORT_SYMBOL_GPL(mux_state_try_select_delay);
0467 
0468 /**
0469  * mux_control_deselect() - Deselect the previously selected multiplexer state.
0470  * @mux: The mux-control to deselect.
0471  *
0472  * It is required that a single call is made to mux_control_deselect() for
0473  * each and every successful call made to either of mux_control_select() or
0474  * mux_control_try_select().
0475  *
0476  * Return: 0 on success and a negative errno on error. An error can only
0477  * occur if the mux has an idle state. Note that even if an error occurs, the
0478  * mux-control is unlocked and is thus free for the next access.
0479  */
0480 int mux_control_deselect(struct mux_control *mux)
0481 {
0482     int ret = 0;
0483 
0484     if (mux->idle_state != MUX_IDLE_AS_IS &&
0485         mux->idle_state != mux->cached_state)
0486         ret = mux_control_set(mux, mux->idle_state);
0487 
0488     up(&mux->lock);
0489 
0490     return ret;
0491 }
0492 EXPORT_SYMBOL_GPL(mux_control_deselect);
0493 
0494 /**
0495  * mux_state_deselect() - Deselect the previously selected multiplexer state.
0496  * @mstate: The mux-state to deselect.
0497  *
0498  * It is required that a single call is made to mux_state_deselect() for
0499  * each and every successful call made to either of mux_state_select() or
0500  * mux_state_try_select().
0501  *
0502  * Return: 0 on success and a negative errno on error. An error can only
0503  * occur if the mux has an idle state. Note that even if an error occurs, the
0504  * mux-control is unlocked and is thus free for the next access.
0505  */
0506 int mux_state_deselect(struct mux_state *mstate)
0507 {
0508     return mux_control_deselect(mstate->mux);
0509 }
0510 EXPORT_SYMBOL_GPL(mux_state_deselect);
0511 
0512 /* Note this function returns a reference to the mux_chip dev. */
0513 static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
0514 {
0515     struct device *dev;
0516 
0517     dev = class_find_device_by_of_node(&mux_class, np);
0518 
0519     return dev ? to_mux_chip(dev) : NULL;
0520 }
0521 
0522 /*
0523  * mux_get() - Get the mux-control for a device.
0524  * @dev: The device that needs a mux-control.
0525  * @mux_name: The name identifying the mux-control.
0526  * @state: Pointer to where the requested state is returned, or NULL when
0527  *         the required multiplexer states are handled by other means.
0528  *
0529  * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
0530  */
0531 static struct mux_control *mux_get(struct device *dev, const char *mux_name,
0532                    unsigned int *state)
0533 {
0534     struct device_node *np = dev->of_node;
0535     struct of_phandle_args args;
0536     struct mux_chip *mux_chip;
0537     unsigned int controller;
0538     int index = 0;
0539     int ret;
0540 
0541     if (mux_name) {
0542         if (state)
0543             index = of_property_match_string(np, "mux-state-names",
0544                              mux_name);
0545         else
0546             index = of_property_match_string(np, "mux-control-names",
0547                              mux_name);
0548         if (index < 0) {
0549             dev_err(dev, "mux controller '%s' not found\n",
0550                 mux_name);
0551             return ERR_PTR(index);
0552         }
0553     }
0554 
0555     if (state)
0556         ret = of_parse_phandle_with_args(np,
0557                          "mux-states", "#mux-state-cells",
0558                          index, &args);
0559     else
0560         ret = of_parse_phandle_with_args(np,
0561                          "mux-controls", "#mux-control-cells",
0562                          index, &args);
0563     if (ret) {
0564         dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
0565             np, state ? "state" : "control", mux_name ?: "", index);
0566         return ERR_PTR(ret);
0567     }
0568 
0569     mux_chip = of_find_mux_chip_by_node(args.np);
0570     of_node_put(args.np);
0571     if (!mux_chip)
0572         return ERR_PTR(-EPROBE_DEFER);
0573 
0574     controller = 0;
0575     if (state) {
0576         if (args.args_count > 2 || args.args_count == 0 ||
0577             (args.args_count < 2 && mux_chip->controllers > 1)) {
0578             dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
0579                 np, args.np);
0580             put_device(&mux_chip->dev);
0581             return ERR_PTR(-EINVAL);
0582         }
0583 
0584         if (args.args_count == 2) {
0585             controller = args.args[0];
0586             *state = args.args[1];
0587         } else {
0588             *state = args.args[0];
0589         }
0590 
0591     } else {
0592         if (args.args_count > 1 ||
0593             (!args.args_count && mux_chip->controllers > 1)) {
0594             dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
0595                 np, args.np);
0596             put_device(&mux_chip->dev);
0597             return ERR_PTR(-EINVAL);
0598         }
0599 
0600         if (args.args_count)
0601             controller = args.args[0];
0602     }
0603 
0604     if (controller >= mux_chip->controllers) {
0605         dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
0606             np, controller, args.np);
0607         put_device(&mux_chip->dev);
0608         return ERR_PTR(-EINVAL);
0609     }
0610 
0611     return &mux_chip->mux[controller];
0612 }
0613 
0614 /**
0615  * mux_control_get() - Get the mux-control for a device.
0616  * @dev: The device that needs a mux-control.
0617  * @mux_name: The name identifying the mux-control.
0618  *
0619  * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
0620  */
0621 struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
0622 {
0623     return mux_get(dev, mux_name, NULL);
0624 }
0625 EXPORT_SYMBOL_GPL(mux_control_get);
0626 
0627 /**
0628  * mux_control_put() - Put away the mux-control for good.
0629  * @mux: The mux-control to put away.
0630  *
0631  * mux_control_put() reverses the effects of mux_control_get().
0632  */
0633 void mux_control_put(struct mux_control *mux)
0634 {
0635     put_device(&mux->chip->dev);
0636 }
0637 EXPORT_SYMBOL_GPL(mux_control_put);
0638 
0639 static void devm_mux_control_release(struct device *dev, void *res)
0640 {
0641     struct mux_control *mux = *(struct mux_control **)res;
0642 
0643     mux_control_put(mux);
0644 }
0645 
0646 /**
0647  * devm_mux_control_get() - Get the mux-control for a device, with resource
0648  *              management.
0649  * @dev: The device that needs a mux-control.
0650  * @mux_name: The name identifying the mux-control.
0651  *
0652  * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
0653  */
0654 struct mux_control *devm_mux_control_get(struct device *dev,
0655                      const char *mux_name)
0656 {
0657     struct mux_control **ptr, *mux;
0658 
0659     ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
0660     if (!ptr)
0661         return ERR_PTR(-ENOMEM);
0662 
0663     mux = mux_control_get(dev, mux_name);
0664     if (IS_ERR(mux)) {
0665         devres_free(ptr);
0666         return mux;
0667     }
0668 
0669     *ptr = mux;
0670     devres_add(dev, ptr);
0671 
0672     return mux;
0673 }
0674 EXPORT_SYMBOL_GPL(devm_mux_control_get);
0675 
0676 /*
0677  * mux_state_get() - Get the mux-state for a device.
0678  * @dev: The device that needs a mux-state.
0679  * @mux_name: The name identifying the mux-state.
0680  *
0681  * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno.
0682  */
0683 static struct mux_state *mux_state_get(struct device *dev, const char *mux_name)
0684 {
0685     struct mux_state *mstate;
0686 
0687     mstate = kzalloc(sizeof(*mstate), GFP_KERNEL);
0688     if (!mstate)
0689         return ERR_PTR(-ENOMEM);
0690 
0691     mstate->mux = mux_get(dev, mux_name, &mstate->state);
0692     if (IS_ERR(mstate->mux)) {
0693         int err = PTR_ERR(mstate->mux);
0694 
0695         kfree(mstate);
0696         return ERR_PTR(err);
0697     }
0698 
0699     return mstate;
0700 }
0701 
0702 /*
0703  * mux_state_put() - Put away the mux-state for good.
0704  * @mstate: The mux-state to put away.
0705  *
0706  * mux_state_put() reverses the effects of mux_state_get().
0707  */
0708 static void mux_state_put(struct mux_state *mstate)
0709 {
0710     mux_control_put(mstate->mux);
0711     kfree(mstate);
0712 }
0713 
0714 static void devm_mux_state_release(struct device *dev, void *res)
0715 {
0716     struct mux_state *mstate = *(struct mux_state **)res;
0717 
0718     mux_state_put(mstate);
0719 }
0720 
0721 /**
0722  * devm_mux_state_get() - Get the mux-state for a device, with resource
0723  *            management.
0724  * @dev: The device that needs a mux-control.
0725  * @mux_name: The name identifying the mux-control.
0726  *
0727  * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
0728  */
0729 struct mux_state *devm_mux_state_get(struct device *dev,
0730                      const char *mux_name)
0731 {
0732     struct mux_state **ptr, *mstate;
0733 
0734     ptr = devres_alloc(devm_mux_state_release, sizeof(*ptr), GFP_KERNEL);
0735     if (!ptr)
0736         return ERR_PTR(-ENOMEM);
0737 
0738     mstate = mux_state_get(dev, mux_name);
0739     if (IS_ERR(mstate)) {
0740         devres_free(ptr);
0741         return mstate;
0742     }
0743 
0744     *ptr = mstate;
0745     devres_add(dev, ptr);
0746 
0747     return mstate;
0748 }
0749 EXPORT_SYMBOL_GPL(devm_mux_state_get);
0750 
0751 /*
0752  * Using subsys_initcall instead of module_init here to try to ensure - for
0753  * the non-modular case - that the subsystem is initialized when mux consumers
0754  * and mux controllers start to use it.
0755  * For the modular case, the ordering is ensured with module dependencies.
0756  */
0757 subsys_initcall(mux_init);
0758 module_exit(mux_exit);
0759 
0760 MODULE_DESCRIPTION("Multiplexer subsystem");
0761 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
0762 MODULE_LICENSE("GPL v2");