2 * GPIO-based I2C Arbitration Using a Challenge & Response Mechanism
4 * Copyright (C) 2012 Google, Inc
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/i2c-mux.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/of_i2c.h>
25 #include <linux/of_gpio.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
31 * struct i2c_arbitrator_data - Driver data for I2C arbitrator
33 * @parent: Parent adapter
35 * @our_gpio: GPIO we'll use to claim.
36 * @our_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
37 * this then consider it released.
38 * @their_gpio: GPIO that the other side will use to claim.
39 * @their_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
40 * this then consider it released.
41 * @slew_delay_us: microseconds to wait for a GPIO to go high.
42 * @wait_retry_us: we'll attempt another claim after this many microseconds.
43 * @wait_free_us: we'll give up after this many microseconds.
46 struct i2c_arbitrator_data
{
47 struct i2c_adapter
*parent
;
48 struct i2c_adapter
*child
;
52 int their_gpio_release
;
53 unsigned int slew_delay_us
;
54 unsigned int wait_retry_us
;
55 unsigned int wait_free_us
;
60 * i2c_arbitrator_select - claim the I2C bus
62 * Use the GPIO-based signalling protocol; return -EBUSY if we fail.
64 static int i2c_arbitrator_select(struct i2c_adapter
*adap
, void *data
, u32 chan
)
66 const struct i2c_arbitrator_data
*arb
= data
;
67 unsigned long stop_retry
, stop_time
;
69 /* Start a round of trying to claim the bus */
70 stop_time
= jiffies
+ usecs_to_jiffies(arb
->wait_free_us
) + 1;
72 /* Indicate that we want to claim the bus */
73 gpio_set_value(arb
->our_gpio
, !arb
->our_gpio_release
);
74 udelay(arb
->slew_delay_us
);
76 /* Wait for the other master to release it */
77 stop_retry
= jiffies
+ usecs_to_jiffies(arb
->wait_retry_us
) + 1;
78 while (time_before(jiffies
, stop_retry
)) {
79 int gpio_val
= !!gpio_get_value(arb
->their_gpio
);
81 if (gpio_val
== arb
->their_gpio_release
) {
82 /* We got it, so return */
86 usleep_range(50, 200);
89 /* It didn't release, so give up, wait, and try again */
90 gpio_set_value(arb
->our_gpio
, arb
->our_gpio_release
);
92 usleep_range(arb
->wait_retry_us
, arb
->wait_retry_us
* 2);
93 } while (time_before(jiffies
, stop_time
));
95 /* Give up, release our claim */
96 gpio_set_value(arb
->our_gpio
, arb
->our_gpio_release
);
97 udelay(arb
->slew_delay_us
);
98 dev_err(&adap
->dev
, "Could not claim bus, timeout\n");
103 * i2c_arbitrator_deselect - release the I2C bus
105 * Release the I2C bus using the GPIO-based signalling protocol.
107 static int i2c_arbitrator_deselect(struct i2c_adapter
*adap
, void *data
,
110 const struct i2c_arbitrator_data
*arb
= data
;
112 /* Release the bus and wait for the other master to notice */
113 gpio_set_value(arb
->our_gpio
, arb
->our_gpio_release
);
114 udelay(arb
->slew_delay_us
);
119 static int i2c_arbitrator_probe(struct platform_device
*pdev
)
121 struct device
*dev
= &pdev
->dev
;
122 struct device_node
*np
= dev
->of_node
;
123 struct device_node
*parent_np
;
124 struct i2c_arbitrator_data
*arb
;
125 enum of_gpio_flags gpio_flags
;
126 unsigned long out_init
;
129 /* We only support probing from device tree; no platform_data */
131 dev_err(dev
, "Cannot find device tree node\n");
134 if (dev
->platform_data
) {
135 dev_err(dev
, "Platform data is not supported\n");
139 arb
= devm_kzalloc(dev
, sizeof(*arb
), GFP_KERNEL
);
141 dev_err(dev
, "Cannot allocate i2c_arbitrator_data\n");
144 platform_set_drvdata(pdev
, arb
);
147 ret
= of_get_named_gpio_flags(np
, "our-claim-gpio", 0, &gpio_flags
);
148 if (!gpio_is_valid(ret
)) {
149 if (ret
!= -EPROBE_DEFER
)
150 dev_err(dev
, "Error getting our-claim-gpio\n");
154 arb
->our_gpio_release
= !!(gpio_flags
& OF_GPIO_ACTIVE_LOW
);
155 out_init
= (gpio_flags
& OF_GPIO_ACTIVE_LOW
) ?
156 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
;
157 ret
= devm_gpio_request_one(dev
, arb
->our_gpio
, out_init
,
160 if (ret
!= -EPROBE_DEFER
)
161 dev_err(dev
, "Error requesting our-claim-gpio\n");
165 ret
= of_get_named_gpio_flags(np
, "their-claim-gpios", 0, &gpio_flags
);
166 if (!gpio_is_valid(ret
)) {
167 if (ret
!= -EPROBE_DEFER
)
168 dev_err(dev
, "Error getting their-claim-gpio\n");
171 arb
->their_gpio
= ret
;
172 arb
->their_gpio_release
= !!(gpio_flags
& OF_GPIO_ACTIVE_LOW
);
173 ret
= devm_gpio_request_one(dev
, arb
->their_gpio
, GPIOF_IN
,
176 if (ret
!= -EPROBE_DEFER
)
177 dev_err(dev
, "Error requesting their-claim-gpio\n");
181 /* At the moment we only support a single two master (us + 1 other) */
182 if (gpio_is_valid(of_get_named_gpio(np
, "their-claim-gpios", 1))) {
183 dev_err(dev
, "Only one other master is supported\n");
187 /* Arbitration parameters */
188 if (of_property_read_u32(np
, "slew-delay-us", &arb
->slew_delay_us
))
189 arb
->slew_delay_us
= 10;
190 if (of_property_read_u32(np
, "wait-retry-us", &arb
->wait_retry_us
))
191 arb
->wait_retry_us
= 3000;
192 if (of_property_read_u32(np
, "wait-free-us", &arb
->wait_free_us
))
193 arb
->wait_free_us
= 50000;
195 /* Find our parent */
196 parent_np
= of_parse_phandle(np
, "i2c-parent", 0);
198 dev_err(dev
, "Cannot parse i2c-parent\n");
201 arb
->parent
= of_find_i2c_adapter_by_node(parent_np
);
203 dev_err(dev
, "Cannot find parent bus\n");
207 /* Actually add the mux adapter */
208 arb
->child
= i2c_add_mux_adapter(arb
->parent
, dev
, arb
, 0, 0, 0,
209 i2c_arbitrator_select
,
210 i2c_arbitrator_deselect
);
212 dev_err(dev
, "Failed to add adapter\n");
214 i2c_put_adapter(arb
->parent
);
220 static int i2c_arbitrator_remove(struct platform_device
*pdev
)
222 struct i2c_arbitrator_data
*arb
= platform_get_drvdata(pdev
);
224 i2c_del_mux_adapter(arb
->child
);
225 i2c_put_adapter(arb
->parent
);
230 static const struct of_device_id i2c_arbitrator_of_match
[] = {
231 { .compatible
= "i2c-arb-gpio-challenge", },
234 MODULE_DEVICE_TABLE(of
, i2c_arbitrator_of_match
);
236 static struct platform_driver i2c_arbitrator_driver
= {
237 .probe
= i2c_arbitrator_probe
,
238 .remove
= i2c_arbitrator_remove
,
240 .owner
= THIS_MODULE
,
241 .name
= "i2c-arb-gpio-challenge",
242 .of_match_table
= of_match_ptr(i2c_arbitrator_of_match
),
246 module_platform_driver(i2c_arbitrator_driver
);
248 MODULE_DESCRIPTION("GPIO-based I2C Arbitration");
249 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
250 MODULE_LICENSE("GPL v2");
251 MODULE_ALIAS("platform:i2c-arb-gpio-challenge");