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