10edff03dSPeter Rosin // SPDX-License-Identifier: GPL-2.0 28f3addf1SPeter Rosin /* 38f3addf1SPeter Rosin * GPIO-controlled multiplexer driver 48f3addf1SPeter Rosin * 58f3addf1SPeter Rosin * Copyright (C) 2017 Axentia Technologies AB 68f3addf1SPeter Rosin * 78f3addf1SPeter Rosin * Author: Peter Rosin <peda@axentia.se> 88f3addf1SPeter Rosin */ 98f3addf1SPeter Rosin 105c777233SAndy Shevchenko #include <linux/bitmap.h> 118f3addf1SPeter Rosin #include <linux/err.h> 128f3addf1SPeter Rosin #include <linux/gpio/consumer.h> 13*7fef54e2SAndy Shevchenko #include <linux/mod_devicetable.h> 148f3addf1SPeter Rosin #include <linux/module.h> 158f3addf1SPeter Rosin #include <linux/mux/driver.h> 168f3addf1SPeter Rosin #include <linux/platform_device.h> 178f3addf1SPeter Rosin #include <linux/property.h> 188f3addf1SPeter Rosin 198f3addf1SPeter Rosin struct mux_gpio { 208f3addf1SPeter Rosin struct gpio_descs *gpios; 218f3addf1SPeter Rosin }; 228f3addf1SPeter Rosin 238f3addf1SPeter Rosin static int mux_gpio_set(struct mux_control *mux, int state) 248f3addf1SPeter Rosin { 258f3addf1SPeter Rosin struct mux_gpio *mux_gpio = mux_chip_priv(mux->chip); 26b9762bebSJanusz Krzysztofik DECLARE_BITMAP(values, BITS_PER_TYPE(state)); 275c777233SAndy Shevchenko u32 value = state; 288f3addf1SPeter Rosin 295c777233SAndy Shevchenko bitmap_from_arr32(values, &value, BITS_PER_TYPE(value)); 308f3addf1SPeter Rosin 318f3addf1SPeter Rosin gpiod_set_array_value_cansleep(mux_gpio->gpios->ndescs, 3277588c14SJanusz Krzysztofik mux_gpio->gpios->desc, 3377588c14SJanusz Krzysztofik mux_gpio->gpios->info, values); 348f3addf1SPeter Rosin 358f3addf1SPeter Rosin return 0; 368f3addf1SPeter Rosin } 378f3addf1SPeter Rosin 388f3addf1SPeter Rosin static const struct mux_control_ops mux_gpio_ops = { 398f3addf1SPeter Rosin .set = mux_gpio_set, 408f3addf1SPeter Rosin }; 418f3addf1SPeter Rosin 428f3addf1SPeter Rosin static const struct of_device_id mux_gpio_dt_ids[] = { 438f3addf1SPeter Rosin { .compatible = "gpio-mux", }, 448f3addf1SPeter Rosin { /* sentinel */ } 458f3addf1SPeter Rosin }; 468f3addf1SPeter Rosin MODULE_DEVICE_TABLE(of, mux_gpio_dt_ids); 478f3addf1SPeter Rosin 488f3addf1SPeter Rosin static int mux_gpio_probe(struct platform_device *pdev) 498f3addf1SPeter Rosin { 508f3addf1SPeter Rosin struct device *dev = &pdev->dev; 518f3addf1SPeter Rosin struct mux_chip *mux_chip; 528f3addf1SPeter Rosin struct mux_gpio *mux_gpio; 538f3addf1SPeter Rosin int pins; 548f3addf1SPeter Rosin s32 idle_state; 558f3addf1SPeter Rosin int ret; 568f3addf1SPeter Rosin 578f3addf1SPeter Rosin pins = gpiod_count(dev, "mux"); 588f3addf1SPeter Rosin if (pins < 0) 598f3addf1SPeter Rosin return pins; 608f3addf1SPeter Rosin 61b9762bebSJanusz Krzysztofik mux_chip = devm_mux_chip_alloc(dev, 1, sizeof(*mux_gpio)); 628f3addf1SPeter Rosin if (IS_ERR(mux_chip)) 638f3addf1SPeter Rosin return PTR_ERR(mux_chip); 648f3addf1SPeter Rosin 658f3addf1SPeter Rosin mux_gpio = mux_chip_priv(mux_chip); 668f3addf1SPeter Rosin mux_chip->ops = &mux_gpio_ops; 678f3addf1SPeter Rosin 688f3addf1SPeter Rosin mux_gpio->gpios = devm_gpiod_get_array(dev, "mux", GPIOD_OUT_LOW); 698f3addf1SPeter Rosin if (IS_ERR(mux_gpio->gpios)) { 708f3addf1SPeter Rosin ret = PTR_ERR(mux_gpio->gpios); 718f3addf1SPeter Rosin if (ret != -EPROBE_DEFER) 728f3addf1SPeter Rosin dev_err(dev, "failed to get gpios\n"); 738f3addf1SPeter Rosin return ret; 748f3addf1SPeter Rosin } 758f3addf1SPeter Rosin WARN_ON(pins != mux_gpio->gpios->ndescs); 765c777233SAndy Shevchenko mux_chip->mux->states = BIT(pins); 778f3addf1SPeter Rosin 788f3addf1SPeter Rosin ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state); 798f3addf1SPeter Rosin if (ret >= 0 && idle_state != MUX_IDLE_AS_IS) { 808f3addf1SPeter Rosin if (idle_state < 0 || idle_state >= mux_chip->mux->states) { 818f3addf1SPeter Rosin dev_err(dev, "invalid idle-state %u\n", idle_state); 828f3addf1SPeter Rosin return -EINVAL; 838f3addf1SPeter Rosin } 848f3addf1SPeter Rosin 858f3addf1SPeter Rosin mux_chip->mux->idle_state = idle_state; 868f3addf1SPeter Rosin } 878f3addf1SPeter Rosin 888f3addf1SPeter Rosin ret = devm_mux_chip_register(dev, mux_chip); 898f3addf1SPeter Rosin if (ret < 0) 908f3addf1SPeter Rosin return ret; 918f3addf1SPeter Rosin 928f3addf1SPeter Rosin dev_info(dev, "%u-way mux-controller registered\n", 938f3addf1SPeter Rosin mux_chip->mux->states); 948f3addf1SPeter Rosin 958f3addf1SPeter Rosin return 0; 968f3addf1SPeter Rosin } 978f3addf1SPeter Rosin 988f3addf1SPeter Rosin static struct platform_driver mux_gpio_driver = { 998f3addf1SPeter Rosin .driver = { 1008f3addf1SPeter Rosin .name = "gpio-mux", 101*7fef54e2SAndy Shevchenko .of_match_table = mux_gpio_dt_ids, 1028f3addf1SPeter Rosin }, 1038f3addf1SPeter Rosin .probe = mux_gpio_probe, 1048f3addf1SPeter Rosin }; 1058f3addf1SPeter Rosin module_platform_driver(mux_gpio_driver); 1068f3addf1SPeter Rosin 1078f3addf1SPeter Rosin MODULE_DESCRIPTION("GPIO-controlled multiplexer driver"); 1088f3addf1SPeter Rosin MODULE_AUTHOR("Peter Rosin <peda@axentia.se>"); 1098f3addf1SPeter Rosin MODULE_LICENSE("GPL v2"); 110