18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Multiplexer subsystem 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Axentia Technologies AB 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author: Peter Rosin <peda@axentia.se> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "mux-core: " fmt 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/device.h> 138c2ecf20Sopenharmony_ci#include <linux/err.h> 148c2ecf20Sopenharmony_ci#include <linux/export.h> 158c2ecf20Sopenharmony_ci#include <linux/idr.h> 168c2ecf20Sopenharmony_ci#include <linux/init.h> 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci#include <linux/mux/consumer.h> 198c2ecf20Sopenharmony_ci#include <linux/mux/driver.h> 208c2ecf20Sopenharmony_ci#include <linux/of.h> 218c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 228c2ecf20Sopenharmony_ci#include <linux/slab.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* 258c2ecf20Sopenharmony_ci * The idle-as-is "state" is not an actual state that may be selected, it 268c2ecf20Sopenharmony_ci * only implies that the state should not be changed. So, use that state 278c2ecf20Sopenharmony_ci * as indication that the cached state of the multiplexer is unknown. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci#define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic struct class mux_class = { 328c2ecf20Sopenharmony_ci .name = "mux", 338c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 348c2ecf20Sopenharmony_ci}; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic DEFINE_IDA(mux_ida); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic int __init mux_init(void) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci ida_init(&mux_ida); 418c2ecf20Sopenharmony_ci return class_register(&mux_class); 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic void __exit mux_exit(void) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci class_unregister(&mux_class); 478c2ecf20Sopenharmony_ci ida_destroy(&mux_ida); 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic void mux_chip_release(struct device *dev) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci struct mux_chip *mux_chip = to_mux_chip(dev); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci ida_simple_remove(&mux_ida, mux_chip->id); 558c2ecf20Sopenharmony_ci kfree(mux_chip); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic const struct device_type mux_type = { 598c2ecf20Sopenharmony_ci .name = "mux-chip", 608c2ecf20Sopenharmony_ci .release = mux_chip_release, 618c2ecf20Sopenharmony_ci}; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/** 648c2ecf20Sopenharmony_ci * mux_chip_alloc() - Allocate a mux-chip. 658c2ecf20Sopenharmony_ci * @dev: The parent device implementing the mux interface. 668c2ecf20Sopenharmony_ci * @controllers: The number of mux controllers to allocate for this chip. 678c2ecf20Sopenharmony_ci * @sizeof_priv: Size of extra memory area for private use by the caller. 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * After allocating the mux-chip with the desired number of mux controllers 708c2ecf20Sopenharmony_ci * but before registering the chip, the mux driver is required to configure 718c2ecf20Sopenharmony_ci * the number of valid mux states in the mux_chip->mux[N].states members and 728c2ecf20Sopenharmony_ci * the desired idle state in the returned mux_chip->mux[N].idle_state members. 738c2ecf20Sopenharmony_ci * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to 748c2ecf20Sopenharmony_ci * provide a pointer to the operations struct in the mux_chip->ops member 758c2ecf20Sopenharmony_ci * before registering the mux-chip with mux_chip_register. 768c2ecf20Sopenharmony_ci * 778c2ecf20Sopenharmony_ci * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno. 788c2ecf20Sopenharmony_ci */ 798c2ecf20Sopenharmony_cistruct mux_chip *mux_chip_alloc(struct device *dev, 808c2ecf20Sopenharmony_ci unsigned int controllers, size_t sizeof_priv) 818c2ecf20Sopenharmony_ci{ 828c2ecf20Sopenharmony_ci struct mux_chip *mux_chip; 838c2ecf20Sopenharmony_ci int i; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (WARN_ON(!dev || !controllers)) 868c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci mux_chip = kzalloc(sizeof(*mux_chip) + 898c2ecf20Sopenharmony_ci controllers * sizeof(*mux_chip->mux) + 908c2ecf20Sopenharmony_ci sizeof_priv, GFP_KERNEL); 918c2ecf20Sopenharmony_ci if (!mux_chip) 928c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci mux_chip->mux = (struct mux_control *)(mux_chip + 1); 958c2ecf20Sopenharmony_ci mux_chip->dev.class = &mux_class; 968c2ecf20Sopenharmony_ci mux_chip->dev.type = &mux_type; 978c2ecf20Sopenharmony_ci mux_chip->dev.parent = dev; 988c2ecf20Sopenharmony_ci mux_chip->dev.of_node = dev->of_node; 998c2ecf20Sopenharmony_ci dev_set_drvdata(&mux_chip->dev, mux_chip); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL); 1028c2ecf20Sopenharmony_ci if (mux_chip->id < 0) { 1038c2ecf20Sopenharmony_ci int err = mux_chip->id; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci pr_err("muxchipX failed to get a device id\n"); 1068c2ecf20Sopenharmony_ci kfree(mux_chip); 1078c2ecf20Sopenharmony_ci return ERR_PTR(err); 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci mux_chip->controllers = controllers; 1128c2ecf20Sopenharmony_ci for (i = 0; i < controllers; ++i) { 1138c2ecf20Sopenharmony_ci struct mux_control *mux = &mux_chip->mux[i]; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci mux->chip = mux_chip; 1168c2ecf20Sopenharmony_ci sema_init(&mux->lock, 1); 1178c2ecf20Sopenharmony_ci mux->cached_state = MUX_CACHE_UNKNOWN; 1188c2ecf20Sopenharmony_ci mux->idle_state = MUX_IDLE_AS_IS; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci device_initialize(&mux_chip->dev); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci return mux_chip; 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_chip_alloc); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic int mux_control_set(struct mux_control *mux, int state) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci int ret = mux->chip->ops->set(mux, state); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci return ret; 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci/** 1378c2ecf20Sopenharmony_ci * mux_chip_register() - Register a mux-chip, thus readying the controllers 1388c2ecf20Sopenharmony_ci * for use. 1398c2ecf20Sopenharmony_ci * @mux_chip: The mux-chip to register. 1408c2ecf20Sopenharmony_ci * 1418c2ecf20Sopenharmony_ci * Do not retry registration of the same mux-chip on failure. You should 1428c2ecf20Sopenharmony_ci * instead put it away with mux_chip_free() and allocate a new one, if you 1438c2ecf20Sopenharmony_ci * for some reason would like to retry registration. 1448c2ecf20Sopenharmony_ci * 1458c2ecf20Sopenharmony_ci * Return: Zero on success or a negative errno on error. 1468c2ecf20Sopenharmony_ci */ 1478c2ecf20Sopenharmony_ciint mux_chip_register(struct mux_chip *mux_chip) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci int i; 1508c2ecf20Sopenharmony_ci int ret; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci for (i = 0; i < mux_chip->controllers; ++i) { 1538c2ecf20Sopenharmony_ci struct mux_control *mux = &mux_chip->mux[i]; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (mux->idle_state == mux->cached_state) 1568c2ecf20Sopenharmony_ci continue; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci ret = mux_control_set(mux, mux->idle_state); 1598c2ecf20Sopenharmony_ci if (ret < 0) { 1608c2ecf20Sopenharmony_ci dev_err(&mux_chip->dev, "unable to set idle state\n"); 1618c2ecf20Sopenharmony_ci return ret; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci ret = device_add(&mux_chip->dev); 1668c2ecf20Sopenharmony_ci if (ret < 0) 1678c2ecf20Sopenharmony_ci dev_err(&mux_chip->dev, 1688c2ecf20Sopenharmony_ci "device_add failed in %s: %d\n", __func__, ret); 1698c2ecf20Sopenharmony_ci return ret; 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_chip_register); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/** 1748c2ecf20Sopenharmony_ci * mux_chip_unregister() - Take the mux-chip off-line. 1758c2ecf20Sopenharmony_ci * @mux_chip: The mux-chip to unregister. 1768c2ecf20Sopenharmony_ci * 1778c2ecf20Sopenharmony_ci * mux_chip_unregister() reverses the effects of mux_chip_register(). 1788c2ecf20Sopenharmony_ci * But not completely, you should not try to call mux_chip_register() 1798c2ecf20Sopenharmony_ci * on a mux-chip that has been registered before. 1808c2ecf20Sopenharmony_ci */ 1818c2ecf20Sopenharmony_civoid mux_chip_unregister(struct mux_chip *mux_chip) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci device_del(&mux_chip->dev); 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_chip_unregister); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci/** 1888c2ecf20Sopenharmony_ci * mux_chip_free() - Free the mux-chip for good. 1898c2ecf20Sopenharmony_ci * @mux_chip: The mux-chip to free. 1908c2ecf20Sopenharmony_ci * 1918c2ecf20Sopenharmony_ci * mux_chip_free() reverses the effects of mux_chip_alloc(). 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_civoid mux_chip_free(struct mux_chip *mux_chip) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci if (!mux_chip) 1968c2ecf20Sopenharmony_ci return; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci put_device(&mux_chip->dev); 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_chip_free); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_cistatic void devm_mux_chip_release(struct device *dev, void *res) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci struct mux_chip *mux_chip = *(struct mux_chip **)res; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci mux_chip_free(mux_chip); 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci/** 2108c2ecf20Sopenharmony_ci * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc(). 2118c2ecf20Sopenharmony_ci * @dev: The parent device implementing the mux interface. 2128c2ecf20Sopenharmony_ci * @controllers: The number of mux controllers to allocate for this chip. 2138c2ecf20Sopenharmony_ci * @sizeof_priv: Size of extra memory area for private use by the caller. 2148c2ecf20Sopenharmony_ci * 2158c2ecf20Sopenharmony_ci * See mux_chip_alloc() for more details. 2168c2ecf20Sopenharmony_ci * 2178c2ecf20Sopenharmony_ci * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno. 2188c2ecf20Sopenharmony_ci */ 2198c2ecf20Sopenharmony_cistruct mux_chip *devm_mux_chip_alloc(struct device *dev, 2208c2ecf20Sopenharmony_ci unsigned int controllers, 2218c2ecf20Sopenharmony_ci size_t sizeof_priv) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct mux_chip **ptr, *mux_chip; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL); 2268c2ecf20Sopenharmony_ci if (!ptr) 2278c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv); 2308c2ecf20Sopenharmony_ci if (IS_ERR(mux_chip)) { 2318c2ecf20Sopenharmony_ci devres_free(ptr); 2328c2ecf20Sopenharmony_ci return mux_chip; 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci *ptr = mux_chip; 2368c2ecf20Sopenharmony_ci devres_add(dev, ptr); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci return mux_chip; 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_mux_chip_alloc); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_cistatic void devm_mux_chip_reg_release(struct device *dev, void *res) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci struct mux_chip *mux_chip = *(struct mux_chip **)res; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci mux_chip_unregister(mux_chip); 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci/** 2508c2ecf20Sopenharmony_ci * devm_mux_chip_register() - Resource-managed version mux_chip_register(). 2518c2ecf20Sopenharmony_ci * @dev: The parent device implementing the mux interface. 2528c2ecf20Sopenharmony_ci * @mux_chip: The mux-chip to register. 2538c2ecf20Sopenharmony_ci * 2548c2ecf20Sopenharmony_ci * See mux_chip_register() for more details. 2558c2ecf20Sopenharmony_ci * 2568c2ecf20Sopenharmony_ci * Return: Zero on success or a negative errno on error. 2578c2ecf20Sopenharmony_ci */ 2588c2ecf20Sopenharmony_ciint devm_mux_chip_register(struct device *dev, 2598c2ecf20Sopenharmony_ci struct mux_chip *mux_chip) 2608c2ecf20Sopenharmony_ci{ 2618c2ecf20Sopenharmony_ci struct mux_chip **ptr; 2628c2ecf20Sopenharmony_ci int res; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL); 2658c2ecf20Sopenharmony_ci if (!ptr) 2668c2ecf20Sopenharmony_ci return -ENOMEM; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci res = mux_chip_register(mux_chip); 2698c2ecf20Sopenharmony_ci if (res) { 2708c2ecf20Sopenharmony_ci devres_free(ptr); 2718c2ecf20Sopenharmony_ci return res; 2728c2ecf20Sopenharmony_ci } 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci *ptr = mux_chip; 2758c2ecf20Sopenharmony_ci devres_add(dev, ptr); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci return res; 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_mux_chip_register); 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci/** 2828c2ecf20Sopenharmony_ci * mux_control_states() - Query the number of multiplexer states. 2838c2ecf20Sopenharmony_ci * @mux: The mux-control to query. 2848c2ecf20Sopenharmony_ci * 2858c2ecf20Sopenharmony_ci * Return: The number of multiplexer states. 2868c2ecf20Sopenharmony_ci */ 2878c2ecf20Sopenharmony_ciunsigned int mux_control_states(struct mux_control *mux) 2888c2ecf20Sopenharmony_ci{ 2898c2ecf20Sopenharmony_ci return mux->states; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_control_states); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci/* 2948c2ecf20Sopenharmony_ci * The mux->lock must be down when calling this function. 2958c2ecf20Sopenharmony_ci */ 2968c2ecf20Sopenharmony_cistatic int __mux_control_select(struct mux_control *mux, int state) 2978c2ecf20Sopenharmony_ci{ 2988c2ecf20Sopenharmony_ci int ret; 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci if (WARN_ON(state < 0 || state >= mux->states)) 3018c2ecf20Sopenharmony_ci return -EINVAL; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci if (mux->cached_state == state) 3048c2ecf20Sopenharmony_ci return 0; 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci ret = mux_control_set(mux, state); 3078c2ecf20Sopenharmony_ci if (ret >= 0) 3088c2ecf20Sopenharmony_ci return 0; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci /* The mux update failed, try to revert if appropriate... */ 3118c2ecf20Sopenharmony_ci if (mux->idle_state != MUX_IDLE_AS_IS) 3128c2ecf20Sopenharmony_ci mux_control_set(mux, mux->idle_state); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci return ret; 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci/** 3188c2ecf20Sopenharmony_ci * mux_control_select() - Select the given multiplexer state. 3198c2ecf20Sopenharmony_ci * @mux: The mux-control to request a change of state from. 3208c2ecf20Sopenharmony_ci * @state: The new requested state. 3218c2ecf20Sopenharmony_ci * 3228c2ecf20Sopenharmony_ci * On successfully selecting the mux-control state, it will be locked until 3238c2ecf20Sopenharmony_ci * there is a call to mux_control_deselect(). If the mux-control is already 3248c2ecf20Sopenharmony_ci * selected when mux_control_select() is called, the caller will be blocked 3258c2ecf20Sopenharmony_ci * until mux_control_deselect() is called (by someone else). 3268c2ecf20Sopenharmony_ci * 3278c2ecf20Sopenharmony_ci * Therefore, make sure to call mux_control_deselect() when the operation is 3288c2ecf20Sopenharmony_ci * complete and the mux-control is free for others to use, but do not call 3298c2ecf20Sopenharmony_ci * mux_control_deselect() if mux_control_select() fails. 3308c2ecf20Sopenharmony_ci * 3318c2ecf20Sopenharmony_ci * Return: 0 when the mux-control state has the requested state or a negative 3328c2ecf20Sopenharmony_ci * errno on error. 3338c2ecf20Sopenharmony_ci */ 3348c2ecf20Sopenharmony_ciint mux_control_select(struct mux_control *mux, unsigned int state) 3358c2ecf20Sopenharmony_ci{ 3368c2ecf20Sopenharmony_ci int ret; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci ret = down_killable(&mux->lock); 3398c2ecf20Sopenharmony_ci if (ret < 0) 3408c2ecf20Sopenharmony_ci return ret; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci ret = __mux_control_select(mux, state); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci if (ret < 0) 3458c2ecf20Sopenharmony_ci up(&mux->lock); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci return ret; 3488c2ecf20Sopenharmony_ci} 3498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_control_select); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci/** 3528c2ecf20Sopenharmony_ci * mux_control_try_select() - Try to select the given multiplexer state. 3538c2ecf20Sopenharmony_ci * @mux: The mux-control to request a change of state from. 3548c2ecf20Sopenharmony_ci * @state: The new requested state. 3558c2ecf20Sopenharmony_ci * 3568c2ecf20Sopenharmony_ci * On successfully selecting the mux-control state, it will be locked until 3578c2ecf20Sopenharmony_ci * mux_control_deselect() called. 3588c2ecf20Sopenharmony_ci * 3598c2ecf20Sopenharmony_ci * Therefore, make sure to call mux_control_deselect() when the operation is 3608c2ecf20Sopenharmony_ci * complete and the mux-control is free for others to use, but do not call 3618c2ecf20Sopenharmony_ci * mux_control_deselect() if mux_control_try_select() fails. 3628c2ecf20Sopenharmony_ci * 3638c2ecf20Sopenharmony_ci * Return: 0 when the mux-control state has the requested state or a negative 3648c2ecf20Sopenharmony_ci * errno on error. Specifically -EBUSY if the mux-control is contended. 3658c2ecf20Sopenharmony_ci */ 3668c2ecf20Sopenharmony_ciint mux_control_try_select(struct mux_control *mux, unsigned int state) 3678c2ecf20Sopenharmony_ci{ 3688c2ecf20Sopenharmony_ci int ret; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci if (down_trylock(&mux->lock)) 3718c2ecf20Sopenharmony_ci return -EBUSY; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci ret = __mux_control_select(mux, state); 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci if (ret < 0) 3768c2ecf20Sopenharmony_ci up(&mux->lock); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci return ret; 3798c2ecf20Sopenharmony_ci} 3808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_control_try_select); 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci/** 3838c2ecf20Sopenharmony_ci * mux_control_deselect() - Deselect the previously selected multiplexer state. 3848c2ecf20Sopenharmony_ci * @mux: The mux-control to deselect. 3858c2ecf20Sopenharmony_ci * 3868c2ecf20Sopenharmony_ci * It is required that a single call is made to mux_control_deselect() for 3878c2ecf20Sopenharmony_ci * each and every successful call made to either of mux_control_select() or 3888c2ecf20Sopenharmony_ci * mux_control_try_select(). 3898c2ecf20Sopenharmony_ci * 3908c2ecf20Sopenharmony_ci * Return: 0 on success and a negative errno on error. An error can only 3918c2ecf20Sopenharmony_ci * occur if the mux has an idle state. Note that even if an error occurs, the 3928c2ecf20Sopenharmony_ci * mux-control is unlocked and is thus free for the next access. 3938c2ecf20Sopenharmony_ci */ 3948c2ecf20Sopenharmony_ciint mux_control_deselect(struct mux_control *mux) 3958c2ecf20Sopenharmony_ci{ 3968c2ecf20Sopenharmony_ci int ret = 0; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci if (mux->idle_state != MUX_IDLE_AS_IS && 3998c2ecf20Sopenharmony_ci mux->idle_state != mux->cached_state) 4008c2ecf20Sopenharmony_ci ret = mux_control_set(mux, mux->idle_state); 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci up(&mux->lock); 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci return ret; 4058c2ecf20Sopenharmony_ci} 4068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_control_deselect); 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci/* Note this function returns a reference to the mux_chip dev. */ 4098c2ecf20Sopenharmony_cistatic struct mux_chip *of_find_mux_chip_by_node(struct device_node *np) 4108c2ecf20Sopenharmony_ci{ 4118c2ecf20Sopenharmony_ci struct device *dev; 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci dev = class_find_device_by_of_node(&mux_class, np); 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci return dev ? to_mux_chip(dev) : NULL; 4168c2ecf20Sopenharmony_ci} 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci/** 4198c2ecf20Sopenharmony_ci * mux_control_get() - Get the mux-control for a device. 4208c2ecf20Sopenharmony_ci * @dev: The device that needs a mux-control. 4218c2ecf20Sopenharmony_ci * @mux_name: The name identifying the mux-control. 4228c2ecf20Sopenharmony_ci * 4238c2ecf20Sopenharmony_ci * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno. 4248c2ecf20Sopenharmony_ci */ 4258c2ecf20Sopenharmony_cistruct mux_control *mux_control_get(struct device *dev, const char *mux_name) 4268c2ecf20Sopenharmony_ci{ 4278c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 4288c2ecf20Sopenharmony_ci struct of_phandle_args args; 4298c2ecf20Sopenharmony_ci struct mux_chip *mux_chip; 4308c2ecf20Sopenharmony_ci unsigned int controller; 4318c2ecf20Sopenharmony_ci int index = 0; 4328c2ecf20Sopenharmony_ci int ret; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci if (mux_name) { 4358c2ecf20Sopenharmony_ci index = of_property_match_string(np, "mux-control-names", 4368c2ecf20Sopenharmony_ci mux_name); 4378c2ecf20Sopenharmony_ci if (index < 0) { 4388c2ecf20Sopenharmony_ci dev_err(dev, "mux controller '%s' not found\n", 4398c2ecf20Sopenharmony_ci mux_name); 4408c2ecf20Sopenharmony_ci return ERR_PTR(index); 4418c2ecf20Sopenharmony_ci } 4428c2ecf20Sopenharmony_ci } 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci ret = of_parse_phandle_with_args(np, 4458c2ecf20Sopenharmony_ci "mux-controls", "#mux-control-cells", 4468c2ecf20Sopenharmony_ci index, &args); 4478c2ecf20Sopenharmony_ci if (ret) { 4488c2ecf20Sopenharmony_ci dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n", 4498c2ecf20Sopenharmony_ci np, mux_name ?: "", index); 4508c2ecf20Sopenharmony_ci return ERR_PTR(ret); 4518c2ecf20Sopenharmony_ci } 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci mux_chip = of_find_mux_chip_by_node(args.np); 4548c2ecf20Sopenharmony_ci of_node_put(args.np); 4558c2ecf20Sopenharmony_ci if (!mux_chip) 4568c2ecf20Sopenharmony_ci return ERR_PTR(-EPROBE_DEFER); 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci if (args.args_count > 1 || 4598c2ecf20Sopenharmony_ci (!args.args_count && (mux_chip->controllers > 1))) { 4608c2ecf20Sopenharmony_ci dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n", 4618c2ecf20Sopenharmony_ci np, args.np); 4628c2ecf20Sopenharmony_ci put_device(&mux_chip->dev); 4638c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci controller = 0; 4678c2ecf20Sopenharmony_ci if (args.args_count) 4688c2ecf20Sopenharmony_ci controller = args.args[0]; 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci if (controller >= mux_chip->controllers) { 4718c2ecf20Sopenharmony_ci dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n", 4728c2ecf20Sopenharmony_ci np, controller, args.np); 4738c2ecf20Sopenharmony_ci put_device(&mux_chip->dev); 4748c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 4758c2ecf20Sopenharmony_ci } 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci return &mux_chip->mux[controller]; 4788c2ecf20Sopenharmony_ci} 4798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_control_get); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci/** 4828c2ecf20Sopenharmony_ci * mux_control_put() - Put away the mux-control for good. 4838c2ecf20Sopenharmony_ci * @mux: The mux-control to put away. 4848c2ecf20Sopenharmony_ci * 4858c2ecf20Sopenharmony_ci * mux_control_put() reverses the effects of mux_control_get(). 4868c2ecf20Sopenharmony_ci */ 4878c2ecf20Sopenharmony_civoid mux_control_put(struct mux_control *mux) 4888c2ecf20Sopenharmony_ci{ 4898c2ecf20Sopenharmony_ci put_device(&mux->chip->dev); 4908c2ecf20Sopenharmony_ci} 4918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mux_control_put); 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_cistatic void devm_mux_control_release(struct device *dev, void *res) 4948c2ecf20Sopenharmony_ci{ 4958c2ecf20Sopenharmony_ci struct mux_control *mux = *(struct mux_control **)res; 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci mux_control_put(mux); 4988c2ecf20Sopenharmony_ci} 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci/** 5018c2ecf20Sopenharmony_ci * devm_mux_control_get() - Get the mux-control for a device, with resource 5028c2ecf20Sopenharmony_ci * management. 5038c2ecf20Sopenharmony_ci * @dev: The device that needs a mux-control. 5048c2ecf20Sopenharmony_ci * @mux_name: The name identifying the mux-control. 5058c2ecf20Sopenharmony_ci * 5068c2ecf20Sopenharmony_ci * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno. 5078c2ecf20Sopenharmony_ci */ 5088c2ecf20Sopenharmony_cistruct mux_control *devm_mux_control_get(struct device *dev, 5098c2ecf20Sopenharmony_ci const char *mux_name) 5108c2ecf20Sopenharmony_ci{ 5118c2ecf20Sopenharmony_ci struct mux_control **ptr, *mux; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL); 5148c2ecf20Sopenharmony_ci if (!ptr) 5158c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci mux = mux_control_get(dev, mux_name); 5188c2ecf20Sopenharmony_ci if (IS_ERR(mux)) { 5198c2ecf20Sopenharmony_ci devres_free(ptr); 5208c2ecf20Sopenharmony_ci return mux; 5218c2ecf20Sopenharmony_ci } 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci *ptr = mux; 5248c2ecf20Sopenharmony_ci devres_add(dev, ptr); 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci return mux; 5278c2ecf20Sopenharmony_ci} 5288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_mux_control_get); 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci/* 5318c2ecf20Sopenharmony_ci * Using subsys_initcall instead of module_init here to try to ensure - for 5328c2ecf20Sopenharmony_ci * the non-modular case - that the subsystem is initialized when mux consumers 5338c2ecf20Sopenharmony_ci * and mux controllers start to use it. 5348c2ecf20Sopenharmony_ci * For the modular case, the ordering is ensured with module dependencies. 5358c2ecf20Sopenharmony_ci */ 5368c2ecf20Sopenharmony_cisubsys_initcall(mux_init); 5378c2ecf20Sopenharmony_cimodule_exit(mux_exit); 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Multiplexer subsystem"); 5408c2ecf20Sopenharmony_ciMODULE_AUTHOR("Peter Rosin <peda@axentia.se>"); 5418c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 542