xref: /linux/drivers/i2c/muxes/i2c-mux-gpio.c (revision 031fba65fc202abf1f193e321be7a2c274fd88ba)
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 #include <linux/gpio/driver.h>
18 
19 struct gpiomux {
20 	struct i2c_mux_gpio_platform_data data;
21 	int ngpios;
22 	struct gpio_desc **gpios;
23 };
24 
25 static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
26 {
27 	DECLARE_BITMAP(values, BITS_PER_TYPE(val));
28 
29 	values[0] = val;
30 
31 	gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values);
32 }
33 
34 static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
35 {
36 	struct gpiomux *mux = i2c_mux_priv(muxc);
37 
38 	i2c_mux_gpio_set(mux, chan);
39 
40 	return 0;
41 }
42 
43 static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
44 {
45 	struct gpiomux *mux = i2c_mux_priv(muxc);
46 
47 	i2c_mux_gpio_set(mux, mux->data.idle);
48 
49 	return 0;
50 }
51 
52 static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
53 				 struct platform_device *pdev)
54 {
55 	struct device *dev = &pdev->dev;
56 	struct fwnode_handle *fwnode = dev_fwnode(dev);
57 	struct device_node *np = dev->of_node;
58 	struct device_node *adapter_np;
59 	struct i2c_adapter *adapter = NULL;
60 	struct fwnode_handle *child;
61 	unsigned *values;
62 	int rc, i = 0;
63 
64 	if (is_of_node(fwnode)) {
65 		if (!np)
66 			return -ENODEV;
67 
68 		adapter_np = of_parse_phandle(np, "i2c-parent", 0);
69 		if (!adapter_np) {
70 			dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
71 			return -ENODEV;
72 		}
73 		adapter = of_find_i2c_adapter_by_node(adapter_np);
74 		of_node_put(adapter_np);
75 
76 	} else if (is_acpi_node(fwnode)) {
77 		/*
78 		 * In ACPI land the mux should be a direct child of the i2c
79 		 * bus it muxes.
80 		 */
81 		acpi_handle dev_handle = ACPI_HANDLE(dev->parent);
82 
83 		adapter = i2c_acpi_find_adapter_by_handle(dev_handle);
84 	}
85 
86 	if (!adapter)
87 		return -EPROBE_DEFER;
88 
89 	mux->data.parent = i2c_adapter_id(adapter);
90 	put_device(&adapter->dev);
91 
92 	mux->data.n_values = device_get_child_node_count(dev);
93 	values = devm_kcalloc(dev,
94 			      mux->data.n_values, sizeof(*mux->data.values),
95 			      GFP_KERNEL);
96 	if (!values) {
97 		dev_err(dev, "Cannot allocate values array");
98 		return -ENOMEM;
99 	}
100 
101 	device_for_each_child_node(dev, child) {
102 		if (is_of_node(child)) {
103 			fwnode_property_read_u32(child, "reg", values + i);
104 
105 		} else if (is_acpi_node(child)) {
106 			rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i);
107 			if (rc) {
108 				fwnode_handle_put(child);
109 				return dev_err_probe(dev, rc, "Cannot get address\n");
110 			}
111 		}
112 
113 		i++;
114 	}
115 	mux->data.values = values;
116 
117 	if (device_property_read_u32(dev, "idle-state", &mux->data.idle))
118 		mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
119 
120 	return 0;
121 }
122 
123 static int i2c_mux_gpio_probe(struct platform_device *pdev)
124 {
125 	struct i2c_mux_core *muxc;
126 	struct gpiomux *mux;
127 	struct i2c_adapter *parent;
128 	struct i2c_adapter *root;
129 	unsigned initial_state;
130 	int i, ngpios, ret;
131 
132 	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
133 	if (!mux)
134 		return -ENOMEM;
135 
136 	if (!dev_get_platdata(&pdev->dev)) {
137 		ret = i2c_mux_gpio_probe_fw(mux, pdev);
138 		if (ret < 0)
139 			return ret;
140 	} else {
141 		memcpy(&mux->data, dev_get_platdata(&pdev->dev),
142 			sizeof(mux->data));
143 	}
144 
145 	ngpios = gpiod_count(&pdev->dev, "mux");
146 	if (ngpios <= 0) {
147 		dev_err(&pdev->dev, "no valid gpios provided\n");
148 		return ngpios ?: -EINVAL;
149 	}
150 	mux->ngpios = ngpios;
151 
152 	parent = i2c_get_adapter(mux->data.parent);
153 	if (!parent)
154 		return -EPROBE_DEFER;
155 
156 	muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
157 			     array_size(ngpios, sizeof(*mux->gpios)), 0,
158 			     i2c_mux_gpio_select, NULL);
159 	if (!muxc) {
160 		ret = -ENOMEM;
161 		goto alloc_failed;
162 	}
163 	mux->gpios = muxc->priv;
164 	muxc->priv = mux;
165 
166 	platform_set_drvdata(pdev, muxc);
167 
168 	root = i2c_root_adapter(&parent->dev);
169 
170 	muxc->mux_locked = true;
171 
172 	if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
173 		initial_state = mux->data.idle;
174 		muxc->deselect = i2c_mux_gpio_deselect;
175 	} else {
176 		initial_state = mux->data.values[0];
177 	}
178 
179 	for (i = 0; i < ngpios; i++) {
180 		struct gpio_device *gdev;
181 		struct device *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 		gdev = gpiod_to_gpio_device(gpiod);
201 		dev = gpio_device_to_device(gdev);
202 		muxc->mux_locked = i2c_root_adapter(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 
222 add_adapter_failed:
223 	i2c_mux_del_adapters(muxc);
224 alloc_failed:
225 	i2c_put_adapter(parent);
226 
227 	return ret;
228 }
229 
230 static 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 
238 static const struct of_device_id i2c_mux_gpio_of_match[] = {
239 	{ .compatible = "i2c-mux-gpio", },
240 	{},
241 };
242 MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
243 
244 static 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 
253 module_platform_driver(i2c_mux_gpio_driver);
254 
255 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
256 MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
257 MODULE_LICENSE("GPL");
258 MODULE_ALIAS("platform:i2c-mux-gpio");
259