19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b81dfaa0SDoug Anderson /*
3b81dfaa0SDoug Anderson * GPIO-based I2C Arbitration Using a Challenge & Response Mechanism
4b81dfaa0SDoug Anderson *
5b81dfaa0SDoug Anderson * Copyright (C) 2012 Google, Inc
6b81dfaa0SDoug Anderson */
7b81dfaa0SDoug Anderson
8b81dfaa0SDoug Anderson #include <linux/delay.h>
9d9a183bfSLinus Walleij #include <linux/gpio/consumer.h>
10b81dfaa0SDoug Anderson #include <linux/kernel.h>
11b81dfaa0SDoug Anderson #include <linux/i2c.h>
12b81dfaa0SDoug Anderson #include <linux/i2c-mux.h>
13b81dfaa0SDoug Anderson #include <linux/module.h>
14b81dfaa0SDoug Anderson #include <linux/platform_device.h>
15b81dfaa0SDoug Anderson #include <linux/slab.h>
16b81dfaa0SDoug Anderson
17b81dfaa0SDoug Anderson
18b81dfaa0SDoug Anderson /**
19b81dfaa0SDoug Anderson * struct i2c_arbitrator_data - Driver data for I2C arbitrator
20b81dfaa0SDoug Anderson *
21d9a183bfSLinus Walleij * @our_gpio: GPIO descriptor we'll use to claim.
22d9a183bfSLinus Walleij * @their_gpio: GPIO descriptor that the other side will use to claim.
23b81dfaa0SDoug Anderson * @slew_delay_us: microseconds to wait for a GPIO to go high.
24b81dfaa0SDoug Anderson * @wait_retry_us: we'll attempt another claim after this many microseconds.
25b81dfaa0SDoug Anderson * @wait_free_us: we'll give up after this many microseconds.
26b81dfaa0SDoug Anderson */
27b81dfaa0SDoug Anderson
28b81dfaa0SDoug Anderson struct i2c_arbitrator_data {
29d9a183bfSLinus Walleij struct gpio_desc *our_gpio;
30d9a183bfSLinus Walleij struct gpio_desc *their_gpio;
31b81dfaa0SDoug Anderson unsigned int slew_delay_us;
32b81dfaa0SDoug Anderson unsigned int wait_retry_us;
33b81dfaa0SDoug Anderson unsigned int wait_free_us;
34b81dfaa0SDoug Anderson };
35b81dfaa0SDoug Anderson
36b81dfaa0SDoug Anderson
3745ce82f5SLee Jones /*
38b81dfaa0SDoug Anderson * i2c_arbitrator_select - claim the I2C bus
39b81dfaa0SDoug Anderson *
40b81dfaa0SDoug Anderson * Use the GPIO-based signalling protocol; return -EBUSY if we fail.
41b81dfaa0SDoug Anderson */
i2c_arbitrator_select(struct i2c_mux_core * muxc,u32 chan)428aacd901SPeter Rosin static int i2c_arbitrator_select(struct i2c_mux_core *muxc, u32 chan)
43b81dfaa0SDoug Anderson {
448aacd901SPeter Rosin const struct i2c_arbitrator_data *arb = i2c_mux_priv(muxc);
45b81dfaa0SDoug Anderson unsigned long stop_retry, stop_time;
46b81dfaa0SDoug Anderson
47b81dfaa0SDoug Anderson /* Start a round of trying to claim the bus */
48b81dfaa0SDoug Anderson stop_time = jiffies + usecs_to_jiffies(arb->wait_free_us) + 1;
49b81dfaa0SDoug Anderson do {
50b81dfaa0SDoug Anderson /* Indicate that we want to claim the bus */
51d9a183bfSLinus Walleij gpiod_set_value(arb->our_gpio, 1);
52b81dfaa0SDoug Anderson udelay(arb->slew_delay_us);
53b81dfaa0SDoug Anderson
54b81dfaa0SDoug Anderson /* Wait for the other master to release it */
55b81dfaa0SDoug Anderson stop_retry = jiffies + usecs_to_jiffies(arb->wait_retry_us) + 1;
56b81dfaa0SDoug Anderson while (time_before(jiffies, stop_retry)) {
57d9a183bfSLinus Walleij int gpio_val = gpiod_get_value(arb->their_gpio);
58b81dfaa0SDoug Anderson
59d9a183bfSLinus Walleij if (!gpio_val) {
60b81dfaa0SDoug Anderson /* We got it, so return */
61b81dfaa0SDoug Anderson return 0;
62b81dfaa0SDoug Anderson }
63b81dfaa0SDoug Anderson
64b81dfaa0SDoug Anderson usleep_range(50, 200);
65b81dfaa0SDoug Anderson }
66b81dfaa0SDoug Anderson
67b81dfaa0SDoug Anderson /* It didn't release, so give up, wait, and try again */
68d9a183bfSLinus Walleij gpiod_set_value(arb->our_gpio, 0);
69b81dfaa0SDoug Anderson
70b81dfaa0SDoug Anderson usleep_range(arb->wait_retry_us, arb->wait_retry_us * 2);
71b81dfaa0SDoug Anderson } while (time_before(jiffies, stop_time));
72b81dfaa0SDoug Anderson
73b81dfaa0SDoug Anderson /* Give up, release our claim */
74d9a183bfSLinus Walleij gpiod_set_value(arb->our_gpio, 0);
75b81dfaa0SDoug Anderson udelay(arb->slew_delay_us);
768aacd901SPeter Rosin dev_err(muxc->dev, "Could not claim bus, timeout\n");
77b81dfaa0SDoug Anderson return -EBUSY;
78b81dfaa0SDoug Anderson }
79b81dfaa0SDoug Anderson
8045ce82f5SLee Jones /*
81b81dfaa0SDoug Anderson * i2c_arbitrator_deselect - release the I2C bus
82b81dfaa0SDoug Anderson *
83b81dfaa0SDoug Anderson * Release the I2C bus using the GPIO-based signalling protocol.
84b81dfaa0SDoug Anderson */
i2c_arbitrator_deselect(struct i2c_mux_core * muxc,u32 chan)858aacd901SPeter Rosin static int i2c_arbitrator_deselect(struct i2c_mux_core *muxc, u32 chan)
86b81dfaa0SDoug Anderson {
878aacd901SPeter Rosin const struct i2c_arbitrator_data *arb = i2c_mux_priv(muxc);
88b81dfaa0SDoug Anderson
89b81dfaa0SDoug Anderson /* Release the bus and wait for the other master to notice */
90d9a183bfSLinus Walleij gpiod_set_value(arb->our_gpio, 0);
91b81dfaa0SDoug Anderson udelay(arb->slew_delay_us);
92b81dfaa0SDoug Anderson
93b81dfaa0SDoug Anderson return 0;
94b81dfaa0SDoug Anderson }
95b81dfaa0SDoug Anderson
i2c_arbitrator_probe(struct platform_device * pdev)96b81dfaa0SDoug Anderson static int i2c_arbitrator_probe(struct platform_device *pdev)
97b81dfaa0SDoug Anderson {
98b81dfaa0SDoug Anderson struct device *dev = &pdev->dev;
99b81dfaa0SDoug Anderson struct device_node *np = dev->of_node;
100b81dfaa0SDoug Anderson struct device_node *parent_np;
1018aacd901SPeter Rosin struct i2c_mux_core *muxc;
102b81dfaa0SDoug Anderson struct i2c_arbitrator_data *arb;
103d9a183bfSLinus Walleij struct gpio_desc *dummy;
104b81dfaa0SDoug Anderson int ret;
105b81dfaa0SDoug Anderson
106b81dfaa0SDoug Anderson /* We only support probing from device tree; no platform_data */
107b81dfaa0SDoug Anderson if (!np) {
108b81dfaa0SDoug Anderson dev_err(dev, "Cannot find device tree node\n");
109b81dfaa0SDoug Anderson return -ENODEV;
110b81dfaa0SDoug Anderson }
1116d4028c6SJingoo Han if (dev_get_platdata(dev)) {
112b81dfaa0SDoug Anderson dev_err(dev, "Platform data is not supported\n");
113b81dfaa0SDoug Anderson return -EINVAL;
114b81dfaa0SDoug Anderson }
115b81dfaa0SDoug Anderson
11640839590SPeter Rosin muxc = i2c_mux_alloc(NULL, dev, 1, sizeof(*arb), I2C_MUX_ARBITRATOR,
1178aacd901SPeter Rosin i2c_arbitrator_select, i2c_arbitrator_deselect);
1188aacd901SPeter Rosin if (!muxc)
119b81dfaa0SDoug Anderson return -ENOMEM;
1208aacd901SPeter Rosin arb = i2c_mux_priv(muxc);
1218aacd901SPeter Rosin
1228aacd901SPeter Rosin platform_set_drvdata(pdev, muxc);
123b81dfaa0SDoug Anderson
124d9a183bfSLinus Walleij /* Request GPIOs, our GPIO as unclaimed to begin with */
125d9a183bfSLinus Walleij arb->our_gpio = devm_gpiod_get(dev, "our-claim", GPIOD_OUT_LOW);
126d9a183bfSLinus Walleij if (IS_ERR(arb->our_gpio)) {
127d9a183bfSLinus Walleij dev_err(dev, "could not get \"our-claim\" GPIO (%ld)\n",
128d9a183bfSLinus Walleij PTR_ERR(arb->our_gpio));
129d9a183bfSLinus Walleij return PTR_ERR(arb->our_gpio);
130b81dfaa0SDoug Anderson }
131b81dfaa0SDoug Anderson
132d9a183bfSLinus Walleij arb->their_gpio = devm_gpiod_get(dev, "their-claim", GPIOD_IN);
133d9a183bfSLinus Walleij if (IS_ERR(arb->their_gpio)) {
134d9a183bfSLinus Walleij dev_err(dev, "could not get \"their-claim\" GPIO (%ld)\n",
135d9a183bfSLinus Walleij PTR_ERR(arb->their_gpio));
136d9a183bfSLinus Walleij return PTR_ERR(arb->their_gpio);
137b81dfaa0SDoug Anderson }
138b81dfaa0SDoug Anderson
139b81dfaa0SDoug Anderson /* At the moment we only support a single two master (us + 1 other) */
140d9a183bfSLinus Walleij dummy = devm_gpiod_get_index(dev, "their-claim", 1, GPIOD_IN);
141d9a183bfSLinus Walleij if (!IS_ERR(dummy)) {
142b81dfaa0SDoug Anderson dev_err(dev, "Only one other master is supported\n");
143b81dfaa0SDoug Anderson return -EINVAL;
144d9a183bfSLinus Walleij } else if (PTR_ERR(dummy) == -EPROBE_DEFER) {
145d9a183bfSLinus Walleij return -EPROBE_DEFER;
146b81dfaa0SDoug Anderson }
147b81dfaa0SDoug Anderson
148b81dfaa0SDoug Anderson /* Arbitration parameters */
149b81dfaa0SDoug Anderson if (of_property_read_u32(np, "slew-delay-us", &arb->slew_delay_us))
150b81dfaa0SDoug Anderson arb->slew_delay_us = 10;
151b81dfaa0SDoug Anderson if (of_property_read_u32(np, "wait-retry-us", &arb->wait_retry_us))
152b81dfaa0SDoug Anderson arb->wait_retry_us = 3000;
153b81dfaa0SDoug Anderson if (of_property_read_u32(np, "wait-free-us", &arb->wait_free_us))
154b81dfaa0SDoug Anderson arb->wait_free_us = 50000;
155b81dfaa0SDoug Anderson
156b81dfaa0SDoug Anderson /* Find our parent */
157b81dfaa0SDoug Anderson parent_np = of_parse_phandle(np, "i2c-parent", 0);
158b81dfaa0SDoug Anderson if (!parent_np) {
159b81dfaa0SDoug Anderson dev_err(dev, "Cannot parse i2c-parent\n");
160b81dfaa0SDoug Anderson return -EINVAL;
161b81dfaa0SDoug Anderson }
1628aacd901SPeter Rosin muxc->parent = of_get_i2c_adapter_by_node(parent_np);
163bdbf4a29SVladimir Zapolskiy of_node_put(parent_np);
1648aacd901SPeter Rosin if (!muxc->parent) {
165b81dfaa0SDoug Anderson dev_err(dev, "Cannot find parent bus\n");
1660cd98069SWolfram Sang return -EPROBE_DEFER;
167b81dfaa0SDoug Anderson }
168b81dfaa0SDoug Anderson
169b81dfaa0SDoug Anderson /* Actually add the mux adapter */
1708aacd901SPeter Rosin ret = i2c_mux_add_adapter(muxc, 0, 0, 0);
1711144d13eSPeter Rosin if (ret)
1728aacd901SPeter Rosin i2c_put_adapter(muxc->parent);
173b81dfaa0SDoug Anderson
174b81dfaa0SDoug Anderson return ret;
175b81dfaa0SDoug Anderson }
176b81dfaa0SDoug Anderson
i2c_arbitrator_remove(struct platform_device * pdev)177*e190a0c3SUwe Kleine-König static void i2c_arbitrator_remove(struct platform_device *pdev)
178b81dfaa0SDoug Anderson {
1798aacd901SPeter Rosin struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
180b81dfaa0SDoug Anderson
1818aacd901SPeter Rosin i2c_mux_del_adapters(muxc);
1828aacd901SPeter Rosin i2c_put_adapter(muxc->parent);
183b81dfaa0SDoug Anderson }
184b81dfaa0SDoug Anderson
185b81dfaa0SDoug Anderson static const struct of_device_id i2c_arbitrator_of_match[] = {
186b81dfaa0SDoug Anderson { .compatible = "i2c-arb-gpio-challenge", },
187b81dfaa0SDoug Anderson {},
188b81dfaa0SDoug Anderson };
189b81dfaa0SDoug Anderson MODULE_DEVICE_TABLE(of, i2c_arbitrator_of_match);
190b81dfaa0SDoug Anderson
191b81dfaa0SDoug Anderson static struct platform_driver i2c_arbitrator_driver = {
192b81dfaa0SDoug Anderson .probe = i2c_arbitrator_probe,
193*e190a0c3SUwe Kleine-König .remove_new = i2c_arbitrator_remove,
194b81dfaa0SDoug Anderson .driver = {
195b81dfaa0SDoug Anderson .name = "i2c-arb-gpio-challenge",
1964e905323SSachin Kamat .of_match_table = i2c_arbitrator_of_match,
197b81dfaa0SDoug Anderson },
198b81dfaa0SDoug Anderson };
199b81dfaa0SDoug Anderson
200b81dfaa0SDoug Anderson module_platform_driver(i2c_arbitrator_driver);
201b81dfaa0SDoug Anderson
202b81dfaa0SDoug Anderson MODULE_DESCRIPTION("GPIO-based I2C Arbitration");
203b81dfaa0SDoug Anderson MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
204b81dfaa0SDoug Anderson MODULE_LICENSE("GPL v2");
205b81dfaa0SDoug Anderson MODULE_ALIAS("platform:i2c-arb-gpio-challenge");
206