1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * I2C multiplexer using GPIO API
4 *
5 * Peter Korsgaard <peter.korsgaard@barco.com>
6 */
7
8#include <linux/i2c.h>
9#include <linux/i2c-mux.h>
10#include <linux/overflow.h>
11#include <linux/platform_data/i2c-mux-gpio.h>
12#include <linux/platform_device.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/bits.h>
16#include <linux/gpio/consumer.h>
17/* FIXME: stop poking around inside gpiolib */
18#include "../../gpio/gpiolib.h"
19
20struct gpiomux {
21	struct i2c_mux_gpio_platform_data data;
22	int ngpios;
23	struct gpio_desc **gpios;
24};
25
26static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
27{
28	DECLARE_BITMAP(values, BITS_PER_TYPE(val));
29
30	values[0] = val;
31
32	gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values);
33}
34
35static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
36{
37	struct gpiomux *mux = i2c_mux_priv(muxc);
38
39	i2c_mux_gpio_set(mux, chan);
40
41	return 0;
42}
43
44static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
45{
46	struct gpiomux *mux = i2c_mux_priv(muxc);
47
48	i2c_mux_gpio_set(mux, mux->data.idle);
49
50	return 0;
51}
52
53static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
54				 struct platform_device *pdev)
55{
56	struct device *dev = &pdev->dev;
57	struct fwnode_handle *fwnode = dev_fwnode(dev);
58	struct device_node *np = dev->of_node;
59	struct device_node *adapter_np;
60	struct i2c_adapter *adapter = NULL;
61	struct fwnode_handle *child;
62	unsigned *values;
63	int rc, i = 0;
64
65	if (is_of_node(fwnode)) {
66		if (!np)
67			return -ENODEV;
68
69		adapter_np = of_parse_phandle(np, "i2c-parent", 0);
70		if (!adapter_np) {
71			dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
72			return -ENODEV;
73		}
74		adapter = of_find_i2c_adapter_by_node(adapter_np);
75		of_node_put(adapter_np);
76
77	} else if (is_acpi_node(fwnode)) {
78		/*
79		 * In ACPI land the mux should be a direct child of the i2c
80		 * bus it muxes.
81		 */
82		acpi_handle dev_handle = ACPI_HANDLE(dev->parent);
83
84		adapter = i2c_acpi_find_adapter_by_handle(dev_handle);
85	}
86
87	if (!adapter)
88		return -EPROBE_DEFER;
89
90	mux->data.parent = i2c_adapter_id(adapter);
91	put_device(&adapter->dev);
92
93	mux->data.n_values = device_get_child_node_count(dev);
94	values = devm_kcalloc(dev,
95			      mux->data.n_values, sizeof(*mux->data.values),
96			      GFP_KERNEL);
97	if (!values) {
98		dev_err(dev, "Cannot allocate values array");
99		return -ENOMEM;
100	}
101
102	device_for_each_child_node(dev, child) {
103		if (is_of_node(child)) {
104			fwnode_property_read_u32(child, "reg", values + i);
105
106		} else if (is_acpi_node(child)) {
107			rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i);
108			if (rc) {
109				fwnode_handle_put(child);
110				return dev_err_probe(dev, rc, "Cannot get address\n");
111			}
112		}
113
114		i++;
115	}
116	mux->data.values = values;
117
118	if (device_property_read_u32(dev, "idle-state", &mux->data.idle))
119		mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
120
121	return 0;
122}
123
124static int i2c_mux_gpio_probe(struct platform_device *pdev)
125{
126	struct i2c_mux_core *muxc;
127	struct gpiomux *mux;
128	struct i2c_adapter *parent;
129	struct i2c_adapter *root;
130	unsigned initial_state;
131	int i, ngpios, ret;
132
133	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
134	if (!mux)
135		return -ENOMEM;
136
137	if (!dev_get_platdata(&pdev->dev)) {
138		ret = i2c_mux_gpio_probe_fw(mux, pdev);
139		if (ret < 0)
140			return ret;
141	} else {
142		memcpy(&mux->data, dev_get_platdata(&pdev->dev),
143			sizeof(mux->data));
144	}
145
146	ngpios = gpiod_count(&pdev->dev, "mux");
147	if (ngpios <= 0) {
148		dev_err(&pdev->dev, "no valid gpios provided\n");
149		return ngpios ?: -EINVAL;
150	}
151	mux->ngpios = ngpios;
152
153	parent = i2c_get_adapter(mux->data.parent);
154	if (!parent)
155		return -EPROBE_DEFER;
156
157	muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
158			     array_size(ngpios, sizeof(*mux->gpios)), 0,
159			     i2c_mux_gpio_select, NULL);
160	if (!muxc) {
161		ret = -ENOMEM;
162		goto alloc_failed;
163	}
164	mux->gpios = muxc->priv;
165	muxc->priv = mux;
166
167	platform_set_drvdata(pdev, muxc);
168
169	root = i2c_root_adapter(&parent->dev);
170
171	muxc->mux_locked = true;
172
173	if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
174		initial_state = mux->data.idle;
175		muxc->deselect = i2c_mux_gpio_deselect;
176	} else {
177		initial_state = mux->data.values[0];
178	}
179
180	for (i = 0; i < ngpios; i++) {
181		struct device *gpio_dev;
182		struct gpio_desc *gpiod;
183		enum gpiod_flags flag;
184
185		if (initial_state & BIT(i))
186			flag = GPIOD_OUT_HIGH;
187		else
188			flag = GPIOD_OUT_LOW;
189		gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
190		if (IS_ERR(gpiod)) {
191			ret = PTR_ERR(gpiod);
192			goto alloc_failed;
193		}
194
195		mux->gpios[i] = gpiod;
196
197		if (!muxc->mux_locked)
198			continue;
199
200		/* FIXME: find a proper way to access the GPIO device */
201		gpio_dev = &gpiod->gdev->dev;
202		muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
203	}
204
205	if (muxc->mux_locked)
206		dev_info(&pdev->dev, "mux-locked i2c mux\n");
207
208	for (i = 0; i < mux->data.n_values; i++) {
209		u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
210		unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
211
212		ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
213		if (ret)
214			goto add_adapter_failed;
215	}
216
217	dev_info(&pdev->dev, "%d port mux on %s adapter\n",
218		 mux->data.n_values, parent->name);
219
220	return 0;
221
222add_adapter_failed:
223	i2c_mux_del_adapters(muxc);
224alloc_failed:
225	i2c_put_adapter(parent);
226
227	return ret;
228}
229
230static void i2c_mux_gpio_remove(struct platform_device *pdev)
231{
232	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
233
234	i2c_mux_del_adapters(muxc);
235	i2c_put_adapter(muxc->parent);
236}
237
238static const struct of_device_id i2c_mux_gpio_of_match[] = {
239	{ .compatible = "i2c-mux-gpio", },
240	{},
241};
242MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
243
244static struct platform_driver i2c_mux_gpio_driver = {
245	.probe	= i2c_mux_gpio_probe,
246	.remove_new = i2c_mux_gpio_remove,
247	.driver	= {
248		.name	= "i2c-mux-gpio",
249		.of_match_table = i2c_mux_gpio_of_match,
250	},
251};
252
253module_platform_driver(i2c_mux_gpio_driver);
254
255MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
256MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
257MODULE_LICENSE("GPL");
258MODULE_ALIAS("platform:i2c-mux-gpio");
259