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