1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * GPIO-controlled multiplexer driver
4 *
5 * Copyright (C) 2017 Axentia Technologies AB
6 *
7 * Author: Peter Rosin <peda@axentia.se>
8 */
9
10 #include <linux/bitmap.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/mux/driver.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/regulator/consumer.h>
19
20 struct mux_gpio {
21 struct gpio_descs *gpios;
22 };
23
mux_gpio_set(struct mux_control * mux,int state)24 static int mux_gpio_set(struct mux_control *mux, int state)
25 {
26 struct mux_gpio *mux_gpio = mux_chip_priv(mux->chip);
27 DECLARE_BITMAP(values, BITS_PER_TYPE(state));
28 u32 value = state;
29
30 bitmap_from_arr32(values, &value, BITS_PER_TYPE(value));
31
32 gpiod_multi_set_value_cansleep(mux_gpio->gpios, values);
33
34 return 0;
35 }
36
37 static const struct mux_control_ops mux_gpio_ops = {
38 .set = mux_gpio_set,
39 };
40
41 static const struct of_device_id mux_gpio_dt_ids[] = {
42 { .compatible = "gpio-mux", },
43 { /* sentinel */ }
44 };
45 MODULE_DEVICE_TABLE(of, mux_gpio_dt_ids);
46
mux_gpio_probe(struct platform_device * pdev)47 static int mux_gpio_probe(struct platform_device *pdev)
48 {
49 struct device *dev = &pdev->dev;
50 struct mux_chip *mux_chip;
51 struct mux_gpio *mux_gpio;
52 int pins;
53 s32 idle_state;
54 int ret;
55
56 pins = gpiod_count(dev, "mux");
57 if (pins < 0)
58 return pins;
59
60 mux_chip = devm_mux_chip_alloc(dev, 1, sizeof(*mux_gpio));
61 if (IS_ERR(mux_chip))
62 return PTR_ERR(mux_chip);
63
64 mux_gpio = mux_chip_priv(mux_chip);
65 mux_chip->ops = &mux_gpio_ops;
66
67 mux_gpio->gpios = devm_gpiod_get_array(dev, "mux", GPIOD_OUT_LOW);
68 if (IS_ERR(mux_gpio->gpios))
69 return dev_err_probe(dev, PTR_ERR(mux_gpio->gpios),
70 "failed to get gpios\n");
71 WARN_ON(pins != mux_gpio->gpios->ndescs);
72 mux_chip->mux->states = BIT(pins);
73
74 ret = device_property_read_u32(dev, "idle-state", (u32 *)&idle_state);
75 if (ret >= 0 && idle_state != MUX_IDLE_AS_IS) {
76 if (idle_state < 0 || idle_state >= mux_chip->mux->states) {
77 dev_err(dev, "invalid idle-state %u\n", idle_state);
78 return -EINVAL;
79 }
80
81 mux_chip->mux->idle_state = idle_state;
82 }
83
84 ret = devm_regulator_get_enable_optional(dev, "mux");
85 if (ret && ret != -ENODEV)
86 return dev_err_probe(dev, ret, "failed to get/enable mux supply\n");
87
88 ret = devm_mux_chip_register(dev, mux_chip);
89 if (ret < 0)
90 return ret;
91
92 dev_info(dev, "%u-way mux-controller registered\n",
93 mux_chip->mux->states);
94
95 return 0;
96 }
97
98 static struct platform_driver mux_gpio_driver = {
99 .driver = {
100 .name = "gpio-mux",
101 .of_match_table = mux_gpio_dt_ids,
102 },
103 .probe = mux_gpio_probe,
104 };
105 module_platform_driver(mux_gpio_driver);
106
107 MODULE_DESCRIPTION("GPIO-controlled multiplexer driver");
108 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
109 MODULE_LICENSE("GPL v2");
110